Skip to content

Commit 3aa1a24

Browse files
authored
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>
2 parents 9b47807 + 5f6645d commit 3aa1a24

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

Diff for: compiler/rustc_middle/src/ty/sty.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1933,6 +1933,7 @@ impl<'tcx> Ty<'tcx> {
19331933
ty::UintTy::U64 => Some(sym::u64),
19341934
ty::UintTy::U128 => Some(sym::u128),
19351935
},
1936+
ty::Str => Some(sym::str),
19361937
_ => None,
19371938
}
19381939
}

Diff for: tests/ui/mismatched_types/similar_paths_primitive.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
#![allow(non_camel_case_types)]
22

33
struct bool;
4+
struct str;
45

56
fn foo(_: bool) {}
7+
fn bar(_: &str) {}
68

79
fn main() {
810
foo(true);
911
//~^ ERROR mismatched types [E0308]
12+
bar("hello");
13+
//~^ ERROR mismatched types [E0308]
1014
}

Diff for: tests/ui/mismatched_types/similar_paths_primitive.stderr

+24-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0308]: mismatched types
2-
--> $DIR/similar_paths_primitive.rs:8:9
2+
--> $DIR/similar_paths_primitive.rs:10:9
33
|
44
LL | foo(true);
55
| --- ^^^^ expected `bool`, found a different `bool`
@@ -14,11 +14,32 @@ note: `bool` is defined in the current crate
1414
LL | struct bool;
1515
| ^^^^^^^^^^^
1616
note: function defined here
17-
--> $DIR/similar_paths_primitive.rs:5:4
17+
--> $DIR/similar_paths_primitive.rs:6:4
1818
|
1919
LL | fn foo(_: bool) {}
2020
| ^^^ -------
2121

22-
error: aborting due to 1 previous error
22+
error[E0308]: mismatched types
23+
--> $DIR/similar_paths_primitive.rs:12:9
24+
|
25+
LL | bar("hello");
26+
| --- ^^^^^^^ expected `str`, found a different `str`
27+
| |
28+
| arguments to this function are incorrect
29+
|
30+
= note: str and `str` have similar names, but are actually distinct types
31+
= note: str is a primitive defined by the language
32+
note: `str` is defined in the current crate
33+
--> $DIR/similar_paths_primitive.rs:4:1
34+
|
35+
LL | struct str;
36+
| ^^^^^^^^^^
37+
note: function defined here
38+
--> $DIR/similar_paths_primitive.rs:7:4
39+
|
40+
LL | fn bar(_: &str) {}
41+
| ^^^ -------
42+
43+
error: aborting due to 2 previous errors
2344

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

0 commit comments

Comments
 (0)