Skip to content

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

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

Closed
mitsuhiko opened this issue Nov 15, 2012 · 6 comments · Fixed by #14974
Closed

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

mitsuhiko opened this issue Nov 15, 2012 · 6 comments · Fixed by #14974
Labels
A-resolve Area: Name/path resolution done by `rustc_resolve` specifically

Comments

@mitsuhiko
Copy link
Contributor

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).

@catamorphism
Copy link
Contributor

Reproduced as of ae0ca9c

catamorphism added a commit that referenced this issue Jan 10, 2013
@catamorphism
Copy link
Contributor

Repro'd with 373504 -- but not critical for 0.6, de-milestoning

@bstrie
Copy link
Contributor

bstrie commented May 6, 2013

Appears to be fixed, as per this updated test case:

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

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

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

fn main() {
}

...which fails with error: method new is not a member of trait ToStr. Closing.

@bstrie bstrie closed this as completed May 6, 2013
@huonw
Copy link
Member

huonw commented Sep 2, 2013

We've regressed: there's a test case (compile-fail/issue-3973.rs), but it's been xfailed since it was added. With 6a3dd30 I see:

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

@huonw huonw reopened this Sep 2, 2013
@alexcrichton
Copy link
Member

Aaaand, we've un-regressed!

Removing all tags except for neesdtest.

@alexcrichton
Copy link
Member

Whoops, had the wrong code example, regression still here.

Ryman added a commit to Ryman/rust that referenced this issue Jun 23, 2014
RalfJung pushed a commit to RalfJung/rust that referenced this issue Oct 15, 2024
ensure that a macOS os_unfair_lock that is moved while being held is not implicitly unlocked

Fixes rust-lang/miri#3859

We mark an os_unfair_lock that is moved while being held as "poisoned", which means it is not considered forever locked. That's not quite what the real implementation does, but allowing arbitrary moves-while-locked would likely expose a ton of implementation details, so hopefully this is good enough.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-resolve Area: Name/path resolution done by `rustc_resolve` specifically
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants