Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

consts in patterns in const contexts with feature effects causes warning (soon: error) about not implementing PartialEq #119398

Closed
bend-n opened this issue Dec 29, 2023 · 13 comments · Fixed by #135064
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. F-const_trait_impl `#![feature(const_trait_impl)]` F-effects `#![feature(effects)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@bend-n
Copy link
Contributor

bend-n commented Dec 29, 2023

Code

#![feature(effects)]
#[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 }
}

Current output

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

Desired output

Rationale and extra context

the T is Derive(PartialEq); why does it warn?

Other cases

No response

Anything else?

as requested by @fmease

@bend-n bend-n added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Dec 29, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Dec 29, 2023
@compiler-errors
Copy link
Member

the T is Derive(PartialEq); why does it warn?

Because T does not implement const PartialEq.

@bend-n
Copy link
Contributor Author

bend-n commented Dec 29, 2023

Yes, i understand that, but it still seems like a incorrect diagnostic.

@fmease
Copy link
Member

fmease commented Dec 29, 2023

#[derive_const] doesn't help btw., it doesn't get recognized by the lint either (it throws an error).

@Noratrieb Noratrieb added F-effects `#![feature(effects)]` and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Dec 29, 2023
@compiler-errors
Copy link
Member

compiler-errors commented Dec 29, 2023

@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.
pub struct Y(u8);
pub const GREEN: Y = Y(4);
pub const fn is_green(x: Y) -> bool {
    match x { GREEN => true, _ => false }
}

@bend-n
Copy link
Contributor Author

bend-n commented 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
Copy link
Member

fmease commented Dec 29, 2023

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

@fmease
Copy link
Member

fmease commented Dec 29, 2023

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).

@fmease fmease added the A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. label Dec 29, 2023
@fmease fmease 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 Dec 29, 2023
@fmease
Copy link
Member

fmease commented 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 ^^'.

@RalfJung RalfJung 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 Feb 14, 2024
@RalfJung
Copy link
Member

The lint will become a hard error when #120805 lands.

Note that this code works on stable and has worked for quite a while:

#[derive(Eq, PartialEq)]
pub struct TypeThatIsPartialEq(u8);
pub const GREEN: TypeThatIsPartialEq = TypeThatIsPartialEq(4);

pub const fn is_green(x: TypeThatIsPartialEq) -> bool {
    match x { GREEN => true, _ => false }
}

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).

@fmease fmease added F-const_trait_impl `#![feature(const_trait_impl)]` F-effects `#![feature(effects)]` and removed F-effects `#![feature(effects)]` labels Nov 15, 2024
@fmease
Copy link
Member

fmease commented Nov 16, 2024

No longer reproduces.

@fmease fmease added the E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. label Nov 16, 2024
@RalfJung
Copy link
Member

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
Copy link
Member

compiler-errors commented Jan 3, 2025

@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?

@RalfJung
Copy link
Member

RalfJung commented Jan 3, 2025

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.

@bors bors closed this as completed in 966a5be Jan 4, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Jan 4, 2025
Rollup merge of rust-lang#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 rust-lang#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`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. F-const_trait_impl `#![feature(const_trait_impl)]` F-effects `#![feature(effects)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants