-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Support const args in type dependent paths #71154
Conversation
cc @eddyb |
I've converted the PR to a draft, and I will not be able to review it too soon. |
705a722
to
d4b7335
Compare
Now also supports struct A;
impl A {
fn foo<const N: usize>() -> usize { N + 1 }
}
fn main() {
assert_eq!(A::foo::<7>(), 8);
} |
f9d4a7f
to
0a12721
Compare
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
282fd2f
to
45cfb4c
Compare
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
☔ The latest upstream changes (presumably #71049) made this pull request unmergeable. Please resolve the merge conflicts. |
c003803
to
20bced3
Compare
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
5f49c29
to
14358b1
Compare
Please keep it draft as per #71154 (comment). |
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
☀️ Try build successful - checks-azure |
Queued 2e0ee43e993a0a97edc6a0af1116bfbccdf1dd36 with parent a647c0c, future comparison URL. |
Finished benchmarking try commit (2e0ee43e993a0a97edc6a0af1116bfbccdf1dd36): comparison url. |
6ae75a1
to
60dae6e
Compare
60dae6e
to
a23a444
Compare
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
☔ The latest upstream changes (presumably #73830) made this pull request unmergeable. Please resolve the merge conflicts. |
Will open a new PR in the next few weeks as I have to rewrite most of this anyways |
in the next few weeks i guess time is relative #74113 |
Support const args in type dependent paths (Take 2) once more, except it is sound this time 🥰 previously rust-lang#71154 ----- ```rust #![feature(const_generics)] struct A; impl A { fn foo<const N: usize>(&self) -> usize { N } } struct B; impl B { fn foo<const N: usize>(&self) -> usize { 42 } } fn main() { let a = A; a.foo::<7>(); } ``` When calling `type_of` for generic const arguments, we now use the `TypeckTables` of the surrounding body to get the expected type. This alone causes cycle errors though, as we now have `typeck_tables_of(main)` -> `...` -> `type_of(main_ANON0 := 7)` -> `typeck_tables_of(main)` ⚡ (see rust-lang#68400 (comment)) To prevent this we must not call `type_of(const_arg)` during `typeck_tables_of`. This is achieved by calling `type_of(param_def_id)` instead. We have to somehow remember the `DefId` of the param through all of typeck, which is done using the struct `ty::WithOptConstParam<DefId>`, which replaces `DefId` where needed and contains an `Option<DefId>` to be able to store the const parameter in case it exists. Queries which are currently cached on disk are split into two variants: `query_name`(cached) and `query_name_(of|for)_const_arg`(not cached), with `query_name_of_const_arg` taking a pair `(did, param_did): (LocalDefId, DefId)`. For some queries a method `query_name_of_opt_const_arg` is added to `TyCtxt` which takes a `ty::WithOptConstParam` and either calls `query_name` or `query_name_of_const_arg` depending on the value of `const_param_did`. r? @eddyb @varkor
When calling
type_of
for generic const arguments, we now use theTypeckTables
of the surrounding body to get the expected type.This alone causes cycle errors though, as we now have
typeck_tables_of(main)
->...
->type_of(main_ANON0 := 7)
->typeck_tables_of(main)
⚡ (see #68400 (comment))To prevent this we must not call
type_of(const_arg)
duringtypeck_tables_of
. This is achieved bycalling
type_of(param_def_id)
instead. As thisDefId
can't always be easily known, I changed someDefId
s toty::WithOptParam<DefId>
which contains the relevant param id. This also changes some queries which may be called during typeck.To prevent us from computing these queries twice,
WithOptParam
must always use the correctDefId
of the parameter. This means that it is either constructed using the new methodtcx.with_opt_param(def_id)
or manually if and only if a paramDefId
is available.To simplify
WithOptParam
creation, I changedIntoQueryParam
from private topub(crate)
.I only use the existing definition of
LocalDefId -> DefId
.const_param_of
requires the HIR, meaning that it can only return the correct parameterDefId
while compiling the crate containing the const argument.To fix this,
const_param_of
is called for all const arguments during analysis. (I currently usepar_body_owner
here, as all const arguments should be a body afaik)This PR may have missed some
type_of(const_arg)
during typeck. While this is unfortunate, these cases should only introduce cycle errors and not lead to miscompilations.We should not have to worry about these cases while merging this IMO.
r? @varkor
fixes #70507, fixes #69816, fixes #63695, fixes #61936, fixes #71516