Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use .name_str() to format primitive types in error messages #85187

Merged
merged 2 commits into from
May 12, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions compiler/rustc_middle/src/ty/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,23 @@ impl<'tcx> fmt::Display for TypeError<'tcx> {
)
}),
IntMismatch(ref values) => {
write!(f, "expected `{:?}`, found `{:?}`", values.expected, values.found)
let expected = match values.expected {
ty::IntVarValue::IntType(ty) => ty.name_str(),
ty::IntVarValue::UintType(ty) => ty.name_str(),
};
let found = match values.found {
ty::IntVarValue::IntType(ty) => ty.name_str(),
ty::IntVarValue::UintType(ty) => ty.name_str(),
};
write!(f, "expected `{}`, found `{}`", expected, found)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the Display output not just call write u32/i32?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If not, that's fine. That's what I would expect.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no Display implementation for IntType, UintType etc., only a derived Debug implementation that causes the wrong formatting observed in #84976 (U32 instead of u32 etc.).

}
FloatMismatch(ref values) => {
write!(f, "expected `{:?}`, found `{:?}`", values.expected, values.found)
write!(
f,
"expected `{}`, found `{}`",
values.expected.name_str(),
values.found.name_str()
)
}
VariadicMismatch(ref values) => write!(
f,
Expand Down
21 changes: 21 additions & 0 deletions src/test/ui/mismatched_types/issue-84976.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
fn foo(length: &u32) -> i32 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Maybe add a short comment here on what this test is for?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

0
}

fn bar(length: &f32) -> f64 {
0.0
}

fn main() {
let mut length = 0;
length = { foo(&length) };
//~^ ERROR mismatched types [E0308]
length = foo(&length);
//~^ ERROR mismatched types [E0308]

let mut float_length = 0.0;
float_length = { bar(&float_length) };
//~^ ERROR mismatched types [E0308]
float_length = bar(&float_length);
//~^ ERROR mismatched types [E0308]
}
27 changes: 27 additions & 0 deletions src/test/ui/mismatched_types/issue-84976.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
error[E0308]: mismatched types
--> $DIR/issue-84976.rs:11:16
|
LL | length = { foo(&length) };
| ^^^^^^^^^^^^ expected `u32`, found `i32`

error[E0308]: mismatched types
--> $DIR/issue-84976.rs:13:14
|
LL | length = foo(&length);
| ^^^^^^^^^^^^ expected `u32`, found `i32`

error[E0308]: mismatched types
--> $DIR/issue-84976.rs:17:22
|
LL | float_length = { bar(&float_length) };
| ^^^^^^^^^^^^^^^^^^ expected `f32`, found `f64`

error[E0308]: mismatched types
--> $DIR/issue-84976.rs:19:20
|
LL | float_length = bar(&float_length);
| ^^^^^^^^^^^^^^^^^^ expected `f32`, found `f64`

error: aborting due to 4 previous errors

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