Skip to content

Commit 47e1445

Browse files
committed
auto merge of #12134 : FlaPer87/rust/temporary-conditions, r=nikomatsakis
Closes #12033 IR Before: ```llvm normal-return: ; preds = %while_body %113 = load i64* %i %114 = sub i64 %113, 1 store i64 %114, i64* %i br label %while_cond ``` IR After: ```llvm normal-return: ; preds = %while_cond store i8 %11, i8* %0 %18 = load i8* %0, !range !0 call void @_ZN9Temporary9glue_drop19he4ee51d3c03b9cf4ajE(%struct.Temporary* %10) %19 = bitcast %struct.Temporary* %10 to i8* call void @_ZN2rt11global_heap14exchange_free_19h4fabdf24a2250163aj4v0.0E(i8* %19) %20 = icmp ne i8 %18, 0 br i1 %20, label %while_body, label %while_exit ```
2 parents d0affa5 + b0ef791 commit 47e1445

File tree

2 files changed

+58
-3
lines changed

2 files changed

+58
-3
lines changed

src/librustc/middle/region.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -501,12 +501,17 @@ fn resolve_expr(visitor: &mut RegionResolutionVisitor,
501501
visitor.region_maps.mark_as_terminating_scope(otherwise.id);
502502
}
503503

504-
ast::ExprIf(_, then, None) => {
504+
ast::ExprIf(expr, then, None) => {
505+
visitor.region_maps.mark_as_terminating_scope(expr.id);
505506
visitor.region_maps.mark_as_terminating_scope(then.id);
506507
}
507508

508-
ast::ExprLoop(body, _) |
509-
ast::ExprWhile(_, body) => {
509+
ast::ExprLoop(body, _) => {
510+
visitor.region_maps.mark_as_terminating_scope(body.id);
511+
}
512+
513+
ast::ExprWhile(expr, body) => {
514+
visitor.region_maps.mark_as_terminating_scope(expr.id);
510515
visitor.region_maps.mark_as_terminating_scope(body.id);
511516
}
512517

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
12+
// This test verifies that temporaries created for `while`'s
13+
// and `if` conditions are correctly cleaned up.
14+
15+
struct Temporary;
16+
17+
static mut DROPPED: int = 0;
18+
19+
impl Drop for Temporary {
20+
fn drop(&mut self) {
21+
unsafe { DROPPED += 1; }
22+
}
23+
}
24+
25+
impl Temporary {
26+
fn do(&self) -> bool {true}
27+
}
28+
29+
fn borrow() -> ~Temporary { ~Temporary }
30+
31+
32+
pub fn main() {
33+
let mut i = 0;
34+
35+
// This loop's condition
36+
// should call `Temporary`'s
37+
// `drop` 6 times.
38+
while borrow().do() {
39+
i += 1;
40+
if i > 5 {
41+
break;
42+
}
43+
}
44+
45+
// This if condition should
46+
// call it 1 time
47+
if borrow().do() {
48+
unsafe { assert_eq!(DROPPED, 7) }
49+
}
50+
}

0 commit comments

Comments
 (0)