Skip to content

improve error message on attempt to impl on typedef #9767

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
pnkfelix opened this issue Oct 8, 2013 · 4 comments
Closed

improve error message on attempt to impl on typedef #9767

pnkfelix opened this issue Oct 8, 2013 · 4 comments
Labels
A-resolve Area: Name/path resolution done by `rustc_resolve` specifically C-cleanup Category: PRs that clean code up or issues documenting cleanup.

Comments

@pnkfelix
Copy link
Member

pnkfelix commented Oct 8, 2013

Support for impl on typedefs was removed (IIUC) in PR #6087.

However, the current error you get for attempting to do such a thing is . . . suboptimal:

type Board = [char, ..9];
impl Board { }
fn main() { }
% ./objdir-dbgopt/x86_64-apple-darwin/stage2/bin/rustc  /tmp/u.rs
/tmp/u.rs:2:0: 2:14 error: no base type found for inherent implementation; implement a trait or new type instead
/tmp/u.rs:2 impl Board { }
            ^~~~~~~~~~~~~~
/tmp/u.rs:2:0: 2:14 error: cannot associate methods with a type outside the crate the type is defined in; define and implement a trait or new type instead
/tmp/u.rs:2 impl Board { }
            ^~~~~~~~~~~~~~
error: aborting due to 2 previous errors

Or better still, maybe we could actually put in correct support for impl on typedef, which I think was part of what pcwalton was proposing in his mailing list post here: https://mail.mozilla.org/pipermail/rust-dev/2013-April/003866.html

@ghost ghost added the E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. label Nov 10, 2014
@ghost ghost removed the E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. label Dec 12, 2014
@steveklabnik steveklabnik added the A-resolve Area: Name/path resolution done by `rustc_resolve` specifically label Jan 21, 2015
@Ryman
Copy link
Contributor

Ryman commented Feb 14, 2015

This can be closed, the following works today:

#[derive(Debug)]
struct MyBox<T>(Box<T>);
type FloatBox = MyBox<f32>;

impl FloatBox {
    fn new(x: f32) -> FloatBox {
        MyBox(Box::new(x))
    }
    fn bar() {
        println!("HI")
    }
}

fn main() {
    let foo = FloatBox::new(3.2);
    println!("{:?}", foo);
    FloatBox::bar()
}

@agrover
Copy link

agrover commented Jun 18, 2015

@Ryman I think that's impl on a tuple struct, not an impl on a typedef?

type MyVec = Vec<u64>;

impl MyVec {
    fn mylen(&self) -> usize {
        self.len()
    }
}

doesn't work now, and it would be nice if it did IMHO. The workaround is define a trait ABC and then impl ABC for MyVec...

@Ryman
Copy link
Contributor

Ryman commented Jun 18, 2015

@agrover Well it's an impl on a typedef of a newtype(no?), but you're right that my comment is missing half the point that the issue. 😵

@steveklabnik
Copy link
Member

Today, this gives a much better error

error[E0116]: cannot define inherent `impl` for a type outside of the crate where the type is defined
 --> <anon>:3:1
  |
3 | impl MyVec {
  | ^ impl for type defined outside of crate.
  |
  = note: define and implement a trait or new type instead

and given that this matches type's semantics, I'm gonna give this one a close! Thanks.

flip1995 pushed a commit to flip1995/rust that referenced this issue Dec 26, 2024
…13834)

changelog: [`result_unit_err`]: do not suggest using `Error` in `no_std`
mode before Rust 1.81

Fix rust-lang#9767
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-resolve Area: Name/path resolution done by `rustc_resolve` specifically C-cleanup Category: PRs that clean code up or issues documenting cleanup.
Projects
None yet
Development

No branches or pull requests

4 participants