From b1a6cf4a0e7d6727516e943d86fdab8587b94722 Mon Sep 17 00:00:00 2001
From: Alona Enraght-Moony <code@alona.page>
Date: Tue, 28 Nov 2023 15:12:46 +0000
Subject: [PATCH 1/3] Precommit test for
 https://github.com/rust-lang/rust/issues/118328.

---
 tests/mir-opt/const_prop/issue_118328.rs      | 20 ++++++++++++++++++
 .../issue_118328.size_of.ConstProp.diff       | 21 +++++++++++++++++++
 2 files changed, 41 insertions(+)
 create mode 100644 tests/mir-opt/const_prop/issue_118328.rs
 create mode 100644 tests/mir-opt/const_prop/issue_118328.size_of.ConstProp.diff

diff --git a/tests/mir-opt/const_prop/issue_118328.rs b/tests/mir-opt/const_prop/issue_118328.rs
new file mode 100644
index 00000000000..5072091ddfc
--- /dev/null
+++ b/tests/mir-opt/const_prop/issue_118328.rs
@@ -0,0 +1,20 @@
+// unit-test: ConstProp
+// compile-flags: -O
+// skip-filecheck
+#![allow(unused_assignments)]
+
+struct SizeOfConst<T>(std::marker::PhantomData<T>);
+impl<T> SizeOfConst<T> {
+    const SIZE: usize = std::mem::size_of::<T>();
+}
+
+// EMIT_MIR issue_118328.size_of.ConstProp.diff
+fn size_of<T>() -> usize {
+    let mut a = 0;
+    a = SizeOfConst::<T>::SIZE;
+    a
+}
+
+fn main() {
+    assert_eq!(size_of::<u32>(), std::mem::size_of::<u32>());
+}
diff --git a/tests/mir-opt/const_prop/issue_118328.size_of.ConstProp.diff b/tests/mir-opt/const_prop/issue_118328.size_of.ConstProp.diff
new file mode 100644
index 00000000000..0f44c49bc4d
--- /dev/null
+++ b/tests/mir-opt/const_prop/issue_118328.size_of.ConstProp.diff
@@ -0,0 +1,21 @@
+- // MIR for `size_of` before ConstProp
++ // MIR for `size_of` after ConstProp
+  
+  fn size_of() -> usize {
+      let mut _0: usize;
+      let mut _1: usize;
+      scope 1 {
+          debug a => _1;
+      }
+  
+      bb0: {
+          StorageLive(_1);
+          _1 = const 0_usize;
+          _1 = const _;
+-         _0 = _1;
++         _0 = const 0_usize;
+          StorageDead(_1);
+          return;
+      }
+  }
+  

From 9121a41450e905fe5a12c11c955acc14ab1f92fe Mon Sep 17 00:00:00 2001
From: Alona Enraght-Moony <code@alona.page>
Date: Tue, 28 Nov 2023 21:43:23 +0000
Subject: [PATCH 2/3] ConstProp: Remove const when rvalue check fails.

---
 compiler/rustc_mir_transform/src/const_prop.rs             | 7 ++++++-
 .../mir-opt/const_prop/issue_118328.size_of.ConstProp.diff | 3 +--
 2 files changed, 7 insertions(+), 3 deletions(-)

diff --git a/compiler/rustc_mir_transform/src/const_prop.rs b/compiler/rustc_mir_transform/src/const_prop.rs
index 9a16003bdc9..b96125de95e 100644
--- a/compiler/rustc_mir_transform/src/const_prop.rs
+++ b/compiler/rustc_mir_transform/src/const_prop.rs
@@ -439,6 +439,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
 
         // FIXME we need to revisit this for #67176
         if rvalue.has_param() {
+            trace!("skipping, has param");
             return None;
         }
         if !rvalue
