about summary refs log tree commit diff
diff options
context:
space:
mode:
authorhkalbasi <hamidrezakalbasi@protonmail.com>2023-05-02 02:28:14 +0330
committerhkalbasi <hamidrezakalbasi@protonmail.com>2023-05-02 03:11:56 +0330
commit3a3c3630a25bdd08db0869022e31f9f380f895f6 (patch)
treeb3b9003fdeb1902f330b56ec86a1f247ea3bd622
parent6312fbf521e42e5a5ff193ceee6a8c7735be3a45 (diff)
downloadrust-3a3c3630a25bdd08db0869022e31f9f380f895f6.tar.gz
rust-3a3c3630a25bdd08db0869022e31f9f380f895f6.zip
fix break-outside-of-loop false positive in try block
-rw-r--r--crates/hir-def/src/body/lower.rs3
-rw-r--r--crates/ide-diagnostics/src/handlers/break_outside_of_loop.rs18
2 files changed, 20 insertions, 1 deletions
diff --git a/crates/hir-def/src/body/lower.rs b/crates/hir-def/src/body/lower.rs
index d31340fe8f3..611031eb8c4 100644
--- a/crates/hir-def/src/body/lower.rs
+++ b/crates/hir-def/src/body/lower.rs
@@ -505,6 +505,7 @@ impl ExprCollector<'_> {
                     .map(|it| Interned::new(TypeRef::from_ast(&this.ctx(), it)));
 
                 let prev_is_lowering_generator = mem::take(&mut this.is_lowering_generator);
+                let prev_try_block_label = this.current_try_block_label.take();
 
                 let body = this.collect_expr_opt(e.body());
 
@@ -520,11 +521,11 @@ impl ExprCollector<'_> {
                 } else {
                     ClosureKind::Closure
                 };
-                this.is_lowering_generator = prev_is_lowering_generator;
                 let capture_by =
                     if e.move_token().is_some() { CaptureBy::Value } else { CaptureBy::Ref };
                 this.is_lowering_generator = prev_is_lowering_generator;
                 this.current_binding_owner = prev_binding_owner;
+                this.current_try_block_label = prev_try_block_label;
                 this.body.exprs[result_expr_id] = Expr::Closure {
                     args: args.into(),
                     arg_types: arg_types.into(),
diff --git a/crates/ide-diagnostics/src/handlers/break_outside_of_loop.rs b/crates/ide-diagnostics/src/handlers/break_outside_of_loop.rs
index 89aa437d75d..7baa7b64268 100644
--- a/crates/ide-diagnostics/src/handlers/break_outside_of_loop.rs
+++ b/crates/ide-diagnostics/src/handlers/break_outside_of_loop.rs
@@ -135,4 +135,22 @@ fn test() {
 "#,
         );
     }
+
+    #[test]
+    fn try_block_desugaring_inside_closure() {
+        // regression test for #14701
+        check_diagnostics(
+            r#"
+//- minicore: option, try
+fn test() {
+    try {
+        || {
+            let x = Some(2);
+            Some(x?)
+        };
+    };
+}
+"#,
+        );
+    }
 }