Skip to content

Compiler out of stack on generic base trait impl restricted to a derived trait #5988

Closed

Description

@mzabaluev

Rustc runs out of stack on this program:

use std::io;
trait B {
    fn f(&self);
}

trait T : B {
}

struct A;

impl<U: T> B for U {
    fn f(&self) { io::println("Hey, I'm a T!"); }
}

impl T for A {
}

fn main() {
    let a = A;
    let br = &a as &B;
    // let tr = &a as &T;
    // let br = tr as &B;
    br.f();
}

The inheritance of trait T from B seemingly triggers the recursion: without it, the program compiles and runs fine.

P.S. The commented-out lines illustrate what I was actually trying to achieve: upcasting a trait pointer to the base trait. That didn't work for other reasons.

Activity

  1. bblum commented on Jul 3, 2013

    @bblum
    Contributor

    A little more minimal:

    use std::io;
    trait B {
        fn f(&self);
    }
    
    trait T : B { }
    
    impl<U: T> B for U {
        fn f(&self) { io::println("Hey, I'm a T!"); }
    }
    
    impl T for int { }
    
    fn main() {
        let _br = @0 as @B;
    }
    

    The stack overflow is a cycle between lookup_vtables, lookup_vtable, back to lookup_vtables, and each_bound_trait_and_supertraits.

  2. bblum commented on Jul 3, 2013

    @bblum
    Contributor

    nominating well-covered milestone

  3. graydon commented on Aug 22, 2013

    @graydon
    Contributor

    accepted for production-ready milestone

  4. pnkfelix commented on Mar 13, 2014

    @pnkfelix
    Contributor

    just a bug, assigning P-low.

  5. japaric commented on Nov 2, 2014

    @japaric
    Contributor

    Both examples compile on:

    rustc 0.13.0-dev (3327ecca4 2014-11-01 22:41:48 +0000)

    @nikomatsakis Can we close this? Or should we add a test (if it's not covered in the test suite already)?

  6. alexcrichton commented on Nov 3, 2014

    @alexcrichton
    Member

    Flagging as needstest (just in case)

  7. added
    E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.
    on Nov 3, 2014
  8. nikomatsakis commented on Nov 4, 2014

    @nikomatsakis
    Contributor

    Wacky. I don't think there is a test specifically covering this scenario.

  9. added 3 commits that reference this issue on Dec 12, 2014
    20cbbff
    c59b9b7
  10. added a commit that references this issue on Sep 10, 2020
  11. added a commit that references this issue on Sep 20, 2022
  12. added a commit that references this issue on Aug 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-trait-systemArea: Trait systemE-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.I-crashIssue: The compiler crashes (SIGSEGV, SIGABRT, etc). Use I-ICE instead when the compiler panics.P-lowLow priority

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions