Skip to content

Commit 74bd233

Browse files
committedApr 16, 2014
auto merge of #13390 : alexcrichton/rust/run-some-destructors, r=brson
Previously, if statements of the form "Foo;" or "let _ = Foo;" were encountered where Foo had a destructor, the destructors were not run. This changes the relevant locations in trans to check for ty::type_needs_drop and invokes trans_to_lvalue instead of trans_into. Closes #4734 Closes #6892
2 parents b400a4d + 0cc257e commit 74bd233

File tree

4 files changed

+109
-2
lines changed

4 files changed

+109
-2
lines changed
 

‎src/librustc/middle/trans/base.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -936,7 +936,7 @@ pub fn init_local<'a>(bcx: &'a Block<'a>, local: &ast::Local)
936936
// Handle let _ = e; just like e;
937937
match local.init {
938938
Some(init) => {
939-
return expr::trans_into(bcx, init, expr::Ignore);
939+
return controlflow::trans_stmt_semi(bcx, init)
940940
}
941941
None => { return bcx; }
942942
}

‎src/librustc/middle/trans/controlflow.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use middle::trans::debuginfo;
1919
use middle::trans::cleanup;
2020
use middle::trans::cleanup::CleanupMethods;
2121
use middle::trans::expr;
22+
use middle::ty;
2223
use util::ppaux::Repr;
2324

2425
use middle::trans::type_::Type;
@@ -49,7 +50,7 @@ pub fn trans_stmt<'a>(cx: &'a Block<'a>,
4950

5051
match s.node {
5152
ast::StmtExpr(e, _) | ast::StmtSemi(e, _) => {
52-
bcx = expr::trans_into(cx, e, expr::Ignore);
53+
bcx = trans_stmt_semi(bcx, e);
5354
}
5455
ast::StmtDecl(d, _) => {
5556
match d.node {
@@ -71,6 +72,16 @@ pub fn trans_stmt<'a>(cx: &'a Block<'a>,
7172
return bcx;
7273
}
7374

75+
pub fn trans_stmt_semi<'a>(cx: &'a Block<'a>, e: &ast::Expr) -> &'a Block<'a> {
76+
let _icx = push_ctxt("trans_stmt_semi");
77+
let ty = expr_ty(cx, e);
78+
if ty::type_needs_drop(cx.tcx(), ty) {
79+
expr::trans_to_lvalue(cx, e, "stmt").bcx
80+
} else {
81+
expr::trans_into(cx, e, expr::Ignore)
82+
}
83+
}
84+
7485
pub fn trans_block<'a>(bcx: &'a Block<'a>,
7586
b: &ast::Block,
7687
mut dest: expr::Dest)

‎src/test/run-pass/issue-4734.rs

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright 2012-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+
// Ensures that destructors are run for expressions of the form "e;" where
12+
// `e` is a type which requires a destructor.
13+
14+
#![allow(path_statement)]
15+
16+
struct A { n: int }
17+
struct B;
18+
19+
static mut NUM_DROPS: uint = 0;
20+
21+
impl Drop for A {
22+
fn drop(&mut self) {
23+
unsafe { NUM_DROPS += 1; }
24+
}
25+
}
26+
27+
impl Drop for B {
28+
fn drop(&mut self) {
29+
unsafe { NUM_DROPS += 1; }
30+
}
31+
}
32+
33+
fn main() {
34+
assert_eq!(unsafe { NUM_DROPS }, 0);
35+
{ let _a = A { n: 1 }; }
36+
assert_eq!(unsafe { NUM_DROPS }, 1);
37+
{ A { n: 3 }; }
38+
assert_eq!(unsafe { NUM_DROPS }, 2);
39+
40+
{ let _b = B; }
41+
assert_eq!(unsafe { NUM_DROPS }, 3);
42+
{ B; }
43+
assert_eq!(unsafe { NUM_DROPS }, 4);
44+
}

‎src/test/run-pass/issue-6892.rs

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright 2012-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+
// Ensures that destructors are run for expressions of the form "let _ = e;"
12+
// where `e` is a type which requires a destructor.
13+
14+
struct Foo;
15+
struct Bar { x: int }
16+
struct Baz(int);
17+
18+
static mut NUM_DROPS: uint = 0;
19+
20+
impl Drop for Foo {
21+
fn drop(&mut self) {
22+
unsafe { NUM_DROPS += 1; }
23+
}
24+
}
25+
impl Drop for Bar {
26+
fn drop(&mut self) {
27+
unsafe { NUM_DROPS += 1; }
28+
}
29+
}
30+
impl Drop for Baz {
31+
fn drop(&mut self) {
32+
unsafe { NUM_DROPS += 1; }
33+
}
34+
}
35+
36+
fn main() {
37+
assert_eq!(unsafe { NUM_DROPS }, 0);
38+
{ let _x = Foo; }
39+
assert_eq!(unsafe { NUM_DROPS }, 1);
40+
{ let _x = Bar { x: 21 }; }
41+
assert_eq!(unsafe { NUM_DROPS }, 2);
42+
{ let _x = Baz(21); }
43+
assert_eq!(unsafe { NUM_DROPS }, 3);
44+
45+
assert_eq!(unsafe { NUM_DROPS }, 3);
46+
{ let _ = Foo; }
47+
assert_eq!(unsafe { NUM_DROPS }, 4);
48+
{ let _ = Bar { x: 21 }; }
49+
assert_eq!(unsafe { NUM_DROPS }, 5);
50+
{ let _ = Baz(21); }
51+
assert_eq!(unsafe { NUM_DROPS }, 6);
52+
}

0 commit comments

Comments
 (0)
Please sign in to comment.