Skip to content

Impls should not allow methods not defined in the trait to be implemented #3973

Closed
@mitsuhiko

Description

@mitsuhiko

This code is wrong because ToStr does not define new. But what's odd is that the compiler error does not happen in the ToStr block but in the main:

struct Point {
    mut x: float,
    mut y: float,
}

impl Point : ToStr {
    static fn new(x: float, y: float) -> Point {
        Point { x: x, y: y }
    }

    pure fn to_str() -> ~str {
        fmt!("(%f, %f)", self.x, self.y)
    }
}

fn main() {
    let p = Point::new(0.0f, 0.0f);
    io::println(p.to_str());
}

Error message:

test.rs:17:12: 17:22 error: unresolved name
test.rs:17     let p = Point::new(0.0f, 0.0f);
                       ^~~~~~~~~~
test.rs:17:12: 17:22 error: use of undeclared module `Point`
test.rs:17     let p = Point::new(0.0f, 0.0f);
                       ^~~~~~~~~~
test.rs:17:12: 17:22 error: unresolved name: Point::new
test.rs:17     let p = Point::new(0.0f, 0.0f);
                       ^~~~~~~~~~
error: aborting due to 3 previous errors

The expected error message would point to the impl block that needs to be split into two (one with ToStr and one without a trait).

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-resolveArea: Name/path resolution done by `rustc_resolve` specifically

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions