-
-
Notifications
You must be signed in to change notification settings - Fork 15.3k
Prefer inherent over supertrait methods in trait object method resolution #158320
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
Open
Jules-Bertholet
wants to merge
4
commits into
rust-lang:main
Choose a base branch
from
Jules-Bertholet:dyn-inherent
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+192
−35
Open
Changes from all commits
Commits
Show all changes
4 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 hidden or 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
32 changes: 32 additions & 0 deletions
32
tests/ui/methods/call-ambig-trait-object-method-supertrait.rs
This file contains hidden or 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,32 @@ | ||
| //@ edition: 2024 | ||
| //@ run-pass | ||
|
|
||
| trait Super { | ||
| fn foo(&self) -> i32 { | ||
| 27 | ||
| } | ||
| } | ||
|
|
||
| trait Sub: Super {} | ||
|
|
||
| impl<'a> dyn Sub + 'a { | ||
| fn foo(&self) -> i32 { | ||
| 42 | ||
| } | ||
| } | ||
|
|
||
| impl Super for i32 {} | ||
| impl Sub for i32 {} | ||
|
|
||
| fn main() { | ||
| let x = &0i32; | ||
| assert_eq!(x.foo(), 27); | ||
|
|
||
| let x: &dyn Sub = &0i32; | ||
| assert_eq!(x.foo(), 42); | ||
| assert_eq!(<dyn Sub>::foo(x), 42); | ||
| assert_eq!(<dyn Sub as Super>::foo(x), 27); | ||
|
|
||
| let x: &(dyn Sub + Send) = &0i32; | ||
| assert_eq!(x.foo(), 27); | ||
| } |
This file contains hidden or 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 |
|---|---|---|
| @@ -1,20 +1,54 @@ | ||
| //! Regression test for <https://github.com/rust-lang/rust/issues/18446>. | ||
| //! Test that name clashes between the method in an impl for the type | ||
| //! and the method in the trait when both are in the same scope. | ||
| //@ edition: 2024 | ||
|
|
||
| trait T { | ||
| fn foo(&self); | ||
| fn foo(&self) -> i32; | ||
| fn bar(&self) -> i32; | ||
| } | ||
|
|
||
| impl<'a> dyn T + 'a { | ||
| fn foo(&self) {} | ||
| fn foo(&self) -> i32 { | ||
| 1 | ||
| } | ||
|
|
||
| fn bar(&self) {} | ||
| } | ||
|
|
||
| impl T for i32 { | ||
| fn foo(&self) {} | ||
| fn foo(&self) -> i32 { | ||
| 0 | ||
| } | ||
|
|
||
| fn bar(&self) -> i32 { | ||
| 0 | ||
| } | ||
| } | ||
|
|
||
| trait OtherTrait { | ||
| fn foo(&self) -> i32 { | ||
| i32::MIN | ||
| } | ||
| } | ||
|
|
||
| impl OtherTrait for dyn T {} | ||
|
|
||
| fn main() { | ||
| let x = &0i32; | ||
| assert_eq!(x.foo(), 0); | ||
| assert_eq!(x.bar(), 0); | ||
|
|
||
| let x: &dyn T = &0i32; | ||
| x.foo(); //~ ERROR multiple applicable items in scope [E0034] | ||
| assert_eq!(x.foo(), 1); | ||
| //~^ ERROR multiple applicable items in scope | ||
| assert_eq!(x.bar(), ()); | ||
| //~^ ERROR multiple applicable items in scope | ||
| assert_eq!(<dyn T>::foo(x), 1); | ||
| //~^ ERROR multiple applicable items in scope | ||
| assert_eq!(<dyn T as T>::foo(x), 0); | ||
| assert_eq!(<dyn T>::bar(x), ()); | ||
| //~^ ERROR multiple applicable items in scope | ||
| assert_eq!(<dyn T as T>::bar(x), 0); | ||
| assert_eq!(<dyn T as OtherTrait>::foo(x), i32::MIN); | ||
|
|
||
| let x: &(dyn T + Send) = &0i32; | ||
| assert_eq!(x.foo(), 0); | ||
| } | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -1,25 +1,91 @@ | ||
| error[E0034]: multiple applicable items in scope | ||
| --> $DIR/call-ambig-trait-object-method.rs:19:7 | ||
| --> $DIR/call-ambig-trait-object-method.rs:40:18 | ||
| | | ||
| LL | x.foo(); | ||
| | ^^^ multiple `foo` found | ||
| LL | assert_eq!(x.foo(), 1); | ||
| | ^^^ multiple `foo` found | ||
| | | ||
| note: candidate #1 is defined in the trait `T` | ||
| --> $DIR/call-ambig-trait-object-method.rs:6:5 | ||
| --> $DIR/call-ambig-trait-object-method.rs:4:5 | ||
| | | ||
| LL | fn foo(&self); | ||
| | ^^^^^^^^^^^^^^ | ||
| LL | fn foo(&self) -> i32; | ||
| | ^^^^^^^^^^^^^^^^^^^^^ | ||
| note: candidate #2 is defined in an impl for the type `(dyn T + 'a)` | ||
| --> $DIR/call-ambig-trait-object-method.rs:10:5 | ||
| --> $DIR/call-ambig-trait-object-method.rs:9:5 | ||
| | | ||
| LL | fn foo(&self) {} | ||
| LL | fn foo(&self) -> i32 { | ||
| | ^^^^^^^^^^^^^^^^^^^^ | ||
| help: disambiguate the method for candidate #1 | ||
| | | ||
| LL - assert_eq!(x.foo(), 1); | ||
| LL + assert_eq!(T::foo(&x), 1); | ||
| | | ||
|
|
||
| error[E0034]: multiple applicable items in scope | ||
| --> $DIR/call-ambig-trait-object-method.rs:42:18 | ||
| | | ||
| LL | assert_eq!(x.bar(), ()); | ||
| | ^^^ multiple `bar` found | ||
| | | ||
| note: candidate #1 is defined in the trait `T` | ||
| --> $DIR/call-ambig-trait-object-method.rs:5:5 | ||
| | | ||
| LL | fn bar(&self) -> i32; | ||
| | ^^^^^^^^^^^^^^^^^^^^^ | ||
| note: candidate #2 is defined in an impl for the type `(dyn T + 'a)` | ||
| --> $DIR/call-ambig-trait-object-method.rs:13:5 | ||
| | | ||
| LL | fn bar(&self) {} | ||
| | ^^^^^^^^^^^^^ | ||
| help: disambiguate the method for candidate #1 | ||
| | | ||
| LL - x.foo(); | ||
| LL + T::foo(&x); | ||
| LL - assert_eq!(x.bar(), ()); | ||
| LL + assert_eq!(T::bar(&x), ()); | ||
| | | ||
|
|
||
| error[E0034]: multiple applicable items in scope | ||
| --> $DIR/call-ambig-trait-object-method.rs:44:25 | ||
| | | ||
| LL | assert_eq!(<dyn T>::foo(x), 1); | ||
| | ^^^ multiple `foo` found | ||
| | | ||
| note: candidate #1 is defined in the trait `T` | ||
| --> $DIR/call-ambig-trait-object-method.rs:4:5 | ||
| | | ||
| LL | fn foo(&self) -> i32; | ||
| | ^^^^^^^^^^^^^^^^^^^^^ | ||
| note: candidate #2 is defined in an impl for the type `(dyn T + 'a)` | ||
| --> $DIR/call-ambig-trait-object-method.rs:9:5 | ||
| | | ||
| LL | fn foo(&self) -> i32 { | ||
| | ^^^^^^^^^^^^^^^^^^^^ | ||
| help: use fully-qualified syntax to disambiguate | ||
| | | ||
| LL - assert_eq!(<dyn T>::foo(x), 1); | ||
| LL + assert_eq!(T::foo(x), 1); | ||
| | | ||
|
|
||
| error[E0034]: multiple applicable items in scope | ||
| --> $DIR/call-ambig-trait-object-method.rs:47:25 | ||
| | | ||
| LL | assert_eq!(<dyn T>::bar(x), ()); | ||
| | ^^^ multiple `bar` found | ||
| | | ||
| note: candidate #1 is defined in the trait `T` | ||
| --> $DIR/call-ambig-trait-object-method.rs:5:5 | ||
| | | ||
| LL | fn bar(&self) -> i32; | ||
| | ^^^^^^^^^^^^^^^^^^^^^ | ||
| note: candidate #2 is defined in an impl for the type `(dyn T + 'a)` | ||
| --> $DIR/call-ambig-trait-object-method.rs:13:5 | ||
| | | ||
| LL | fn bar(&self) {} | ||
| | ^^^^^^^^^^^^^ | ||
| help: use fully-qualified syntax to disambiguate | ||
| | | ||
| LL - assert_eq!(<dyn T>::bar(x), ()); | ||
| LL + assert_eq!(T::bar(x), ()); | ||
| | | ||
|
|
||
| error: aborting due to 1 previous error | ||
| error: aborting due to 4 previous errors | ||
|
|
||
| For more information about this error, try `rustc --explain E0034`. |
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
This part is not so nice… ideally this would use the inherent impl too.
View changes since the review
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.
I looked into making this work, but it seems non-trivial. The
inherent_implsquery is based onDefIds, so information about auto traits is lost