summary refs log tree commit diff
path: root/src/test
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2015-06-10 03:15:16 +0000
committerbors <bors@rust-lang.org>2015-06-10 03:15:16 +0000
commit172cd83490cc66065e72861aed53e3efec29b34f (patch)
tree8e6f40e39f444f46c2da396728adf9e0a94f242c /src/test
parent5ea3ed0fd0ee8c17ddfdfee58381053a849fdd03 (diff)
parentd6b7ca041a68a9056b2295a3901800c727ccee03 (diff)
downloadrust-172cd83490cc66065e72861aed53e3efec29b34f.tar.gz
rust-172cd83490cc66065e72861aed53e3efec29b34f.zip
Auto merge of #26058 - Kimundi:issue15609, r=nikomatsakis
Closes #15609
Diffstat (limited to 'src/test')
-rw-r--r--src/test/run-pass/overloaded_deref_with_ref_pattern.rs102
-rw-r--r--src/test/run-pass/overloaded_deref_with_ref_pattern_issue15609.rs44
2 files changed, 146 insertions, 0 deletions
diff --git a/src/test/run-pass/overloaded_deref_with_ref_pattern.rs b/src/test/run-pass/overloaded_deref_with_ref_pattern.rs
new file mode 100644
index 00000000000..f72d4964251
--- /dev/null
+++ b/src/test/run-pass/overloaded_deref_with_ref_pattern.rs
@@ -0,0 +1,102 @@
+// Copyright 2015 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 we choose Deref or DerefMut appropriately based on mutability of ref bindings (#15609).
+
+use std::ops::{Deref, DerefMut};
+
+struct DerefOk<T>(T);
+struct DerefMutOk<T>(T);
+
+impl<T> Deref for DerefOk<T> {
+    type Target = T;
+    fn deref(&self) -> &Self::Target {
+        &self.0
+    }
+}
+
+impl<T> DerefMut for DerefOk<T> {
+    fn deref_mut(&mut self) -> &mut Self::Target {
+        panic!()
+    }
+}
+
+impl<T> Deref for DerefMutOk<T> {
+    type Target = T;
+    fn deref(&self) -> &Self::Target {
+        panic!()
+    }
+}
+
+impl<T> DerefMut for DerefMutOk<T> {
+    fn deref_mut(&mut self) -> &mut Self::Target {
+        &mut self.0
+    }
+}
+
+fn main() {
+    // Check that mutable ref binding in match picks DerefMut
+    let mut b = DerefMutOk(0);
+    match *b {
+        ref mut n => n,
+    };
+
+    // Check that mutable ref binding in let picks DerefMut
+    let mut y = DerefMutOk(1);
+    let ref mut z = *y;
+
+    // Check that immutable ref binding in match picks Deref
+    let mut b = DerefOk(2);
+    match *b {
+        ref n => n,
+    };
+
+    // Check that immutable ref binding in let picks Deref
+    let mut y = DerefOk(3);
+    let ref z = *y;
+
+    // Check that mixed mutable/immutable ref binding in match picks DerefMut
+    let mut b = DerefMutOk((0, 9));
+    match *b {
+        (ref mut n, ref m) => (n, m),
+    };
+
+    let mut b = DerefMutOk((0, 9));
+    match *b {
+        (ref n, ref mut m) => (n, m),
+    };
+
+    // Check that mixed mutable/immutable ref binding in let picks DerefMut
+    let mut y = DerefMutOk((1, 8));
+    let (ref mut z, ref a) = *y;
+
+    let mut y = DerefMutOk((1, 8));
+    let (ref z, ref mut a) = *y;
+
+    // Check that multiple immutable ref bindings in match picks Deref
+    let mut b = DerefOk((2, 7));
+    match *b {
+        (ref n, ref m) => (n, m),
+    };
+
+    // Check that multiple immutable ref bindings in let picks Deref
+    let mut y = DerefOk((3, 6));
+    let (ref z, ref a) = *y;
+
+    // Check that multiple mutable ref bindings in match picks DerefMut
+    let mut b = DerefMutOk((4, 5));
+    match *b {
+        (ref mut n, ref mut m) => (n, m),
+    };
+
+    // Check that multiple mutable ref bindings in let picks DerefMut
+    let mut y = DerefMutOk((5, 4));
+    let (ref mut z, ref mut a) = *y;
+}
diff --git a/src/test/run-pass/overloaded_deref_with_ref_pattern_issue15609.rs b/src/test/run-pass/overloaded_deref_with_ref_pattern_issue15609.rs
new file mode 100644
index 00000000000..e5eb6ab8f61
--- /dev/null
+++ b/src/test/run-pass/overloaded_deref_with_ref_pattern_issue15609.rs
@@ -0,0 +1,44 @@
+// Copyright 2015 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 we choose Deref or DerefMut appropriately based on mutability of ref bindings (#15609).
+
+fn main() {
+    use std::cell::RefCell;
+
+    struct S {
+        node: E,
+    }
+
+    enum E {
+        Foo(u32),
+        Bar,
+    }
+
+    // Check match
+    let x = RefCell::new(S { node: E::Foo(0) });
+
+    let mut b = x.borrow_mut();
+    match b.node {
+        E::Foo(ref mut n) => *n += 1,
+        _ => (),
+    }
+
+    // Check let
+    let x = RefCell::new(0);
+    let mut y = x.borrow_mut();
+    let ref mut z = *y;
+
+    fn foo(a: &mut RefCell<Option<String>>) {
+        if let Some(ref mut s) = *a.borrow_mut() {
+            s.push('a')
+        }
+    }
+}