]> git.lizzy.rs Git - rust.git/commitdiff
Make `if` and `while` conditions temporary
authorFlavio Percoco <flaper87@gmail.com>
Sun, 9 Feb 2014 12:44:10 +0000 (13:44 +0100)
committerFlavio Percoco <flaper87@gmail.com>
Sun, 9 Feb 2014 18:46:44 +0000 (19:46 +0100)
Closes #12033

src/librustc/middle/region.rs
src/test/run-pass/temporary-lifetime-for-conditions.rs [new file with mode: 0644]

index 86de1399acbb9b6cdf4c09791ead8719cebc18b7..232b35bb82afce3412d2215a3e143e228a3521f8 100644 (file)
@@ -501,12 +501,17 @@ fn resolve_expr(visitor: &mut RegionResolutionVisitor,
             visitor.region_maps.mark_as_terminating_scope(otherwise.id);
         }
 
-        ast::ExprIf(_, then, None) => {
+        ast::ExprIf(expr, then, None) => {
+            visitor.region_maps.mark_as_terminating_scope(expr.id);
             visitor.region_maps.mark_as_terminating_scope(then.id);
         }
 
-        ast::ExprLoop(body, _) |
-        ast::ExprWhile(_, body) => {
+        ast::ExprLoop(body, _) => {
+            visitor.region_maps.mark_as_terminating_scope(body.id);
+        }
+
+        ast::ExprWhile(expr, body) => {
+            visitor.region_maps.mark_as_terminating_scope(expr.id);
             visitor.region_maps.mark_as_terminating_scope(body.id);
         }
 
diff --git a/src/test/run-pass/temporary-lifetime-for-conditions.rs b/src/test/run-pass/temporary-lifetime-for-conditions.rs
new file mode 100644 (file)
index 0000000..1985970
--- /dev/null
@@ -0,0 +1,50 @@
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+
+// This test verifies that temporaries created for `while`'s
+// and `if` conditions are correctly cleaned up.
+
+struct Temporary;
+
+static mut DROPPED: int = 0;
+
+impl Drop for Temporary {
+    fn drop(&mut self) {
+        unsafe { DROPPED += 1; }
+    }
+}
+
+impl Temporary {
+    fn do(&self) -> bool {true}
+}
+
+fn borrow() -> ~Temporary { ~Temporary }
+
+
+pub fn main() {
+    let mut i = 0;
+
+    // This loop's condition
+    // should call `Temporary`'s
+    // `drop` 6 times.
+    while borrow().do() {
+        i += 1;
+        if i > 5 {
+            break;
+        }
+    }
+
+    // This if condition should
+    // call it 1 time
+    if borrow().do() {
+        unsafe { assert_eq!(DROPPED, 7) }
+    }
+}