-
Notifications
You must be signed in to change notification settings - Fork 547
ty::Unevaluated
: dealing with unused substs
#1190
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# Constants in the type system | ||
|
||
Constants used in the type system are represented as [`ty::Const`]. | ||
The variants of their [`ty::ConstKind`] mostly mirror the variants of [`ty::TyKind`] | ||
with the two *additional* variants being `ConstKind::Value` and `ConstKind::Unevaluated`. | ||
|
||
|
||
## Unevaluated constants | ||
|
||
*This section talks about what's happening with `feature(const_generics)` enabled. | ||
On stable we do not yet supply any generic parameters to anonymous constants, | ||
avoiding most of the issues mentioned here.* | ||
|
||
Unless a constant is either a simple literal, e.g. `[u8; 3]` or `foo::<{ 'c' }>()`, | ||
or a generic parameter, e.g. `[u8; N]`, converting a constant to its [`ty::Const`] representation | ||
returns an unevaluated constant. Even fully concrete constants which do not depend on a | ||
generic parameter are not evaluated right away. | ||
|
||
We do not eagerly evaluate constant as they can be used in the `where`-clauses of their | ||
parent item, for example: | ||
|
||
```rust | ||
#[feature(const_generics, const_evaluatable_checked)] | ||
fn foo<T: Trait>() | ||
where | ||
[u8; <T as Trait>::ASSOC + 1]: SomeOtherTrait, | ||
{} | ||
``` | ||
|
||
The constant `<T as Trait>::ASSOC + 1` depends on the `T: Trait` bound of | ||
its parents caller bounds, but is also part of another bound itself. | ||
If we were to eagerly evaluate this constant while computing its parents bounds | ||
this would cause a query cycle. | ||
|
||
### Generic arguments of anonymous constants | ||
|
||
Anonymous constants inherit the generic parameters of their parent, which is | ||
why the array length in `foo<const N: usize>() -> [u8; N + 1]` can use `N`. | ||
|
||
Without any manual adjustments, this causes us to include parameters even if | ||
the constant doesn't use them in any way. This can cause | ||
[some interesting errors](pcg-unused-substs) and breaks some already stable code. | ||
|
||
To deal with this, we intend to look at the generic parameters explicitly mentioned | ||
by the constants and then search the predicates of its parents to figure out which | ||
of the other generic parameters are reachable by our constant. | ||
|
||
**TODO**: Expand this section once the parameter filtering is implemented. | ||
|
||
As constants can be part of their parents `where`-clauses, we mention unevaluated | ||
constants in their parents predicates. It is therefore necessary to mention unevaluated | ||
constants without being able to first compute the generic parameters | ||
available to these constants. | ||
|
||
To do this unevaluated constants start out with [`substs_`] being `None` while assuming | ||
that their generic arguments could be arbitrary generic parameters. | ||
When first accessing the generic arguments of an unevaluated constants, we then replace | ||
`substs_` with the actual default arguments of a constants, which are the generic parameters | ||
of their parent we assume to be used by this constant. | ||
|
||
[`ty::Const`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.Const.html | ||
[`ty::ConstKind`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/enum.ConstKind.html | ||
[`ty::TyKind`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/enum.TyKind.html | ||
[pcg-unused-substs]: https://github.com/rust-lang/project-const-generics/blob/master/design-docs/anon-const-substs.md#unused-substs | ||
[`substs_`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/consts/kind/struct.Unevaluated.html#structfield.substs_ |
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.