Skip to content

Commit

Permalink
auto merge of #13390 : alexcrichton/rust/run-some-destructors, r=brson
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bors committed Apr 16, 2014
2 parents b400a4d + 0cc257e commit 74bd233
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/librustc/middle/trans/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -936,7 +936,7 @@ pub fn init_local<'a>(bcx: &'a Block<'a>, local: &ast::Local)
// Handle let _ = e; just like e;
match local.init {
Some(init) => {
return expr::trans_into(bcx, init, expr::Ignore);
return controlflow::trans_stmt_semi(bcx, init)
}
None => { return bcx; }
}
Expand Down
13 changes: 12 additions & 1 deletion src/librustc/middle/trans/controlflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use middle::trans::debuginfo;
use middle::trans::cleanup;
use middle::trans::cleanup::CleanupMethods;
use middle::trans::expr;
use middle::ty;
use util::ppaux::Repr;

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

match s.node {
ast::StmtExpr(e, _) | ast::StmtSemi(e, _) => {
bcx = expr::trans_into(cx, e, expr::Ignore);
bcx = trans_stmt_semi(bcx, e);
}
ast::StmtDecl(d, _) => {
match d.node {
Expand All @@ -71,6 +72,16 @@ pub fn trans_stmt<'a>(cx: &'a Block<'a>,
return bcx;
}

pub fn trans_stmt_semi<'a>(cx: &'a Block<'a>, e: &ast::Expr) -> &'a Block<'a> {
let _icx = push_ctxt("trans_stmt_semi");
let ty = expr_ty(cx, e);
if ty::type_needs_drop(cx.tcx(), ty) {
expr::trans_to_lvalue(cx, e, "stmt").bcx
} else {
expr::trans_into(cx, e, expr::Ignore)
}
}

pub fn trans_block<'a>(bcx: &'a Block<'a>,
b: &ast::Block,
mut dest: expr::Dest)
Expand Down
44 changes: 44 additions & 0 deletions src/test/run-pass/issue-4734.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2012-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.

// Ensures that destructors are run for expressions of the form "e;" where
// `e` is a type which requires a destructor.

#![allow(path_statement)]

struct A { n: int }
struct B;

static mut NUM_DROPS: uint = 0;

impl Drop for A {
fn drop(&mut self) {
unsafe { NUM_DROPS += 1; }
}
}

impl Drop for B {
fn drop(&mut self) {
unsafe { NUM_DROPS += 1; }
}
}

fn main() {
assert_eq!(unsafe { NUM_DROPS }, 0);
{ let _a = A { n: 1 }; }
assert_eq!(unsafe { NUM_DROPS }, 1);
{ A { n: 3 }; }
assert_eq!(unsafe { NUM_DROPS }, 2);

{ let _b = B; }
assert_eq!(unsafe { NUM_DROPS }, 3);
{ B; }
assert_eq!(unsafe { NUM_DROPS }, 4);
}
52 changes: 52 additions & 0 deletions src/test/run-pass/issue-6892.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2012-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.

// Ensures that destructors are run for expressions of the form "let _ = e;"
// where `e` is a type which requires a destructor.

struct Foo;
struct Bar { x: int }
struct Baz(int);

static mut NUM_DROPS: uint = 0;

impl Drop for Foo {
fn drop(&mut self) {
unsafe { NUM_DROPS += 1; }
}
}
impl Drop for Bar {
fn drop(&mut self) {
unsafe { NUM_DROPS += 1; }
}
}
impl Drop for Baz {
fn drop(&mut self) {
unsafe { NUM_DROPS += 1; }
}
}

fn main() {
assert_eq!(unsafe { NUM_DROPS }, 0);
{ let _x = Foo; }
assert_eq!(unsafe { NUM_DROPS }, 1);
{ let _x = Bar { x: 21 }; }
assert_eq!(unsafe { NUM_DROPS }, 2);
{ let _x = Baz(21); }
assert_eq!(unsafe { NUM_DROPS }, 3);

assert_eq!(unsafe { NUM_DROPS }, 3);
{ let _ = Foo; }
assert_eq!(unsafe { NUM_DROPS }, 4);
{ let _ = Bar { x: 21 }; }
assert_eq!(unsafe { NUM_DROPS }, 5);
{ let _ = Baz(21); }
assert_eq!(unsafe { NUM_DROPS }, 6);
}

0 comments on commit 74bd233

Please sign in to comment.