about summary refs log tree commit diff
diff options
context:
space:
mode:
authorYuki Okushi <jtitor@2k36.org>2021-05-12 07:18:04 +0900
committerGitHub <noreply@github.com>2021-05-12 07:18:04 +0900
commitc4c654f422e34530f2728c0aa4d89bd837dbfd8b (patch)
tree9e27391d7f99776ff884f0868b720e18375c7154
parent9ad5c4d8ea514a7685c2c9f7ee2e96206d477311 (diff)
parent69a4ae2030c7b0ad9e228d3021dae128a443bb19 (diff)
downloadrust-c4c654f422e34530f2728c0aa4d89bd837dbfd8b.tar.gz
rust-c4c654f422e34530f2728c0aa4d89bd837dbfd8b.zip
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.
-rw-r--r--compiler/rustc_middle/src/ty/error.rs17
-rw-r--r--src/test/ui/mismatched_types/issue-84976.rs25
-rw-r--r--src/test/ui/mismatched_types/issue-84976.stderr27
3 files changed, 67 insertions, 2 deletions
diff --git a/compiler/rustc_middle/src/ty/error.rs b/compiler/rustc_middle/src/ty/error.rs
index 008e6d015e8..96aae3bd70c 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 00000000000..db6fe0b45dc
--- /dev/null
+++ b/src/test/ui/mismatched_types/issue-84976.rs
@@ -0,0 +1,25 @@
+/* Checks whether primitive type names are formatted correctly in the
+ * error messages about mismatched types (#84976).
+ */
+
+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 00000000000..0c27e172941
--- /dev/null
+++ b/src/test/ui/mismatched_types/issue-84976.stderr
@@ -0,0 +1,27 @@
+error[E0308]: mismatched types
+  --> $DIR/issue-84976.rs:15:16
+   |
+LL |     length = { foo(&length) };
+   |                ^^^^^^^^^^^^ expected `u32`, found `i32`
+
+error[E0308]: mismatched types
+  --> $DIR/issue-84976.rs:17:14
+   |
+LL |     length = foo(&length);
+   |              ^^^^^^^^^^^^ expected `u32`, found `i32`
+
+error[E0308]: mismatched types
+  --> $DIR/issue-84976.rs:21:22
+   |
+LL |     float_length = { bar(&float_length) };
+   |                      ^^^^^^^^^^^^^^^^^^ expected `f32`, found `f64`
+
+error[E0308]: mismatched types
+  --> $DIR/issue-84976.rs:23: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`.