about summary refs log tree commit diff
path: root/src/test
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2014-10-01 07:22:18 +0000
committerbors <bors@rust-lang.org>2014-10-01 07:22:18 +0000
commit60e7317345f246a8169bbfe721473f693d54cade (patch)
treeffd2472717f972aaca1d50b73a531c5d1dbfd150 /src/test
parentfe93a549a49983837747986797a40d85ba047cad (diff)
parent496cc4c0d4a06047c4f78965c8bc6e2c812c7812 (diff)
downloadrust-60e7317345f246a8169bbfe721473f693d54cade.tar.gz
rust-60e7317345f246a8169bbfe721473f693d54cade.zip
auto merge of #17501 : pcwalton/rust/improve-method-lookup-autoderef, r=nikomatsakis
prefer `Deref` over `DerefMut` in all other circumstances.

Because the compiler now prefers `Deref`, this can break code that
looked like:

    let mut foo = bar.borrow_mut();
    (*foo).call_something_that_requires_mutable_self();

Replace this code with:

    let mut foo = bar.baz();
    (&mut *foo).call_something_that_requires_mutable_self();

Closes #12825.

[breaking-change]

r? @nikomatsakis
Diffstat (limited to 'src/test')
-rw-r--r--src/test/run-pass/dst-deref-mut.rs1
-rw-r--r--src/test/run-pass/fixup-deref-mut.rs52
-rw-r--r--src/test/run-pass/overloaded-autoderef-count.rs2
3 files changed, 53 insertions, 2 deletions
diff --git a/src/test/run-pass/dst-deref-mut.rs b/src/test/run-pass/dst-deref-mut.rs
index d59c24e07b8..465529ac909 100644
--- a/src/test/run-pass/dst-deref-mut.rs
+++ b/src/test/run-pass/dst-deref-mut.rs
@@ -27,7 +27,6 @@ impl DerefMut<[uint]> for Arr {
 }
 
 pub fn foo(arr: &mut Arr) {
-    assert!(arr.len() == 3);
     let x: &mut [uint] = &mut **arr;
     assert!(x[0] == 1);
     assert!(x[1] == 2);
diff --git a/src/test/run-pass/fixup-deref-mut.rs b/src/test/run-pass/fixup-deref-mut.rs
new file mode 100644
index 00000000000..d2812ce1d2c
--- /dev/null
+++ b/src/test/run-pass/fixup-deref-mut.rs
@@ -0,0 +1,52 @@
+// 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.
+
+// Generic unique/owned smaht pointer.
+struct Own<T> {
+    value: *mut T
+}
+
+impl<T> Deref<T> for Own<T> {
+    fn deref<'a>(&'a self) -> &'a T {
+        unsafe { &*self.value }
+    }
+}
+
+impl<T> DerefMut<T> for Own<T> {
+    fn deref_mut<'a>(&'a mut self) -> &'a mut T {
+        unsafe { &mut *self.value }
+    }
+}
+
+struct Point {
+    x: int,
+    y: int
+}
+
+impl Point {
+    fn get(&mut self) -> (int, int) {
+        (self.x, self.y)
+    }
+}
+
+fn test0(mut x: Own<Point>) {
+    let _ = x.get();
+}
+
+fn test1(mut x: Own<Own<Own<Point>>>) {
+    let _ = x.get();
+}
+
+fn test2(mut x: Own<Own<Own<Point>>>) {
+    let _ = (**x).get();
+}
+
+fn main() {}
+
diff --git a/src/test/run-pass/overloaded-autoderef-count.rs b/src/test/run-pass/overloaded-autoderef-count.rs
index 24146c4a6ea..4cf5e074139 100644
--- a/src/test/run-pass/overloaded-autoderef-count.rs
+++ b/src/test/run-pass/overloaded-autoderef-count.rs
@@ -74,7 +74,7 @@ pub fn main() {
     assert_eq!(p.counts(), (2, 2));
 
     p.get();
-    assert_eq!(p.counts(), (2, 3));
+    assert_eq!(p.counts(), (3, 2));
 
     // Check the final state.
     assert_eq!(*p, Point {x: 3, y: 0});