Add branchy const in pattern tests

This commit is contained in:
Dylan MacKenzie 2020-04-07 12:11:29 -07:00
parent 135cfcb5cd
commit b58da533bc
5 changed files with 129 additions and 0 deletions

View File

@ -0,0 +1,33 @@
// run-pass
#![feature(const_if_match)]
#![warn(indirect_structural_match)]
struct CustomEq;
impl Eq for CustomEq {}
impl PartialEq for CustomEq {
fn eq(&self, _: &Self) -> bool {
false
}
}
#[derive(PartialEq, Eq)]
enum Foo {
Bar,
Baz,
Qux(CustomEq),
}
const BAR_BAZ: Foo = if 42 == 42 {
Foo::Bar
} else {
Foo::Baz
};
fn main() {
match Foo::Qux(CustomEq) {
BAR_BAZ => panic!(),
_ => {}
}
}

View File

@ -0,0 +1,39 @@
// check-pass
#![feature(const_if_match)]
#![warn(indirect_structural_match)]
//~^ NOTE lint level is defined here
struct CustomEq;
impl Eq for CustomEq {}
impl PartialEq for CustomEq {
fn eq(&self, _: &Self) -> bool {
false
}
}
#[derive(PartialEq, Eq)]
enum Foo {
Bar,
Baz,
Qux(CustomEq),
}
// We know that `BAR_BAZ` will always be `Foo::Bar` and thus eligible for structural matching, but
// dataflow will be more conservative.
const BAR_BAZ: Foo = if 42 == 42 {
Foo::Bar
} else {
Foo::Qux(CustomEq)
};
fn main() {
match Foo::Qux(CustomEq) {
BAR_BAZ => panic!(),
//~^ WARN must be annotated with `#[derive(PartialEq, Eq)]`
//~| WARN this was previously accepted
//~| NOTE see issue #62411
_ => {}
}
}

View File

@ -0,0 +1,16 @@
warning: to use a constant of type `CustomEq` in a pattern, `CustomEq` must be annotated with `#[derive(PartialEq, Eq)]`
--> $DIR/custom-eq-branch-warn.rs:33:9
|
LL | BAR_BAZ => panic!(),
| ^^^^^^^
|
note: the lint level is defined here
--> $DIR/custom-eq-branch-warn.rs:4:9
|
LL | #![warn(indirect_structural_match)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #62411 <https://github.com/rust-lang/rust/issues/62411>
warning: 1 warning emitted

View File

@ -0,0 +1,27 @@
#![feature(const_if_match)]
#![warn(indirect_structural_match)]
struct NoEq;
enum Foo {
Bar,
Baz,
Qux(NoEq),
}
// Even though any of these values can be compared structurally, we still disallow it in a pattern
// because `Foo` does not impl `PartialEq`.
const BAR_BAZ: Foo = if 42 == 42 {
Foo::Baz
} else {
Foo::Bar
};
fn main() {
match Foo::Qux(NoEq) {
BAR_BAZ => panic!(),
//~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]`
//~| ERROR must be annotated with `#[derive(PartialEq, Eq)]`
_ => {}
}
}

View File

@ -0,0 +1,14 @@
error: to use a constant of type `Foo` in a pattern, `Foo` must be annotated with `#[derive(PartialEq, Eq)]`
--> $DIR/no-eq-branch-fail.rs:22:9
|
LL | BAR_BAZ => panic!(),
| ^^^^^^^
error: to use a constant of type `Foo` in a pattern, `Foo` must be annotated with `#[derive(PartialEq, Eq)]`
--> $DIR/no-eq-branch-fail.rs:22:9
|
LL | BAR_BAZ => panic!(),
| ^^^^^^^
error: aborting due to 2 previous errors