Skip to content

Bug with trait inheritance and trait objects #9394

Closed
@Kimundi

Description

@Kimundi

If you try to call an inherited method on an trait object, it instead calls a regular method of the trait itself, or potentially crashes if the signatures don't match.

Test case:

trait Base {
    fn baz(&self);
}

trait Super: Base {
    fn bar(&self);
}

struct X;

impl Base for X {
    fn baz(&self) {
        println("base baz");
    }
}

impl Super for X {
    fn bar(&self) {
        println("super bar");
    }
}

fn main() {
    let n = X;
    let b = &n as &Base;
    let s = &n as &Super;

    n.baz();   // base baz
    n.bar();   // super bar

    println("");

    b.baz();   // base baz
    //b.bar(); // Base has no bar()

    println("");

    s.baz();   // BUG: super bar - should be base baz
    s.bar();   // super bar
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions