Skip to content

Commit febd7ee

Browse files
committed
Make let _ = e; have the same semantics as e;
The first case was getting treated like a variable binding, meaning that if e had a destructor, it wouldn't run until the end of the enclosing scope. To me it seems less confusing for let _ = e; and e; to work exactly the same way, so now, the destructor for e runs immediately in both cases.
1 parent d99ca69 commit febd7ee

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

src/rustc/middle/trans/base.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4206,9 +4206,26 @@ fn build_return(bcx: block) {
42064206
Br(bcx, bcx.fcx.llreturn);
42074207
}
42084208

4209+
fn ignore_lhs(_bcx: block, local: @ast::local) -> bool {
4210+
match local.node.pat.node {
4211+
ast::pat_wild => true, _ => false
4212+
}
4213+
}
4214+
42094215
fn init_local(bcx: block, local: @ast::local) -> block {
42104216
let _icx = bcx.insn_ctxt(~"init_local");
42114217
let ty = node_id_type(bcx, local.node.id);
4218+
4219+
if ignore_lhs(bcx, local) {
4220+
// Handle let _ = e; just like e;
4221+
match local.node.init {
4222+
some(init) => {
4223+
return trans_expr(bcx, init.expr, ignore);
4224+
}
4225+
none => { return bcx; }
4226+
}
4227+
}
4228+
42124229
let llptr = match bcx.fcx.lllocals.find(local.node.id) {
42134230
some(local_mem(v)) => v,
42144231
_ => { bcx.tcx().sess.span_bug(local.span,

src/test/run-pass/issue-2735-2.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// This test should behave exactly like issue-2735-3
2+
class defer {
3+
let b: &mut bool;
4+
new(b: &mut bool) {
5+
self.b = b;
6+
}
7+
drop { *(self.b) = true; }
8+
}
9+
10+
fn main() {
11+
let mut dtor_ran = false;
12+
let _ = defer(&mut dtor_ran);
13+
assert(dtor_ran);
14+
}

src/test/run-pass/issue-2735-3.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// This test should behave exactly like issue-2735-2
2+
class defer {
3+
let b: &mut bool;
4+
new(b: &mut bool) {
5+
self.b = b;
6+
}
7+
drop { *(self.b) = true; }
8+
}
9+
10+
fn main() {
11+
let mut dtor_ran = false;
12+
defer(&mut dtor_ran);
13+
assert(dtor_ran);
14+
}

0 commit comments

Comments
 (0)