diff --git a/compiler/rustc_typeck/src/collect/type_of.rs b/compiler/rustc_typeck/src/collect/type_of.rs index 2e607de1d3371..cee3679d0a052 100644 --- a/compiler/rustc_typeck/src/collect/type_of.rs +++ b/compiler/rustc_typeck/src/collect/type_of.rs @@ -752,29 +752,31 @@ fn infer_placeholder_type<'a>( // us to improve in typeck so we do that now. match tcx.sess.diagnostic().steal_diagnostic(span, StashKey::ItemNoType) { Some(mut err) => { - // The parser provided a sub-optimal `HasPlaceholders` suggestion for the type. - // We are typeck and have the real type, so remove that and suggest the actual type. - err.suggestions.clear(); - - // Suggesting unnameable types won't help. - let mut mk_nameable = MakeNameable::new(tcx); - let ty = mk_nameable.fold_ty(ty); - let sugg_ty = if mk_nameable.success { Some(ty) } else { None }; - if let Some(sugg_ty) = sugg_ty { - err.span_suggestion( - span, - &format!("provide a type for the {item}", item = kind), - format!("{}: {}", item_ident, sugg_ty), - Applicability::MachineApplicable, - ); - } else { - err.span_note( - tcx.hir().body(body_id).value.span, - &format!("however, the inferred type `{}` cannot be named", ty.to_string()), - ); + if !ty.references_error() { + // The parser provided a sub-optimal `HasPlaceholders` suggestion for the type. + // We are typeck and have the real type, so remove that and suggest the actual type. + err.suggestions.clear(); + + // Suggesting unnameable types won't help. + let mut mk_nameable = MakeNameable::new(tcx); + let ty = mk_nameable.fold_ty(ty); + let sugg_ty = if mk_nameable.success { Some(ty) } else { None }; + if let Some(sugg_ty) = sugg_ty { + err.span_suggestion( + span, + &format!("provide a type for the {item}", item = kind), + format!("{}: {}", item_ident, sugg_ty), + Applicability::MachineApplicable, + ); + } else { + err.span_note( + tcx.hir().body(body_id).value.span, + &format!("however, the inferred type `{}` cannot be named", ty.to_string()), + ); + } } - err.emit_unless(ty.references_error()); + err.emit(); } None => { let mut diag = bad_placeholder_type(tcx, vec![span], kind); diff --git a/src/test/ui/parser/issue-89574.rs b/src/test/ui/parser/issue-89574.rs new file mode 100644 index 0000000000000..0a477f1aa5fb6 --- /dev/null +++ b/src/test/ui/parser/issue-89574.rs @@ -0,0 +1,4 @@ +fn main() { + const EMPTY_ARRAY = []; + //~^ missing type for `const` item +} diff --git a/src/test/ui/parser/issue-89574.stderr b/src/test/ui/parser/issue-89574.stderr new file mode 100644 index 0000000000000..cbee3d35155c7 --- /dev/null +++ b/src/test/ui/parser/issue-89574.stderr @@ -0,0 +1,8 @@ +error: missing type for `const` item + --> $DIR/issue-89574.rs:2:11 + | +LL | const EMPTY_ARRAY = []; + | ^^^^^^^^^^^ help: provide a type for the item: `EMPTY_ARRAY: ` + +error: aborting due to previous error + 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 15a15a207b1ca..613b3c9856171 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,3 +4,4 @@ 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-const-no-body-semantic-fail.stderr b/src/test/ui/parser/item-free-const-no-body-semantic-fail.stderr index aa75e5cee01d4..c340e958ee585 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,5 +14,11 @@ LL | const B; | | | help: provide a definition for the constant: `= ;` -error: aborting due to 2 previous errors +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: ` + +error: aborting due to 3 previous errors 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 61d3eab24d8c7..780479e3d26ac 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,6 +4,8 @@ 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 7b408323674de..4d542b79861fd 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:8:1 + --> $DIR/item-free-static-no-body-semantic-fail.rs:9:1 | LL | static mut C: u8; | ^^^^^^^^^^^^^^^^- @@ -23,12 +23,24 @@ 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:9:1 + --> $DIR/item-free-static-no-body-semantic-fail.rs:10:1 | LL | static mut D; | ^^^^^^^^^^^^- | | | help: provide a definition for the static: `= ;` -error: aborting due to 4 previous errors +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: ` + +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: ` + +error: aborting due to 6 previous errors diff --git a/src/test/ui/typeck/issue-79040.rs b/src/test/ui/typeck/issue-79040.rs index af2a9c1ba87ef..941612542207c 100644 --- a/src/test/ui/typeck/issue-79040.rs +++ b/src/test/ui/typeck/issue-79040.rs @@ -1,5 +1,5 @@ fn main() { const FOO = "hello" + 1; //~ ERROR cannot add `{integer}` to `&str` - //~^ ERROR cannot add `{integer}` to `&str` + //~^ missing type for `const` item println!("{}", FOO); } diff --git a/src/test/ui/typeck/issue-79040.stderr b/src/test/ui/typeck/issue-79040.stderr index 32049e5d96860..aec2e1ec9e4ef 100644 --- a/src/test/ui/typeck/issue-79040.stderr +++ b/src/test/ui/typeck/issue-79040.stderr @@ -6,13 +6,11 @@ LL | const FOO = "hello" + 1; | | | &str -error[E0369]: cannot add `{integer}` to `&str` - --> $DIR/issue-79040.rs:2:25 +error: missing type for `const` item + --> $DIR/issue-79040.rs:2:11 | LL | const FOO = "hello" + 1; - | ------- ^ - {integer} - | | - | &str + | ^^^ help: provide a type for the item: `FOO: ` error: aborting due to 2 previous errors