Skip to content

"Static" methods should not be object safe #19949

Description

@nikomatsakis

While reading the object safety code I found this:

ty::StaticExplicitSelfCategory => {
    // Static methods are always object-safe since they
    // can't be called through a trait object
    return msgs
}

I think this is sadly incorrect. The whole idea of object safety is to ensure that an object type Foo implements the trait Foo. But if that is the case, then we can have an example like this one:

trait Foo for Sized? {
    fn make() -> Self; // static method
}

fn use<Sized? F:Foo>() {
    let x = Foo::make();
}

fn main() {
    use::<Foo>();
}

cc @nick29581
cc @aturon

Activity

  1. nikomatsakis commented on Dec 17, 2014

    @nikomatsakis
    ContributorAuthor

    I hope that fixing this doesn't cause another round of annoyed users. :(

  2. ftxqxd commented on Dec 17, 2014

    @ftxqxd
    Contributor

    See #18527, the PR that introduced this, and rust-lang/rfcs#428, which is the same issue as this one.

  3. added a commit that references this issue on Dec 30, 2014
    cd31e6f
  4. added a commit that references this issue on Dec 31, 2014
    d058520
  5. added a commit that references this issue on Jun 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions