Skip to content
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

Suggest dereferencing when Deref is implemented. #60155

Merged
merged 3 commits into from
Apr 23, 2019

Conversation

davidtwco
Copy link
Member

Fixes #59819.

r? @oli-obk
cc @estebank

This commit adds a test that demonstrates the current behaviour where
suggestions to add a dereference aren't given for non-references.
This commit suggests dereferencing a type when it implements `Deref`
with the correct `Output` associated type.
@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 21, 2019
@@ -463,6 +463,57 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
}
}
}
_ if sp == expr.span => {
// If neither type is a reference, then check for `Deref` implementations by
Copy link
Contributor

Choose a reason for hiding this comment

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

Seems like a good place to refactor into function(s)?

@oli-obk
Copy link
Contributor

oli-obk commented Apr 22, 2019

General note: When I wrote the issue I hoped that we could deduplicate the logic on references with the new logic you wrote. References also implement &T: Deref<Target = T>, so it should hopefully work out of the box?

@davidtwco
Copy link
Member Author

General note: When I wrote the issue I hoped that we could deduplicate the logic on references with the new logic you wrote. References also implement &T: Deref<Target = T>, so it should hopefully work out of the box?

I just tried this and I don't think it does, when I check if &{integer}: Deref<Target = i32> then it returns false. I assume that this is because &{integer}: Deref<Target = {integer}> and not i32 specifically but that's just a guess.

@oli-obk
Copy link
Contributor

oli-obk commented Apr 22, 2019

I just tried this and I don't think it does, when I check if &{integer}: Deref<Target = i32> then it returns false. I assume that this is because &{integer}: Deref<Target = {integer}> and not i32 specifically but that's just a guess.

Maybe we need to try something different then. Check whether the type implements Deref, get the Target type, try to coerce to the destination type.

@davidtwco
Copy link
Member Author

Maybe we need to try something different then. Check whether the type implements Deref, get the Target type, try to coerce to the destination type.

I have no idea what I was trying earlier, pushed a version that de-duplicates that logic.

This commit makes the suggestion to dereference when a type implements
`Deref` only apply if the dereference would succeed (ie. the type is
`Copy`, otherwise a borrow check error would occur).
@oli-obk
Copy link
Contributor

oli-obk commented Apr 23, 2019

@bors r+

This is absolutely awesome!

@bors
Copy link
Contributor

bors commented Apr 23, 2019

📌 Commit 7ab1bfd has been approved by oli-obk

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 23, 2019
@bors
Copy link
Contributor

bors commented Apr 23, 2019

⌛ Testing commit 7ab1bfd with merge 4eff852...

bors added a commit that referenced this pull request Apr 23, 2019
Suggest dereferencing when `Deref` is implemented.

Fixes #59819.

r? @oli-obk
cc @estebank
@bors
Copy link
Contributor

bors commented Apr 23, 2019

☀️ Test successful - checks-travis, status-appveyor
Approved by: oli-obk
Pushing 4eff852 to master...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Dereferencing Deref types other than references is missing suggestion
6 participants