Skip to content

Commit f740923

Browse files
committed
Use .name_str() to format primitive types in error messages
1 parent 506e75c commit f740923

File tree

3 files changed

+63
-2
lines changed

3 files changed

+63
-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,21 @@
1+
fn foo(length: &u32) -> i32 {
2+
0
3+
}
4+
5+
fn bar(length: &f32) -> f64 {
6+
0.0
7+
}
8+
9+
fn main() {
10+
let mut length = 0;
11+
length = { foo(&length) };
12+
//~^ ERROR mismatched types [E0308]
13+
length = foo(&length);
14+
//~^ ERROR mismatched types [E0308]
15+
16+
let mut float_length = 0.0;
17+
float_length = { bar(&float_length) };
18+
//~^ ERROR mismatched types [E0308]
19+
float_length = bar(&float_length);
20+
//~^ ERROR mismatched types [E0308]
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/issue-84976.rs:11:16
3+
|
4+
LL | length = { foo(&length) };
5+
| ^^^^^^^^^^^^ expected `u32`, found `i32`
6+
7+
error[E0308]: mismatched types
8+
--> $DIR/issue-84976.rs:13:14
9+
|
10+
LL | length = foo(&length);
11+
| ^^^^^^^^^^^^ expected `u32`, found `i32`
12+
13+
error[E0308]: mismatched types
14+
--> $DIR/issue-84976.rs:17: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:19: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)