Skip to content

Commit a4d257b

Browse files
committed
auto merge of #16954 : nick29581/rust/dst-bug-deref, r=nikomatsakis
Closes #16930 r?
2 parents 3b5d92c + ab3999f commit a4d257b

File tree

4 files changed

+81
-4
lines changed

4 files changed

+81
-4
lines changed

src/librustc/middle/trans/callee.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -816,7 +816,7 @@ pub fn trans_call_inner<'a>(
816816

817817
// The code below invokes the function, using either the Rust
818818
// conventions (if it is a rust fn) or the native conventions
819-
// (otherwise). The important part is that, when all is sad
819+
// (otherwise). The important part is that, when all is said
820820
// and done, either the return value of the function will have been
821821
// written in opt_llretslot (if it is Some) or `llresult` will be
822822
// set appropriately (otherwise).

src/librustc/middle/trans/expr.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2064,10 +2064,13 @@ fn deref_once<'a>(bcx: &'a Block<'a>,
20642064
typeck::AutoDeref(_) => unpack_datum!(bcx, auto_ref(bcx, datum, expr)),
20652065
_ => datum
20662066
};
2067-
let val = unpack_result!(bcx, trans_overloaded_op(bcx, expr, method_call,
2068-
datum, None, None));
2067+
20692068
let ref_ty = ty::ty_fn_ret(monomorphize_type(bcx, method_ty));
2070-
Datum::new(val, ref_ty, RvalueExpr(Rvalue::new(ByValue)))
2069+
let scratch = rvalue_scratch_datum(bcx, ref_ty, "overloaded_deref");
2070+
2071+
unpack_result!(bcx, trans_overloaded_op(bcx, expr, method_call,
2072+
datum, None, Some(SaveIn(scratch.val))));
2073+
scratch.to_expr_datum()
20712074
}
20722075
None => {
20732076
// Not overloaded. We already have a pointer we know how to deref.

src/test/run-pass/dst-deref-mut.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright 2014 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+
// Test that a custom deref with a fat pointer return type does not ICE
12+
13+
pub struct Arr {
14+
ptr: Box<[uint]>
15+
}
16+
17+
impl Deref<[uint]> for Arr {
18+
fn deref(&self) -> &[uint] {
19+
fail!();
20+
}
21+
}
22+
23+
impl DerefMut<[uint]> for Arr {
24+
fn deref_mut(&mut self) -> &mut [uint] {
25+
&mut *self.ptr
26+
}
27+
}
28+
29+
pub fn foo(arr: &mut Arr) {
30+
assert!(arr.len() == 3);
31+
let x: &mut [uint] = &mut **arr;
32+
assert!(x[0] == 1);
33+
assert!(x[1] == 2);
34+
assert!(x[2] == 3);
35+
}
36+
37+
fn main() {
38+
let mut a = Arr { ptr: box [1, 2, 3] };
39+
foo(&mut a);
40+
}

src/test/run-pass/dst-deref.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2014 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+
// Test that a custom deref with a fat pointer return type does not ICE
12+
13+
pub struct Arr {
14+
ptr: Box<[uint]>
15+
}
16+
17+
impl Deref<[uint]> for Arr {
18+
fn deref(&self) -> &[uint] {
19+
&*self.ptr
20+
}
21+
}
22+
23+
pub fn foo(arr: &Arr) {
24+
assert!(arr.len() == 3);
25+
let x: &[uint] = &**arr;
26+
assert!(x[0] == 1);
27+
assert!(x[1] == 2);
28+
assert!(x[2] == 3);
29+
}
30+
31+
fn main() {
32+
let a = Arr { ptr: box [1, 2, 3] };
33+
foo(&a);
34+
}

0 commit comments

Comments
 (0)