@@ -707,7 +708,11 @@ impl<'tcx> Visitor<'tcx> for ConstPropagator<'_, 'tcx> {
     fn visit_assign(&mut self, place: &Place<'tcx>, rvalue: &Rvalue<'tcx>, location: Location) {
         self.super_assign(place, rvalue, location);
 
-        let Some(()) = self.check_rvalue(rvalue) else { return };
+        let Some(()) = self.check_rvalue(rvalue) else {
+            trace!("rvalue check failed, removing const");
+            Self::remove_const(&mut self.ecx, place.local);
+            return;
+        };
 
         match self.ecx.machine.can_const_prop[place.local] {
             // Do nothing if the place is indirect.
diff --git a/tests/mir-opt/const_prop/issue_118328.size_of.ConstProp.diff b/tests/mir-opt/const_prop/issue_118328.size_of.ConstProp.diff
index 0f44c49bc4d..ad8318832d6 100644
--- a/tests/mir-opt/const_prop/issue_118328.size_of.ConstProp.diff
+++ b/tests/mir-opt/const_prop/issue_118328.size_of.ConstProp.diff
@@ -12,8 +12,7 @@
           StorageLive(_1);
           _1 = const 0_usize;
           _1 = const _;
--         _0 = _1;
-+         _0 = const 0_usize;
+          _0 = _1;
           StorageDead(_1);
           return;
       }

From 6e956c0a383444a25cc0e690cbc9fa6f859b07b4 Mon Sep 17 00:00:00 2001
From: Alona Enraght-Moony <code@alona.page>
Date: Tue, 28 Nov 2023 22:31:53 +0000
Subject: [PATCH 3/3] Rename and add another test

---
 .../overwrite_with_const_with_params.rs       | 26 +++++++++++++++++++
 ..._const_with_params.size_of.ConstProp.diff} |  0
 .../overwrite_with_const_with_params.rs}      |  7 ++---
 3 files changed, 30 insertions(+), 3 deletions(-)
 create mode 100644 tests/mir-opt/const_prop/overwrite_with_const_with_params.rs
 rename tests/mir-opt/const_prop/{issue_118328.size_of.ConstProp.diff => overwrite_with_const_with_params.size_of.ConstProp.diff} (100%)
 rename tests/{mir-opt/const_prop/issue_118328.rs => ui/const_prop/overwrite_with_const_with_params.rs} (79%)

diff --git a/tests/mir-opt/const_prop/overwrite_with_const_with_params.rs b/tests/mir-opt/const_prop/overwrite_with_const_with_params.rs
new file mode 100644
index 00000000000..4cf6d7c1396
--- /dev/null
+++ b/tests/mir-opt/const_prop/overwrite_with_const_with_params.rs
@@ -0,0 +1,26 @@
+// unit-test: ConstProp
+// compile-flags: -O
+
+// Regression test for https://github.com/rust-lang/rust/issues/118328
+
+#![allow(unused_assignments)]
+
+struct SizeOfConst<T>(std::marker::PhantomData<T>);
+impl<T> SizeOfConst<T> {
+    const SIZE: usize = std::mem::size_of::<T>();
+}
+
+// EMIT_MIR overwrite_with_const_with_params.size_of.ConstProp.diff
+fn size_of<T>() -> usize {
+    // CHECK-LABEL: fn size_of(
+    // CHECK: _1 = const 0_usize;
+    // CHECK-NEXT: _1 = const _;
+    // CHECK-NEXT: _0 = _1;
+    let mut a = 0;
+    a = SizeOfConst::<T>::SIZE;
+    a
+}
+
+fn main() {
+    assert_eq!(size_of::<u32>(), std::mem::size_of::<u32>());
+}
diff --git a/tests/mir-opt/const_prop/issue_118328.size_of.ConstProp.diff b/tests/mir-opt/const_prop/overwrite_with_const_with_params.size_of.ConstProp.diff
similarity index 100%
rename from tests/mir-opt/const_prop/issue_118328.size_of.ConstProp.diff
rename to tests/mir-opt/const_prop/overwrite_with_const_with_params.size_of.ConstProp.diff
diff --git a/tests/mir-opt/const_prop/issue_118328.rs b/tests/ui/const_prop/overwrite_with_const_with_params.rs
similarity index 79%
rename from tests/mir-opt/const_prop/issue_118328.rs
rename to tests/ui/const_prop/overwrite_with_const_with_params.rs
index 5072091ddfc..6f533919a47 100644
--- a/tests/mir-opt/const_prop/issue_118328.rs
+++ b/tests/ui/const_prop/overwrite_with_const_with_params.rs
@@ -1,6 +1,8 @@
-// unit-test: ConstProp
 // compile-flags: -O
-// skip-filecheck
+// run-pass
+
+// Regression test for https://github.com/rust-lang/rust/issues/118328
+
 #![allow(unused_assignments)]
 
 struct SizeOfConst<T>(std::marker::PhantomData<T>);
@@ -8,7 +10,6 @@ impl<T> SizeOfConst<T> {
     const SIZE: usize = std::mem::size_of::<T>();
 }
 
-// EMIT_MIR issue_118328.size_of.ConstProp.diff
 fn size_of<T>() -> usize {
     let mut a = 0;
     a = SizeOfConst::<T>::SIZE;