summaryrefslogtreecommitdiffstats
path: root/tests/ui/let-else/let-else-deref-coercion.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:19:03 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:19:03 +0000
commit64d98f8ee037282c35007b64c2649055c56af1db (patch)
tree5492bcf97fce41ee1c0b1cc2add283f3e66cdab0 /tests/ui/let-else/let-else-deref-coercion.rs
parentAdding debian version 1.67.1+dfsg1-1. (diff)
downloadrustc-64d98f8ee037282c35007b64c2649055c56af1db.tar.xz
rustc-64d98f8ee037282c35007b64c2649055c56af1db.zip
Merging upstream version 1.68.2+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tests/ui/let-else/let-else-deref-coercion.rs')
-rw-r--r--tests/ui/let-else/let-else-deref-coercion.rs75
1 files changed, 75 insertions, 0 deletions
diff --git a/tests/ui/let-else/let-else-deref-coercion.rs b/tests/ui/let-else/let-else-deref-coercion.rs
new file mode 100644
index 000000000..052a5a8c7
--- /dev/null
+++ b/tests/ui/let-else/let-else-deref-coercion.rs
@@ -0,0 +1,75 @@
+// 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(_) = foo else { ... }` where foo is meant to Deref/DerefMut to
+// Bar. This fails, you must add a type annotation like `let _: &mut Bar = _ else { ... }`
+
+
+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 {
+ // Try without the type annotation
+ fn set_bar_unannotated(&mut self, value: u32) {
+ let Bar::Present(z) = self else { //~ ERROR mismatched types
+ return;
+ };
+ *z = value;
+ }
+}
+
+fn main() {
+ let mut foo = Foo(Bar::Present(1));
+ foo.set_bar_unannotated(54);
+ assert_eq!(foo.bar(), Some(54));
+ irrefutable::inner();
+}
+
+// The original, to show it fails 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) = x; //~ ERROR mismatched types
+ *z = 54;
+ assert_eq!((x.0).0, 54);
+ }
+ pub fn inner() {
+ foo(&mut Foo(Bar(1)));
+ }
+}