rust/tests/ui/pattern/usefulness/consts-opaque.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

149 lines
3.6 KiB
Rust
Raw Normal View History

2020-10-17 13:01:10 -05:00
// This file tests the exhaustiveness algorithm on opaque constants. Most of the examples give
// unnecessary warnings because const_to_pat.rs converts a constant pattern to a wildcard when the
// constant is not allowed as a pattern. This is an edge case so we may not care to fix it.
// See also https://github.com/rust-lang/rust/issues/78057
#![deny(unreachable_patterns)]
#[derive(PartialEq)]
struct Foo(i32);
impl Eq for Foo {}
const FOO: Foo = Foo(42);
const FOO_REF: &Foo = &Foo(42);
const FOO_REF_REF: &&Foo = &&Foo(42);
#[derive(PartialEq)]
struct Bar;
impl Eq for Bar {}
const BAR: Bar = Bar;
#[derive(PartialEq)]
enum Baz {
Baz1,
Baz2,
2020-10-17 13:01:10 -05:00
}
impl Eq for Baz {}
const BAZ: Baz = Baz::Baz1;
#[rustfmt::skip]
2020-10-17 13:01:10 -05:00
fn main() {
match FOO {
FOO => {}
//~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]`
_ => {}
2020-10-17 13:01:10 -05:00
}
match FOO_REF {
FOO_REF => {}
//~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]`
Foo(_) => {}
2020-10-17 13:01:10 -05:00
}
2020-10-19 18:06:00 -05:00
// This used to cause an ICE (https://github.com/rust-lang/rust/issues/78071)
match FOO_REF_REF {
FOO_REF_REF => {}
//~^ WARNING must be annotated with `#[derive(PartialEq, Eq)]`
//~| WARNING this was previously accepted by the compiler but is being phased out
Foo(_) => {}
}
2020-10-17 13:01:10 -05:00
match BAR {
Bar => {}
BAR => {}
2020-10-17 13:01:10 -05:00
//~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]`
_ => {}
}
match BAR {
BAR => {}
//~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]`
Bar => {}
2020-10-17 13:01:10 -05:00
_ => {}
}
match BAR {
BAR => {}
//~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]`
BAR => {}
2020-10-17 13:01:10 -05:00
//~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]`
_ => {}
2020-10-17 13:01:10 -05:00
}
match BAZ {
BAZ => {}
//~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]`
Baz::Baz1 => {}
2020-10-17 13:01:10 -05:00
_ => {}
}
match BAZ {
Baz::Baz1 => {}
BAZ => {}
//~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]`
_ => {}
}
match BAZ {
BAZ => {}
//~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]`
Baz::Baz2 => {}
_ => {}
2020-10-17 13:01:10 -05:00
}
2020-11-14 15:50:53 -06:00
type Quux = fn(usize, usize) -> usize;
fn quux(a: usize, b: usize) -> usize { a + b }
const QUUX: Quux = quux;
2020-10-17 13:01:10 -05:00
match QUUX {
QUUX => {} //~WARN behave unpredictably
//~| previously accepted
QUUX => {} //~WARN behave unpredictably
//~| previously accepted
2020-10-17 13:01:10 -05:00
_ => {}
}
2020-11-14 15:50:53 -06:00
#[derive(PartialEq, Eq)]
struct Wrap<T>(T);
const WRAPQUUX: Wrap<Quux> = Wrap(quux);
match WRAPQUUX {
WRAPQUUX => {} //~WARN behave unpredictably
//~| previously accepted
WRAPQUUX => {} //~WARN behave unpredictably
//~| previously accepted
2020-11-14 15:50:53 -06:00
Wrap(_) => {}
}
match WRAPQUUX {
Wrap(_) => {}
WRAPQUUX => {} //~WARN behave unpredictably
//~| previously accepted
}
match WRAPQUUX {
Wrap(_) => {}
}
match WRAPQUUX {
//~^ ERROR: non-exhaustive patterns: `Wrap(_)` not covered
WRAPQUUX => {} //~WARN behave unpredictably
//~| previously accepted
2020-11-14 15:50:53 -06:00
}
#[derive(PartialEq, Eq)]
enum WhoKnows<T> {
Yay(T),
Nope,
};
const WHOKNOWSQUUX: WhoKnows<Quux> = WhoKnows::Yay(quux);
match WHOKNOWSQUUX {
WHOKNOWSQUUX => {} //~WARN behave unpredictably
//~| previously accepted
2020-11-14 15:50:53 -06:00
WhoKnows::Yay(_) => {}
WHOKNOWSQUUX => {} //~WARN behave unpredictably
//~| previously accepted
2020-11-14 15:50:53 -06:00
WhoKnows::Nope => {}
}
2020-10-17 13:01:10 -05:00
}