Skip to content

internal: Refactor autoderef/method resolution #11550

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 4 commits into from
Feb 25, 2022

Conversation

flodiebold
Copy link
Member

  • don't return the receiver type from method resolution; instead just
    return the autorefs/autoderefs that happened and repeat them. This
    ensures all the effects like trait obligations and whatever we learned
    about type variables from derefing them are actually applied. Also, it
    allows us to get rid of decanonicalize_ty, which was just wrong in
    principle.

  • Autoderef itself now directly works with an inference table. Sadly
    this has the effect of making it harder to use as an iterator, often
    requiring manual while let loops. (rustc works around this by using
    inner mutability in the inference context, so that things like unifying
    types don't require a unique reference.)

  • We now record the adjustments (autoref/deref) for method receivers
    and index expressions, which we didn't before.

  • Removed the redundant crate parameter from method resolution, since
    the trait_env contains the crate as well.

  • in the HIR API, the methods now take a scope to determine the trait env.
    Type carries a trait env, but I think that's probably a bad decision
    because it's easy to create it with the wrong env, e.g. by using
    Adt::ty. This mostly didn't matter so far because
    iterate_method_candidates took a crate parameter and ignored
    self.krate, but the trait env would still have been wrong in those
    cases, which I think would give some wrong results in some edge cases.

Fixes #10058.

 - don't return the receiver type from method resolution; instead just
 return the autorefs/autoderefs that happened and repeat them. This
 ensures all the effects like trait obligations and whatever we learned
 about type variables from derefing them are actually applied. Also, it
 allows us to get rid of `decanonicalize_ty`, which was just wrong in
 principle.

 - Autoderef itself now directly works with an inference table. Sadly
 this has the effect of making it harder to use as an iterator, often
 requiring manual `while let` loops. (rustc works around this by using
 inner mutability in the inference context, so that things like unifying
 types don't require a unique reference.)

 - We now record the adjustments (autoref/deref) for method receivers
 and index expressions, which we didn't before.

 - Removed the redundant crate parameter from method resolution, since
 the trait_env contains the crate as well.

 - in the HIR API, the methods now take a scope to determine the trait env.
 `Type` carries a trait env, but I think that's probably a bad decision
 because it's easy to create it with the wrong env, e.g. by using
 `Adt::ty`. This mostly didn't matter so far because
 `iterate_method_candidates` took a crate parameter and ignored
 `self.krate`, but the trait env would still have been wrong in those
 cases, which I think would give some wrong results in some edge cases.

Fixes rust-lang#10058.
@flodiebold
Copy link
Member Author

bors r+

@bors
Copy link
Contributor

bors bot commented Feb 25, 2022

@bors bors bot merged commit ab896e3 into rust-lang:master Feb 25, 2022
@lnicola
Copy link
Member

lnicola commented Feb 25, 2022

As seen in the metrics:

image

@flodiebold flodiebold deleted the autoderef-overhaul-2 branch February 25, 2022 17:18
@Veykril Veykril changed the title Refactor autoderef/method resolution internal: Refactor autoderef/method resolution Feb 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

once_cell::Lazy fails to infer type parameter
2 participants