Skip to content

assign to part of possibly uninitialized variable is flagged as UB (but only in 2018 edition) #60450

Closed
@jethrogb

Description

@jethrogb

Code sample:

fn main() {
    let mut _tmp: (u64, u64);
    
    _tmp.0 = 1;
    _tmp.1 = 1;
}

When compiling this with rustc +1.31.0 --edition 2018, I get:

warning[E0381]: assign to part of possibly uninitialized variable: `_tmp`
 --> t.rs:4:5
  |
4 |     _tmp.0 = 1;
  |     ^^^^^^^^^^ use of possibly uninitialized `_tmp`
  |
  = warning: This error has been downgraded to a warning for backwards compatibility with previous releases.
          It represents potential unsoundness in your code.
          This warning will become a hard error in the future.

In later versions, the warning message has been changed somewhat:

   = warning: this error has been downgraded to a warning for backwards compatibility with previous releases
   = warning: this represents potential undefined behavior in your code and this warning will become a hard error in the future

If I compile with --edition 2015 I get no warnings at all. (Edit: this is still silent on 1.34.0 2015, but an warning is emitted in edition 2015 on the latest nightly).

There are several issues as far as I can see:

  • “This error has been downgraded to a warning”: this is the first stable compiler for the 2018 edition, so when was this downgrade supposed to have happened? This code never generated an error, for any edition, on any previous stable compiler.
  • How can there be UB only when I'm compiling for the 2018 edition?
  • What actually is the UB? The compiler is already tracking validity of individual fields just fine (e.g. when moving out of structs).
  • The explanation for E0381 doesn't talk about assignment of uninitialized variables at all.

Edit: I ran into this issue with inline assembly, where you may be required to specify an output register even if it's not used.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-NLLArea: Non-lexical lifetimes (NLL)NLL-diagnosticsWorking towards the "diagnostic parity" goal

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions