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

Segfault related to trait bounds #9008

Closed
novalis opened this issue Sep 6, 2013 · 4 comments
Closed

Segfault related to trait bounds #9008

novalis opened this issue Sep 6, 2013 · 4 comments

Comments

@novalis
Copy link
Contributor

novalis commented Sep 6, 2013

I am saying "related to trait bounds" because I have managed to get this in a few different ways, all of which involved trait bounds. But since I don't fully understand how traits work yet, I'm not really sure what is going on. Here's a few examples:

This one spits out a zillion of these before dying:
segme.rs:14:0: 14:17 error: expected Op<S,S>, but found Op<int,S> (expected struct S but found int)
segme.rs:14 impl Bar for S {}

struct S {
    a: u64
}

trait Op<RHS,Result> {
    fn foo(&self, rhs: &RHS) -> Result;
}

pub trait Bar: Op<S,Self> {
}

impl Bar for S {}

impl<T:Bar> Op<int, T> for T {
    fn foo(&self, a: &int) -> T {
        //yes, I know this doesn't make sense
        return 0;
    }
}

fn main() {}

This one just segfaults:

trait Shape {
    fn n_sides(&self) -> uint;
}

trait Quadrilateral : Shape {
    fn sides(&self) -> ~[float, ..4];
}

impl<T:Quadrilateral> Shape for T {
    fn n_sides(&self) -> uint {
        return 4;
    }
}

trait Rectangle : Quadrilateral {
    //only rectangles have equal-sided diagonals
    fn diagonal(&self) -> float {
        let sides = self.sides();
        return (sides[0] * sides[0] + sides[1] * sides[1]).sqrt();
    }
}

struct SquareImpl {
    side : float
}

impl Quadrilateral for SquareImpl {
    fn sides(&self) -> ~[float, ..4] {
        let sides : ~[float, ..4] = ~([self.side, self.side, self.side, self.side]);
        return sides;
    }
}

impl Rectangle for SquareImpl {
    //Rectangle's diagonal is implemented by Rectangle
}

fn main() {}
@novalis
Copy link
Contributor Author

novalis commented Sep 6, 2013

Note that the segfault is in a different place than #8762:

#0  0x00007ffff62b8d51 in middle::typeck::infer::lattice::__extensions__::set_var_to_merged_bounds_63789::_294ad465868072::_0$x2e8$x2dpre () from /usr/local/bin/../lib/librustc-d3cb8c2ccd84a7a7-0.8-pre.so
#1  0x00007ffff62ae412 in middle::typeck::infer::sub::__extensions__::meth_63531::tys::_5bcb7e7ec8e26d49::_0$x2e8$x2dpre ()
   from /usr/local/bin/../lib/librustc-d3cb8c2ccd84a7a7-0.8-pre.so
#2  0x00007ffff62aeb11 in middle::typeck::infer::sub::__extensions__::meth_63538::contratys::_5bcb7e7ec8e26d49::_0$x2e8$x2dpre ()
   from /usr/local/bin/../lib/librustc-d3cb8c2ccd84a7a7-0.8-pre.so
#3  0x00007ffff632f1c0 in middle::typeck::infer::combine::Combine::anon::expr_fn_67001 ()
   from /usr/local/bin/../lib/librustc-d3cb8c2ccd84a7a7-0.8-pre.so
#4  0x00007ffff62c59bb in result::__extensions__::chain_64119::_92c2298aed66c6a::_0$x2e8$x2dpre ()
   from /usr/local/bin/../lib/librustc-d3cb8c2ccd84a7a7-0.8-pre.so
#5  0x00007ffff632f043 in middle::typeck::infer::combine::Combine::substs_66993::_ef3a55d95551ab4::_0$x2e8$x2dpre ()
   from /usr/local/bin/../lib/librustc-d3cb8c2ccd84a7a7-0.8-pre.so
#6  0x00007ffff634d8d0 in middle::typeck::infer::mk_sub_trait_refs::anon::anon::expr_fn_67613 ()
   from /usr/local/bin/../lib/librustc-d3cb8c2ccd84a7a7-0.8-pre.so
#7  0x00007ffff634d2b4 in middle::typeck::infer::__extensions__::try_67607::anon::expr_fn_67609 ()
   from /usr/local/bin/../lib/librustc-d3cb8c2ccd84a7a7-0.8-pre.so
#8  0x00007ffff634bfef in util::common::indent_67575::_5977847d83168476::_0$x2e8$x2dpre ()
   from /usr/local/bin/../lib/librustc-d3cb8c2ccd84a7a7-0.8-pre.so
#9  0x00007ffff634cb16 in middle::typeck::infer::__extensions__::commit_67603::anon::expr_fn_67605 ()
   from /usr/local/bin/../lib/librustc-d3cb8c2ccd84a7a7-0.8-pre.so
#10 0x00007ffff634bfef in util::common::indent_67575::_5977847d83168476::_0$x2e8$x2dpre ()
   from /usr/local/bin/../lib/librustc-d3cb8c2ccd84a7a7-0.8-pre.so
#11 0x00007ffff634c841 in middle::typeck::infer::mk_sub_trait_refs::anon::expr_fn_67601 ()
   from /usr/local/bin/../lib/librustc-d3cb8c2ccd84a7a7-0.8-pre.so
#12 0x00007ffff634bfef in util::common::indent_67575::_5977847d83168476::_0$x2e8$x2dpre ()
   from /usr/local/bin/../lib/librustc-d3cb8c2ccd84a7a7-0.8-pre.so
