Show break type expectation cause for let-else

This commit is contained in:
yukang 2026-01-30 07:20:28 +00:00
parent ef2657cbaf
commit 9c135ad1e0
3 changed files with 52 additions and 0 deletions

View file

@ -605,6 +605,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
parent_id = self.tcx.parent_hir_id(*hir_id);
parent
}
hir::Node::Stmt(hir::Stmt { hir_id, kind: hir::StmtKind::Let(_), .. }) => {
parent_id = self.tcx.parent_hir_id(*hir_id);
parent
}
hir::Node::LetStmt(hir::LetStmt { hir_id, .. }) => {
parent_id = self.tcx.parent_hir_id(*hir_id);
parent
}
hir::Node::Block(_) => {
parent_id = self.tcx.parent_hir_id(parent_id);
parent

View file

@ -0,0 +1,23 @@
// testcase from https://github.com/rust-lang/rust/issues/142602
pub fn main() {
// Case 1: break before let-else
let _a = loop {
if true {
break;
}
let Some(_) = Some(5) else {
break 3; //~ ERROR mismatched types
};
};
// Case 2: two let-else statements
let _b = loop {
let Some(_) = Some(5) else {
break;
};
let Some(_) = Some(4) else {
break 3; //~ ERROR mismatched types
};
};
}

View file

@ -0,0 +1,21 @@
error[E0308]: mismatched types
--> $DIR/let-else-break-help-issue-142602.rs:10:19
|
LL | break;
| ----- expected because of this `break`
...
LL | break 3;
| ^ expected `()`, found integer
error[E0308]: mismatched types
--> $DIR/let-else-break-help-issue-142602.rs:20:19
|
LL | break;
| ----- expected because of this `break`
...
LL | break 3;
| ^ expected `()`, found integer
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0308`.