-
-
Notifications
You must be signed in to change notification settings - Fork 15.4k
Abort const-eval queries early when there are generics in the type #159504
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
base: main
Are you sure you want to change the base?
Changes from all commits
b09cb8d
81c48f0
58889d1
eb27207
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -111,7 +111,7 @@ impl<'tcx> ConstToPat<'tcx> { | |
| // implemented as `type` consts (project-const-generics#76). At that point it'll | ||
| // become necessary to just use type system normalization for all const patterns | ||
| // but that's not yet possible. | ||
| let mut thir_pat = if alias_const.kind.is_type_const(self.tcx) { | ||
| let const_value = if alias_const.kind.is_type_const(self.tcx) { | ||
| let Ok(normalize) = self | ||
| .tcx | ||
| .try_normalize_erasing_regions(self.typing_env, Unnormalized::new_wip(self.c)) | ||
|
|
@@ -124,7 +124,7 @@ impl<'tcx> ConstToPat<'tcx> { | |
| let err = self.tcx.dcx().create_err(CouldNotEvalConstPattern { span: self.span }); | ||
| return self.mk_err(err, ty); | ||
| }; | ||
| self.valtree_to_pat(value) | ||
| value | ||
| } else { | ||
| // try to resolve e.g. associated constants to their definition on an impl, and then | ||
| // evaluate the const. | ||
|
|
@@ -192,8 +192,10 @@ impl<'tcx> ConstToPat<'tcx> { | |
| }; | ||
|
|
||
| // Lower the valtree to a THIR pattern. | ||
| self.valtree_to_pat(ty::Value { ty, valtree }) | ||
| ty::Value { ty, valtree } | ||
| }; | ||
| debug_assert!(!const_value.ty.has_param()); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems fairly clear that we want this property to hold here. However I have no idea how to ensure that it actually holds without checking it just above (which we currently don't do, but an earlier version of this PR did). OTOH at least our test suite also doesn't seem to have a counterexample to this... |
||
| let mut thir_pat = self.valtree_to_pat(const_value); | ||
|
|
||
| if !thir_pat.references_error() { | ||
| // Always check for `PartialEq` if we had no other errors yet. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| //! Ensure we can deal with a pattern that depends on a generic in its path, but the | ||
| //! actual pattern value can be computed independent of the generic. | ||
| #[derive(PartialEq)] | ||
| pub struct Thing<const N: usize>; | ||
|
|
||
| impl<const N: usize> Thing<N> { | ||
| const A: Self = Thing; | ||
| } | ||
|
|
||
| fn broken<const N: usize>(x: Thing<N>) { | ||
| match x { | ||
| <Thing<N>>::A => {} //~ERROR: cannot depend on generic | ||
| _ => {} | ||
| } | ||
| } | ||
|
|
||
| fn main() {} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| error[E0158]: constant pattern cannot depend on generic parameters | ||
| --> $DIR/generic-in-path.rs:12:9 | ||
| | | ||
| LL | impl<const N: usize> Thing<N> { | ||
| | ----------------------------- | ||
| LL | const A: Self = Thing; | ||
| | ------------- constant defined here | ||
| ... | ||
| LL | <Thing<N>>::A => {} | ||
| | ^^^^^^^^^^^^^ `const` depends on a generic parameter | ||
|
|
||
| error: aborting due to 1 previous error | ||
|
|
||
| For more information about this error, try `rustc --explain E0158`. |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not 100% sure about removing this, but we don't have a test any more where it makes a difference so for how I'd prefer to avoid needless type traversals if we can. It anyway makes no sense to only check this once, we'd have to re-check every time we enter a struct as that can reveal new hidden generics (right?).
View changes since the review