Closed
Description
Trying to compile code with this kind of structure leads to an unsupported cyclic reference error:
trait Trait {
type A;
type B;
fn id(self) -> Self { self }
}
struct S;
struct SA;
struct SB;
struct SAA;
impl Trait for S {
type A = SA;
type B = SB;
}
impl Trait for SA {
type A = SAA;
type B = SB;
}
fn test<T: Trait>() where T::A : Trait<B=T::B> {}
fn main() {}
<anon>:22:42: 22:46 error: unsupported cyclic reference between types/traits detected
<anon>:22 fn test<T: Trait>() where T::A : Trait<B=T::B> {}
^~~~
note: the cycle begins when computing the bounds for type parameter `T`...
note: ...which then again requires computing the bounds for type parameter `T`, completing the cycle.
error: aborting due to previous error
This seems somewhat surprising, since, T
is merely acting as the scope for T::A
and T::B
, which are distinct types. Also, it seems quite reasonable to want to assert a relationship between two associated types. Is there actually something inherently cyclic about this that I'm not seeing, or is it an artefact of how the implementation detects cycles? Also, is this kind of constraint likely to eventually be supported?
Metadata
Metadata
Assignees
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
[-]Can't relate two associated types – unsupported cyclic reference between traits[/-][+]Can't relate two associated types – unsupported cyclic reference between types/traits[/+]steveklabnik commentedon Apr 6, 2015
/cc @nikomatsakis
stevenblenkinsop commentedon Apr 7, 2015
Turns out the problem goes away when I use the explicit
<T as Trait>::A
notation: http://is.gd/3sJObaAside from the overall problem of it being somewhat inconsistent whether you need to use this syntax or not, this is a deeply misleading error. The fact that it shows up might indicate a problem with how the associated type syntax is seen by the compiler. If not, the error should probably be improved regardless.
mitchmindtree commentedon Apr 7, 2015
I'm running into this also on
rustc 1.0.0-nightly (f207ecbe0 2015-04-03) (built 2015-04-03)
@stevenblenkinsop thanks for posting this along with the hint for temporarily side-stepping it 👍nikomatsakis commentedon Apr 8, 2015
Hmm, yes. I'm a bit surprised that we wind up with a cycle in that particular example, we may be able to tighten the code to avoid it.
brendanzab commentedon Oct 29, 2015
I came across this too.
This errors:
playground
Where as this compiles fine:
playground
mitchmindtree commentedon Mar 11, 2016
I have also come across this same issue working on some generic DSP code (I'd post an example of the error however they look very much the same as the examples here by bjz and steveblenkinsop).
Mark-Simulacrum commentedon May 1, 2017
The original example compiles with an addition of
Self: Sized
, but the one here still doesn't. I suspect that this is related to #38864, but I'm uncertain... the error is different. Triage: Still a bug.Mark-Simulacrum commentedon May 10, 2017
This is basically the same as #35237, closing.