Skip to content

Private supertraits are not callable on concrete types #83882

Open
@MaikKlein

Description

@MaikKlein
pub mod foo {
    pub trait Foo: sealed::Bar {
        fn foo(&self) {}
    }
    mod sealed {
        pub trait Bar {
            fn bar(&self) {}
        }
    }
    impl Foo for u32 {}
    impl sealed::Bar for u32 {}
}


fn meow_dyn(f: &dyn foo::Foo) {
    f.foo();
    f.bar();
}

fn meow_static(f: impl foo::Foo) {
    f.foo();
    f.bar();
}

fn meow_u32(f: u32) {
    use foo::Foo;
    // use crate::foo::sealed::Bar;
    // ^ doesn't work because it is private
    f.foo();
    f.bar(); // Why does this error now?
}


fn main() {
    meow_static(42);
    meow_dyn(&42);
}
error[E0599]: no method named `bar` found for type `u32` in the current scope
  --> src/main.rs:30:7
   |
30 |     f.bar(); // Why does this error now?
   |       ^^^ method not found in `u32`
   |
   = help: items from traits can only be used if the trait is in scope
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
   |
1  | use crate::foo::sealed::Bar;
   |

playground

I am reporting this because I believe this is a bug.

Both meow_dyn and meow_static type check, but calling .bar() on a concrete type is currently not possible. Is there a reason for that?

I would expect any of those two cases:

  1. meow_u32 should actually type check, and .bar() should be callable if Foo is visible, because Bar is a supertrait.
  2. Bar should not be usable in any other context, because the trait is private.

I could not find any information about this in the reference.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-trait-systemArea: Trait systemA-visibilityArea: Visibility / privacyC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions