Support const args in type dependent paths#71154
Support const args in type dependent paths#71154lcnr wants to merge 14 commits intorust-lang:masterfrom
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 |
|
Finished benchmarking try commit (0a204d6a1aef863bca037ca6e88ba49ce3e734ed): comparison url. |
src/librustc_middle/query/mod.rs
Outdated
There was a problem hiding this comment.
Is this expected to be called for non-const-argument def_ids?
There was a problem hiding this comment.
Yes, made this more explicit in the query description.
varkor
left a comment
There was a problem hiding this comment.
I've had a skim through, and I don't have any major comments. I also think the perf results are acceptable, consider most differences are negligible, and this is necessary to move const generics forward to a point where it starts to become more usable. However, I don't want to sign off without @eddyb's approval: they may well have more comments, or ideas on how to improve the performance hit.
src/librustc_middle/query/mod.rs
Outdated
There was a problem hiding this comment.
Could you expand on this comment a little, for a reader who may not have the full context?
There was a problem hiding this comment.
Folding DefIds is a noop anyways 😆 Kind of weird that we explicitly fold them here 🤔 We don't do this while folding TyKind
There was a problem hiding this comment.
Could you add a comment explaining the general purpose of this function, with a small example?
There was a problem hiding this comment.
Added a comment referring back to the query tcx.const_param_of.
|
I'm going to convert this to a proper pull request now that it seems to be ready for a final review. |
|
@bors try @rust-timer queue |
|
Awaiting bors try build completion |
|
⌛ Trying commit 34648b9559268f8e7521c458162525f2cf95bb4b with merge 2e0ee43e993a0a97edc6a0af1116bfbccdf1dd36... |
|
☀️ Try build successful - checks-azure |
|
Queued 2e0ee43e993a0a97edc6a0af1116bfbccdf1dd36 with parent a647c0c, future comparison URL. |
|
Finished benchmarking try commit (2e0ee43e993a0a97edc6a0af1116bfbccdf1dd36): comparison url. |
|
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 |
When calling
type_offor generic const arguments, we now use theTypeckTablesof 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 thisDefIdcan't always be easily known, I changed someDefIds 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,
WithOptParammust always use the correctDefIdof 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 paramDefIdis available.To simplify
WithOptParamcreation, I changedIntoQueryParamfrom private topub(crate).I only use the existing definition of
LocalDefId -> DefId.const_param_ofrequires the HIR, meaning that it can only return the correct parameterDefIdwhile compiling the crate containing the const argument.To fix this,
const_param_ofis called for all const arguments during analysis. (I currently usepar_body_ownerhere, 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