Skip to content
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

Writing a non-trait impl for a struct in a different module makes it impossible to construct that struct directly #9052

Closed
huonw opened this issue Sep 8, 2013 · 5 comments
Labels
A-resolve Area: Name/path resolution done by `rustc_resolve` specifically

Comments

@huonw
Copy link
Member

huonw commented Sep 8, 2013

Commenting out the impl makes the build succeed.

mod foo {
    use super::*;

    fn bar() -> Foo {
        Foo { x: 1 }
    }
    impl Foo { }
}

pub struct Foo { x: int }

fn main() {}
mod-error.rs:5:8: 5:11 error: `Foo` does not name a structure
mod-error.rs:5         Foo { x: 1 }
                       ^~~
error: aborting due to previous error
@SiegeLord
Copy link
Contributor

A workabout for this is to qualify Foo. E.g. this works:

mod foo {
    fn bar() -> super::Foo {
        super::Foo { x: 1 }
    }
    impl super::Foo { }
}

pub struct Foo { x: int }

fn main() {}

Note that it still won't work perfectly, as you are liable to hit #9584 in the process.

@chris-morgan
Copy link
Member

Just ran across this when implementing an extra method only in tests.

You actually don’t need to always qualify foo; just on the impl. (This will trigger the unnecessary_qualifications lint, but it’s set to allow by default—I just happen to have decided to be very strict in the Teepee codebase.)

@bstrie
Copy link
Contributor

bstrie commented May 22, 2014

@chris-morgan assures me that this is a manifestation of the same bug, although it expresses itself as the sudden invisibility of an inherent impl from a different module:

use Foo::Bar;

mod Foo {
    pub struct Bar {
        pub x: int,
        y: int
    }

    impl Bar {
        pub fn new(new_x: int, new_y: int) -> Bar {
            Bar { x: new_x, y: new_y }
        }
    }
}

impl Bar {
    fn set_x(&self, new_x: int) {
        self.x = new_x;
    }
}

fn main() {
    let bar = Bar::new(1, 2);  // error: unresolved name `Bar::new`.
    println!("{}", bar.x);
}

@ghost
Copy link

ghost commented Sep 30, 2014

So this is no longer an issue given that inherent impls are now only allowed in the same module?

@steveklabnik
Copy link
Member

@jakub- yup

flip1995 pushed a commit to flip1995/rust that referenced this issue Aug 11, 2022
Fix suggestions for `async` closures in redundant_closure_call

Fixes rust-lang#9052

changelog: Fix suggestions given by [`redundant_closure_call`] for async closures
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

No branches or pull requests

6 participants