]> git.lizzy.rs Git - rust.git/commitdiff
middle: reset loop labels while visiting closure
authorGeorg Brandl <georg@python.org>
Mon, 2 May 2016 14:47:07 +0000 (16:47 +0200)
committerGeorg Brandl <georg@python.org>
Mon, 2 May 2016 14:48:36 +0000 (16:48 +0200)
This should fix #31754 and follow-up #25343.  Before the latter, the
closure was visited twice in the context of the enclosing fn, which
made even a single closure with a loop label emit a warning.

With this change, the closure is still visited within the context
of the main fn (which is intended, since it is not a separate item)
but resets the found loop labels while being visited.

Fixes: #31754
src/librustc/middle/resolve_lifetime.rs
src/test/run-pass/issue-25343.rs

index 23eb5a56c8439e4f25469f09bb1c1137d1216d63..5c062316310101a35478092e4d5d3418fd5a250d 100644 (file)
@@ -193,7 +193,12 @@ fn visit_fn(&mut self, fk: FnKind<'v>, fd: &'v hir::FnDecl,
                 })
             }
             FnKind::Closure(_) => {
-                self.add_scope_and_walk_fn(fk, fd, b, s, fn_id)
+                // Closures have their own set of labels, save labels just
+                // like for foreign items above.
+                let saved = replace(&mut self.labels_in_fn, vec![]);
+                let result = self.add_scope_and_walk_fn(fk, fd, b, s, fn_id);
+                replace(&mut self.labels_in_fn, saved);
+                result
             }
         }
     }
index 9e01d577276b8a7fc53fcae40614d0f4a35d0da0..64e7350fb824471fc39a716efd39822b7c7a619f 100644 (file)
@@ -8,9 +8,24 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
+#[allow(unused)]
 fn main() {
     || {
         'label: loop {
         }
     };
+
+    // More cases added from issue 31754
+
+    'label2: loop {
+        break;
+    }
+
+    let closure = || {
+        'label2: loop {}
+    };
+
+    fn inner_fn() {
+        'label2: loop {}
+    }
 }