Skip to content

Commit 9762532

Browse files
committed
Avoid follow-up errors on erroneous patterns
1 parent 948e40d commit 9762532

File tree

3 files changed

+8
-17
lines changed

3 files changed

+8
-17
lines changed

compiler/rustc_hir_typeck/src/pat.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -1223,12 +1223,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12231223

12241224
// Type-check the tuple struct pattern against the expected type.
12251225
let diag = self.demand_eqtype_pat_diag(pat.span, expected, pat_ty, pat_info.top_info);
1226-
let had_err = if let Some(err) = diag {
1227-
err.emit();
1228-
true
1229-
} else {
1230-
false
1231-
};
1226+
let had_err = diag.map(|diag| diag.emit());
12321227

12331228
// Type-check subpatterns.
12341229
if subpats.len() == variant.fields.len()
@@ -1249,6 +1244,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12491244
None,
12501245
);
12511246
}
1247+
if let Some(e) = had_err {
1248+
on_error(e);
1249+
return Ty::new_error(tcx, e);
1250+
}
12521251
} else {
12531252
let e = self.emit_err_pat_wrong_number_of_fields(
12541253
pat.span,
@@ -1257,7 +1256,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12571256
subpats,
12581257
&variant.fields.raw,
12591258
expected,
1260-
had_err,
1259+
had_err.is_some(),
12611260
);
12621261
on_error(e);
12631262
return Ty::new_error(tcx, e);

tests/ui/issues/issue-7092.rs

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ fn foo(x: Whatever) {
99
//~| expected enum `Whatever`
1010
//~| found enum `Option<_>`
1111
field.access(),
12-
//~^ ERROR: type annotations needed
1312
}
1413
}
1514

tests/ui/issues/issue-7092.stderr

+2-9
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,6 @@ LL | Some(field) =>
99
= note: expected enum `Whatever`
1010
found enum `Option<_>`
1111

12-
error[E0282]: type annotations needed
13-
--> $DIR/issue-7092.rs:11:19
14-
|
15-
LL | field.access(),
16-
| ^^^^^^ cannot infer type
17-
18-
error: aborting due to 2 previous errors
12+
error: aborting due to 1 previous error
1913

20-
Some errors have detailed explanations: E0282, E0308.
21-
For more information about an error, try `rustc --explain E0282`.
14+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)