Skip to content

distinguish the duplicate item of rpitit #143484

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

Merged
merged 1 commit into from
Jul 6, 2025
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
31 changes: 29 additions & 2 deletions compiler/rustc_ty_utils/src/assoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,39 @@ fn associated_types_for_impl_traits_in_associated_fn(
match tcx.def_kind(parent_def_id) {
DefKind::Trait => {
if let Some(output) = tcx.hir_get_fn_output(fn_def_id) {
let data = DefPathData::AnonAssocTy(tcx.item_name(fn_def_id.to_def_id()));
let def_path_id = |def_id: LocalDefId| tcx.item_name(def_id.to_def_id());
let def_path_data = def_path_id(fn_def_id);

let (.., trait_item_refs) = tcx.hir_expect_item(parent_def_id).expect_trait();
// The purpose of `disambiguator_idx` is to ensure there are
// no duplicate `def_id` in certain cases, such as:
// ```
// trait Foo {
// fn bar() -> impl Trait;
// fn bar() -> impl Trait;
// // ~~~~~~~~~~ It will generate the same ID if we don’t disambiguate it.
// }
// ```
let disambiguator_idx = trait_item_refs
.iter()
.take_while(|item| item.id.owner_id.def_id != fn_def_id)
.fold(0, |acc, item| {
if !matches!(item.kind, hir::AssocItemKind::Fn { .. }) {
acc
} else if def_path_id(item.id.owner_id.def_id) == def_path_data {
tcx.def_key(item.id.owner_id.def_id).disambiguated_data.disambiguator
+ 1
} else {
acc
}
});

let data = DefPathData::AnonAssocTy(def_path_data);
let mut visitor = RPITVisitor {
tcx,
synthetics: vec![],
data,
disambiguator: DisambiguatorState::with(parent_def_id, data, 0),
disambiguator: DisambiguatorState::with(parent_def_id, data, disambiguator_idx),
};
visitor.visit_fn_ret_ty(output);
tcx.arena.alloc_from_iter(
Expand Down
30 changes: 30 additions & 0 deletions tests/ui/impl-trait/in-trait/rpitit-duplicate-associated-fn.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// issue#140796

trait Bar {
fn method() -> impl Sized;
fn method() -> impl Sized; //~ ERROR: the name `method` is defined multiple times
}

impl Bar for () { //~ ERROR: not all trait items implemented, missing: `method`
fn method() -> impl Sized {
42
}
fn method() -> impl Sized { //~ ERROR: duplicate definitions with name `method`
42
}
}

trait T {
fn method() -> impl Sized;
}

impl T for () {
fn method() -> impl Sized {
42
}
fn method() -> impl Sized { //~ ERROR: duplicate definitions with name `method`
42
}
}

fn main() {}
53 changes: 53 additions & 0 deletions tests/ui/impl-trait/in-trait/rpitit-duplicate-associated-fn.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
error[E0428]: the name `method` is defined multiple times
--> $DIR/rpitit-duplicate-associated-fn.rs:5:5
|
LL | fn method() -> impl Sized;
| -------------------------- previous definition of the value `method` here
LL | fn method() -> impl Sized;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ `method` redefined here
|
= note: `method` must be defined only once in the value namespace of this trait

error[E0201]: duplicate definitions with name `method`:
--> $DIR/rpitit-duplicate-associated-fn.rs:12:5
|
LL | fn method() -> impl Sized;
| -------------------------- item in trait
...
LL | / fn method() -> impl Sized {
LL | | 42
LL | | }
| |_____- previous definition here
LL | / fn method() -> impl Sized {
LL | | 42
LL | | }
| |_____^ duplicate definition

error[E0201]: duplicate definitions with name `method`:
--> $DIR/rpitit-duplicate-associated-fn.rs:25:5
|
LL | fn method() -> impl Sized;
| -------------------------- item in trait
...
LL | / fn method() -> impl Sized {
LL | | 42
LL | | }
| |_____- previous definition here
LL | / fn method() -> impl Sized {
LL | | 42
LL | | }
| |_____^ duplicate definition

error[E0046]: not all trait items implemented, missing: `method`
--> $DIR/rpitit-duplicate-associated-fn.rs:8:1
|
LL | fn method() -> impl Sized;
| -------------------------- `method` from trait
...
LL | impl Bar for () {
| ^^^^^^^^^^^^^^^ missing `method` in implementation

error: aborting due to 4 previous errors

Some errors have detailed explanations: E0046, E0201, E0428.
For more information about an error, try `rustc --explain E0046`.
Loading