about summary refs log tree commit diff
diff options
context:
space:
mode:
authorNick Cameron <ncameron@mozilla.com>2014-09-03 15:31:36 +1200
committerNick Cameron <ncameron@mozilla.com>2014-09-03 15:31:36 +1200
commitab3999f6151347169c1871627f27bf2d10ad4b3f (patch)
treebe309f6205d1563a5f430704ae9e40bfe55b28d1
parent4e5d5ba1ed7365f515b438b3ab4fcda31b182faf (diff)
downloadrust-ab3999f6151347169c1871627f27bf2d10ad4b3f.tar.gz
rust-ab3999f6151347169c1871627f27bf2d10ad4b3f.zip
Handle custom deref returning fat pointers
Closes #16930
-rw-r--r--src/librustc/middle/trans/callee.rs2
-rw-r--r--src/librustc/middle/trans/expr.rs9
-rw-r--r--src/test/run-pass/dst-deref-mut.rs40
-rw-r--r--src/test/run-pass/dst-deref.rs34
4 files changed, 81 insertions, 4 deletions
diff --git a/src/librustc/middle/trans/callee.rs b/src/librustc/middle/trans/callee.rs
index 7566cfbac56..4513eb4f520 100644
--- a/src/librustc/middle/trans/callee.rs
+++ b/src/librustc/middle/trans/callee.rs
@@ -816,7 +816,7 @@ pub fn trans_call_inner<'a>(
 
     // The code below invokes the function, using either the Rust
     // conventions (if it is a rust fn) or the native conventions
-    // (otherwise).  The important part is that, when all is sad
+    // (otherwise).  The important part is that, when all is said
     // and done, either the return value of the function will have been
     // written in opt_llretslot (if it is Some) or `llresult` will be
     // set appropriately (otherwise).
diff --git a/src/librustc/middle/trans/expr.rs b/src/librustc/middle/trans/expr.rs
index ce4c5c9de16..52a76bf349e 100644
--- a/src/librustc/middle/trans/expr.rs
+++ b/src/librustc/middle/trans/expr.rs
@@ -2045,10 +2045,13 @@ fn deref_once<'a>(bcx: &'a Block<'a>,
                 typeck::AutoDeref(_) => unpack_datum!(bcx, auto_ref(bcx, datum, expr)),
                 _ => datum
             };
-            let val = unpack_result!(bcx, trans_overloaded_op(bcx, expr, method_call,
-                                                              datum, None, None));
+
             let ref_ty = ty::ty_fn_ret(monomorphize_type(bcx, method_ty));
-            Datum::new(val, ref_ty, RvalueExpr(Rvalue::new(ByValue)))
+            let scratch = rvalue_scratch_datum(bcx, ref_ty, "overloaded_deref");
+
+            unpack_result!(bcx, trans_overloaded_op(bcx, expr, method_call,
+                                                    datum, None, Some(SaveIn(scratch.val))));
+            scratch.to_expr_datum()
         }
         None => {
             // Not overloaded. We already have a pointer we know how to deref.
diff --git a/src/test/run-pass/dst-deref-mut.rs b/src/test/run-pass/dst-deref-mut.rs
new file mode 100644
index 00000000000..d59c24e07b8
--- /dev/null
+++ b/src/test/run-pass/dst-deref-mut.rs
@@ -0,0 +1,40 @@
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+// Test that a custom deref with a fat pointer return type does not ICE
+
+pub struct Arr {
+    ptr: Box<[uint]>
+}
+
+impl Deref<[uint]> for Arr {
+    fn deref(&self) -> &[uint] {
+        fail!();
+    }
+}
+
+impl DerefMut<[uint]> for Arr {
+    fn deref_mut(&mut self) -> &mut [uint] {
+        &mut *self.ptr
+    }
+}
+
+pub fn foo(arr: &mut Arr) {
+    assert!(arr.len() == 3);
+    let x: &mut [uint] = &mut **arr;
+    assert!(x[0] == 1);
+    assert!(x[1] == 2);
+    assert!(x[2] == 3);
+}
+
+fn main() {
+    let mut a = Arr { ptr: box [1, 2, 3] };
+    foo(&mut a);
+}
diff --git a/src/test/run-pass/dst-deref.rs b/src/test/run-pass/dst-deref.rs
new file mode 100644
index 00000000000..43b7d116d30
--- /dev/null
+++ b/src/test/run-pass/dst-deref.rs
@@ -0,0 +1,34 @@
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+// Test that a custom deref with a fat pointer return type does not ICE
+
+pub struct Arr {
+    ptr: Box<[uint]>
+}
+
+impl Deref<[uint]> for Arr {
+    fn deref(&self) -> &[uint] {
+        &*self.ptr
+    }
+}
+
+pub fn foo(arr: &Arr) {
+    assert!(arr.len() == 3);
+    let x: &[uint] = &**arr;
+    assert!(x[0] == 1);
+    assert!(x[1] == 2);
+    assert!(x[2] == 3);
+}
+
+fn main() {
+    let a = Arr { ptr: box [1, 2, 3] };
+    foo(&a);
+}