Skip to content

Incorrectly unified locals at -Zmir-opt-level=2 #79191

Closed
@tmiasko

Description

@tmiasko
fn main() {
    if f(false) != 1 {
        panic!();
    }
}

#[inline(always)]
fn f(use_y: bool) -> u8 {
    let (x, y) = (1, 2);
    if use_y { y } else { x }
}
$ rustc -Zmir-opt-level=1 a.rs && ./a
$ rustc -Zmir-opt-level=2 a.rs && ./a
thread 'main' panicked at 'explicit panic', a.rs:3:9
--- a.main.005-011.DestinationPropagation.before.mir
+++ a.main.005-011.DestinationPropagation.after.mir
@@ -1,70 +1,70 @@
-// MIR for `main` before DestinationPropagation
+// MIR for `main` after DestinationPropagation
 
 fn main() -> () {
     let mut _0: ();
     let mut _1: bool;
     let mut _2: u8;
     let mut _3: !;
     let _4: ();
     let mut _5: !;
     let mut _6: bool;
     scope 1 (inlined f) {
         debug use_y => _6;
         let _7: u8;
         let mut _8: bool;
         scope 2 {
             debug x => _2;
-            debug y => _7;
+            debug y => _2;
         }
     }
 
     bb0: {
         StorageLive(_1);
-        StorageLive(_2);
+        nop;
         StorageLive(_6);
         _6 = const false;
         _2 = const 1_u8;
-        StorageLive(_7);
-        _7 = const 2_u8;
+        nop;
+        _2 = const 2_u8;
         StorageLive(_8);
         _8 = const false;
         goto -> bb4;
     }
 
     bb1: {
-        StorageDead(_2);
+        nop;
         _0 = const ();
         StorageDead(_1);
         return;
     }
 
     bb2: {
-        StorageDead(_2);
+        nop;
         StorageLive(_4);
         StorageLive(_5);
         begin_panic::<&str>(const "explicit panic");
     }
 
     bb3: {
-        _2 = _7;
+        nop;
         goto -> bb4;
     }
 
     bb4: {
-        StorageDead(_7);
+        nop;
         StorageDead(_8);
         StorageDead(_6);
         _1 = Ne(_2, const 1_u8);
         nop;
         switchInt(move _2) -> [1_u8: bb1, otherwise: bb2];
     }
 }



<!-- TRIAGEBOT_START -->

<!-- TRIAGEBOT_ASSIGN_START -->

<!-- TRIAGEBOT_ASSIGN_DATA_START$${"user":"JakobDegen"}$$TRIAGEBOT_ASSIGN_DATA_END -->

<!-- TRIAGEBOT_ASSIGN_END -->
<!-- TRIAGEBOT_END -->

Metadata

Metadata

Assignees

Labels

A-mir-optArea: MIR optimizationsA-mir-opt-nrvoFixed by the Named Return Value Opt. (NRVO)C-bugCategory: This is a bug.I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.requires-nightlyThis issue requires a nightly compiler in some way.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions