rust-lang / rust-lang/rust

Error message suggests to use a Box to wrap dyn Trait when it's unnecessary

Open
#129,357 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Code
use std::io::Write;

fn foo(f: Option<&mut dyn Write>) {
    if let Some(f) = f {
        writeln!(f, "Hello").unwrap();
    }
}

fn bar() {
    let mut out = Some(std::io::stdout());
    foo(out.as_mut());
}
Current output
error[E0308]: mismatched types
  --> src/lib.rs:11:9
   |
11 |     foo(out.as_mut());
   |     --- ^^^^^^^^^^^^ expected `Option<&mut dyn Write>`, found `Option<&mut Stdout>`
   |     |
   |     arguments to this function are incorrect
   |
   = note: expected enum `Option<&mut dyn std::io::Write>`
              found enum `Option<&mut Stdout>`
   = help: `Stdout` implements `Write` so you could box the found value and coerce it to the trait object `Box<dyn Write>`, you will have to change the expected type as well
note: function defined here
  --> src/lib.rs:3:4
   |
3  | fn foo(f: Option<&mut dyn Write>) {
   |    ^^^ -------------------------
Desired output
= help: `Stdout` implements `Write` so you could make a reference to the found value and coerce it to the trait object `&mut dyn Write` or `Box<&dyn Write>`, you will have to change the expected type as well
Rationale and extra context

I can make it compile without using a Box.

fn bar() {
    let mut out = Some(std::io::stdout());
    foo(out.as_mut().map(|p| p as &mut dyn Write));
}

I don't think error message should suggest to use a Box when it's not necessary.

Also maybe it's just me but I'm not quite sure what you will have to change the expected type as well means.

Other cases

No response

Rust Version
$ rustc --version --verbose
rustc 1.80.1 (3f5fd8dd4 2024-08-06)
binary: rustc
commit-hash: 3f5fd8dd41153bc5fdca9427e9e05be2c767ba23
commit-date: 2024-08-06
host: x86_64-unknown-linux-gnu
release: 1.80.1
Anything else?

No response

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

Start by compiling the reproducer in src/lib.rs with the reported Rust version and compare the current diagnostic with the desired help text. Trace the compiler diagnostic that suggests boxing a dyn Write value, then update its wording and add coverage for the Option<&mut dyn Write> case; done means the output no longer unnecessarily recommends Box and clearly explains the reference coercion.

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
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.