Skip to content

Commit b802b18

Browse files
committed
Don't call drop when taking the address of unsized fields
When taking the address of an unsized field we generate a rvalue datum for the field and then convert it to an lvalue datum. At that point, cleanup is scheduled for the field, leading to multiple drop calls. The problem is that we generate an rvalue datum for the field, since the pointer does not own the data and there's already cleanup scheduled elsewhere by the true owner. Instead, an lvalue datum must be created. Thanks to @eddyb for identifying the underlying cause and suggesting the correct fix. Fixes #25549 Fixes #25515
1 parent 84b1e08 commit b802b18

File tree

3 files changed

+73
-2
lines changed

3 files changed

+73
-2
lines changed

src/librustc_trans/trans/expr.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -730,8 +730,9 @@ fn trans_field<'blk, 'tcx, F>(bcx: Block<'blk, 'tcx>,
730730
let info = Load(bcx, get_len(bcx, base_datum.val));
731731
Store(bcx, info, get_len(bcx, scratch.val));
732732

733-
DatumBlock::new(bcx, scratch.to_expr_datum())
734-
733+
// Always generate an lvalue datum, because this pointer doesn't own
734+
// the data and cleanup is scheduled elsewhere.
735+
DatumBlock::new(bcx, Datum::new(scratch.val, scratch.ty, LvalueExpr))
735736
}
736737
})
737738

src/test/run-pass/issue-25515.rs

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2015 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+
use std::rc::Rc;
12+
13+
struct Foo<'r>(&'r mut i32);
14+
15+
impl<'r> Drop for Foo<'r> {
16+
fn drop(&mut self) {
17+
*self.0 += 1;
18+
}
19+
}
20+
21+
fn main() {
22+
let mut drops = 0;
23+
24+
{
25+
let _: Rc<Send> = Rc::new(Foo(&mut drops));
26+
}
27+
28+
assert_eq!(1, drops);
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright 2015 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+
struct Foo<'r>(&'r mut i32);
12+
13+
impl<'r> Drop for Foo<'r> {
14+
fn drop(&mut self) {
15+
*self.0 += 1;
16+
}
17+
}
18+
19+
trait Trait {}
20+
impl<'r> Trait for Foo<'r> {}
21+
22+
struct Holder<T: ?Sized>(T);
23+
24+
fn main() {
25+
let mut drops = 0;
26+
27+
{
28+
let y = &Holder([Foo(&mut drops)]) as &Holder<[Foo]>;
29+
// this used to cause an extra drop of the Foo instance
30+
let x = &y.0;
31+
}
32+
assert_eq!(1, drops);
33+
34+
drops = 0;
35+
{
36+
let y = &Holder(Foo(&mut drops)) as &Holder<Trait>;
37+
// this used to cause an extra drop of the Foo instance
38+
let x = &y.0;
39+
}
40+
assert_eq!(1, drops);
41+
}

0 commit comments

Comments
 (0)