diff options
| author | Albert Larsan <74931857+albertlarsan68@users.noreply.github.com> | 2023-01-05 09:13:28 +0100 |
|---|---|---|
| committer | Albert Larsan <74931857+albertlarsan68@users.noreply.github.com> | 2023-01-11 09:32:08 +0000 |
| commit | cf2dff2b1e3fa55fa5415d524200070d0d7aacfe (patch) | |
| tree | 40a88d9a46aaf3e8870676eb2538378b75a263eb /tests/ui/let-else/let-else-deref-coercion-annotated.rs | |
| parent | ca855e6e42787ecd062d81d53336fe6788ef51a9 (diff) | |
| download | rust-cf2dff2b1e3fa55fa5415d524200070d0d7aacfe.tar.gz rust-cf2dff2b1e3fa55fa5415d524200070d0d7aacfe.zip | |
Move /src/test to /tests
Diffstat (limited to 'tests/ui/let-else/let-else-deref-coercion-annotated.rs')
| -rw-r--r-- | tests/ui/let-else/let-else-deref-coercion-annotated.rs | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/tests/ui/let-else/let-else-deref-coercion-annotated.rs b/tests/ui/let-else/let-else-deref-coercion-annotated.rs new file mode 100644 index 00000000000..60fdf825a33 --- /dev/null +++ b/tests/ui/let-else/let-else-deref-coercion-annotated.rs @@ -0,0 +1,77 @@ +// check-pass +// +// Taken from https://github.com/rust-lang/rust/blob/6cc0a764e082d9c0abcf37a768d5889247ba13e2/compiler/rustc_typeck/src/check/_match.rs#L445-L462 +// +// We attempt to `let Bar::Present(_): &mut Bar = foo else { ... }` where foo is meant to +// Deref/DerefMut to Bar. You can do this with an irrefutable binding, so it should work with +// let-else too. + + +use std::ops::{Deref, DerefMut}; + +struct Foo(Bar); + +enum Bar { + Present(u32), + Absent, +} +impl Deref for Foo { + type Target = Bar; + fn deref(&self) -> &Bar { + &self.0 + } +} +impl DerefMut for Foo { + fn deref_mut(&mut self) -> &mut Bar { + &mut self.0 + } +} +impl Bar { + fn bar(&self) -> Option<u32> { + let Bar::Present(z): &Bar = self else { + return None; + }; + return Some(*z); + } +} +impl Foo { + fn set_bar_annotated(&mut self, value: u32) { + let Bar::Present(z): &mut Bar = self else { // OK + return; + }; + *z = value; + } +} + +fn main() { + let mut foo = Foo(Bar::Present(1)); + foo.set_bar_annotated(42); + assert_eq!(foo.bar(), Some(42)); + irrefutable::inner(); +} + +// The original, to show it works for irrefutable let decls +mod irrefutable { + use std::ops::{Deref, DerefMut}; + struct Foo(Bar); + struct Bar(u32); + impl Deref for Foo { + type Target = Bar; + fn deref(&self) -> &Bar { + &self.0 + } + } + impl DerefMut for Foo { + fn deref_mut(&mut self) -> &mut Bar { + &mut self.0 + } + } + fn foo(x: &mut Foo) { + let Bar(z): &mut Bar = x; // OK + *z = 42; + assert_eq!((x.0).0, 42); + } + pub fn inner() { + foo(&mut Foo(Bar(1))); + } +} |
