rust-lang / rust-lang/rust

Assignment into Deref is allowed and creates irrelevant errors

Open
#130,326 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics D-confusing D-verbose T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No source file or test is named. Reproduce the supplied Rust example and inspect the compiler diagnostic path for assignment through Deref; done means the error explains that the dereferenced value cannot be assigned and suggests removing *.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.