2024-04-05 21:42:29 +00:00
|
|
|
//@ run-pass
|
|
|
|
#![feature(deref_patterns)]
|
|
|
|
#![allow(incomplete_features)]
|
|
|
|
|
2025-03-17 01:21:00 +00:00
|
|
|
use std::rc::Rc;
|
|
|
|
|
2025-04-17 01:41:52 +00:00
|
|
|
struct NoCopy;
|
|
|
|
|
2024-04-05 21:42:29 +00:00
|
|
|
fn main() {
|
2025-03-17 01:21:00 +00:00
|
|
|
let b = Rc::new("aaa".to_string());
|
2024-04-05 21:42:29 +00:00
|
|
|
let f = || {
|
2025-04-17 09:33:24 +00:00
|
|
|
let deref!(ref s) = b;
|
2024-04-05 21:42:29 +00:00
|
|
|
assert_eq!(s.len(), 3);
|
|
|
|
};
|
|
|
|
assert_eq!(b.len(), 3);
|
|
|
|
f();
|
|
|
|
|
2025-03-14 05:34:26 +00:00
|
|
|
let v = vec![1, 2, 3];
|
|
|
|
let f = || {
|
|
|
|
// this should count as a borrow of `v` as a whole
|
|
|
|
let [.., x] = v else { unreachable!() };
|
|
|
|
assert_eq!(x, 3);
|
|
|
|
};
|
|
|
|
assert_eq!(v, [1, 2, 3]);
|
|
|
|
f();
|
|
|
|
|
2025-03-17 01:21:00 +00:00
|
|
|
let mut b = "aaa".to_string();
|
2024-04-05 21:42:29 +00:00
|
|
|
let mut f = || {
|
2025-04-17 09:33:24 +00:00
|
|
|
let deref!(ref mut s) = b;
|
2025-03-17 01:21:00 +00:00
|
|
|
s.make_ascii_uppercase();
|
2024-04-05 21:42:29 +00:00
|
|
|
};
|
|
|
|
f();
|
2025-03-17 01:21:00 +00:00
|
|
|
assert_eq!(b, "AAA");
|
2025-03-14 05:34:26 +00:00
|
|
|
|
|
|
|
let mut v = vec![1, 2, 3];
|
|
|
|
let mut f = || {
|
|
|
|
// this should count as a mutable borrow of `v` as a whole
|
|
|
|
let [.., ref mut x] = v else { unreachable!() };
|
|
|
|
*x = 4;
|
|
|
|
};
|
|
|
|
f();
|
|
|
|
assert_eq!(v, [1, 2, 4]);
|
|
|
|
|
|
|
|
let mut v = vec![1, 2, 3];
|
|
|
|
let mut f = || {
|
|
|
|
// here, `[.., x]` is adjusted by both an overloaded deref and a builtin deref
|
|
|
|
let [.., x] = &mut v else { unreachable!() };
|
|
|
|
*x = 4;
|
|
|
|
};
|
|
|
|
f();
|
|
|
|
assert_eq!(v, [1, 2, 4]);
|
2025-04-17 01:41:52 +00:00
|
|
|
|
|
|
|
let b = Box::new(NoCopy);
|
|
|
|
let f = || {
|
|
|
|
// this should move out of the box rather than borrow.
|
2025-04-17 09:33:24 +00:00
|
|
|
let deref!(x) = b;
|
2025-04-17 01:41:52 +00:00
|
|
|
drop::<NoCopy>(x);
|
|
|
|
};
|
|
|
|
f();
|
|
|
|
|
|
|
|
let b = Box::new((NoCopy,));
|
|
|
|
let f = || {
|
|
|
|
// this should move out of the box rather than borrow.
|
2025-04-17 09:33:24 +00:00
|
|
|
let (x,) = b;
|
2025-04-17 01:41:52 +00:00
|
|
|
drop::<NoCopy>(x);
|
|
|
|
};
|
|
|
|
f();
|
2024-04-05 21:42:29 +00:00
|
|
|
}
|