Skip to content

Commit d8e53a6

Browse files
committed
const-in-pattern: test that the PartialEq impl does not need to be const
1 parent c528b8c commit d8e53a6

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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() {}

0 commit comments

Comments
 (0)