-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Handle generic associated constants in const qualification for structural match #73448
Comments
I am not completely sure how the i.e. The current Afaik this is only a problem when dealing with generic associated constants using other trait Foo {
const A: Self;
}
impl Foo for Option<f32> {
const A: Self = None;
}
impl Foo for usize {
const A: Self = 7;
}
struct A<T>(T);
impl<T: Foo> A<T> {
const VALUE: T = T::A;
}
fn main() {
match Some(1.0f32) {
// I do not want to use value based reasoning for `T::A`,
// so the following should error/warn because `Option<f32>` is not structurally match.
A::<Option<f32>>::VALUE => (),
_ => (),
}
match 42 {
// This should be accepted.
A::<usize>::VALUE => (),
_ => (),
}
} |
This seems to happen for |
@bjorn3 what exactly do you mean here? https://github.com/rust-lang/rust/blob/master/src/test/ui/consts/const_in_pattern/issue-73431.rs was the issue which first hit this problem. #73431 is similar to this part of my code snippet: match 42 {
// This should be accepted.
A::<usize>::VALUE => (),
_ => (),
} |
DIdn't read the top-level comment properly. I just saw the warning while running the test suite with |
Assigning |
@lcnr Sorry I missed your comment.
This is indeed possible, although not with the current dataflow framework, which only works on If we were to go this route, const qualification will become slower and more memory hungry, so like you I would prefer to avoid it. Given that, it seems we would need to break code like this to convert fully to a const-qualification-based approach. Presumably, having two layers of associated constants in a pattern is presumably something very few users care about. However, I don't think this meets the requirements for a breaking change, since the code isn't actually unsound. |
Not sure if I should make a separate issue for this, but I found some surprising behavior when running into this when using The following compiles without complaint use arrayvec::ArrayString;
#[derive(PartialEq,Eq)]
enum Foo {
A,
}
#[derive(PartialEq, Eq)]
enum Bar {
Foo(Foo),
#[allow(dead_code)]
Other(ArrayString<64>)
}
impl Bar {
const A: Bar = Bar::Foo(Foo::A);
}
fn main() {
match Bar::A {
Bar::A => {},
_ => {}
}
} But if I change use arrayvec::ArrayString;
#[derive(PartialEq,Eq)]
enum Foo {
A,
}
#[derive(PartialEq, Eq)]
enum Bar {
Foo(Foo),
#[allow(dead_code)]
Other(ArrayString<64>)
}
impl Bar {
const A: Bar = Self::Foo(Foo::A);
}
fn main() {
match Bar::A {
Bar::A => {},
_ => {}
}
} with the following warning on both stable
|
That seems to be an accident in the analysis, which does not know how to handle
|
I also came across this warning while trying to match a Here is the playground link : https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=b8a524fa8d710581190495a481ecad60 Note that the warning is gone for |
Ran into a compiler warning that referenced this issue in rustc 1.62.0. In case it's useful to anyone, here is a reproduction. |
I think I'm a bit confused about the point of this custom_eq MIR qualif analysis. Thinking about the two major possible design options for consts-in-patterns that I describe in this WIP t-lang design meeting document, I don't think we really need the MIR qualif analysis in either case?
Either way I don't think I see the benefit of doing a static analysis of the MIR code before running it, with the goal of predicting whether the final value will have custom equality. We need the final value anyway in any case where we want to exploit that it doesn't have custom equality. So we can just do the check on that value. I checked our test suite for code that gets rejected by this lint, and I don't think we have to reject any of that code:
Cc @rust-lang/wg-const-eval |
I was somewhat confused by your comment for a while 😅 to summarize my understanding:
I think you're saying that no matter how we go forward wrt to structural match, we want to accept the new code allowed by going with option 2. here. My only worry is that currently constants in patterns participate in exhaustiveness checking if types simply derive I don't think that the const qualification adds a lot of technical debt to the compiler, so I would personally wait until we decided when constants in patterns should participate in exhaustiveness checking before being more liberal here. |
Yeah I'm not suggesting to remove it right now. I'm just saying I think the final design we go for shouldn't involve a MIR qualif. |
The RFC at rust-lang/rfcs#3535, if accepted, would lead to us removing the structural match qualification, also making this issue moot. |
…, r=petrochenkov update indirect structural match lints to match RFC and to show up for dependencies This is a large step towards implementing rust-lang/rfcs#3535. We currently have five lints related to "the structural match situation": - nontrivial_structural_match - indirect_structural_match - pointer_structural_match - const_patterns_without_partial_eq - illegal_floating_point_literal_pattern This PR concerns the first 3 of them. (The 4th already is set up to show for dependencies, and the 5th is removed by rust-lang#116284.) nontrivial_structural_match is being removed as per the RFC; the other two are enabled to show up in dependencies. Fixes rust-lang#73448 by removing the affected analysis.
…, r=petrochenkov update indirect structural match lints to match RFC and to show up for dependencies This is a large step towards implementing rust-lang/rfcs#3535. We currently have five lints related to "the structural match situation": - nontrivial_structural_match - indirect_structural_match - pointer_structural_match - const_patterns_without_partial_eq - illegal_floating_point_literal_pattern This PR concerns the first 3 of them. (The 4th already is set up to show for dependencies, and the 5th is removed by rust-lang#116284.) nontrivial_structural_match is being removed as per the RFC; the other two are enabled to show up in dependencies. Fixes rust-lang#73448 by removing the affected analysis.
…, r=petrochenkov update indirect structural match lints to match RFC and to show up for dependencies This is a large step towards implementing rust-lang/rfcs#3535. We currently have five lints related to "the structural match situation": - nontrivial_structural_match - indirect_structural_match - pointer_structural_match - const_patterns_without_partial_eq - illegal_floating_point_literal_pattern This PR concerns the first 3 of them. (The 4th already is set up to show for dependencies, and the 5th is removed by rust-lang#116284.) nontrivial_structural_match is being removed as per the RFC; the other two are enabled to show up in dependencies. Fixes rust-lang#73448 by removing the affected analysis.
…, r=petrochenkov update indirect structural match lints to match RFC and to show up for dependencies This is a large step towards implementing rust-lang/rfcs#3535. We currently have five lints related to "the structural match situation": - nontrivial_structural_match - indirect_structural_match - pointer_structural_match - const_patterns_without_partial_eq - illegal_floating_point_literal_pattern This PR concerns the first 3 of them. (The 4th already is set up to show for dependencies, and the 5th is removed by rust-lang#116284.) nontrivial_structural_match is being removed as per the RFC; the other two are enabled to show up in dependencies. Fixes rust-lang#73448 by removing the affected analysis.
…, r=petrochenkov update indirect structural match lints to match RFC and to show up for dependencies This is a large step towards implementing rust-lang/rfcs#3535. We currently have five lints related to "the structural match situation": - nontrivial_structural_match - indirect_structural_match - pointer_structural_match - const_patterns_without_partial_eq - illegal_floating_point_literal_pattern This PR concerns the first 3 of them. (The 4th already is set up to show for dependencies, and the 5th is removed by rust-lang#116284.) nontrivial_structural_match is being removed as per the RFC; the other two are enabled to show up in dependencies. Fixes rust-lang#73448 by removing the affected analysis.
…, r=petrochenkov update indirect structural match lints to match RFC and to show up for dependencies This is a large step towards implementing rust-lang/rfcs#3535. We currently have five lints related to "the structural match situation": - nontrivial_structural_match - indirect_structural_match - pointer_structural_match - const_patterns_without_partial_eq - illegal_floating_point_literal_pattern This PR concerns the first 3 of them. (The 4th already is set up to show for dependencies, and the 5th is removed by rust-lang#116284.) nontrivial_structural_match is being removed as per the RFC; the other two are enabled to show up in dependencies. Fixes rust-lang#73448 by removing the affected analysis.
…, r=petrochenkov update indirect structural match lints to match RFC and to show up for dependencies This is a large step towards implementing rust-lang/rfcs#3535. We currently have five lints related to "the structural match situation": - nontrivial_structural_match - indirect_structural_match - pointer_structural_match - const_patterns_without_partial_eq - illegal_floating_point_literal_pattern This PR concerns the first 3 of them. (The 4th already is set up to show for dependencies, and the 5th is removed by rust-lang#116284.) nontrivial_structural_match is being removed as per the RFC; the other two are enabled to show up in dependencies. Fixes rust-lang#73448 by removing the affected analysis.
…, r=petrochenkov update indirect structural match lints to match RFC and to show up for dependencies This is a large step towards implementing rust-lang/rfcs#3535. We currently have five lints related to "the structural match situation": - nontrivial_structural_match - indirect_structural_match - pointer_structural_match - const_patterns_without_partial_eq - illegal_floating_point_literal_pattern This PR concerns the first 3 of them. (The 4th already is set up to show for dependencies, and the 5th is removed by rust-lang#116284.) nontrivial_structural_match is being removed as per the RFC; the other two are enabled to show up in dependencies. Fixes rust-lang#73448 by removing the affected analysis.
Rollup merge of rust-lang#120423 - RalfJung:indirect-structural-match, r=petrochenkov update indirect structural match lints to match RFC and to show up for dependencies This is a large step towards implementing rust-lang/rfcs#3535. We currently have five lints related to "the structural match situation": - nontrivial_structural_match - indirect_structural_match - pointer_structural_match - const_patterns_without_partial_eq - illegal_floating_point_literal_pattern This PR concerns the first 3 of them. (The 4th already is set up to show for dependencies, and the 5th is removed by rust-lang#116284.) nontrivial_structural_match is being removed as per the RFC; the other two are enabled to show up in dependencies. Fixes rust-lang#73448 by removing the affected analysis.
#67343 started using const qualification for more precise structural match warnings. The idea is that const qualification should be strictly more precise than the current type visitor (
search_for_structural_match_violation
). However, const qualification is more conservative in the face of generic associated constants than the type visitor, which typically runs after substitution. This was the case in the example in #73431.We will need to make const qualification handle all cases that
search_for_structural_match_violation
does before switching away from the latter.The text was updated successfully, but these errors were encountered: