You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
consts in patterns in const contexts with feature effects causes warning (soon: error) about not implementing PartialEq #119398
warning: to use a constant of type`Y`in a pattern, the type must implement `PartialEq`
--> src/main.rs:7:15
|
7 | match x { GREEN => true, _ =>false }
| ^^^^^
|
= 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 #116122 <https://github.com/rust-lang/rust/issues/116122>
= note: `#[warn(const_patterns_without_partial_eq)]` on by default
@fmease: What do you mean by that? That lint isn't recognizing a derive, it's looking for a PartialEq impl.
For the record, this code works:
#![feature(derive_const, effects)]#[derive_const(PartialEq)]#[derive(Eq)]// `Eq` is not a const trait, so we must regular-derive it.pubstructY(u8);pubconstGREEN:Y = Y(4);pubconstfnis_green(x:Y) -> bool{match x {GREEN => true, _ => false}}
Revisiting this, yesterday I totally missed that the match was inside a const fn, I just saw fn 🤦. So it definitely makes sense that const_patterns_without_partial_eq expects an impl const.
Therefore this reduces to a pure diagnostics issue (actually I'm not super sure, see RalfJ's RFC, rust-lang/rfcs#3535). Ideally, we would emit something like:
- warning: to use a constant of type `Y` in a pattern, the type must implement `PartialEq`+ warning: to use a constant of type `Y` in a pattern in a const context, the type must implement `const PartialEq`
--> src/main.rs:7:15
|
7 | match x { GREEN => true, _ => false }
| ^^^^^
|
= 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 #116122 <https://github.com/rust-lang/rust/issues/116122>
= note: `#[warn(const_patterns_without_partial_eq)]` on by default
the unstable docs just say "this feature is internal to the rust compiler".
Oh, that's unfortunate, it's an auto-generated message. To clarify, it says likely internal (via). In this case, I don't think it's internal but simply an experimental feature without a tracking issue since the keyword generics / effects story is still ongoing.
#[derive_const] is similar to #[derive] except that the generated impl becomes an impl const (see feature const_trait_impl for more).
changed the title [-]match on const triggers incorrect warning with `effects` feature[/-][+]confusing lint diagnostic on consts in patterns in const contexts with feature `effects`[/+]on Dec 29, 2023
Hmm, nonetheless it's weird that we don't lint against that without effects, for forward compat we should (?), although there wouldn't be a way to fix it on stable. I also don't know how the lint code would be able to check that properly without effects enabled. Again, see rust-lang/rfcs#3535, section Unresolved Questions. By the way, sorry for my rambling and back and forth, it's still morning over here and I'm a tad tired ^^'.
changed the title [-]confusing lint diagnostic on consts in patterns in const contexts with feature `effects`[/-][+]consts in patterns in const contexts with feature `effects` causes warning (soon: error) about not implementing PartialEq[/+]on Feb 14, 2024
So, I think there's a bug with the effects feature here somewhere, in how it interacts with the const-pattern check, which leads to the code being rejected with feature(effects).
@RalfJung: This is a real fix, because we no longer erroneously require that T: const PartialEq hold for it to be used in a pattern in a const function.
It's still somewhat conceptually dissatisfying/strange, though. In an abstract sense, it kinda feels right that we enforce that T: const PartialEq hold if it's used in a pattern in a const function, since it seems antithetical to the whole concept of matching being equivalent to emitting PartialEq calls if we don't?
In an abstract sense, it kinda feels right that we enforce that T: const PartialEq hold if it's used in a pattern in a const function, since it seems antithetical to the whole concept of matching being equivalent to emitting PartialEq calls if we don't?
Agreed. But it'd be a breaking change to do that now... so this is basically just a back-compat hack.
Activity
compiler-errors commentedon Dec 29, 2023
Because
Tdoes not implementconst PartialEq.bend-n commentedon Dec 29, 2023
Yes, i understand that, but it still seems like a incorrect diagnostic.
fmease commentedon Dec 29, 2023
#[derive_const]doesn't help btw., it doesn't get recognized by the lint either (it throws an error).compiler-errors commentedon Dec 29, 2023
@fmease: What do you mean by that? That lint isn't recognizing a
derive, it's looking for aPartialEqimpl.For the record, this code works:
bend-n commentedon Dec 29, 2023
Er, excuse me, but what is
derive_const? the unstable docs just say "this feature is internal to the rust compiler".fmease commentedon Dec 29, 2023
Revisiting this, yesterday I totally missed that the
matchwas inside aconst fn, I just sawfn🤦. So it definitely makes sense thatconst_patterns_without_partial_eqexpects animpl const.Therefore this reduces to a pure diagnostics issue(actually I'm not super sure, see RalfJ's RFC, rust-lang/rfcs#3535). Ideally, we would emit something like:fmease commentedon Dec 29, 2023
Oh, that's unfortunate, it's an auto-generated message. To clarify, it says likely internal (via). In this case, I don't think it's internal but simply an experimental feature without a tracking issue since the keyword generics / effects story is still ongoing.
#[derive_const]is similar to#[derive]except that the generatedimplbecomes animpl const(see featureconst_trait_implfor more).[-]match on const triggers incorrect warning with `effects` feature[/-][+]confusing lint diagnostic on consts in patterns in const contexts with feature `effects`[/+]fmease commentedon Dec 29, 2023
Hmm, nonetheless it's weird that we don't lint against that without
effects, for forward compat we should (?), although there wouldn't be a way to fix it on stable. I also don't know how the lint code would be able to check that properly withouteffectsenabled. Again, see rust-lang/rfcs#3535, section Unresolved Questions. By the way, sorry for my rambling and back and forth, it's still morning over here and I'm a tad tired ^^'.[-]confusing lint diagnostic on consts in patterns in const contexts with feature `effects`[/-][+]consts in patterns in const contexts with feature `effects` causes warning (soon: error) about not implementing PartialEq[/+]RalfJung commentedon Feb 14, 2024
The lint will become a hard error when #120805 lands.
Note that this code works on stable and has worked for quite a while:
So, I think there's a bug with the
effectsfeature here somewhere, in how it interacts with the const-pattern check, which leads to the code being rejected withfeature(effects).fmease commentedon Nov 16, 2024
No longer reproduces.
RalfJung commentedon Nov 16, 2024
Cc @compiler-errors who changed a bunch of stuff here recently.
Not sure if there was a proper fix or the issue is just masked for now.
compiler-errors commentedon Jan 3, 2025
@RalfJung: This is a real fix, because we no longer erroneously require that
T: const PartialEqhold for it to be used in a pattern in a const function.It's still somewhat conceptually dissatisfying/strange, though. In an abstract sense, it kinda feels right that we enforce that
T: const PartialEqhold if it's used in a pattern in a const function, since it seems antithetical to the whole concept of matching being equivalent to emittingPartialEqcalls if we don't?RalfJung commentedon Jan 3, 2025
Agreed. But it'd be a breaking change to do that now... so this is basically just a back-compat hack.
Rollup merge of rust-lang#135064 - RalfJung:const-in-pat-partial-eq-n…
Unrolled build for rust-lang#135064