Skip to content

Commit c4c654f

Browse files
authored
Rollup merge of #85187 - FabianWolff:issue-84976, r=jackh726
Use .name_str() to format primitive types in error messages This pull request fixes #84976. The problem described there is caused by this code https://github.com/rust-lang/rust/blob/506e75cbf8cb5305e49a41326307004ca3976029/compiler/rustc_middle/src/ty/error.rs#L161-L166 using `Debug` formatting (`{:?}`), while the proper solution is to call `name_str()` of `ty::IntTy`, `ty::UintTy` and `ty::FloatTy`, respectively.
2 parents 9ad5c4d + 69a4ae2 commit c4c654f

File tree

3 files changed

+67
-2
lines changed

3 files changed

+67
-2
lines changed

compiler/rustc_middle/src/ty/error.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,23 @@ impl<'tcx> fmt::Display for TypeError<'tcx> {
159159
)
160160
}),
161161
IntMismatch(ref values) => {
162-
write!(f, "expected `{:?}`, found `{:?}`", values.expected, values.found)
162+
let expected = match values.expected {
163+
ty::IntVarValue::IntType(ty) => ty.name_str(),
164+
ty::IntVarValue::UintType(ty) => ty.name_str(),
165+
};
166+
let found = match values.found {
167+
ty::IntVarValue::IntType(ty) => ty.name_str(),
168+
ty::IntVarValue::UintType(ty) => ty.name_str(),
169+
};
170+
write!(f, "expected `{}`, found `{}`", expected, found)
163171
}
164172
FloatMismatch(ref values) => {
165-
write!(f, "expected `{:?}`, found `{:?}`", values.expected, values.found)
173+
write!(
174+
f,
175+
"expected `{}`, found `{}`",
176+
values.expected.name_str(),
177+
values.found.name_str()
178+
)
166179
}
167180
VariadicMismatch(ref values) => write!(
168181
f,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/* Checks whether primitive type names are formatted correctly in the
2+
* error messages about mismatched types (#84976).
3+
*/
4+
5+
fn foo(length: &u32) -> i32 {
6+
0
7+
}
8+
9+
fn bar(length: &f32) -> f64 {
10+
0.0
11+
}
12+
13+
fn main() {
14+
let mut length = 0;
15+
length = { foo(&length) };
16+
//~^ ERROR mismatched types [E0308]
17+
length = foo(&length);
18+
//~^ ERROR mismatched types [E0308]
19+
20+
let mut float_length = 0.0;
21+
float_length = { bar(&float_length) };
22+
//~^ ERROR mismatched types [E0308]
23+
float_length = bar(&float_length);
24+
//~^ ERROR mismatched types [E0308]
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/issue-84976.rs:15:16
3+
|
4+
LL | length = { foo(&length) };
5+
| ^^^^^^^^^^^^ expected `u32`, found `i32`
6+
7+
error[E0308]: mismatched types
8+
--> $DIR/issue-84976.rs:17:14
9+
|
10+
LL | length = foo(&length);
11+
| ^^^^^^^^^^^^ expected `u32`, found `i32`
12+
13+
error[E0308]: mismatched types
14+
--> $DIR/issue-84976.rs:21:22
15+
|
16+
LL | float_length = { bar(&float_length) };
17+
| ^^^^^^^^^^^^^^^^^^ expected `f32`, found `f64`
18+
19+
error[E0308]: mismatched types
20+
--> $DIR/issue-84976.rs:23:20
21+
|
22+
LL | float_length = bar(&float_length);
23+
| ^^^^^^^^^^^^^^^^^^ expected `f32`, found `f64`
24+
25+
error: aborting due to 4 previous errors
26+
27+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)