-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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: correct the args for disambiguate the associated function
diagnostic
#118911
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
tests/ui/methods/disambiguate-associated-function-first-arg.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
struct A {} | ||
|
||
fn main() { | ||
let _a = A {}; | ||
_a.new(1); | ||
//~^ ERROR no method named `new` found for struct `A` in the current scope | ||
} | ||
|
||
trait M { | ||
fn new(_a: i32); | ||
} | ||
impl M for A { | ||
fn new(_a: i32) {} | ||
} | ||
|
||
trait N { | ||
fn new(_a: Self, _b: i32); | ||
} | ||
impl N for A { | ||
fn new(_a: Self, _b: i32) {} | ||
} | ||
|
||
trait O { | ||
fn new(_a: Self, _b: i32); | ||
} | ||
impl O for A { | ||
fn new(_a: A, _b: i32) {} | ||
} | ||
|
||
struct S; | ||
|
||
trait TraitA { | ||
fn f(self); | ||
} | ||
trait TraitB { | ||
fn f(self); | ||
} | ||
|
||
impl<T> TraitA for T { | ||
fn f(self) {} | ||
} | ||
impl<T> TraitB for T { | ||
fn f(self) {} | ||
} | ||
|
||
fn test() { | ||
S.f(); | ||
//~^ multiple applicable items in scope | ||
} |
67 changes: 67 additions & 0 deletions
67
tests/ui/methods/disambiguate-associated-function-first-arg.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
error[E0599]: no method named `new` found for struct `A` in the current scope | ||
--> $DIR/disambiguate-associated-function-first-arg.rs:5:8 | ||
| | ||
LL | struct A {} | ||
| -------- method `new` not found for this struct | ||
... | ||
LL | _a.new(1); | ||
| ^^^ this is an associated function, not a method | ||
| | ||
= note: found the following associated functions; to be used as methods, functions must have a `self` parameter | ||
note: candidate #1 is defined in the trait `M` | ||
--> $DIR/disambiguate-associated-function-first-arg.rs:10:5 | ||
| | ||
LL | fn new(_a: i32); | ||
| ^^^^^^^^^^^^^^^^ | ||
note: candidate #2 is defined in the trait `N` | ||
--> $DIR/disambiguate-associated-function-first-arg.rs:17:5 | ||
| | ||
LL | fn new(_a: Self, _b: i32); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
note: candidate #3 is defined in the trait `O` | ||
--> $DIR/disambiguate-associated-function-first-arg.rs:24:5 | ||
| | ||
LL | fn new(_a: Self, _b: i32); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
help: disambiguate the associated function for candidate #1 | ||
| | ||
LL | <A as M>::new(1); | ||
| ~~~~~~~~~~~~~~~~ | ||
help: disambiguate the associated function for candidate #2 | ||
| | ||
LL | <A as N>::new(_a, 1); | ||
| ~~~~~~~~~~~~~~~~~~~~ | ||
help: disambiguate the associated function for candidate #3 | ||
| | ||
LL | <A as O>::new(_a, 1); | ||
| ~~~~~~~~~~~~~~~~~~~~ | ||
|
||
error[E0034]: multiple applicable items in scope | ||
--> $DIR/disambiguate-associated-function-first-arg.rs:47:7 | ||
| | ||
LL | S.f(); | ||
| ^ multiple `f` found | ||
| | ||
note: candidate #1 is defined in an impl of the trait `TraitA` for the type `T` | ||
--> $DIR/disambiguate-associated-function-first-arg.rs:40:5 | ||
| | ||
LL | fn f(self) {} | ||
| ^^^^^^^^^^ | ||
note: candidate #2 is defined in an impl of the trait `TraitB` for the type `T` | ||
--> $DIR/disambiguate-associated-function-first-arg.rs:43:5 | ||
| | ||
LL | fn f(self) {} | ||
| ^^^^^^^^^^ | ||
help: disambiguate the method for candidate #1 | ||
| | ||
LL | TraitA::f(S); | ||
| ~~~~~~~~~~~~ | ||
help: disambiguate the method for candidate #2 | ||
| | ||
LL | TraitB::f(S); | ||
| ~~~~~~~~~~~~ | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
Some errors have detailed explanations: E0034, E0599. | ||
For more information about an error, try `rustc --explain E0034`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While this check works for a lot of cases, it doesn't work for more complex ones. E.g., here
However, I don't think you need to address this in this PR since it's super broken anyway at the moment, it suggests
<Box<S> as TraitA>::f();
&<Box<S> as TraitB>::f()
while it should suggestTraitA::f(Box::new(S))
/<S as TraitA>::f(Box::new(S))
&TraitB::f(Box::new(S))
/<S as TraitB>::f(Box::new(S))
.I think this could be solved by using
FnCtxt::can_eq
instead of==
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Further, with plain
==
we will never consider type parameters to be valid receiver types while they very well could be:You could think about
instantiate
'ing thefn_sig
withfresh_args_for_item
instead of callingskip_binder
and usingFnCtxt::can_eq
with theParamEnv
of the assoc fn. Well, it can still have false positives due to autoref/autoderef. This way, we would rely less on the checkarbitrary_self_types_check(…)
/item.fn_has_self_parameter
.