-
-
Notifications
You must be signed in to change notification settings - Fork 15.4k
check wf before const-evaluating #160237
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
Open
zedddie
wants to merge
4
commits into
rust-lang:main
Choose a base branch
from
zedddie:wf-before-const-eval
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
check wf before const-evaluating #160237
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -1282,19 +1282,58 @@ where | |
| Ok(()) | ||
| } | ||
|
|
||
| // Try to evaluate a const, or return `None` if the const is too generic. | ||
| // This doesn't mean the const isn't evaluatable, though, and should be treated | ||
| // as an ambiguity rather than no-solution. | ||
| // Try to evaluate a const, returning `(Option<Const>, Certainty)`, this returns | ||
| // `NoSolution` when concrete const fails to prove well-formedness in empty env. | ||
| // | ||
| // `(None, Maybe(_))` means we couldn't fully check const's well-formedness, and | ||
| // bailed without evaluation. | ||
| // | ||
| // `(None, Certainty::Yes)` means const is too generic, and doesn't indicate the | ||
| // const isn't evaluatable, should be treated as an ambiguity rather than no-solution. | ||
| pub(super) fn evaluate_const( | ||
| &mut self, | ||
| param_env: I::ParamEnv, | ||
| alias_const: ty::AliasConst<I>, | ||
| ) -> Result<Option<I::Const>, RerunNonErased> { | ||
| ) -> Result<(Option<I::Const>, Certainty), NoSolutionOrRerunNonErased> { | ||
| if self.typing_mode().is_erased_not_coherence() { | ||
| match self.opaque_accesses.rerun_always(RerunReason::EvaluateConst)? {} | ||
| } | ||
| let cx = self.cx(); | ||
|
|
||
| // Checking in empty env to be sure const is WF without relying on assumptions from env, | ||
| // preventing ill-formed consts from reaching CTFE. | ||
| // | ||
| // For example without this check const with `[u8]: Sized` bound could get to be evaluated | ||
| // in environment with the same assumption, pass and ICE later in CTFE. | ||
| let certainty = if alias_const.has_non_region_infer() | ||
| || alias_const.has_non_region_param() | ||
| || alias_const.has_non_region_placeholders() | ||
| { | ||
| // Skip proving for not fully concrete consts as they either won't reach CTFE or | ||
| // can't depend on generics even if they have them to pass CTFE. | ||
| Certainty::Yes | ||
| } | ||
| // We do this only for GCA consts. Doing this check for GCE introduces cycles with anon | ||
| // consts in impl blocks. | ||
| else if cx.features().generic_const_args() { | ||
| // FIXME(zedddie): we should check this in `fully_monomorphized()` Typing mode. | ||
| let goal = Goal::new( | ||
|
Member
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. we definitely want a nice comment explaining why we do this :3 |
||
| cx, | ||
| ParamEnv::empty(), | ||
| ty::ClauseKind::WellFormed(alias_const.to_const(cx, ty::IsRigid::Yes).into()), | ||
| ); | ||
| self.add_goal(GoalSource::AliasWellFormed, goal)?; | ||
| let certainty = self.try_evaluate_added_goals()?; | ||
|
|
||
| if matches!(certainty, Certainty::Maybe(_)) { | ||
| return Ok((None, certainty)); | ||
| } | ||
| certainty | ||
| } else { | ||
| Certainty::Yes | ||
| }; | ||
|
|
||
| Ok(self.delegate.evaluate_const(param_env, alias_const)) | ||
| Ok((self.delegate.evaluate_const(param_env, alias_const), certainty)) | ||
| } | ||
|
|
||
| pub(super) fn evaluate_const_and_instantiate_projection_term( | ||
|
|
@@ -1305,11 +1344,11 @@ where | |
| alias_const: ty::AliasConst<I>, | ||
| ) -> QueryResultOrRerunNonErased<I> { | ||
| match self.evaluate_const(param_env, alias_const)? { | ||
| Some(evaluated) => { | ||
| (Some(evaluated), _) => { | ||
| self.eq(param_env, expected_term, evaluated.into())?; | ||
| self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes) | ||
| } | ||
| None if self.cx().features().generic_const_args() => { | ||
| (None, certainty) if self.cx().features().generic_const_args() => { | ||
| // HACK(khyperia): calling `resolve_vars_if_possible` here shouldn't be necessary, | ||
| // `try_evaluate_const` calls `resolve_vars_if_possible` already. However, we want | ||
| // to check `has_non_region_infer` against the type with vars resolved (i.e. check | ||
|
|
@@ -1331,10 +1370,10 @@ where | |
| projection_term.to_term(self.cx(), ty::IsRigid::Yes), | ||
| expected_term, | ||
| )?; | ||
| self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes) | ||
| self.evaluate_added_goals_and_make_canonical_response(certainty) | ||
| } | ||
| } | ||
| None => { | ||
| (None, _) => { | ||
| // Legacy behavior: always treat as ambiguous | ||
| self.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS) | ||
| } | ||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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
13 changes: 13 additions & 0 deletions
13
tests/ui/const-generics/gca/impl-block-type-const-type-mismatch.rs
This file contains hidden or 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,13 @@ | ||
| //! Regression test for <https://github.com/rust-lang/rust/issues/156780>. | ||
| //@ compile-flags: -Znext-solver=globally | ||
| #![feature(min_generic_const_args)] | ||
| #![feature(generic_const_args)] | ||
| #![feature(generic_const_items)] | ||
| #![feature(macroless_generic_const_args)] | ||
|
|
||
| const ADD1<const N:usize>: usize = N + 1; | ||
| type const A<const N:usize>: usize = ADD1::<N>; | ||
| impl [(); A::<1f64>] {} //~ ERROR: type mismatch resolving `A<1f64> == _` [E0271] | ||
| //~| ERROR: cannot define inherent `impl` for primitive types [E0390] | ||
| //~^^ ERROR: the type `[(); A::<1f64>]` is not well-formed | ||
| fn main() {} |
24 changes: 24 additions & 0 deletions
24
tests/ui/const-generics/gca/impl-block-type-const-type-mismatch.stderr
This file contains hidden or 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,24 @@ | ||
| error[E0271]: type mismatch resolving `A<1f64> == _` | ||
| --> $DIR/impl-block-type-const-type-mismatch.rs:10:1 | ||
| | | ||
| LL | impl [(); A::<1f64>] {} | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^ types differ | ||
|
|
||
| error: the type `[(); A::<1f64>]` is not well-formed | ||
| --> $DIR/impl-block-type-const-type-mismatch.rs:10:6 | ||
| | | ||
| LL | impl [(); A::<1f64>] {} | ||
| | ^^^^^^^^^^^^^^^ | ||
|
|
||
| error[E0390]: cannot define inherent `impl` for primitive types | ||
| --> $DIR/impl-block-type-const-type-mismatch.rs:10:1 | ||
| | | ||
| LL | impl [(); A::<1f64>] {} | ||
| | ^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| = help: consider using an extension trait instead | ||
|
|
||
| error: aborting due to 3 previous errors | ||
|
|
||
| Some errors have detailed explanations: E0271, E0390. | ||
| For more information about an error, try `rustc --explain E0271`. |
12 changes: 12 additions & 0 deletions
12
tests/ui/const-generics/gca/rp-type-const-type-mismatch.rs
This file contains hidden or 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,12 @@ | ||
| //! Regression test for <https://github.com/rust-lang/rust/issues/154805>. | ||
| //@ compile-flags: -Znext-solver=globally | ||
| #![feature(generic_const_items,min_generic_const_args)] | ||
| #![feature(macroless_generic_const_args, generic_const_args)] | ||
| const ADD1<const N: usize>: usize = N + 1; | ||
| fn a() -> [usize; ADD1::<b"">] {} //~ ERROR: type mismatch resolving `ADD1<*b""> == _` [E0271] | ||
| //~| ERROR: the type `[usize; ADD1::<*b"">]` is not well-formed | ||
| //~| ERROR: mismatched types [E0308] | ||
| //~| ERROR: type mismatch resolving `ADD1<*b""> == _` [E0271] | ||
|
|
||
|
|
||
| fn main() {} |
32 changes: 32 additions & 0 deletions
32
tests/ui/const-generics/gca/rp-type-const-type-mismatch.stderr
This file contains hidden or 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,32 @@ | ||
| error[E0271]: type mismatch resolving `ADD1<*b""> == _` | ||
| --> $DIR/rp-type-const-type-mismatch.rs:6:11 | ||
| | | ||
| LL | fn a() -> [usize; ADD1::<b"">] {} | ||
| | ^^^^^^^^^^^^^^^^^^^^ types differ | ||
|
|
||
| error: the type `[usize; ADD1::<*b"">]` is not well-formed | ||
| --> $DIR/rp-type-const-type-mismatch.rs:6:11 | ||
| | | ||
| LL | fn a() -> [usize; ADD1::<b"">] {} | ||
| | ^^^^^^^^^^^^^^^^^^^^ | ||
|
|
||
| error[E0308]: mismatched types | ||
| --> $DIR/rp-type-const-type-mismatch.rs:6:11 | ||
| | | ||
| LL | fn a() -> [usize; ADD1::<b"">] {} | ||
| | - ^^^^^^^^^^^^^^^^^^^^ expected `[usize; _]`, found `()` | ||
| | | | ||
| | implicitly returns `()` as its body has no tail or `return` expression | ||
|
|
||
| error[E0271]: type mismatch resolving `ADD1<*b""> == _` | ||
| --> $DIR/rp-type-const-type-mismatch.rs:6:11 | ||
| | | ||
| LL | fn a() -> [usize; ADD1::<b"">] {} | ||
| | ^^^^^^^^^^^^^^^^^^^^ types differ | ||
| | | ||
| = note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no` | ||
|
|
||
| error: aborting due to 4 previous errors | ||
|
|
||
| Some errors have detailed explanations: E0271, E0308. | ||
| For more information about an error, try `rustc --explain E0271`. |
11 changes: 11 additions & 0 deletions
11
tests/ui/const-generics/gca/type-const-arg-type-mismatch-1.rs
This file contains hidden or 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,11 @@ | ||
| //! Regression test for <https://github.com/rust-lang/rust/issues/154805>. | ||
| //@ compile-flags: -Znext-solver=globally | ||
| #![feature(min_generic_const_args)] | ||
| #![feature(generic_const_args)] | ||
| #![feature(generic_const_items)] | ||
|
|
||
| const ADD1<const N: usize>: usize = N + 1; | ||
| type const ONE: usize = ADD1::<core::direct_const_arg!(b"")>; //~ ERROR type mismatch resolving | ||
| //~| ERROR the constant `*b""` is not of type `usize` | ||
| //~| ERROR the constant `ADD1::<*b"">` is not of type `usize` | ||
| fn main() {} |
27 changes: 27 additions & 0 deletions
27
tests/ui/const-generics/gca/type-const-arg-type-mismatch-1.stderr
This file contains hidden or 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,27 @@ | ||
| error[E0271]: type mismatch resolving `ADD1<*b""> == _` | ||
| --> $DIR/type-const-arg-type-mismatch-1.rs:8:1 | ||
| | | ||
| LL | type const ONE: usize = ADD1::<core::direct_const_arg!(b"")>; | ||
| | ^^^^^^^^^^^^^^^^^^^^^ types differ | ||
|
|
||
| error: the constant `*b""` is not of type `usize` | ||
| --> $DIR/type-const-arg-type-mismatch-1.rs:8:1 | ||
| | | ||
| LL | type const ONE: usize = ADD1::<core::direct_const_arg!(b"")>; | ||
| | ^^^^^^^^^^^^^^^^^^^^^ expected `usize`, found `[u8; 0]` | ||
| | | ||
| note: required by a const generic parameter in `ADD1` | ||
| --> $DIR/type-const-arg-type-mismatch-1.rs:7:12 | ||
| | | ||
| LL | const ADD1<const N: usize>: usize = N + 1; | ||
| | ^^^^^^^^^^^^^^ required by this const generic parameter in `ADD1` | ||
|
|
||
| error: the constant `ADD1::<*b"">` is not of type `usize` | ||
| --> $DIR/type-const-arg-type-mismatch-1.rs:8:1 | ||
| | | ||
| LL | type const ONE: usize = ADD1::<core::direct_const_arg!(b"")>; | ||
| | ^^^^^^^^^^^^^^^^^^^^^ expected `usize`, found a different `usize` | ||
|
|
||
| error: aborting due to 3 previous errors | ||
|
|
||
| For more information about this error, try `rustc --explain E0271`. |
12 changes: 12 additions & 0 deletions
12
tests/ui/const-generics/gca/type-const-arg-type-mismatch-2.rs
This file contains hidden or 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,12 @@ | ||
| //! Regression test for <https://github.com/rust-lang/rust/issues/154805>. | ||
| //@ compile-flags: -Znext-solver=globally | ||
| #![feature(min_generic_const_args)] | ||
| #![feature(macroless_generic_const_args)] | ||
| #![feature(generic_const_args)] | ||
| #![feature(generic_const_items)] | ||
|
|
||
| const ADD1<const N: usize>: usize = N + 1; | ||
| type const ONE: usize = ADD1::<b"">; //~ ERROR type mismatch resolving | ||
| //~| ERROR the constant `*b""` is not of type `usize` | ||
| //~| ERROR the constant `ADD1::<*b"">` is not of type `usize` | ||
| fn main() {} |
27 changes: 27 additions & 0 deletions
27
tests/ui/const-generics/gca/type-const-arg-type-mismatch-2.stderr
This file contains hidden or 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,27 @@ | ||
| error[E0271]: type mismatch resolving `ADD1<*b""> == _` | ||
| --> $DIR/type-const-arg-type-mismatch-2.rs:9:1 | ||
| | | ||
| LL | type const ONE: usize = ADD1::<b"">; | ||
| | ^^^^^^^^^^^^^^^^^^^^^ types differ | ||
|
|
||
| error: the constant `*b""` is not of type `usize` | ||
| --> $DIR/type-const-arg-type-mismatch-2.rs:9:1 | ||
| | | ||
| LL | type const ONE: usize = ADD1::<b"">; | ||
| | ^^^^^^^^^^^^^^^^^^^^^ expected `usize`, found `[u8; 0]` | ||
| | | ||
| note: required by a const generic parameter in `ADD1` | ||
| --> $DIR/type-const-arg-type-mismatch-2.rs:8:12 | ||
| | | ||
| LL | const ADD1<const N: usize>: usize = N + 1; | ||
| | ^^^^^^^^^^^^^^ required by this const generic parameter in `ADD1` | ||
|
|
||
| error: the constant `ADD1::<*b"">` is not of type `usize` | ||
| --> $DIR/type-const-arg-type-mismatch-2.rs:9:1 | ||
| | | ||
| LL | type const ONE: usize = ADD1::<b"">; | ||
| | ^^^^^^^^^^^^^^^^^^^^^ expected `usize`, found a different `usize` | ||
|
|
||
| error: aborting due to 3 previous errors | ||
|
|
||
| For more information about this error, try `rustc --explain E0271`. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
not super sure about this comment
View changes since the review