Skip to content

fix: diagnostics for 'while let' loop with label in condition #15517

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion crates/hir-def/src/body/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,27 @@ impl ExprCollector<'_> {
fn collect_while_loop(&mut self, syntax_ptr: AstPtr<ast::Expr>, e: ast::WhileExpr) -> ExprId {
let label = e.label().map(|label| self.collect_label(label));
let body = self.collect_labelled_block_opt(label, e.loop_body());
let condition = self.collect_expr_opt(e.condition());

// Labels can also be used in the condition expression, like this:
// ```
// fn main() {
// let mut optional = Some(0);
// 'my_label: while let Some(a) = match optional {
// None => break 'my_label,
// Some(val) => Some(val),
// } {
// println!("{}", a);
// optional = None;
// }
// }
// ```
let condition = match label {
Some(label) => {
self.with_labeled_rib(label, |this| this.collect_expr_opt(e.condition()))
}
None => self.collect_expr_opt(e.condition()),
};

let break_expr =
self.alloc_expr(Expr::Break { expr: None, label: None }, syntax_ptr.clone());
let if_expr = self.alloc_expr(
Expand Down
19 changes: 19 additions & 0 deletions crates/ide-diagnostics/src/handlers/undeclared_label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,25 @@ fn foo() {
);
}

#[test]
fn while_let_loop_with_label_in_condition() {
check_diagnostics(
r#"
fn foo() {
let mut optional = Some(0);

'my_label: while let Some(a) = match optional {
None => break 'my_label,
Some(val) => Some(val),
} {
optional = None;
continue 'my_label;
}
}
"#,
);
}

#[test]
fn for_loop() {
check_diagnostics(
Expand Down