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

fix: dedup static_candidates before report #111872

Merged
merged 1 commit into from
May 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions compiler/rustc_hir_typeck/src/method/suggest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let mut custom_span_label = false;

let static_candidates = &mut no_match_data.static_candidates;

// `static_candidates` may have same candidates appended by
// inherent and extension, which may result in incorrect
// diagnostic.
static_candidates.dedup();
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you add a comment why we cannot remove the duplicated call to record_static_candidate?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

inherent and extension may append the same item to static_candidates.

For example:

trait Cat {
    fn nya() {}
}

fn uwu<T: Cat>(c: T) {
    c.nya();
}

Both <T as Cat> (inherent) and <_ as Cat> (extension) had been tried.

And another:

trait Cat {
    fn nya() {}
}

fn uwu<T>(c: T) {
    c.nya();
}

<_ as Cat> (extension) should be tried.

Therefore, we can't remove duplicated call of record_static_candidate.

Copy link
Contributor

Choose a reason for hiding this comment

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

The comment you added says that in order to improve the diagnostic, we record both inherent and extension cases. But this PR does the opposite: we deduplicate to improve the diagnostic.
You don't need to link to the PR. The 2 lines-long explanation is enough.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have two questions:

  1. Is it correct to say that we cannot remove the duplicated call of record_static_candidate? Also, is it intended behavior that the unexpected diagnostic of Unhelpful help when calling associated trait function as method #103646 was caused by invoking record_static_candidate for the same item in both assemble_extension_candidates_for_trait and assemble_inherent_impl_probe?
  2. Is static_candidates.dedup() the right approach here? If this statement has too broad impact, can we change the condition to !self.has_applicable_self(&item) && !self.static_candidates.contains(&item) in extension_candidates?


if !static_candidates.is_empty() {
err.note(
"found the following associated functions; to be used as methods, \
Expand Down
11 changes: 11 additions & 0 deletions tests/ui/suggestions/issue-103646.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
trait Cat {
fn nya() {}
}

fn uwu<T: Cat>(c: T) {
c.nya();
//~^ ERROR no method named `nya` found for type parameter `T` in the current scope
//~| Suggestion T::nya()
}

fn main() {}
21 changes: 21 additions & 0 deletions tests/ui/suggestions/issue-103646.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
error[E0599]: no method named `nya` found for type parameter `T` in the current scope
--> $DIR/issue-103646.rs:6:7
|
LL | fn uwu<T: Cat>(c: T) {
| - method `nya` not found for this type parameter
LL | c.nya();
| --^^^--
| | |
| | this is an associated function, not a method
| help: use associated function syntax instead: `T::nya()`
|
= note: found the following associated functions; to be used as methods, functions must have a `self` parameter
note: the candidate is defined in the trait `Cat`
--> $DIR/issue-103646.rs:2:5
|
LL | fn nya() {}
| ^^^^^^^^

error: aborting due to previous error

For more information about this error, try `rustc --explain E0599`.