1 // Copyright 2015 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.
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.
11 // Test that we choose Deref or DerefMut appropriately based on mutability of ref bindings (#15609).
13 use std::ops::{Deref, DerefMut};
16 struct DerefMutOk<T>(T);
18 impl<T> Deref for DerefOk<T> {
20 fn deref(&self) -> &Self::Target {
25 impl<T> DerefMut for DerefOk<T> {
26 fn deref_mut(&mut self) -> &mut Self::Target {
31 impl<T> Deref for DerefMutOk<T> {
33 fn deref(&self) -> &Self::Target {
38 impl<T> DerefMut for DerefMutOk<T> {
39 fn deref_mut(&mut self) -> &mut Self::Target {
45 // Check that mutable ref binding in match picks DerefMut
46 let mut b = DerefMutOk(0);
51 // Check that mutable ref binding in let picks DerefMut
52 let mut y = DerefMutOk(1);
55 // Check that immutable ref binding in match picks Deref
56 let mut b = DerefOk(2);
61 // Check that immutable ref binding in let picks Deref
62 let mut y = DerefOk(3);
65 // Check that mixed mutable/immutable ref binding in match picks DerefMut
66 let mut b = DerefMutOk((0, 9));
68 (ref mut n, ref m) => (n, m),
71 let mut b = DerefMutOk((0, 9));
73 (ref n, ref mut m) => (n, m),
76 // Check that mixed mutable/immutable ref binding in let picks DerefMut
77 let mut y = DerefMutOk((1, 8));
78 let (ref mut z, ref a) = *y;
80 let mut y = DerefMutOk((1, 8));
81 let (ref z, ref mut a) = *y;
83 // Check that multiple immutable ref bindings in match picks Deref
84 let mut b = DerefOk((2, 7));
86 (ref n, ref m) => (n, m),
89 // Check that multiple immutable ref bindings in let picks Deref
90 let mut y = DerefOk((3, 6));
91 let (ref z, ref a) = *y;
93 // Check that multiple mutable ref bindings in match picks DerefMut
94 let mut b = DerefMutOk((4, 5));
96 (ref mut n, ref mut m) => (n, m),
99 // Check that multiple mutable ref bindings in let picks DerefMut
100 let mut y = DerefMutOk((5, 4));
101 let (ref mut z, ref mut a) = *y;