From f740923b8cd984b5aa5d8e6af46b87c61b7ddc4d Mon Sep 17 00:00:00 2001 From: Fabian Wolff Date: Tue, 11 May 2021 18:12:36 +0200 Subject: [PATCH 1/2] Use .name_str() to format primitive types in error messages --- compiler/rustc_middle/src/ty/error.rs | 17 ++++++++++-- src/test/ui/mismatched_types/issue-84976.rs | 21 +++++++++++++++ .../ui/mismatched_types/issue-84976.stderr | 27 +++++++++++++++++++ 3 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 src/test/ui/mismatched_types/issue-84976.rs create mode 100644 src/test/ui/mismatched_types/issue-84976.stderr diff --git a/compiler/rustc_middle/src/ty/error.rs b/compiler/rustc_middle/src/ty/error.rs index 008e6d015e879..96aae3bd70cd2 100644 --- a/compiler/rustc_middle/src/ty/error.rs +++ b/compiler/rustc_middle/src/ty/error.rs @@ -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) } 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, diff --git a/src/test/ui/mismatched_types/issue-84976.rs b/src/test/ui/mismatched_types/issue-84976.rs new file mode 100644 index 0000000000000..b4ea872979ada --- /dev/null +++ b/src/test/ui/mismatched_types/issue-84976.rs @@ -0,0 +1,21 @@ +fn foo(length: &u32) -> i32 { + 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] +} diff --git a/src/test/ui/mismatched_types/issue-84976.stderr b/src/test/ui/mismatched_types/issue-84976.stderr new file mode 100644 index 0000000000000..b9f6954dbbb25 --- /dev/null +++ b/src/test/ui/mismatched_types/issue-84976.stderr @@ -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`. From 69a4ae2030c7b0ad9e228d3021dae128a443bb19 Mon Sep 17 00:00:00 2001 From: Fabian Wolff Date: Tue, 11 May 2021 21:51:58 +0200 Subject: [PATCH 2/2] Add explanatory comment to the issue-84976.rs test case --- src/test/ui/mismatched_types/issue-84976.rs | 4 ++++ src/test/ui/mismatched_types/issue-84976.stderr | 8 ++++---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/test/ui/mismatched_types/issue-84976.rs b/src/test/ui/mismatched_types/issue-84976.rs index b4ea872979ada..db6fe0b45dcd3 100644 --- a/src/test/ui/mismatched_types/issue-84976.rs +++ b/src/test/ui/mismatched_types/issue-84976.rs @@ -1,3 +1,7 @@ +/* Checks whether primitive type names are formatted correctly in the + * error messages about mismatched types (#84976). + */ + fn foo(length: &u32) -> i32 { 0 } diff --git a/src/test/ui/mismatched_types/issue-84976.stderr b/src/test/ui/mismatched_types/issue-84976.stderr index b9f6954dbbb25..0c27e17294131 100644 --- a/src/test/ui/mismatched_types/issue-84976.stderr +++ b/src/test/ui/mismatched_types/issue-84976.stderr @@ -1,23 +1,23 @@ error[E0308]: mismatched types - --> $DIR/issue-84976.rs:11:16 + --> $DIR/issue-84976.rs:15:16 | LL | length = { foo(&length) }; | ^^^^^^^^^^^^ expected `u32`, found `i32` error[E0308]: mismatched types - --> $DIR/issue-84976.rs:13:14 + --> $DIR/issue-84976.rs:17:14 | LL | length = foo(&length); | ^^^^^^^^^^^^ expected `u32`, found `i32` error[E0308]: mismatched types - --> $DIR/issue-84976.rs:17:22 + --> $DIR/issue-84976.rs:21:22 | LL | float_length = { bar(&float_length) }; | ^^^^^^^^^^^^^^^^^^ expected `f32`, found `f64` error[E0308]: mismatched types - --> $DIR/issue-84976.rs:19:20 + --> $DIR/issue-84976.rs:23:20 | LL | float_length = bar(&float_length); | ^^^^^^^^^^^^^^^^^^ expected `f32`, found `f64`