Skip to content

Assignment into Deref is allowed and creates irrelevant errors #130326

Open

Description

Code

fn main() {
    let i_am_sized = String::new();
    *i_am_sized = String::new();
}

I thought I've had &mut String, not String, so * was unnecessary here.

Because the lvalue has been dereferenced anyway, it caused confusing type error about a type that is not relevant, and assigning to an unsized type that is not at fault here. But the root cause was unexpectedly triggering Deref instead of a dereference on &mut or DerefMut.

Current output

error[E0308]: mismatched types
 --> src/main.rs:3:19
  |
3 |     *i_am_sized = String::new();
  |     -----------   ^^^^^^^^^^^^^ expected `str`, found `String`
  |     |
  |     expected due to the type of this binding

error[E0277]: the size for values of type `str` cannot be known at compilation time
 --> src/main.rs:3:5
  |
3 |     *i_am_sized = String::new();
  |     ^^^^^^^^^^^ doesn't have a size known at compile-time
  |
  = help: the trait `Sized` is not implemented for `str`

Desired output

Something that *i_am_sized due to Deref is can't be assigned to, and suggest removing * instead.

Rust Version

rustc 1.83.0-nightly (8d6b88b16 2024-09-11)
binary: rustc
commit-hash: 8d6b88b168e45ee1624699c19443c49665322a91
commit-date: 2024-09-11
host: aarch64-apple-darwin
release: 1.83.0-nightly
LLVM version: 19.1.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsD-confusingDiagnostics: Confusing error or lint that should be reworked.D-verboseDiagnostics: Too much output caused by a single piece of incorrect code.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