Skip to content

Commit ab3999f

Browse files
committed
Handle custom deref returning fat pointers
Closes #16930
1 parent 4e5d5ba commit ab3999f

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
@@ -2045,10 +2045,13 @@ fn deref_once<'a>(bcx: &'a Block<'a>,
20452045
typeck::AutoDeref(_) => unpack_datum!(bcx, auto_ref(bcx, datum, expr)),
20462046
_ => datum
20472047
};
2048-
let val = unpack_result!(bcx, trans_overloaded_op(bcx, expr, method_call,
2049-
datum, None, None));
2048+
20502049
let ref_ty = ty::ty_fn_ret(monomorphize_type(bcx, method_ty));
2051-
Datum::new(val, ref_ty, RvalueExpr(Rvalue::new(ByValue)))
2050+
let scratch = rvalue_scratch_datum(bcx, ref_ty, "overloaded_deref");
2051+
2052+
unpack_result!(bcx, trans_overloaded_op(bcx, expr, method_call,
2053+
datum, None, Some(SaveIn(scratch.val))));
2054+
scratch.to_expr_datum()
20522055
}
20532056
None => {
20542057
// 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)