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

Require trait definition to be in scope to be able to call trait methods on type. #6864

Open
TomAFrench opened this issue Dec 19, 2024 · 0 comments

Comments

@TomAFrench
Copy link
Member

In the below program we have a trait Foo which is private to the module private_mod, however we can call the foo() method on Bar in main despite the root module not being supposed to know about Foo at all.

fn main() {
    assert_eq(Bar::foo(), 42);
}

pub struct Bar {}

mod private_mod {
    trait Foo {
        fn foo() -> Field;
    }

    impl Foo for super::Bar {
        fn foo() -> Field {
            42
        }
    }
}

This means that a trait used for internal implementation details (such as in #6845) leaks into user code. The way that Rust addresses this issue is by requiring the trait definition to be in scope in order to call trait methods.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: 📋 Backlog
Development

No branches or pull requests

1 participant