summaryrefslogtreecommitdiffstats
path: root/src/test/ui/consts/const_in_pattern/warn_corner_cases.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/warn_corner_cases.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/warn_corner_cases.rs')
-rw-r--r--src/test/ui/consts/const_in_pattern/warn_corner_cases.rs41
1 files changed, 41 insertions, 0 deletions
diff --git a/src/test/ui/consts/const_in_pattern/warn_corner_cases.rs b/src/test/ui/consts/const_in_pattern/warn_corner_cases.rs
new file mode 100644
index 000000000..15cf3c84d
--- /dev/null
+++ b/src/test/ui/consts/const_in_pattern/warn_corner_cases.rs
@@ -0,0 +1,41 @@
+// run-pass
+
+// 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 but we want
+// to begin warning the user that a future version of Rust may start rejecting
+// such const expressions.
+
+// The specific corner cases we are exploring here are instances where the
+// const-evaluator computes a value that *does* meet the conditions for
+// structural-match, but the const expression itself has abstractions (like
+// calls to const functions) that may fit better with a type-based analysis
+// rather than a commitment to a specific value.
+
+#![warn(indirect_structural_match)]
+
+#[derive(Copy, Clone, Debug)]
+struct NoDerive(#[allow(unused_tuple_struct_fields)] u32);
+
+// This impl makes `NoDerive` irreflexive.
+impl PartialEq for NoDerive { fn eq(&self, _: &Self) -> bool { false } }
+impl Eq for NoDerive { }
+
+fn main() {
+ const INDEX: Option<NoDerive> = [None, Some(NoDerive(10))][0];
+ match None { Some(_) => panic!("whoops"), INDEX => dbg!(INDEX), };
+ //~^ WARN must be annotated with `#[derive(PartialEq, Eq)]`
+ //~| WARN this was previously accepted
+
+ const fn build() -> Option<NoDerive> { None }
+ const CALL: Option<NoDerive> = build();
+ match None { Some(_) => panic!("whoops"), CALL => dbg!(CALL), };
+ //~^ WARN must be annotated with `#[derive(PartialEq, Eq)]`
+ //~| WARN this was previously accepted
+
+ impl NoDerive { const fn none() -> Option<NoDerive> { None } }
+ const METHOD_CALL: Option<NoDerive> = NoDerive::none();
+ match None { Some(_) => panic!("whoops"), METHOD_CALL => dbg!(METHOD_CALL), };
+ //~^ WARN must be annotated with `#[derive(PartialEq, Eq)]`
+ //~| WARN this was previously accepted
+}