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

When encountering move error on an `Option`, suggest using `as_ref` #61147

Merged
merged 2 commits into from May 27, 2019

Conversation

Projects
None yet
7 participants
@estebank
Copy link
Contributor

commented May 25, 2019

Fix #61109, cc #15457.

@rust-highfive

This comment has been minimized.

Copy link
Collaborator

commented May 25, 2019

r? @eddyb

(rust_highfive has picked a reviewer for you, use r? to override)

@estebank

This comment has been minimized.

Copy link
Contributor Author

commented May 25, 2019

@rust-highfive rust-highfive assigned oli-obk and unassigned eddyb May 25, 2019

@czipperz

This comment has been minimized.

Copy link
Contributor

commented May 25, 2019

Does this correctly handle as_mut as well?

@czipperz

This comment has been minimized.

Copy link
Contributor

commented May 25, 2019

Perhaps this should support Result as well?

@estebank

This comment has been minimized.

Copy link
Contributor Author

commented May 25, 2019

@czipperz adding Result support now.

Does this correctly handle as_mut as well?

Could you expand?

@czipperz

This comment has been minimized.

Copy link
Contributor

commented May 25, 2019

I don't know if this is possible, but it would be nice if it suggested as_mut when you need mutable access to call a method. as_ref().unwrap() converts Option<T> to &T but sometimes you need &mut T.

original_path.ty(self.mir, self.infcx.tcx).ty,
);
let snippet = self.infcx.tcx.sess.source_map().span_to_snippet(span).unwrap();
let is_option = orig_path_ty.starts_with("std::option::Option");

This comment has been minimized.

Copy link
@oli-obk

oli-obk May 25, 2019

Contributor

How do you feel about waiting a bit with merging this PR (a bit being until #60966 is merged)? Then you can annotate Option and Result with #[rustc_diagnostic_item = "option"] and "result" respectively and then check for these items via

if let Some(adt) = original_path.ty(self.mir, self.infcx.tcx).adt_def() {
	if self.infcx.tcx.is_diagnostic_item(adt.did, sym::option) {
        ...
    }
}

This comment has been minimized.

Copy link
@estebank

estebank May 25, 2019

Author Contributor

There are a couple of places where we're already doing string comparisons like this, so I love the fact that we'll be able to get rid of them soon!

This comment has been minimized.

Copy link
@oli-obk

oli-obk May 25, 2019

Contributor

Hmm... or we can just merge this and fix it up later, doesn't seem like a big deal in fact.

This comment has been minimized.

Copy link
@estebank

estebank May 25, 2019

Author Contributor

I'm fine either way

@estebank estebank force-pushed the estebank:suggest-as-ref branch from 00b254e to 1cc42ea May 25, 2019

@estebank

This comment has been minimized.

Copy link
Contributor Author

commented May 25, 2019

@czipperz suggesting both is bit heavy, but can be done. I'm a bit ambivalent on whether it is worth it. On one hand, it teaches the API surface nicely, on the other hand it is verbose and might confuse newcomers.

@oli-obk

This comment has been minimized.

Copy link
Contributor

commented May 25, 2019

My vote is for just suggesting as_ref and figuring out how to make sure we emit good errors on subsequent errors that may occur due to the code actually requiring mutable references

@estebank

This comment has been minimized.

Copy link
Contributor Author

commented May 26, 2019

Should I just remove the last commit or are there any further changes needed (beyond #60966)?

@oli-obk

This comment has been minimized.

Copy link
Contributor

commented May 26, 2019

r=me with last commit removed. I agree with your assessment that we'd be overwhelming users for little gain

@estebank estebank force-pushed the estebank:suggest-as-ref branch from d2e933d to 1cc42ea May 26, 2019

@estebank

This comment has been minimized.

Copy link
Contributor Author

commented May 26, 2019

@bors r=oli-obk

@bors

This comment has been minimized.

Copy link
Contributor

commented May 26, 2019

📌 Commit 1cc42ea has been approved by oli-obk

@bors

This comment has been minimized.

Copy link
Contributor

commented May 27, 2019

⌛️ Testing commit 1cc42ea with merge ed2a511...

bors added a commit that referenced this pull request May 27, 2019

Auto merge of #61147 - estebank:suggest-as-ref, r=oli-obk
When encountering move error on an `Option`, suggest using `as_ref`

Fix #61109, cc #15457.
@bors

This comment has been minimized.

Copy link
Contributor

commented May 27, 2019

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

@bors bors added the merged-by-bors label May 27, 2019

@bors bors merged commit 1cc42ea into rust-lang:master May 27, 2019

2 checks passed

Travis CI - Pull Request Build Passed
Details
homu Test successful
Details
@nnethercote

This comment has been minimized.

Copy link
Contributor

commented Jun 6, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.