1 // Taken from https://github.com/rust-lang/rust/blob/6cc0a764e082d9c0abcf37a768d5889247ba13e2/compiler/rustc_typeck/src/check/_match.rs#L445-L462
3 // We attempt to `let Bar::Present(_) = foo else { ... }` where foo is meant to Deref/DerefMut to
4 // Bar. This fails, you must add a type annotation like `let _: &mut Bar = _ else { ... }`
7 use std::ops::{Deref, DerefMut};
17 fn deref(&self) -> &Bar {
21 impl DerefMut for Foo {
22 fn deref_mut(&mut self) -> &mut Bar {
27 fn bar(&self) -> Option<u32> {
28 let Bar::Present(z): &Bar = self else {
35 // Try without the type annotation
36 fn set_bar_unannotated(&mut self, value: u32) {
37 let Bar::Present(z) = self else { //~ ERROR mismatched types
45 let mut foo = Foo(Bar::Present(1));
46 foo.set_bar_unannotated(54);
47 assert_eq!(foo.bar(), Some(54));
51 // The original, to show it fails for irrefutable let decls
53 use std::ops::{Deref, DerefMut};
58 fn deref(&self) -> &Bar {
62 impl DerefMut for Foo {
63 fn deref_mut(&mut self) -> &mut Bar {
68 let Bar(z) = x; //~ ERROR mismatched types
70 assert_eq!((x.0).0, 54);
73 foo(&mut Foo(Bar(1)));