Skip to content

Improve compile error for borrowing mut while missing Sized trait bound #93078

Closed
@wheird-lee

Description

@wheird-lee

Given the following code:

trait Modify {
    fn modify(&mut self) ;
}

impl<T> Modify for T  {
    fn modify(&mut self)  {
        // ...
    }
}

trait Foo {
    fn mute(&mut self) {
        self.modify();
    }
}

The current output is:

self.modify();
^^^^^^^^^^^^^ cannot borrow as mutable

Ideally the output should look like:

the size for values of type `Self` cannot be known at compilation time

add Sized trait bound and the code compiles successfully:

trait Modify {
    fn modify(&mut self) ;
}

impl<T> Modify for T  {
    fn modify(&mut self)  {
        // ...
    }
}

trait Foo: Sized {
    fn mute(&mut self) {
        self.modify();
    }
}

rust version: rustc 1.58.0 (02072b4 2022-01-11)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsD-confusingDiagnostics: Confusing error or lint that should be reworked.D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.D-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions