-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of #135064 - RalfJung:const-in-pat-partial-eq-not-const,…
… r=compiler-errors const-in-pattern: test that the PartialEq impl does not need to be const Fixes #119398 by adding a test. `@compiler-errors` is there some place in the code where we could add a comment saying "as a backcompat hack, here we only require `PartialEq` and not `const PartialEq`"? r? `@compiler-errors`
- Loading branch information
Showing
2 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
//! Ensure that a `const fn` can match on constants of a type that is `PartialEq` | ||
//! but not `const PartialEq`. This is accepted for backwards compatibility reasons. | ||
//@ check-pass | ||
#![feature(const_trait_impl)] | ||
|
||
#[derive(Eq, PartialEq)] | ||
pub struct Y(u8); | ||
pub const GREEN: Y = Y(4); | ||
pub const fn is_green(x: Y) -> bool { | ||
match x { GREEN => true, _ => false } | ||
} | ||
|
||
struct CustomEq; | ||
|
||
impl Eq for CustomEq {} | ||
impl PartialEq for CustomEq { | ||
fn eq(&self, _: &Self) -> bool { | ||
false | ||
} | ||
} | ||
|
||
#[derive(PartialEq, Eq)] | ||
#[allow(unused)] | ||
enum Foo { | ||
Bar, | ||
Baz, | ||
Qux(CustomEq), | ||
} | ||
|
||
const BAR_BAZ: Foo = if 42 == 42 { | ||
Foo::Bar | ||
} else { | ||
Foo::Qux(CustomEq) // dead arm | ||
}; | ||
|
||
const EMPTY: &[CustomEq] = &[]; | ||
|
||
const fn test() { | ||
// BAR_BAZ itself is fine but the enum has other variants | ||
// that are non-structural. Still, this should be accepted. | ||
match Foo::Qux(CustomEq) { | ||
BAR_BAZ => panic!(), | ||
_ => {} | ||
} | ||
|
||
// Similarly, an empty slice of a type that is non-structural | ||
// is accepted. | ||
match &[CustomEq] as &[CustomEq] { | ||
EMPTY => panic!(), | ||
_ => {}, | ||
} | ||
} | ||
|
||
fn main() {} |