mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-12 06:53:05 +00:00
Avoid follow-up errors on erroneous patterns
This commit is contained in:
parent
bbe9a9c20b
commit
2733b8ab8d
@ -1223,12 +1223,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||||||
|
|
||||||
// Type-check the tuple struct pattern against the expected type.
|
// Type-check the tuple struct pattern against the expected type.
|
||||||
let diag = self.demand_eqtype_pat_diag(pat.span, expected, pat_ty, pat_info.top_info);
|
let diag = self.demand_eqtype_pat_diag(pat.span, expected, pat_ty, pat_info.top_info);
|
||||||
let had_err = if let Some(err) = diag {
|
let had_err = diag.map(|diag| diag.emit());
|
||||||
err.emit();
|
|
||||||
true
|
|
||||||
} else {
|
|
||||||
false
|
|
||||||
};
|
|
||||||
|
|
||||||
// Type-check subpatterns.
|
// Type-check subpatterns.
|
||||||
if subpats.len() == variant.fields.len()
|
if subpats.len() == variant.fields.len()
|
||||||
@ -1249,6 +1244,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||||||
None,
|
None,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
if let Some(e) = had_err {
|
||||||
|
on_error(e);
|
||||||
|
return Ty::new_error(tcx, e);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
let e = self.emit_err_pat_wrong_number_of_fields(
|
let e = self.emit_err_pat_wrong_number_of_fields(
|
||||||
pat.span,
|
pat.span,
|
||||||
@ -1257,7 +1256,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||||||
subpats,
|
subpats,
|
||||||
&variant.fields.raw,
|
&variant.fields.raw,
|
||||||
expected,
|
expected,
|
||||||
had_err,
|
had_err.is_some(),
|
||||||
);
|
);
|
||||||
on_error(e);
|
on_error(e);
|
||||||
return Ty::new_error(tcx, e);
|
return Ty::new_error(tcx, e);
|
||||||
|
@ -1,22 +0,0 @@
|
|||||||
//@ known-bug: #109812
|
|
||||||
|
|
||||||
#![warn(rust_2021_incompatible_closure_captures)]
|
|
||||||
|
|
||||||
enum Either {
|
|
||||||
One(X),
|
|
||||||
Two(X),
|
|
||||||
}
|
|
||||||
|
|
||||||
struct X(Y);
|
|
||||||
|
|
||||||
struct Y;
|
|
||||||
|
|
||||||
fn move_into_fnmut() {
|
|
||||||
let x = X(Y);
|
|
||||||
|
|
||||||
consume_fnmut(|| {
|
|
||||||
let Either::Two(ref mut _t) = x;
|
|
||||||
|
|
||||||
let X(mut _t) = x;
|
|
||||||
});
|
|
||||||
}
|
|
@ -1,20 +0,0 @@
|
|||||||
//@ known-bug: rust-lang/rust#125914
|
|
||||||
enum AstKind<'ast> {
|
|
||||||
ExprInt,
|
|
||||||
}
|
|
||||||
|
|
||||||
enum Foo {
|
|
||||||
Bar(isize),
|
|
||||||
Baz,
|
|
||||||
}
|
|
||||||
|
|
||||||
enum Other {
|
|
||||||
Other1(Foo),
|
|
||||||
Other2(AstKind),
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
match Other::Other1(Foo::Baz) {
|
|
||||||
::Other::Other2(::Foo::Bar(..)) => {}
|
|
||||||
}
|
|
||||||
}
|
|
25
tests/ui/pattern/missing_lifetime.rs
Normal file
25
tests/ui/pattern/missing_lifetime.rs
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
//! This test used to ICE: rust-lang/rust#125914
|
||||||
|
//! Instead of actually analyzing the erroneous patterns,
|
||||||
|
//! we instead stop after typeck where errors are already
|
||||||
|
//! reported.
|
||||||
|
|
||||||
|
enum AstKind<'ast> {
|
||||||
|
//~^ ERROR: `'ast` is never used
|
||||||
|
ExprInt,
|
||||||
|
}
|
||||||
|
|
||||||
|
enum Foo {
|
||||||
|
Bar(isize),
|
||||||
|
Baz,
|
||||||
|
}
|
||||||
|
|
||||||
|
enum Other {
|
||||||
|
Other1(Foo),
|
||||||
|
Other2(AstKind), //~ ERROR: missing lifetime specifier
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
match Other::Other1(Foo::Baz) {
|
||||||
|
::Other::Other2(::Foo::Bar(..)) => {}
|
||||||
|
}
|
||||||
|
}
|
25
tests/ui/pattern/missing_lifetime.stderr
Normal file
25
tests/ui/pattern/missing_lifetime.stderr
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
error[E0106]: missing lifetime specifier
|
||||||
|
--> $DIR/missing_lifetime.rs:18:12
|
||||||
|
|
|
||||||
|
LL | Other2(AstKind),
|
||||||
|
| ^^^^^^^ expected named lifetime parameter
|
||||||
|
|
|
||||||
|
help: consider introducing a named lifetime parameter
|
||||||
|
|
|
||||||
|
LL ~ enum Other<'a> {
|
||||||
|
LL | Other1(Foo),
|
||||||
|
LL ~ Other2(AstKind<'a>),
|
||||||
|
|
|
||||||
|
|
||||||
|
error[E0392]: lifetime parameter `'ast` is never used
|
||||||
|
--> $DIR/missing_lifetime.rs:6:14
|
||||||
|
|
|
||||||
|
LL | enum AstKind<'ast> {
|
||||||
|
| ^^^^ unused lifetime parameter
|
||||||
|
|
|
||||||
|
= help: consider removing `'ast`, referring to it in a field, or using a marker such as `PhantomData`
|
||||||
|
|
||||||
|
error: aborting due to 2 previous errors
|
||||||
|
|
||||||
|
Some errors have detailed explanations: E0106, E0392.
|
||||||
|
For more information about an error, try `rustc --explain E0106`.
|
30
tests/ui/pattern/type_mismatch.rs
Normal file
30
tests/ui/pattern/type_mismatch.rs
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
//! This test used to ICE: rust-lang/rust#109812
|
||||||
|
//! Instead of actually analyzing the erroneous patterns,
|
||||||
|
//! we instead stop after typeck where errors are already
|
||||||
|
//! reported.
|
||||||
|
|
||||||
|
#![warn(rust_2021_incompatible_closure_captures)]
|
||||||
|
|
||||||
|
enum Either {
|
||||||
|
One(X),
|
||||||
|
Two(X),
|
||||||
|
}
|
||||||
|
|
||||||
|
struct X(Y);
|
||||||
|
|
||||||
|
struct Y;
|
||||||
|
|
||||||
|
fn consume_fnmut(_: impl FnMut()) {}
|
||||||
|
|
||||||
|
fn move_into_fnmut() {
|
||||||
|
let x = X(Y);
|
||||||
|
|
||||||
|
consume_fnmut(|| {
|
||||||
|
let Either::Two(ref mut _t) = x;
|
||||||
|
//~^ ERROR: mismatched types
|
||||||
|
|
||||||
|
let X(mut _t) = x;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {}
|
11
tests/ui/pattern/type_mismatch.stderr
Normal file
11
tests/ui/pattern/type_mismatch.stderr
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
error[E0308]: mismatched types
|
||||||
|
--> $DIR/type_mismatch.rs:23:13
|
||||||
|
|
|
||||||
|
LL | let Either::Two(ref mut _t) = x;
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^ - this expression has type `X`
|
||||||
|
| |
|
||||||
|
| expected `X`, found `Either`
|
||||||
|
|
||||||
|
error: aborting due to 1 previous error
|
||||||
|
|
||||||
|
For more information about this error, try `rustc --explain E0308`.
|
Loading…
Reference in New Issue
Block a user