Skip to content

Commit b705cd7

Browse files
committed
use the disambiguated idx from last impl trait
1 parent 32cd911 commit b705cd7

File tree

3 files changed

+53
-23
lines changed

3 files changed

+53
-23
lines changed

compiler/rustc_ty_utils/src/assoc.rs

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -177,17 +177,6 @@ impl<'tcx> Visitor<'tcx> for RPITVisitor<'tcx> {
177177
}
178178
}
179179

180-
struct DisambiguatorIdxVisitor {
181-
depth: u32,
182-
}
183-
184-
impl<'tcx> Visitor<'tcx> for DisambiguatorIdxVisitor {
185-
fn visit_opaque_ty(&mut self, opaque: &'tcx hir::OpaqueTy<'tcx>) -> Self::Result {
186-
self.depth += 1;
187-
intravisit::walk_opaque_ty(self, opaque)
188-
}
189-
}
190-
191180
/// Given an `fn_def_id` of a trait or a trait implementation:
192181
///
193182
/// if `fn_def_id` is a function defined inside a trait, then it synthesizes
@@ -221,18 +210,25 @@ fn associated_types_for_impl_traits_in_associated_fn(
221210
// ```
222211
let disambiguator_idx = trait_item_refs
223212
.iter()
224-
.take_while(|item| item.id.owner_id.def_id != fn_def_id)
225-
.filter(|item| {
226-
matches!(item.kind, hir::AssocItemKind::Fn { .. })
213+
.rev()
214+
.skip_while(|item| item.id.owner_id.def_id != fn_def_id)
215+
.skip(1)
216+
.find_map(|item| {
217+
if matches!(item.kind, hir::AssocItemKind::Fn { .. })
227218
&& def_path_id(item.id.owner_id.def_id) == def_path_data
228-
})
229-
.last()
230-
.map(|item| {
231-
let output = tcx.hir_get_fn_output(item.id.owner_id.def_id).unwrap();
232-
let mut visitor = DisambiguatorIdxVisitor { depth: 0 };
233-
visitor.visit_fn_ret_ty(output);
234-
tcx.def_key(item.id.owner_id.def_id).disambiguated_data.disambiguator
235-
+ visitor.depth
219+
&& let Some(last_impl_trait_id) = tcx
220+
.associated_types_for_impl_traits_in_associated_fn(
221+
item.id.owner_id.def_id,
222+
)
223+
.last()
224+
{
225+
Some(
226+
tcx.def_key(last_impl_trait_id).disambiguated_data.disambiguator
227+
+ 1,
228+
)
229+
} else {
230+
None
231+
}
236232
})
237233
.unwrap_or_default();
238234

tests/ui/impl-trait/in-trait/rpitit-duplicate-associated-fn-with-nested.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,17 @@ trait Qux {
4343
//~^^^^ ERROR: the name `foo` is defined multiple times
4444
}
4545

46+
trait T0<T> {
47+
type Target;
48+
}
49+
trait T1<T> {}
50+
51+
trait X {
52+
fn a() -> impl T0<(), Target = impl T1<()>>;
53+
fn a() -> impl T0<(), Target = impl T1<()>>;
54+
//~^ ERROR the name `a` is defined multiple times
55+
fn a() -> impl T0<(), Target = impl T1<()>>;
56+
//~^ ERROR the name `a` is defined multiple times
57+
}
58+
4659
fn main() {}

tests/ui/impl-trait/in-trait/rpitit-duplicate-associated-fn-with-nested.stderr

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,27 @@ LL | | >;
4444
|
4545
= note: `foo` must be defined only once in the value namespace of this trait
4646

47-
error: aborting due to 4 previous errors
47+
error[E0428]: the name `a` is defined multiple times
48+
--> $DIR/rpitit-duplicate-associated-fn-with-nested.rs:53:5
49+
|
50+
LL | fn a() -> impl T0<(), Target = impl T1<()>>;
51+
| -------------------------------------------- previous definition of the value `a` here
52+
LL | fn a() -> impl T0<(), Target = impl T1<()>>;
53+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `a` redefined here
54+
|
55+
= note: `a` must be defined only once in the value namespace of this trait
56+
57+
error[E0428]: the name `a` is defined multiple times
58+
--> $DIR/rpitit-duplicate-associated-fn-with-nested.rs:55:5
59+
|
60+
LL | fn a() -> impl T0<(), Target = impl T1<()>>;
61+
| -------------------------------------------- previous definition of the value `a` here
62+
...
63+
LL | fn a() -> impl T0<(), Target = impl T1<()>>;
64+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `a` redefined here
65+
|
66+
= note: `a` must be defined only once in the value namespace of this trait
67+
68+
error: aborting due to 6 previous errors
4869

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

0 commit comments

Comments
 (0)