Rollup merge of #89585 - nbdd0121:issue-89574, r=estebank

Emit item no type error even if type inference fails

Fix #89574

The stashed error should be emitted regardless whether ty references error or not.
This commit is contained in:
Guillaume Gomez 2021-10-07 16:24:52 +02:00 committed by GitHub
commit de0b4f9444
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 63 additions and 30 deletions

View File

@ -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();
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()),
);
// 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);

View File

@ -0,0 +1,4 @@
fn main() {
const EMPTY_ARRAY = [];
//~^ missing type for `const` item
}

View File

@ -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: <type>`
error: aborting due to previous error

View File

@ -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

View File

@ -14,5 +14,11 @@ LL | const B;
| |
| help: provide a definition for the constant: `= <expr>;`
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: <type>`
error: aborting due to 3 previous errors

View File

@ -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

View File

@ -15,7 +15,7 @@ LL | static B;
| help: provide a definition for the static: `= <expr>;`
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: `= <expr>;`
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: `= <expr>;`
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: <type>`
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: aborting due to 6 previous errors

View File

@ -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);
}

View File

@ -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: <type>`
error: aborting due to 2 previous errors