You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
error[E0221]: ambiguous associated type `A` in bounds of `Self`
--> src/test/compile-fail/E0221.rs:21:16
|
21 | let _: Self::A;
| ^^^^^^^ ambiguous associated type `A`
|
note: associated type `Self` could derive from `Foo`
--> src/test/compile-fail/E0221.rs:21:16
|
21 | let _: Self::A;
| ^^^^^^^
note: associated type `Self` could derive from `Bar`
--> src/test/compile-fail/E0221.rs:21:16
|
21 | let _: Self::A;
| ^^^^^^^
This isn't terribly helpful. Ideally, we'd like to show where these associated types that cause ambiguity are coming from:
error[E0221]: ambiguous associated type `A` in bounds of `Self`
--> src/test/compile-fail/E0221.rs:21:16
|
15 | type A: T1;
| ---------- ambiguous `A` from `Foo`
...
19 | type A: T2;
| ---------- ambiguous `A` from `Bar`
20 | fn do_something() {
21 | let _: Self::A;
| ^^^^^^^ ambiguous associated type `A`
I'd be up to mentor this. The message is reported by some code in src/librustc_typeck/astconv.rs, which has the job of converting the HIR representation of a type into the compiler's internal representation. The challenge here is that the HIR types have spans (because they are basically the things that the user wrote) whereas the internal representations do not. Right now, the routine in question, one_bound_for_associated_type, only has access to a list of the representations in internal form (bounds: Vec<ty::PolyTraitRef<'tcx>>. To give better spans, we have to trace those spans in (so e.g. we want bounds: Vec<(Span, ty::PolyTraitRef<'tcx>)>).
What this routine does is, given a type like A::Item, it selects what traits the associated type Item might have been defined in, and tries to pick one. The list bounds is the list of traits that A implements. So e.g. it might include something like A: Iterator, which would suggest that A::Item could be expanded to <A as Iterator>::Item. But if there was another bound, say A: Foo, that also defines an associated type Item, that'd be an amiguity.
If you look at the callers, there aren't that many (3, I think). Two of those are for trait objects, I think, where the bounds come from the supertraits of the trait object. Probably we could give those the spans of the trait type itself, though we may want to get the spans out of the trait definition where the supertraits come from (but that could be in another crate). That last part is a bit tricky because we'd have to extend the TraitDef. Let's assume we're not doing that for now, and we're willing to give a span referring to a trait X when in fact the item lies in a supertrait Y where X: Y.
The other is the more general case of a type parameter. I think the major piece of work then is to augment this routine get_type_parameter_bounds in the AstConv trait:
so that it returns a Vec<(Span, ty::PolyTraitRef<'tcx>)>, where we would thread through the spans whence each bound was derived.
Anyway, this is non-trivial, but seems to me to be achievable if anyone wants to dig in. If so, ping me (either on IRC, nmatsakis, or here) and I'll try to give more hints. :)
@nikomatsakis get_type_parameter_bounds in caller_bounds contains vec of Predicate w/o spans. May be is it easy only for error case in one_bound_for_associated_type get spans from ast_map (hir::map) by DefId extracted from TypeTraitItems like trait_defines_associated_type_named do?
Activity
nikomatsakis commentedon Aug 24, 2016
I'd be up to mentor this. The message is reported by some code in
src/librustc_typeck/astconv.rs, which has the job of converting the HIR representation of a type into the compiler's internal representation. The challenge here is that the HIR types have spans (because they are basically the things that the user wrote) whereas the internal representations do not. Right now, the routine in question,one_bound_for_associated_type, only has access to a list of the representations in internal form (bounds: Vec<ty::PolyTraitRef<'tcx>>. To give better spans, we have to trace those spans in (so e.g. we wantbounds: Vec<(Span, ty::PolyTraitRef<'tcx>)>).What this routine does is, given a type like
A::Item, it selects what traits the associated typeItemmight have been defined in, and tries to pick one. The listboundsis the list of traits thatAimplements. So e.g. it might include something likeA: Iterator, which would suggest thatA::Itemcould be expanded to<A as Iterator>::Item. But if there was another bound, sayA: Foo, that also defines an associated typeItem, that'd be an amiguity.If you look at the callers, there aren't that many (3, I think). Two of those are for trait objects, I think, where the bounds come from the supertraits of the trait object. Probably we could give those the spans of the trait type itself, though we may want to get the spans out of the trait definition where the supertraits come from (but that could be in another crate). That last part is a bit tricky because we'd have to extend the
TraitDef. Let's assume we're not doing that for now, and we're willing to give a span referring to a traitXwhen in fact the item lies in a supertraitYwhereX: Y.The other is the more general case of a type parameter. I think the major piece of work then is to augment this routine
get_type_parameter_boundsin theAstConvtrait:so that it returns a
Vec<(Span, ty::PolyTraitRef<'tcx>)>, where we would thread through the spans whence each bound was derived.Anyway, this is non-trivial, but seems to me to be achievable if anyone wants to dig in. If so, ping me (either on IRC,
nmatsakis, or here) and I'll try to give more hints. :)mikhail-m1 commentedon Oct 17, 2016
@nikomatsakis I'll try, I need some time to look around
mikhail-m1 commentedon Oct 20, 2016
@nikomatsakis get_type_parameter_bounds in caller_bounds contains vec of Predicate w/o spans. May be is it easy only for error case in one_bound_for_associated_type get spans from ast_map (hir::map) by DefId extracted from TypeTraitItems like trait_defines_associated_type_named do?
mikhail-m1 commentedon Oct 23, 2016
I've updated it mikhail-m1@585f835.
But what do you think about trait from extern crate?