Skip to content

declaration of non-object-safe trait object type should be invalid #21953

Closed
@mandel59

Description

@mandel59
trait A {
    fn foo(&self, _x: &Self);
}

impl A for () {
    fn foo(&self, _x: &Self) {}
}

fn main() {                                                 
    let _x: &A;
}

This code passes, while the codes like let x = &() as &A; cause compile error because the trait A is not object-safe. 0255-object-safety

Is there any reason for type &A to exist? If not, the declaration of variables of type &A should be invalid.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lints

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions