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

Add method disambiguation help for trait implementation #62921

Merged

Conversation

iluuu1994
Copy link
Contributor

Closes #51046
Closes #40471

@rust-highfive
Copy link
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @cramertj (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jul 23, 2019
@cramertj
Copy link
Member

r? @estebank

@rust-highfive rust-highfive assigned estebank and unassigned cramertj Jul 23, 2019
Copy link
Contributor

@estebank estebank left a comment

Choose a reason for hiding this comment

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

Given the nature of the change, you can ignore my nitpicks, but it would be a good follow up task.

r=me once ci is happy

| ^^^^^^^^^^^^
= help: to disambiguate the method call, write `B::foo(AB{})` instead

error: aborting due to previous error
Copy link
Contributor

Choose a reason for hiding this comment

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

I think that the ideal output for this would instead be:

error[E0034]: multiple applicable items in scope
  --> $DIR/method-ambig-two-traits-from-impls.rs:15:11
   |
LL |     AB {}.foo();
   |           ^^^ multiple `foo` found
   |
note: candidate #1 is defined in an impl of the trait `A` for the type `AB`
  --> $DIR/method-ambig-two-traits-from-impls2.rs:7:5
   |
LL |     fn foo() {}
   |     ^^^^^^^^
note: candidate #2 is defined in an impl of the trait `B` for the type `AB`
  --> $DIR/method-ambig-two-traits-from-impls2.rs:11:5
   |
LL |     fn foo() {}
   |     ^^^^^^^^
help: disambiguate the method call to a specific trait:
  --> $DIR/method-ambig-two-traits-from-impls.rs:15:4
   |
LL |     A::foo(AB {})
   |     ^^^^^^^^^^^^^
LL |     B::foo(AB {})
   |     ^^^^^^^^^^^^^

The only reason I'm leaving the notes it's because that information is still useful and we can't (yet, at least) interleave suggestions and notes.

src/librustc_typeck/check/method/suggest.rs Outdated Show resolved Hide resolved
src/librustc_typeck/check/method/suggest.rs Outdated Show resolved Hide resolved
| ^^^^^^^^
= help: to disambiguate the method call, write `B::foo(...)` instead

error: aborting due to previous error
Copy link
Contributor

Choose a reason for hiding this comment

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

I think that ideally the output would be closer to

error[E0034]: multiple applicable items in scope
  --> $DIR/method-ambig-two-traits-from-impls.rs:15:11
   |
LL |     AB {}.foo();
   |           ^^^ multiple `foo` found
   |
note: candidate #1 is defined in an impl of the trait `A` for the type `AB`
  --> $DIR/method-ambig-two-traits-from-impls2.rs:7:5
   |
LL |     fn foo() {}
   |     ^^^^^^^^
note: candidate #2 is defined in an impl of the trait `B` for the type `AB`
  --> $DIR/method-ambig-two-traits-from-impls2.rs:11:5
   |
LL |     fn foo() {}
   |     ^^^^^^^^
help: disambiguate the method call to a specific trait:
  --> $DIR/method-ambig-two-traits-from-impls.rs:15:4
   |
LL |     A::foo(AB {})
   |     ^^^^^^^^^^^^^
LL |     B::foo(AB {})
   |     ^^^^^^^^^^^^^

Note that the help here would be created with err.span_suggestions, not err.span_note.

|s| s.print_expr(arg)))
.collect::<Vec<_>>()
.join(", ")).unwrap_or_else(|| "...".to_owned())));
print_disambiguation_help(err, self.tcx.def_path_str(trait_did));
Copy link
Contributor

Choose a reason for hiding this comment

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

I see that you're just moving code that was already there. If changing this to use a span_suggestion feels too much work for you to take on, we can merge this PR as is.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

feels too much work for you to take on, we can merge this PR as is

It's fine, I'll try to implement your suggestions 🙂

Copy link
Contributor

Choose a reason for hiding this comment

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

I created #62922 to track that work. If the tests pass on the current state, I'll just approve and then you can implement those suggestions at your own pace. They aren't difficult but might be time consuming. If you need any help with them, do not hesitate to reach out, and there are plenty of examples of how the span_suggestion api is supposed to be used.

@rust-highfive

This comment has been minimized.

args.map(|arg| arg
.iter()
.map(|arg| self.tcx.sess.source_map().span_to_snippet(arg.span)
.unwrap_or_else(|_| "...".to_owned()))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@estebank This this unwrap fine? When can this happen?

Copy link
Contributor

Choose a reason for hiding this comment

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

It shouldn't happen, but it can if the span is coming from a proc macro or an external crate.

@iluuu1994
Copy link
Contributor Author

I made the changes (except for #62922). Tests are passing 🙂

@estebank
Copy link
Contributor

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Jul 24, 2019

📌 Commit be510db has been approved by estebank

@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 Jul 24, 2019
Centril added a commit to Centril/rust that referenced this pull request Jul 24, 2019
…isambiguation, r=estebank

Add method disambiguation help for trait implementation

Closes rust-lang#51046
Closes rust-lang#40471
Centril added a commit to Centril/rust that referenced this pull request Jul 24, 2019
Rollup of 13 pull requests

Successful merges:

 - rust-lang#60938 (rustdoc: make #[doc(include)] relative to the containing file)
 - rust-lang#61890 (Fix some sanity checks)
 - rust-lang#62084 (allow clippy::unreadable_literal in unicode tables)
 - rust-lang#62261 (Take substs into account in `conservative_is_privately_uninhabited`)
 - rust-lang#62528 (Add joining slices of slices with a slice separator, not just a single item)
 - rust-lang#62735 (Turn `#[global_allocator]` into a regular attribute macro)
 - rust-lang#62801 (Remove support for -Zlower-128bit-ops)
 - rust-lang#62808 (Revert "Disable stack probing for gnux32.")
 - rust-lang#62822 (Improve some pointer-related documentation)
 - rust-lang#62904 (Disable d32 on armv6 hf targets)
 - rust-lang#62907 (Initialize the MSP430 AsmParser)
 - rust-lang#62921 (Add method disambiguation help for trait implementation)
 - rust-lang#62942 (Use match ergonomics in Condvar documentation)

Failed merges:

r? @ghost
Centril added a commit to Centril/rust that referenced this pull request Jul 25, 2019
…isambiguation, r=estebank

Add method disambiguation help for trait implementation

Closes rust-lang#51046
Closes rust-lang#40471
Centril added a commit to Centril/rust that referenced this pull request Jul 25, 2019
…isambiguation, r=estebank

Add method disambiguation help for trait implementation

Closes rust-lang#51046
Closes rust-lang#40471
bors added a commit that referenced this pull request Jul 25, 2019
Rollup of 15 pull requests

Successful merges:

 - #60066 (Stabilize the type_name intrinsic in core::any)
 - #60938 (rustdoc: make #[doc(include)] relative to the containing file)
 - #61884 (Stablize Euclidean Modulo (feature euclidean_division))
 - #61890 (Fix some sanity checks)
 - #62528 (Add joining slices of slices with a slice separator, not just a single item)
 - #62707 (Add tests for overlapping explicitly dropped locals in generators)
 - #62735 (Turn `#[global_allocator]` into a regular attribute macro)
 - #62822 (Improve some pointer-related documentation)
 - #62887 (Make the parser TokenStream more resilient after mismatched delimiter recovery)
 - #62921 (Add method disambiguation help for trait implementation)
 - #62930 (Add test for #51559)
 - #62942 (Use match ergonomics in Condvar documentation)
 - #62977 (Fix inconsistent highlight blocks.)
 - #62978 (Remove `cfg(bootstrap)` code for array implementations)
 - #62981 (Add note suggesting to borrow a String argument to find)

Failed merges:

 - #62964 (clarify and unify some type test names)

r? @ghost
@bors bors merged commit be510db into rust-lang:master Jul 26, 2019
@haraldh
Copy link
Contributor

haraldh commented Aug 1, 2019

@iluuu1994
Copy link
Contributor Author

@haraldh Similar cases are already tracked here: #58988

Feel free to leave another comment there with your snippet.

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