Skip to content

Commit de0b4f9

Browse files
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.
2 parents 110d289 + b4c62d5 commit de0b4f9

9 files changed

+64
-31
lines changed

compiler/rustc_typeck/src/collect/type_of.rs

+23-21
Original file line numberDiff line numberDiff line change
@@ -752,29 +752,31 @@ fn infer_placeholder_type<'a>(
752752
// us to improve in typeck so we do that now.
753753
match tcx.sess.diagnostic().steal_diagnostic(span, StashKey::ItemNoType) {
754754
Some(mut err) => {
755-
// The parser provided a sub-optimal `HasPlaceholders` suggestion for the type.
756-
// We are typeck and have the real type, so remove that and suggest the actual type.
757-
err.suggestions.clear();
758-
759-
// Suggesting unnameable types won't help.
760-
let mut mk_nameable = MakeNameable::new(tcx);
761-
let ty = mk_nameable.fold_ty(ty);
762-
let sugg_ty = if mk_nameable.success { Some(ty) } else { None };
763-
if let Some(sugg_ty) = sugg_ty {
764-
err.span_suggestion(
765-
span,
766-
&format!("provide a type for the {item}", item = kind),
767-
format!("{}: {}", item_ident, sugg_ty),
768-
Applicability::MachineApplicable,
769-
);
770-
} else {
771-
err.span_note(
772-
tcx.hir().body(body_id).value.span,
773-
&format!("however, the inferred type `{}` cannot be named", ty.to_string()),
774-
);
755+
if !ty.references_error() {
756+
// The parser provided a sub-optimal `HasPlaceholders` suggestion for the type.
757+
// We are typeck and have the real type, so remove that and suggest the actual type.
758+
err.suggestions.clear();
759+
760+
// Suggesting unnameable types won't help.
761+
let mut mk_nameable = MakeNameable::new(tcx);
762+
let ty = mk_nameable.fold_ty(ty);
763+
let sugg_ty = if mk_nameable.success { Some(ty) } else { None };
764+
if let Some(sugg_ty) = sugg_ty {
765+
err.span_suggestion(
766+
span,
767+
&format!("provide a type for the {item}", item = kind),
768+
format!("{}: {}", item_ident, sugg_ty),
769+
Applicability::MachineApplicable,
770+
);
771+
} else {
772+
err.span_note(
773+
tcx.hir().body(body_id).value.span,
774+
&format!("however, the inferred type `{}` cannot be named", ty.to_string()),
775+
);
776+
}
775777
}
776778

777-
err.emit_unless(ty.references_error());
779+
err.emit();
778780
}
779781
None => {
780782
let mut diag = bad_placeholder_type(tcx, vec![span], kind);

src/test/ui/parser/issue-89574.rs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
fn main() {
2+
const EMPTY_ARRAY = [];
3+
//~^ missing type for `const` item
4+
}

src/test/ui/parser/issue-89574.stderr

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: missing type for `const` item
2+
--> $DIR/issue-89574.rs:2:11
3+
|
4+
LL | const EMPTY_ARRAY = [];
5+
| ^^^^^^^^^^^ help: provide a type for the item: `EMPTY_ARRAY: <type>`
6+
7+
error: aborting due to previous error
8+

src/test/ui/parser/item-free-const-no-body-semantic-fail.rs

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ fn main() {}
44

55
const A: u8; //~ ERROR free constant item without body
66
const B; //~ ERROR free constant item without body
7+
//~^ ERROR missing type for `const` item

src/test/ui/parser/item-free-const-no-body-semantic-fail.stderr

+7-1
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,11 @@ LL | const B;
1414
| |
1515
| help: provide a definition for the constant: `= <expr>;`
1616

17-
error: aborting due to 2 previous errors
17+
error: missing type for `const` item
18+
--> $DIR/item-free-const-no-body-semantic-fail.rs:6:7
19+
|
20+
LL | const B;
21+
| ^ help: provide a type for the item: `B: <type>`
22+
23+
error: aborting due to 3 previous errors
1824

src/test/ui/parser/item-free-static-no-body-semantic-fail.rs

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ fn main() {}
44

55
static A: u8; //~ ERROR free static item without body
66
static B; //~ ERROR free static item without body
7+
//~^ ERROR missing type for `static` item
78

89
static mut C: u8; //~ ERROR free static item without body
910
static mut D; //~ ERROR free static item without body
11+
//~^ ERROR missing type for `static mut` item

src/test/ui/parser/item-free-static-no-body-semantic-fail.stderr

+15-3
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,32 @@ LL | static B;
1515
| help: provide a definition for the static: `= <expr>;`
1616

1717
error: free static item without body
18-
--> $DIR/item-free-static-no-body-semantic-fail.rs:8:1
18+
--> $DIR/item-free-static-no-body-semantic-fail.rs:9:1
1919
|
2020
LL | static mut C: u8;
2121
| ^^^^^^^^^^^^^^^^-
2222
| |
2323
| help: provide a definition for the static: `= <expr>;`
2424

2525
error: free static item without body
26-
--> $DIR/item-free-static-no-body-semantic-fail.rs:9:1
26+
--> $DIR/item-free-static-no-body-semantic-fail.rs:10:1
2727
|
2828
LL | static mut D;
2929
| ^^^^^^^^^^^^-
3030
| |
3131
| help: provide a definition for the static: `= <expr>;`
3232

33-
error: aborting due to 4 previous errors
33+
error: missing type for `static` item
34+
--> $DIR/item-free-static-no-body-semantic-fail.rs:6:8
35+
|
36+
LL | static B;
37+
| ^ help: provide a type for the item: `B: <type>`
38+
39+
error: missing type for `static mut` item
40+
--> $DIR/item-free-static-no-body-semantic-fail.rs:10:12
41+
|
42+
LL | static mut D;
43+
| ^ help: provide a type for the item: `D: <type>`
44+
45+
error: aborting due to 6 previous errors
3446

src/test/ui/typeck/issue-79040.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
fn main() {
22
const FOO = "hello" + 1; //~ ERROR cannot add `{integer}` to `&str`
3-
//~^ ERROR cannot add `{integer}` to `&str`
3+
//~^ missing type for `const` item
44
println!("{}", FOO);
55
}

src/test/ui/typeck/issue-79040.stderr

+3-5
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@ LL | const FOO = "hello" + 1;
66
| |
77
| &str
88

9-
error[E0369]: cannot add `{integer}` to `&str`
10-
--> $DIR/issue-79040.rs:2:25
9+
error: missing type for `const` item
10+
--> $DIR/issue-79040.rs:2:11
1111
|
1212
LL | const FOO = "hello" + 1;
13-
| ------- ^ - {integer}
14-
| |
15-
| &str
13+
| ^^^ help: provide a type for the item: `FOO: <type>`
1614

1715
error: aborting due to 2 previous errors
1816

0 commit comments

Comments
 (0)