Skip to content

rustc: Run destructors when dest=Ignore #13390

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 16, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mind adding a small description about what's being tested? (for future readers of this test)

// 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);
}