summaryrefslogtreecommitdiffstats
path: root/src/test/ui/consts/const_in_pattern/accept_structural.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
commit698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch)
tree173a775858bd501c378080a10dca74132f05bc50 /src/test/ui/consts/const_in_pattern/accept_structural.rs
parentInitial commit. (diff)
downloadrustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz
rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/test/ui/consts/const_in_pattern/accept_structural.rs')
-rw-r--r--src/test/ui/consts/const_in_pattern/accept_structural.rs66
1 files changed, 66 insertions, 0 deletions
diff --git a/src/test/ui/consts/const_in_pattern/accept_structural.rs b/src/test/ui/consts/const_in_pattern/accept_structural.rs
new file mode 100644
index 000000000..5093fe539
--- /dev/null
+++ b/src/test/ui/consts/const_in_pattern/accept_structural.rs
@@ -0,0 +1,66 @@
+// run-pass
+
+#![warn(indirect_structural_match)]
+
+// This test is checking our logic for structural match checking by enumerating
+// the different kinds of const expressions. This test is collecting cases where
+// we have accepted the const expression as a pattern in the past and wish to
+// continue doing so.
+//
+// Even if a non-structural-match type is part of an expression in a const's
+// definition, that does not necessarily disqualify the const from being a match
+// pattern: in principle, we just need the types involved in the final value to
+// be structurally matchable.
+
+// See also RFC 1445
+
+#![feature(type_ascription)]
+
+#[derive(Copy, Clone, Debug)]
+struct NoPartialEq(u32);
+
+#[derive(Copy, Clone, Debug)]
+struct NoDerive(u32);
+
+// This impl makes `NoDerive` irreflexive.
+impl PartialEq for NoDerive { fn eq(&self, _: &Self) -> bool { false } }
+impl Eq for NoDerive { }
+
+type OND = Option<NoDerive>;
+
+fn main() {
+ const FIELD1: u32 = NoPartialEq(1).0;
+ match 1 { FIELD1 => dbg!(FIELD1), _ => panic!("whoops"), };
+ const FIELD2: u32 = NoDerive(1).0;
+ match 1 { FIELD2 => dbg!(FIELD2), _ => panic!("whoops"), };
+
+ enum CLike { One = 1, #[allow(dead_code)] Two = 2, }
+ const ONE_CAST: u32 = CLike::One as u32;
+ match 1 { ONE_CAST => dbg!(ONE_CAST), _ => panic!("whoops"), };
+
+ const NO_DERIVE_NONE: OND = None;
+ const INDIRECT: OND = NO_DERIVE_NONE;
+ match None { INDIRECT => dbg!(INDIRECT), _ => panic!("whoops"), };
+
+ const TUPLE: (OND, OND) = (None, None);
+ match (None, None) { TUPLE => dbg!(TUPLE), _ => panic!("whoops"), };
+
+ const TYPE_ASCRIPTION: OND = None: OND;
+ match None { TYPE_ASCRIPTION => dbg!(TYPE_ASCRIPTION), _ => panic!("whoops"), };
+
+ const ARRAY: [OND; 2] = [None, None];
+ match [None; 2] { ARRAY => dbg!(ARRAY), _ => panic!("whoops"), };
+
+ const REPEAT: [OND; 2] = [None; 2];
+ match [None, None] { REPEAT => dbg!(REPEAT), _ => panic!("whoops"), };
+
+ trait Trait: Sized { const ASSOC: Option<Self>; }
+ impl Trait for NoDerive { const ASSOC: Option<NoDerive> = None; }
+ match None { NoDerive::ASSOC => dbg!(NoDerive::ASSOC), _ => panic!("whoops"), };
+
+ const BLOCK: OND = { NoDerive(10); None };
+ match None { BLOCK => dbg!(BLOCK), _ => panic!("whoops"), };
+
+ const ADDR_OF: &OND = &None;
+ match &None { ADDR_OF => dbg!(ADDR_OF), _ => panic!("whoops"), };
+}