Skip to content

More correct method resolution #2463

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 6 commits into from
Dec 3, 2019

Conversation

flodiebold
Copy link
Member

This should fix the order in which candidates for method resolution are considered, i.e. (&Foo).clone() should now be of type Foo instead of &Foo. It also checks for inherent candidates that the self type unifies properly with the self type in the impl (i.e. impl Foo<u32> methods will only be considered for Foo<u32>).

To be able to get the correct receiver type to check in the method resolution, I needed the unification logic, so I extracted it to the unify.rs module.

Should fix #2435.

@flodiebold flodiebold requested a review from matklad December 2, 2019 18:45
| (Ty::Infer(InferTy::IntVar(tv)), other @ ty_app!(TypeCtor::Int(_)))
| (other @ ty_app!(TypeCtor::Int(_)), Ty::Infer(InferTy::IntVar(tv)))
| (Ty::Infer(InferTy::FloatVar(tv)), other @ ty_app!(TypeCtor::Float(_)))
| (other @ ty_app!(TypeCtor::Float(_)), Ty::Infer(InferTy::FloatVar(tv))) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is hard to read but I'm not sure how I would clean it up.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. I feel that type inference is that sort of code where the essential complexity is pretty high, and the only way to make sure that code is maintainable is thorough test coverage.

let self_ty_with_vars = db.impl_self_ty(impl_id).subst(&vars);
let self_ty_with_vars =
Canonical { num_vars: vars.len(), value: &self_ty_with_vars };
if let Some(substs) = super::infer::unify(self_ty_with_vars, &self_ty.value)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

match might read better here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's refactored away 🙂

@matklad
Copy link
Member

matklad commented Dec 3, 2019

LGTM!

@flodiebold
Copy link
Member Author

bors r+

bors bot added a commit that referenced this pull request Dec 3, 2019
2463: More correct method resolution r=flodiebold a=flodiebold

This should fix the order in which candidates for method resolution are considered, i.e. `(&Foo).clone()` should now be of type `Foo` instead of `&Foo`. It also checks for inherent candidates that the self type unifies properly with the self type in the impl (i.e. `impl Foo<u32>` methods will only be considered for `Foo<u32>`).

To be able to get the correct receiver type to check in the method resolution, I needed the unification logic, so I extracted it to the `unify.rs` module.

Should fix #2435.

Co-authored-by: Florian Diebold <[email protected]>
@bors
Copy link
Contributor

bors bot commented Dec 3, 2019

Build succeeded

  • Rust
  • TypeScript

@bors bors bot merged commit 18f25ac into rust-lang:master Dec 3, 2019
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.

Wrong inference for clone()
3 participants