File tree 1 file changed +53
-0
lines changed
tests/ui/traits/const-traits
1 file changed +53
-0
lines changed Original file line number Diff line number Diff line change
1
+ //! Ensure that a `const fn` can match on constants of a type that is `PartialEq`
2
+ //! but not `const PartialEq`.
3
+ #![feature(const_trait_impl)]
4
+
5
+ #[derive(Eq, PartialEq)]
6
+ pub struct Y(u8);
7
+ pub const GREEN: Y = Y(4);
8
+ pub const fn is_green(x: Y) -> bool {
9
+ match x { GREEN => true, _ => false }
10
+ }
11
+
12
+ struct CustomEq;
13
+
14
+ impl Eq for CustomEq {}
15
+ impl PartialEq for CustomEq {
16
+ fn eq(&self, _: &Self) -> bool {
17
+ false
18
+ }
19
+ }
20
+
21
+ #[derive(PartialEq, Eq)]
22
+ #[allow(unused)]
23
+ enum Foo {
24
+ Bar,
25
+ Baz,
26
+ Qux(CustomEq),
27
+ }
28
+
29
+ const BAR_BAZ: Foo = if 42 == 42 {
30
+ Foo::Bar
31
+ } else {
32
+ Foo::Qux(CustomEq) // dead arm
33
+ };
34
+
35
+ const EMPTY: &[CustomEq] = &[];
36
+
37
+ const fn test() {
38
+ // BAR_BAZ itself is fine but the enum has other variants
39
+ // that are non-structural. Still, this should be accepted.
40
+ match Foo::Qux(CustomEq) {
41
+ BAR_BAZ => panic!(),
42
+ _ => {}
43
+ }
44
+
45
+ // Similarly, an empty slice of a type that is non-structural
46
+ // is accepted.
47
+ match &[CustomEq] as &[CustomEq] {
48
+ EMPTY => panic!(),
49
+ _ => {},
50
+ }
51
+ }
52
+
53
+ fn main() {}
You can’t perform that action at this time.
0 commit comments