1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
|
//@ run-pass
#![allow(unused_mut)]
#![allow(unused_variables)]
// 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;
}
|