about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorGeorg Brandl <georg@python.org>2016-05-02 16:47:07 +0200
committerGeorg Brandl <georg@python.org>2016-05-02 16:48:36 +0200
commit6fed0132f314622700da9cd9f5746cfc4f4bb87f (patch)
treed37b7028de650d0b71281be802235d67ee0c2782 /src
parent855fb6192263a5c059325bb4b4e10b55e4e8ddbb (diff)
downloadrust-6fed0132f314622700da9cd9f5746cfc4f4bb87f.tar.gz
rust-6fed0132f314622700da9cd9f5746cfc4f4bb87f.zip
middle: reset loop labels while visiting closure
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
Diffstat (limited to 'src')
-rw-r--r--src/librustc/middle/resolve_lifetime.rs7
-rw-r--r--src/test/run-pass/issue-25343.rs15
2 files changed, 21 insertions, 1 deletions
diff --git a/src/librustc/middle/resolve_lifetime.rs b/src/librustc/middle/resolve_lifetime.rs
index 23eb5a56c84..5c062316310 100644
--- a/src/librustc/middle/resolve_lifetime.rs
+++ b/src/librustc/middle/resolve_lifetime.rs
@@ -193,7 +193,12 @@ impl<'a, 'v> Visitor<'v> for LifetimeContext<'a> {
                 })
             }
             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
             }
         }
     }
diff --git a/src/test/run-pass/issue-25343.rs b/src/test/run-pass/issue-25343.rs
index 9e01d577276..64e7350fb82 100644
--- a/src/test/run-pass/issue-25343.rs
+++ b/src/test/run-pass/issue-25343.rs
@@ -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 {}
+    }
 }