Skip to content

Commit 3a3c363

Browse files
committed
fix break-outside-of-loop false positive in try block
1 parent 6312fbf commit 3a3c363

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,7 @@ impl ExprCollector<'_> {
505505
.map(|it| Interned::new(TypeRef::from_ast(&this.ctx(), it)));
506506

507507
let prev_is_lowering_generator = mem::take(&mut this.is_lowering_generator);
508+
let prev_try_block_label = this.current_try_block_label.take();
508509

509510
let body = this.collect_expr_opt(e.body());
510511

@@ -520,11 +521,11 @@ impl ExprCollector<'_> {
520521
} else {
521522
ClosureKind::Closure
522523
};
523-
this.is_lowering_generator = prev_is_lowering_generator;
524524
let capture_by =
525525
if e.move_token().is_some() { CaptureBy::Value } else { CaptureBy::Ref };
526526
this.is_lowering_generator = prev_is_lowering_generator;
527527
this.current_binding_owner = prev_binding_owner;
528+
this.current_try_block_label = prev_try_block_label;
528529
this.body.exprs[result_expr_id] = Expr::Closure {
529530
args: args.into(),
530531
arg_types: arg_types.into(),

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,24 @@ fn test() {
132132
// ^^^^^^^ error: can't break with a value in this position
133133
}
134134
}
135+
"#,
136+
);
137+
}
138+
139+
#[test]
140+
fn try_block_desugaring_inside_closure() {
141+
// regression test for #14701
142+
check_diagnostics(
143+
r#"
144+
//- minicore: option, try
145+
fn test() {
146+
try {
147+
|| {
148+
let x = Some(2);
149+
Some(x?)
150+
};
151+
};
152+
}
135153
"#,
136154
);
137155
}

0 commit comments

Comments
 (0)