Skip to content

Commit 307f2dd

Browse files
Rollup merge of #88899 - FabianWolff:issue-88844, r=matthewjasper
Do not issue E0071 if a type error has already been reported Fixes #88844. A suggested fix is already included in the error message for E0412, so with my changes, E0071 is simply not emitted anymore if the type in question is a "type error". This makes sense, I think, because we cannot confidently state that something is "not a struct" if we couldn't resolve it properly; and it's unnecessary to pollute the output with this additional error message, as it is a direct consequence of the former error. I have also addressed the issue mentioned in #88844 (comment) by changing the fixed example in the documentation to more closely match the erroneous code example.
2 parents 765f153 + ab83d50 commit 307f2dd

File tree

4 files changed

+52
-16
lines changed

4 files changed

+52
-16
lines changed

compiler/rustc_error_codes/src/error_codes/E0071.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ form of initializer was used.
1515
For example, the code above can be fixed to:
1616

1717
```
18-
enum Foo {
19-
FirstValue(i32)
20-
}
18+
type U32 = u32;
19+
let t: U32 = 4;
20+
```
2121

22-
fn main() {
23-
let u = Foo::FirstValue(0i32);
22+
or:
2423

25-
let t = 4;
26-
}
24+
```
25+
struct U32 { value: u32 }
26+
let t = U32 { value: 4 };
2727
```

compiler/rustc_typeck/src/check/fn_ctxt/checks.rs

+19-9
Original file line numberDiff line numberDiff line change
@@ -532,15 +532,25 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
532532

533533
Some((variant, ty))
534534
} else {
535-
struct_span_err!(
536-
self.tcx.sess,
537-
path_span,
538-
E0071,
539-
"expected struct, variant or union type, found {}",
540-
ty.sort_string(self.tcx)
541-
)
542-
.span_label(path_span, "not a struct")
543-
.emit();
535+
match ty.kind() {
536+
ty::Error(_) => {
537+
// E0071 might be caused by a spelling error, which will have
538+
// already caused an error message and probably a suggestion
539+
// elsewhere. Refrain from emitting more unhelpful errors here
540+
// (issue #88844).
541+
}
542+
_ => {
543+
struct_span_err!(
544+
self.tcx.sess,
545+
path_span,
546+
E0071,
547+
"expected struct, variant or union type, found {}",
548+
ty.sort_string(self.tcx)
549+
)
550+
.span_label(path_span, "not a struct")
551+
.emit();
552+
}
553+
}
544554
None
545555
}
546556
}

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

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Regression test for #88844.
2+
3+
struct Struct { value: i32 }
4+
//~^ NOTE: similarly named struct `Struct` defined here
5+
6+
impl Stuct {
7+
//~^ ERROR: cannot find type `Stuct` in this scope [E0412]
8+
//~| HELP: a struct with a similar name exists
9+
fn new() -> Self {
10+
Self { value: 42 }
11+
}
12+
}
13+
14+
fn main() {}

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

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0412]: cannot find type `Stuct` in this scope
2+
--> $DIR/issue-88844.rs:6:6
3+
|
4+
LL | struct Struct { value: i32 }
5+
| ------------- similarly named struct `Struct` defined here
6+
...
7+
LL | impl Stuct {
8+
| ^^^^^ help: a struct with a similar name exists: `Struct`
9+
10+
error: aborting due to previous error
11+
12+
For more information about this error, try `rustc --explain E0412`.

0 commit comments

Comments
 (0)