Skip to content
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

Circular trait bounds causes stack overflow in rustc #12644

Closed
mikejsavage opened this issue Mar 1, 2014 · 10 comments
Closed

Circular trait bounds causes stack overflow in rustc #12644

mikejsavage opened this issue Mar 1, 2014 · 10 comments
Labels
I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️

Comments

@mikejsavage
Copy link

overflow.rs:

pub struct Deg { d : f64 }
pub trait Angle : Equiv< Self > { }

impl Angle for Deg { }

impl< T : Angle > Equiv< T > for T {
        fn equiv( &self, other : &T ) -> bool {
                return true;
        }
}

pub fn main() { }

compiling:

$ rustc -v
rustc 0.9
host: x86_64-unknown-linux-gnu
$ rustc overflow.rs 
overflow.rs:9:1: 13:2 error: cannot provide an extension implementation where both trait and type are not defined in this crate
overflow.rs:9 impl< T : Angle > Equiv< T > for T {
overflow.rs:10  fn equiv( &self, other : &T ) -> bool {
overflow.rs:11          return self.normalize() == other.normalize();
overflow.rs:12  }
overflow.rs:13 }
task 'rustc' has overflowed its stack
Illegal instruction (core dumped)
@Aatch
Copy link
Contributor

Aatch commented Mar 4, 2014

Definitely a bug. Causes an infinte loop while trying to create the vtables it seems, the loop I got was

middle::typeck::check::vtable::search_for_vtable
middle::typeck::check::vtable::lookup_vtables_for_param::closure
middle::ty::each_bound_trait_and_supertraits
middle::typeck::check::vtable::lookup_vtables_for_param
middle::typeck::check::vtable::lookup_vtables
...

Looking at the RUST_LOG output, theres something odd about the way it's substituting, each loop creates a new generic type to substitute into Equiv.

@Aatch Aatch added the I-ICE label Mar 4, 2014
@Aatch
Copy link
Contributor

Aatch commented Mar 4, 2014

Upon further inspection (including reading the gdb backtrace properly), it isn't actually an infinite loop, instead is just a very, very long one. since it breaks out of that loop eventually and then overflows.

@Aatch
Copy link
Contributor

Aatch commented Mar 4, 2014

Aha! There are other generic implementations of Equiv in std which mean you cannot make your own generic implementations of Equiv (see #3429).

As for why the loop happens... I don't know.

@Aatch
Copy link
Contributor

Aatch commented Mar 4, 2014

Related to #12511

@doy
Copy link
Contributor

doy commented Sep 9, 2014

This is enough to cause the stack overflow, if it helps any:

trait Foo : Bar<Self> { }
trait Bar<T: Foo> { }

@steveklabnik
Copy link
Member

Triage: @doy's sample still overflows on

rustc 1.0.0-dev (9ade482b3 2015-01-15 17:12:55 +0000)

@reem
Copy link
Contributor

reem commented Jan 16, 2015

Just to add another case, using associated types doesn't work either:

trait One { type Foo: Two; }
trait Two { type Foo: One; }

also overflows.

@grissiom
Copy link

On rustc 1.0.0-dev (593db00 2015-03-25) (built 2015-03-25),

trait One { type Foo: Two; }
trait Two { type Foo: One; }

does not overflow.

trait Foo : Bar<Self> { }
trait Bar<T: Foo> { }

and the original code does not compile.

@tamird
Copy link
Contributor

tamird commented Apr 21, 2015

@steveklabnik this should have been closed by #23013. Would you do the honours?

@alexcrichton
Copy link
Member

Steal!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️
Projects
None yet
Development

No branches or pull requests

8 participants