rust/compiler/rustc_middle/src/ty
Matthias Krüger 3aa1a24799
Rollup merge of #132799 - zachs18:str-primitive-symbol, r=compiler-errors
Make `Ty::primitive_symbol` recognize `str`

Make `Ty::primitive_symbol` recognize `str`, which makes `str` eligible for the "expected primitive, found local type" (and vice versa) [diagnostic](https://github.com/rust-lang/rust/blob/master/compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs#L1430-L1437) that already exists for other primitives.

<details><summary> diagnostic difference</summary>

```rs
#[allow(non_camel_case_types)]
struct str;

fn foo() {
    let _: &str = "hello";
    let _: &core::primitive::str = &str;
}
```

`rustc --crate-type lib --edition 2021 a.rs`

Current nightly:

```rs
error[E0308]: mismatched types
 --> a.rs:5:19
  |
5 |     let _: &str = "hello";
  |            ----   ^^^^^^^ expected `str`, found a different `str`
  |            |
  |            expected due to this
  |
  = note: expected reference `&str`
             found reference `&'static str`

error[E0308]: mismatched types
 --> a.rs:6:36
  |
6 |     let _: &core::primitive::str = &str;
  |            ---------------------   ^^^^ expected `str`, found a different `str`
  |            |
  |            expected due to this
  |
  = note: expected reference `&str` (`str`)
             found reference `&str` (`str`)

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0308`.
```

With this patch:

```rs
error[E0308]: mismatched types
 --> a.rs:5:19
  |
5 |     let _: &str = "hello";
  |            ----   ^^^^^^^ expected `str`, found a different `str`
  |            |
  |            expected due to this
  |
  = note: str and `str` have similar names, but are actually distinct types
  = note: str is a primitive defined by the language
note: `str` is defined in the current crate
 --> a.rs:2:1
  |
2 | struct str;
  | ^^^^^^^^^^

error[E0308]: mismatched types
 --> a.rs:6:36
  |
6 |     let _: &core::primitive::str = &str;
  |            ---------------------   ^^^^ expected `str`, found a different `str`
  |            |
  |            expected due to this
  |
  = note: str and `str` have similar names, but are actually distinct types
  = note: str is a primitive defined by the language
note: `str` is defined in the current crate
 --> a.rs:2:1
  |
2 | struct str;
  | ^^^^^^^^^^

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0308`.
```

</details>
2024-11-09 19:16:46 +01:00
..
consts compiler: Directly use rustc_abi in metadata and middle 2024-11-03 13:38:47 -08:00
context Reformat use declarations. 2024-07-29 08:26:52 +10:00
inhabitedness Get rid of const eval_* and try_eval_* helpers 2024-10-19 18:07:35 +00:00
print ty::BrK -> ty::BoundRegionKind::K 2024-11-04 04:45:52 +00:00
abstract_const.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
adjustment.rs Register const preds for Deref adjustments in HIR typeck 2024-11-04 04:51:31 +00:00
adt.rs compiler: Directly use rustc_abi in metadata and middle 2024-11-03 13:38:47 -08:00
assoc.rs ty::KContainer -> ty::AssocItemContainer::K 2024-11-04 04:45:52 +00:00
cast.rs unify dyn* coercions with other pointer coercions 2024-09-24 22:17:55 +02:00
closure.rs Remove BorrowKind glob, make names longer 2024-11-04 04:45:52 +00:00
codec.rs compiler: Directly use rustc_abi in metadata and middle 2024-11-03 13:38:47 -08:00
consts.rs Rollup merge of #131049 - compiler-errors:more-validation, r=spastorino 2024-10-22 15:28:38 +02:00
context.rs ty::BrK -> ty::BoundRegionKind::K 2024-11-04 04:45:52 +00:00
diagnostics.rs Effects cleanup 2024-10-26 10:19:07 +08:00
elaborate_impl.rs Uplift elaboration 2024-07-07 11:28:01 -04:00
erase_regions.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
error.rs Fix type shortening writing to file 2024-11-02 03:08:04 +00:00
fast_reject.rs Use DeepRejectCtxt to quickly reject ParamEnv candidates 2024-09-02 19:59:18 +03:00
flags.rs Merge HostPolarity and BoundConstness 2024-10-30 16:23:16 +00:00
fold.rs ty::BrK -> ty::BoundRegionKind::K 2024-11-04 04:45:52 +00:00
generic_args.rs Effects cleanup 2024-10-26 10:19:07 +08:00
generics.rs Effects cleanup 2024-10-26 10:19:07 +08:00
impls_ty.rs const-eval interning: accpt interior mutable pointers in final value (but keep rejecting mutable references) 2024-09-10 10:26:16 +02:00
instance.rs remove 'platform-intrinsic' ABI leftovers 2024-11-07 17:42:49 +01:00
intrinsic.rs add new rustc_const_stable_intrinsic attribute for const-stable intrinsics 2024-11-04 23:27:46 +01:00
layout.rs pointee_info_at: fix logic for recursing into enums 2024-11-08 07:35:29 +01:00
list.rs Use &raw in the compiler 2024-09-26 20:33:26 -07:00
mod.rs Do not reveal opaques in the param-env, we got lazy norm instead 2024-11-09 03:55:07 +00:00
normalize_erasing_regions.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
opaque_types.rs Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
parameterized.rs Encode cross-crate opaque type origin 2024-10-31 01:35:13 +00:00
pattern.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
predicate.rs Implement const effect predicate in new solver 2024-10-24 09:46:36 +00:00
region.rs ty::BrK -> ty::BoundRegionKind::K 2024-11-04 04:45:52 +00:00
relate.rs do not relate Abi and Safety 2024-10-22 23:13:04 +02:00
rvalue_scopes.rs rescope temp lifetime in let-chain into IfElse 2024-09-11 04:10:00 +08:00
structural_impls.rs ty::BrK -> ty::BoundRegionKind::K 2024-11-04 04:45:52 +00:00
sty.rs Add str to "expected primitive, found type" diagnostic 2024-11-09 00:18:47 -06:00
trait_def.rs Separate collection of crate-local inherent impls from error reporting 2024-09-24 10:12:05 -04:00
typeck_results.rs compiler: Directly use rustc_abi in metadata and middle 2024-11-03 13:38:47 -08:00
util.rs Do not reveal opaques in the param-env, we got lazy norm instead 2024-11-09 03:55:07 +00:00
visit.rs Handle rustc-hir-analysis cases of rustc::potential_query_instability lint 2024-10-02 08:28:45 +03:00
vtable.rs Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
walk.rs Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00