Skip to content

"Static" methods should not be object safe #19949

Closed
@nikomatsakis

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

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