#13 0x00007ffff61e426d in middle::typeck::infer::mk_sub_trait_refs::_f7ea5d6609058ad::_0$x2e8$x2dpre ()
   from /usr/local/bin/../lib/librustc-d3cb8c2ccd84a7a7-0.8-pre.so
#14 0x00007ffff61e35c1 in middle::typeck::check::vtable::relate_trait_refs::_5b7aa9d4975c2048::_0$x2e8$x2dpre ()
   from /usr/local/bin/../lib/librustc-d3cb8c2ccd84a7a7-0.8-pre.so
#15 0x00007ffff61e5caf in middle::typeck::check::vtable::search_for_vtable::_ce718d4bacc81570::_0$x2e8$x2dpre ()
   from /usr/local/bin/../lib/librustc-d3cb8c2ccd84a7a7-0.8-pre.so
#16 0x00007ffff61e2c84 in middle::typeck::check::vtable::lookup_vtable::_ce718d4bacc81570::_0$x2e8$x2dpre ()
   from /usr/local/bin/../lib/librustc-d3cb8c2ccd84a7a7-0.8-pre.so
#17 0x00007ffff61e1faf in middle::typeck::check::vtable::lookup_vtables_for_param::anon::expr_fn_57896 ()
   from /usr/local/bin/../lib/librustc-d3cb8c2ccd84a7a7-0.8-pre.so
#18 0x00007ffff6111cdc in middle::ty::each_bound_trait_and_supertraits::_fa9077d6aceae27b::_0$x2e8$x2dpre ()
   from /usr/local/bin/../lib/librustc-d3cb8c2ccd84a7a7-0.8-pre.so
#19 0x00007ffff61e14f1 in middle::typeck::check::vtable::lookup_vtables_for_param::_e53c5d2999aba97::_0$x2e8$x2dpre ()
   from /usr/local/bin/../lib/librustc-d3cb8c2ccd84a7a7-0.8-pre.so
#20 0x00007ffff61dfa6d in middle::typeck::check::vtable::lookup_vtables::_419eb61d9f602274::_0$x2e8$x2dpre ()
   from /usr/local/bin/../lib/librustc-d3cb8c2ccd84a7a7-0.8-pre.so
#21 0x00007ffff61e5d63 in middle::typeck::check::vtable::search_for_vtable::_ce718d4bacc81570::_0$x2e8$x2dpre ()
   from /usr/local/bin/../lib/librustc-d3cb8c2ccd84a7a7-0.8-pre.so
#22 0x00007ffff61e2c84 in middle::typeck::check::vtable::lookup_vtable::_ce718d4bacc81570::_0$x2e8$x2dpre ()
   from /usr/local/bin/../lib/librustc-d3cb8c2ccd84a7a7-0.8-pre.so
#23 0x00007ffff61e1faf in middle::typeck::check::vtable::lookup_vtables_for_param::anon::expr_fn_57896 ()
   from /usr/local/bin/../lib/librustc-d3cb8c2ccd84a7a7-0.8-pre.so
#24 0x00007ffff6111cdc in middle::ty::each_bound_trait_and_supertraits::_fa9077d6aceae27b::_0$x2e8$x2dpre ()
   from /usr/local/bin/../lib/librustc-d3cb8c2ccd84a7a7-0.8-pre.so
#25 0x00007ffff61e14f1 in middle::typeck::check::vtable::lookup_vtables_for_param::_e53c5d2999aba97::_0$x2e8$x2dpre ()
   from /usr/local/bin/../lib/librustc-d3cb8c2ccd84a7a7-0.8-pre.so
#26 0x00007ffff61dfa6d in middle::typeck::check::vtable::lookup_vtables::_419eb61d9f602274::_0$x2e8$x2dpre ()
   from /usr/local/bin/../lib/librustc-d3cb8c2ccd84a7a7-0.8-pre.so
...

(and so on, for a zillion frames)

@novalis
Copy link
Contributor Author

novalis commented Sep 6, 2013

I just realized that the following is bogus (since I won't be able to impl any other Shape):

impl<T:Quadrilateral> Shape for T {
    fn n_sides(&self) -> uint {
        return 4;
    }
}

(but it's still bad that the compiler crashes, of course)

@pzol
Copy link
Contributor

pzol commented Jan 28, 2014

Updated example - s/float/f32/

Compiler error message task 'rustc' has overflowed its stack

trait Shape {
    fn n_sides(&self) -> uint;
}

trait Quadrilateral : Shape {
    fn sides(&self) -> ~[f32, ..4];
}

impl<T:Quadrilateral> Shape for T {
    fn n_sides(&self) -> uint {
        return 4;
    }
}

trait Rectangle : Quadrilateral {
    //only rectangles have equal-sided diagonals
    fn diagonal(&self) -> f32 {
        let sides = self.sides();
        return (sides[0] * sides[0] + sides[1] * sides[1]).sqrt();
    }
}

struct SquareImpl {
    side : f32
}

impl Quadrilateral for SquareImpl {
    fn sides(&self) -> ~[f32, ..4] {
        let sides : ~[f32, ..4] = ~([self.side, self.side, self.side, self.side]);
        return sides;
    }
}

impl Rectangle for SquareImpl {
    //Rectangle's diagonal is implemented by Rectangle
}

fn main() {}

@alexcrichton
Copy link
Member

This issue has since been fixed, and I imagine it was probably due to some random change in the internals over the years, so we probably don't need a test for this :)

flip1995 pushed a commit to flip1995/rust that referenced this issue Jun 30, 2022
…ffate

Use `RefCell` in `needless_return` tests

changelog: none

The stdio locks no longer fail to compile if the `return` is removed due to them now being `'static` (rust-lang#9008)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants