-
Notifications
You must be signed in to change notification settings - Fork 13.8k
Do two passes of handle_opaque_type_uses_next
#147249
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: master
Are you sure you want to change the base?
Conversation
tests/ui/traits/next-solver/opaques/hidden-types-equate-before-fallback.rs
Show resolved
Hide resolved
…pre-fallback where we equate hidden types but do not report errors
ca75834
to
a3fbae5
Compare
This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
if first_pass { | ||
assert!(prev.is_none()); | ||
} | ||
} |
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 feel conflicted about this change and slightly dislike it.
I like having apply_definition_site_hidden_types
in a separate function. It more closely matches the setup in MIR borrowck, so it feels more consistent.
I guess you don't want to equate things again if you've already had a defining use in a previous iteration. We actually kind of need to do so in case we end up with new non-defining uses since a previous call to try_handle_opaque_type_uses_next
.
I wouldn't worry about the perf of equating a few opaque types too frequently.
Idk if you've got strong opinions here yourself, but I would prefer to keep it as two functions 🤔
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 want to push back on this comment a bit. I definitely understand wanting to mirror MIR borrowck a bit, but the work done here is much simpler than the work done there - so it's not entirely a 1:1 comparison.
Second, there is no change here in when we equate opaque type. It's just a change in the flow of thought:
before:
- for each opaque ty, search through possible defining uses, find the first valid one
- store it
- if none, error
- separately, for each opaque ty use site, lookup the defining use, and equate the current hidden ty with that
here:
- for each opaque ty, search through possible defining uses, find the first valid one
- equate all other hidden tys with that defining use
- store it
- if none, error
Basically, rather than going through this "store the hidden ty then later look it up for every use", we equate immediately after identifying it - for me this is much more understandable (which is why I did this refactor)
There is a slight perf benefit, in that we don't do n hashmap looks (for each opaque type use site), but like you said, that isn't something too much to worry about and was not the motivation for this change.
Actually, MIR borrowck could be written in the same way (actually, briefly skimming the code there, it seems like it could be cleaner too, since there we're even emitting a NonDefiningUseInDefiningScope
error in apply_definition_site_hidden_types
, even though I would expect an invariant of compute_definition_site_hidden_types
to be a hidden type (or error). I'm happy to make that change separately, but I am not convinced that we need to match that structure here.
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.
Actually, MIR borrowck could be written in the same way (actually, briefly skimming the code there, it seems like it could be cleaner too, since there we're even emitting a
NonDefiningUseInDefiningScope
error inapply_definition_site_hidden_types
, even though I would expect an invariant ofcompute_definition_site_hidden_types
to be a hidden type (or error). I'm happy to make that change separately, but I am not convinced that we need to match that structure here.
Taking a deeper look at this now, I didn't quite grok the MIR borrowck code on first glance. I'm not fully convinced that there isn't a cleanup that could be done there (but it's certainly not as simple as I thought), but I'm even more convinced that mirroring simple function names is not all that helpful. MIR borrowck handling of opaque types is much more complex than HIR typeck, and trying to mirror them just makes HIR typeck harder to understand.
@lcnr I fixed your first three comments. The rename from |
This comment has been minimized.
This comment has been minimized.
…ve the assert in favor over a comment, and change to & for &mut
c113b29
to
283ad66
Compare
let mut opaque_types: Vec<_> = self.infcx.clone_opaque_types(); | ||
for entry in &mut opaque_types { | ||
*entry = self.resolve_vars_if_possible(*entry); | ||
} | ||
debug!(?opaque_types); |
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.
given that we only ever use the opaques in compute_definition_site_hidden_types
, just move this into that function as well, or even have a single handle_opaque_type_uses_next(HandleOpaqueTypeUses::ErrorOnMissingDefiningUse9
// We do actually need to check this the second pass (we can't just | ||
// store this), because we can go from `UnconstrainedHiddenType` to | ||
// `HasDefiningUse` (because of fallback) |
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.
we can also just go from no uses to a defining use due to fallback, e.g. if you have soem ?will_fallback: Trait<opaque>
goal
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.
please also/instead add the more minimized tests from rust-lang/trait-system-refactor-initiative#240
Fixes rust-lang/trait-system-refactor-initiative#240
Also did a little bit of cleanup, can squash the commits if decided.
r? lcnr