From d9d69212c3d1dca7e8975d5163f09289db4a7420 Mon Sep 17 00:00:00 2001 From: Henry Boisdequin <65845077+henryboisdequin@users.noreply.github.com> Date: Wed, 3 Mar 2021 16:50:07 +0530 Subject: [PATCH 1/2] Fix diagnostic suggests adding type `[type error]` Fixes #79040 Unresolved questions: Why does this change output the diagnostic twice? Why does the CI fail when the errors are pointed out (UI tests)? --- compiler/rustc_typeck/src/collect/type_of.rs | 2 +- src/test/ui/79040.rs | 5 +++++ src/test/ui/79040.stderr | 19 +++++++++++++++++++ ...em-free-const-no-body-semantic-fail.stderr | 8 +------- ...m-free-static-no-body-semantic-fail.stderr | 14 +------------- 5 files changed, 27 insertions(+), 21 deletions(-) create mode 100644 src/test/ui/79040.rs create mode 100644 src/test/ui/79040.stderr diff --git a/compiler/rustc_typeck/src/collect/type_of.rs b/compiler/rustc_typeck/src/collect/type_of.rs index e4eabca9c3b76..92e2b5232027b 100644 --- a/compiler/rustc_typeck/src/collect/type_of.rs +++ b/compiler/rustc_typeck/src/collect/type_of.rs @@ -659,7 +659,7 @@ fn infer_placeholder_type( format!("{}: {}", item_ident, ty), Applicability::MachineApplicable, ) - .emit(); + .emit_unless(matches!(ty.kind(), ty::Error(_))); } None => { let mut diag = bad_placeholder_type(tcx, vec![span]); diff --git a/src/test/ui/79040.rs b/src/test/ui/79040.rs new file mode 100644 index 0000000000000..adc9dc8b42656 --- /dev/null +++ b/src/test/ui/79040.rs @@ -0,0 +1,5 @@ +fn main() { + const FOO = "hello" + 1; + //^~ ERROR cannot add `{integer}` to `&str` + println!("{}", FOO); +} \ No newline at end of file diff --git a/src/test/ui/79040.stderr b/src/test/ui/79040.stderr new file mode 100644 index 0000000000000..be7d2363c2d52 --- /dev/null +++ b/src/test/ui/79040.stderr @@ -0,0 +1,19 @@ +error[E0369]: cannot add `{integer}` to `&str` + --> $DIR/79040.rs:2:25 + | +LL | const FOO = "hello" + 1; + | ------- ^ - {integer} + | | + | &str + +error[E0369]: cannot add `{integer}` to `&str` + --> $DIR/79040.rs:2:25 + | +LL | const FOO = "hello" + 1; + | ------- ^ - {integer} + | | + | &str + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0369`. diff --git a/src/test/ui/parser/item-free-const-no-body-semantic-fail.stderr b/src/test/ui/parser/item-free-const-no-body-semantic-fail.stderr index 4e97229fa1a41..aa75e5cee01d4 100644 --- a/src/test/ui/parser/item-free-const-no-body-semantic-fail.stderr +++ b/src/test/ui/parser/item-free-const-no-body-semantic-fail.stderr @@ -14,11 +14,5 @@ LL | const B; | | | help: provide a definition for the constant: `= ;` -error: missing type for `const` item - --> $DIR/item-free-const-no-body-semantic-fail.rs:6:7 - | -LL | const B; - | ^ help: provide a type for the item: `B: [type error]` - -error: aborting due to 3 previous errors +error: aborting due to 2 previous errors diff --git a/src/test/ui/parser/item-free-static-no-body-semantic-fail.stderr b/src/test/ui/parser/item-free-static-no-body-semantic-fail.stderr index 60b7bb34c698b..2a270c8290fd4 100644 --- a/src/test/ui/parser/item-free-static-no-body-semantic-fail.stderr +++ b/src/test/ui/parser/item-free-static-no-body-semantic-fail.stderr @@ -30,17 +30,5 @@ LL | static mut D; | | | help: provide a definition for the static: `= ;` -error: missing type for `static` item - --> $DIR/item-free-static-no-body-semantic-fail.rs:6:8 - | -LL | static B; - | ^ help: provide a type for the item: `B: [type error]` - -error: missing type for `static mut` item - --> $DIR/item-free-static-no-body-semantic-fail.rs:10:12 - | -LL | static mut D; - | ^ help: provide a type for the item: `D: [type error]` - -error: aborting due to 6 previous errors +error: aborting due to 4 previous errors From 7d3a6f16551fa9ff54fbfa9c648c21ae0f6d550f Mon Sep 17 00:00:00 2001 From: Henry Boisdequin <65845077+henryboisdequin@users.noreply.github.com> Date: Wed, 3 Mar 2021 17:09:40 +0530 Subject: [PATCH 2/2] address comments --- compiler/rustc_typeck/src/collect/type_of.rs | 6 ++++-- src/test/ui/79040.rs | 5 ----- src/test/ui/parser/item-free-const-no-body-semantic-fail.rs | 1 - .../ui/parser/item-free-static-no-body-semantic-fail.rs | 2 -- .../ui/parser/item-free-static-no-body-semantic-fail.stderr | 4 ++-- src/test/ui/typeck/issue-79040.rs | 5 +++++ src/test/ui/{79040.stderr => typeck/issue-79040.stderr} | 4 ++-- 7 files changed, 13 insertions(+), 14 deletions(-) delete mode 100644 src/test/ui/79040.rs create mode 100644 src/test/ui/typeck/issue-79040.rs rename src/test/ui/{79040.stderr => typeck/issue-79040.stderr} (88%) diff --git a/compiler/rustc_typeck/src/collect/type_of.rs b/compiler/rustc_typeck/src/collect/type_of.rs index 92e2b5232027b..1f47e4899f18c 100644 --- a/compiler/rustc_typeck/src/collect/type_of.rs +++ b/compiler/rustc_typeck/src/collect/type_of.rs @@ -659,11 +659,12 @@ fn infer_placeholder_type( format!("{}: {}", item_ident, ty), Applicability::MachineApplicable, ) - .emit_unless(matches!(ty.kind(), ty::Error(_))); + .emit_unless(ty.references_error()); } None => { let mut diag = bad_placeholder_type(tcx, vec![span]); - if !matches!(ty.kind(), ty::Error(_)) { + + if !ty.references_error() { diag.span_suggestion( span, "replace `_` with the correct type", @@ -671,6 +672,7 @@ fn infer_placeholder_type( Applicability::MaybeIncorrect, ); } + diag.emit(); } } diff --git a/src/test/ui/79040.rs b/src/test/ui/79040.rs deleted file mode 100644 index adc9dc8b42656..0000000000000 --- a/src/test/ui/79040.rs +++ /dev/null @@ -1,5 +0,0 @@ -fn main() { - const FOO = "hello" + 1; - //^~ ERROR cannot add `{integer}` to `&str` - println!("{}", FOO); -} \ No newline at end of file diff --git a/src/test/ui/parser/item-free-const-no-body-semantic-fail.rs b/src/test/ui/parser/item-free-const-no-body-semantic-fail.rs index 613b3c9856171..15a15a207b1ca 100644 --- a/src/test/ui/parser/item-free-const-no-body-semantic-fail.rs +++ b/src/test/ui/parser/item-free-const-no-body-semantic-fail.rs @@ -4,4 +4,3 @@ fn main() {} const A: u8; //~ ERROR free constant item without body const B; //~ ERROR free constant item without body -//~^ ERROR missing type for `const` item diff --git a/src/test/ui/parser/item-free-static-no-body-semantic-fail.rs b/src/test/ui/parser/item-free-static-no-body-semantic-fail.rs index 780479e3d26ac..61d3eab24d8c7 100644 --- a/src/test/ui/parser/item-free-static-no-body-semantic-fail.rs +++ b/src/test/ui/parser/item-free-static-no-body-semantic-fail.rs @@ -4,8 +4,6 @@ fn main() {} static A: u8; //~ ERROR free static item without body static B; //~ ERROR free static item without body -//~^ ERROR missing type for `static` item static mut C: u8; //~ ERROR free static item without body static mut D; //~ ERROR free static item without body -//~^ ERROR missing type for `static mut` item diff --git a/src/test/ui/parser/item-free-static-no-body-semantic-fail.stderr b/src/test/ui/parser/item-free-static-no-body-semantic-fail.stderr index 2a270c8290fd4..7b408323674de 100644 --- a/src/test/ui/parser/item-free-static-no-body-semantic-fail.stderr +++ b/src/test/ui/parser/item-free-static-no-body-semantic-fail.stderr @@ -15,7 +15,7 @@ LL | static B; | help: provide a definition for the static: `= ;` error: free static item without body - --> $DIR/item-free-static-no-body-semantic-fail.rs:9:1 + --> $DIR/item-free-static-no-body-semantic-fail.rs:8:1 | LL | static mut C: u8; | ^^^^^^^^^^^^^^^^- @@ -23,7 +23,7 @@ LL | static mut C: u8; | help: provide a definition for the static: `= ;` error: free static item without body - --> $DIR/item-free-static-no-body-semantic-fail.rs:10:1 + --> $DIR/item-free-static-no-body-semantic-fail.rs:9:1 | LL | static mut D; | ^^^^^^^^^^^^- diff --git a/src/test/ui/typeck/issue-79040.rs b/src/test/ui/typeck/issue-79040.rs new file mode 100644 index 0000000000000..af2a9c1ba87ef --- /dev/null +++ b/src/test/ui/typeck/issue-79040.rs @@ -0,0 +1,5 @@ +fn main() { + const FOO = "hello" + 1; //~ ERROR cannot add `{integer}` to `&str` + //~^ ERROR cannot add `{integer}` to `&str` + println!("{}", FOO); +} diff --git a/src/test/ui/79040.stderr b/src/test/ui/typeck/issue-79040.stderr similarity index 88% rename from src/test/ui/79040.stderr rename to src/test/ui/typeck/issue-79040.stderr index be7d2363c2d52..32049e5d96860 100644 --- a/src/test/ui/79040.stderr +++ b/src/test/ui/typeck/issue-79040.stderr @@ -1,5 +1,5 @@ error[E0369]: cannot add `{integer}` to `&str` - --> $DIR/79040.rs:2:25 + --> $DIR/issue-79040.rs:2:25 | LL | const FOO = "hello" + 1; | ------- ^ - {integer} @@ -7,7 +7,7 @@ LL | const FOO = "hello" + 1; | &str error[E0369]: cannot add `{integer}` to `&str` - --> $DIR/79040.rs:2:25 + --> $DIR/issue-79040.rs:2:25 | LL | const FOO = "hello" + 1; | ------- ^ - {integer}