Skip to content

Commit 5209709

Browse files
committed
Fix matching of rvalues with destructors
Fixes #4542.
1 parent c492a21 commit 5209709

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

src/librustc/middle/trans/_match.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1701,7 +1701,7 @@ pub fn trans_match_inner(scope_cx: block,
17011701
None
17021702
}
17031703
};
1704-
let lldiscr = discr_datum.to_ref_llval(bcx);
1704+
let lldiscr = discr_datum.to_zeroable_ref_llval(bcx);
17051705
compile_submatch(bcx, matches, [lldiscr], chk);
17061706

17071707
let mut arm_cxs = ~[];

src/librustc/middle/trans/datum.rs

+25
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,31 @@ pub impl Datum {
471471
}
472472
}
473473

474+
fn to_zeroable_ref_llval(&self, bcx: block) -> ValueRef {
475+
/*!
476+
* Returns a by-ref llvalue that can be zeroed in order to
477+
* cancel cleanup. This is a kind of hokey bridge used
478+
* to adapt to the match code. Please don't use it for new code.
479+
*/
480+
481+
match self.mode {
482+
// All by-ref datums are zeroable, even if we *could* just
483+
// cancel the cleanup.
484+
ByRef(_) => self.val,
485+
486+
// By value datums can't be zeroed (where would you store
487+
// the zero?) so we have to spill them. Add a temp cleanup
488+
// for this spilled value and cancel the cleanup on this
489+
// current value.
490+
ByValue => {
491+
let slot = self.to_ref_llval(bcx);
492+
self.cancel_clean(bcx);
493+
add_clean_temp_mem(bcx, slot, self.ty);
494+
slot
495+
}
496+
}
497+
}
498+
474499
fn appropriate_mode(&self) -> DatumMode {
475500
/*! See the `appropriate_mode()` function */
476501

src/test/run-pass/match-vec-rvalue.rs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Tests that matching rvalues with drops does not crash.
2+
3+
fn main() {
4+
match ~[1, 2, 3] {
5+
x => {
6+
assert_eq!(x.len(), 3);
7+
assert_eq!(x[0], 1);
8+
assert_eq!(x[1], 2);
9+
assert_eq!(x[2], 3);
10+
}
11+
}
12+
}

0 commit comments

Comments
 (0)