#163443·Rust

Confusing diagnostics about return type for input type mismatch

Author: nbdd0121Created Sep 28, 2026Updated Sep 28, 2026
LabelsA-diagnosticsT-compiler

Code

rust
use std::mem::MaybeUninit;

fn test(x: &mut MaybeUninit<u32>) {
    x.write(1i32);
}

Current output

bash
error[E0308]: mismatched types
   --> src/lib.rs:4:13
    |
  4 |     x.write(1i32);
    |       ----- ^^^^ expected `u32`, found `i32`
    |       |
    |       arguments to this method are incorrect
    |
help: the return type of this call is `i32` due to the type of the argument passed
   --> src/lib.rs:4:5
    |
  4 |     x.write(1i32);
    |     ^^^^^^^^----^
    |             |
    |             this argument influences the return type of `write`
note: method defined here
   --> /playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/mem/maybe_uninit.rs:574:18
    |
574 |     pub const fn write(&mut self, val: T) -> &mut T {
    |                  ^^^^^
help: change the type of the numeric literal from `i32` to `u32`
    |
  4 -     x.write(1i32);
  4 +     x.write(1u32);
    |

Desired output

bash
error[E0308]: mismatched types
   --> src/lib.rs:4:13
    |
  4 |     x.write(1i32);
    |       ----- ^^^^ expected `u32`, found `i32`
    |       |
    |       arguments to this method are incorrect
    |
note: method defined here
   --> /playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/mem/maybe_uninit.rs:574:18
    |
574 |     pub const fn write(&mut self, val: T) -> &mut T {
    |                  ^^^^^
help: change the type of the numeric literal from `i32` to `u32`
    |
  4 -     x.write(1i32);
  4 +     x.write(1u32);
    |

Rationale and extra context

The type is already fully constrained without having type inference affected by the return type. The help about return type is only adding confusion in this case.

Other cases

rust

Rust Version

bash
rustc 1.101.0-nightly (d080e7dff 2026-09-27)
binary: rustc
commit-hash: d080e7dff1b0fc54541545252818f8cccf995d05
commit-date: 2026-09-27
host: x86_64-unknown-linux-gnu
release: 1.101.0-nightly
LLVM version: 23.1.1

Anything else?

No response