Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit b58da53

Browse files
Add branchy const in pattern tests
1 parent 135cfcb commit b58da53

File tree

5 files changed

+129
-0
lines changed

5 files changed

+129
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// run-pass
2+
3+
#![feature(const_if_match)]
4+
#![warn(indirect_structural_match)]
5+
6+
struct CustomEq;
7+
8+
impl Eq for CustomEq {}
9+
impl PartialEq for CustomEq {
10+
fn eq(&self, _: &Self) -> bool {
11+
false
12+
}
13+
}
14+
15+
#[derive(PartialEq, Eq)]
16+
enum Foo {
17+
Bar,
18+
Baz,
19+
Qux(CustomEq),
20+
}
21+
22+
const BAR_BAZ: Foo = if 42 == 42 {
23+
Foo::Bar
24+
} else {
25+
Foo::Baz
26+
};
27+
28+
fn main() {
29+
match Foo::Qux(CustomEq) {
30+
BAR_BAZ => panic!(),
31+
_ => {}
32+
}
33+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// check-pass
2+
3+
#![feature(const_if_match)]
4+
#![warn(indirect_structural_match)]
5+
//~^ NOTE lint level is defined here
6+
7+
struct CustomEq;
8+
9+
impl Eq for CustomEq {}
10+
impl PartialEq for CustomEq {
11+
fn eq(&self, _: &Self) -> bool {
12+
false
13+
}
14+
}
15+
16+
#[derive(PartialEq, Eq)]
17+
enum Foo {
18+
Bar,
19+
Baz,
20+
Qux(CustomEq),
21+
}
22+
23+
// We know that `BAR_BAZ` will always be `Foo::Bar` and thus eligible for structural matching, but
24+
// dataflow will be more conservative.
25+
const BAR_BAZ: Foo = if 42 == 42 {
26+
Foo::Bar
27+
} else {
28+
Foo::Qux(CustomEq)
29+
};
30+
31+
fn main() {
32+
match Foo::Qux(CustomEq) {
33+
BAR_BAZ => panic!(),
34+
//~^ WARN must be annotated with `#[derive(PartialEq, Eq)]`
35+
//~| WARN this was previously accepted
36+
//~| NOTE see issue #62411
37+
_ => {}
38+
}
39+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
warning: to use a constant of type `CustomEq` in a pattern, `CustomEq` must be annotated with `#[derive(PartialEq, Eq)]`
2+
--> $DIR/custom-eq-branch-warn.rs:33:9
3+
|
4+
LL | BAR_BAZ => panic!(),
5+
| ^^^^^^^
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/custom-eq-branch-warn.rs:4:9
9+
|
10+
LL | #![warn(indirect_structural_match)]
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
12+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
13+
= note: for more information, see issue #62411 <https://github.com/rust-lang/rust/issues/62411>
14+
15+
warning: 1 warning emitted
16+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#![feature(const_if_match)]
2+
#![warn(indirect_structural_match)]
3+
4+
struct NoEq;
5+
6+
enum Foo {
7+
Bar,
8+
Baz,
9+
Qux(NoEq),
10+
}
11+
12+
// Even though any of these values can be compared structurally, we still disallow it in a pattern
13+
// because `Foo` does not impl `PartialEq`.
14+
const BAR_BAZ: Foo = if 42 == 42 {
15+
Foo::Baz
16+
} else {
17+
Foo::Bar
18+
};
19+
20+
fn main() {
21+
match Foo::Qux(NoEq) {
22+
BAR_BAZ => panic!(),
23+
//~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]`
24+
//~| ERROR must be annotated with `#[derive(PartialEq, Eq)]`
25+
_ => {}
26+
}
27+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: to use a constant of type `Foo` in a pattern, `Foo` must be annotated with `#[derive(PartialEq, Eq)]`
2+
--> $DIR/no-eq-branch-fail.rs:22:9
3+
|
4+
LL | BAR_BAZ => panic!(),
5+
| ^^^^^^^
6+
7+
error: to use a constant of type `Foo` in a pattern, `Foo` must be annotated with `#[derive(PartialEq, Eq)]`
8+
--> $DIR/no-eq-branch-fail.rs:22:9
9+
|
10+
LL | BAR_BAZ => panic!(),
11+
| ^^^^^^^
12+
13+
error: aborting due to 2 previous errors
14+

0 commit comments

Comments
 (0)