-
Notifications
You must be signed in to change notification settings - Fork 12.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of #100387 - cjgillot:hygiene-trait-impl, r=petrochenkov
Check uniqueness of impl items by trait item when applicable. When checking uniqueness of item names in impl blocks, we currently use the same definition of hygiene as for toplevel items. This means that a plain item and one generated by a macro 2.0 do not collide. This hygiene rule does not match with how impl items resolve to associated trait items. As a consequence, we misdiagnose the trait impls. This PR proposes to consider that trait impl items are uses of the corresponding trait items during resolution, instead of checking for duplicates later. An error is emitted when a trait impl item is used twice. There should be no stable breakage, since macros 2.0 are still unstable. r? ``@petrochenkov`` cc ``@RalfJung`` Fixes #71614.
- Loading branch information
Showing
11 changed files
with
137 additions
and
25 deletions.
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
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
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
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
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 |
---|---|---|
|
@@ -16,4 +16,5 @@ impl Foo for Baz { | |
|
||
fn main() { | ||
let x: Baz::Bar = 5; | ||
//~^ ERROR ambiguous associated type | ||
} |
18 changes: 14 additions & 4 deletions
18
src/test/ui/associated-item/associated-item-duplicate-names-3.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 |
---|---|---|
@@ -1,11 +1,21 @@ | ||
error[E0201]: duplicate definitions with name `Bar`: | ||
--> $DIR/associated-item-duplicate-names-3.rs:14:5 | ||
| | ||
LL | type Bar; | ||
| --------- item in trait | ||
... | ||
LL | type Bar = i16; | ||
| -------- previous definition of `Bar` here | ||
| --------------- previous definition here | ||
LL | type Bar = u16; | ||
| ^^^^^^^^ duplicate definition | ||
| ^^^^^^^^^^^^^^^ duplicate definition | ||
|
||
error: aborting due to previous error | ||
error[E0223]: ambiguous associated type | ||
--> $DIR/associated-item-duplicate-names-3.rs:18:12 | ||
| | ||
LL | let x: Baz::Bar = 5; | ||
| ^^^^^^^^ help: use fully-qualified syntax: `<Baz as Trait>::Bar` | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0201`. | ||
Some errors have detailed explanations: E0201, E0223. | ||
For more information about an error, try `rustc --explain E0201`. |
14 changes: 10 additions & 4 deletions
14
src/test/ui/associated-item/associated-item-duplicate-names.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
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
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,26 @@ | ||
#![feature(decl_macro)] | ||
|
||
trait Trait { | ||
fn foo() {} | ||
} | ||
|
||
macro trait_impl() { | ||
fn foo() {} | ||
} | ||
|
||
// Check that we error on multiple impl items that resolve to the same trait item. | ||
impl Trait for i32 { | ||
trait_impl!(); | ||
fn foo() {} | ||
//~^ ERROR duplicate definitions with name `foo`: [E0201] | ||
} | ||
|
||
struct Type; | ||
|
||
// Check that we do not error with inherent impls. | ||
impl Type { | ||
trait_impl!(); | ||
fn foo() {} | ||
} | ||
|
||
fn main() {} |
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,15 @@ | ||
error[E0201]: duplicate definitions with name `foo`: | ||
--> $DIR/impl_items-2.rs:14:5 | ||
| | ||
LL | fn foo() {} | ||
| ----------- item in trait | ||
... | ||
LL | fn foo() {} | ||
| ----------- previous definition here | ||
... | ||
LL | fn foo() {} | ||
| ^^^^^^^^^^^ duplicate definition | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0201`. |
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