Skip to content

Segfault related to trait bounds #9008

Closed
@novalis

Description

@novalis

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() {}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions