]> git.lizzy.rs Git - rust.git/blob - src/test/ui/consts/const_in_pattern/warn_corner_cases.rs
Rollup merge of #98609 - TaKO8Ki:fix-ice-for-associated-constant-generics, r=lcnr
[rust.git] / src / test / ui / consts / const_in_pattern / warn_corner_cases.rs
1 // run-pass
2
3 // This test is checking our logic for structural match checking by enumerating
4 // the different kinds of const expressions. This test is collecting cases where
5 // we have accepted the const expression as a pattern in the past but we want
6 // to begin warning the user that a future version of Rust may start rejecting
7 // such const expressions.
8
9 // The specific corner cases we are exploring here are instances where the
10 // const-evaluator computes a value that *does* meet the conditions for
11 // structural-match, but the const expression itself has abstractions (like
12 // calls to const functions) that may fit better with a type-based analysis
13 // rather than a commitment to a specific value.
14
15 #![warn(indirect_structural_match)]
16
17 #[derive(Copy, Clone, Debug)]
18 struct NoDerive(u32);
19
20 // This impl makes `NoDerive` irreflexive.
21 impl PartialEq for NoDerive { fn eq(&self, _: &Self) -> bool { false } }
22 impl Eq for NoDerive { }
23
24 fn main() {
25     const INDEX: Option<NoDerive> = [None, Some(NoDerive(10))][0];
26     match None { Some(_) => panic!("whoops"), INDEX => dbg!(INDEX), };
27     //~^ WARN must be annotated with `#[derive(PartialEq, Eq)]`
28     //~| WARN this was previously accepted
29
30     const fn build() -> Option<NoDerive> { None }
31     const CALL: Option<NoDerive> = build();
32     match None { Some(_) => panic!("whoops"), CALL => dbg!(CALL), };
33     //~^ WARN must be annotated with `#[derive(PartialEq, Eq)]`
34     //~| WARN this was previously accepted
35
36     impl NoDerive { const fn none() -> Option<NoDerive> { None } }
37     const METHOD_CALL: Option<NoDerive> = NoDerive::none();
38     match None { Some(_) => panic!("whoops"), METHOD_CALL => dbg!(METHOD_CALL), };
39     //~^ WARN must be annotated with `#[derive(PartialEq, Eq)]`
40     //~| WARN this was previously accepted
41 }