You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Taken from https://github.com/rust-lang/rust/blob/6cc0a764e082d9c0abcf37a768d5889247ba13e2/compiler/rustc_typeck/src/check/_match.rs#L445-L462
4
+
//
5
+
// We attempt to `let Bar::Present(_): &mut Bar = foo else { ... }` where foo is meant to
6
+
// Deref/DerefMut to Bar. You can do this with an irrefutable binding, so it should work with
7
+
// let-else too.
8
+
9
+
#![feature(let_else)]
10
+
use std::ops::{Deref,DerefMut};
11
+
12
+
structFoo(Bar);
13
+
14
+
enumBar{
15
+
Present(u32),
16
+
Absent,
17
+
}
18
+
implDerefforFoo{
19
+
typeTarget = Bar;
20
+
fnderef(&self) -> &Bar{
21
+
&self.0
22
+
}
23
+
}
24
+
implDerefMutforFoo{
25
+
fnderef_mut(&mutself) -> &mutBar{
26
+
&mutself.0
27
+
}
28
+
}
29
+
implBar{
30
+
fnbar(&self) -> Option<u32>{
31
+
letBar::Present(z):&Bar = selfelse{
32
+
returnNone;
33
+
};
34
+
returnSome(*z);
35
+
}
36
+
}
37
+
implFoo{
38
+
fnset_bar_annotated(&mutself,value:u32){
39
+
letBar::Present(z):&mutBar = selfelse{// OK
40
+
return;
41
+
};
42
+
*z = value;
43
+
}
44
+
}
45
+
46
+
fnmain(){
47
+
letmut foo = Foo(Bar::Present(1));
48
+
foo.set_bar_annotated(42);
49
+
assert_eq!(foo.bar(),Some(42));
50
+
irrefutable::inner();
51
+
}
52
+
53
+
// The original, to show it works for irrefutable let decls
// Taken from https://github.com/rust-lang/rust/blob/6cc0a764e082d9c0abcf37a768d5889247ba13e2/compiler/rustc_typeck/src/check/_match.rs#L445-L462
2
+
//
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 { ... }`
0 commit comments