Skip to content

Commit 029baaa

Browse files
committed
Auto merge of #15517 - xffxff:label_in_condition, r=lnicola
fix: diagnostics for 'while let' loop with label in condition fix #15516
2 parents 0a0bb77 + 204bc2c commit 029baaa

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

crates/hir-def/src/body/lower.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,27 @@ impl ExprCollector<'_> {
744744
fn collect_while_loop(&mut self, syntax_ptr: AstPtr<ast::Expr>, e: ast::WhileExpr) -> ExprId {
745745
let label = e.label().map(|label| self.collect_label(label));
746746
let body = self.collect_labelled_block_opt(label, e.loop_body());
747-
let condition = self.collect_expr_opt(e.condition());
747+
748+
// Labels can also be used in the condition expression, like this:
749+
// ```
750+
// fn main() {
751+
// let mut optional = Some(0);
752+
// 'my_label: while let Some(a) = match optional {
753+
// None => break 'my_label,
754+
// Some(val) => Some(val),
755+
// } {
756+
// println!("{}", a);
757+
// optional = None;
758+
// }
759+
// }
760+
// ```
761+
let condition = match label {
762+
Some(label) => {
763+
self.with_labeled_rib(label, |this| this.collect_expr_opt(e.condition()))
764+
}
765+
None => self.collect_expr_opt(e.condition()),
766+
};
767+
748768
let break_expr =
749769
self.alloc_expr(Expr::Break { expr: None, label: None }, syntax_ptr.clone());
750770
let if_expr = self.alloc_expr(

crates/ide-diagnostics/src/handlers/undeclared_label.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,25 @@ fn foo() {
3434
);
3535
}
3636

37+
#[test]
38+
fn while_let_loop_with_label_in_condition() {
39+
check_diagnostics(
40+
r#"
41+
fn foo() {
42+
let mut optional = Some(0);
43+
44+
'my_label: while let Some(a) = match optional {
45+
None => break 'my_label,
46+
Some(val) => Some(val),
47+
} {
48+
optional = None;
49+
continue 'my_label;
50+
}
51+
}
52+
"#,
53+
);
54+
}
55+
3756
#[test]
3857
fn for_loop() {
3958
check_diagnostics(

0 commit comments

Comments
 (0)