From c3ab4f28d37c9577dc401a08a72f82b752d32e53 Mon Sep 17 00:00:00 2001
From: Andy Wang <cbeuw.andy@gmail.com>
Date: Mon, 15 May 2023 12:05:10 +0200
Subject: [PATCH 1/3] Add CopyForDeref to custom MIR

---
 .../src/build/custom/parse/instruction.rs        |  1 +
 library/core/src/intrinsics/mir.rs               |  1 +
 .../projections.copy_for_deref.built.after.mir   | 12 ++++++++++++
 tests/mir-opt/building/custom/projections.rs     | 16 ++++++++++++++++
 4 files changed, 30 insertions(+)
 create mode 100644 tests/mir-opt/building/custom/projections.copy_for_deref.built.after.mir

diff --git a/compiler/rustc_mir_build/src/build/custom/parse/instruction.rs b/compiler/rustc_mir_build/src/build/custom/parse/instruction.rs
index 931fe1b2433..b74422708ce 100644
--- a/compiler/rustc_mir_build/src/build/custom/parse/instruction.rs
+++ b/compiler/rustc_mir_build/src/build/custom/parse/instruction.rs
@@ -154,6 +154,7 @@ impl<'tcx, 'body> ParseCtxt<'tcx, 'body> {
                 Ok(Rvalue::BinaryOp(BinOp::Offset, Box::new((ptr, offset))))
             },
             @call("mir_len", args) => Ok(Rvalue::Len(self.parse_place(args[0])?)),
+            @call("mir_copy_for_deref", args) => Ok(Rvalue::CopyForDeref(self.parse_place(args[0])?)),
             ExprKind::Borrow { borrow_kind, arg } => Ok(
                 Rvalue::Ref(self.tcx.lifetimes.re_erased, *borrow_kind, self.parse_place(*arg)?)
             ),
diff --git a/library/core/src/intrinsics/mir.rs b/library/core/src/intrinsics/mir.rs
index 19f8b944c1f..b1a112849d8 100644
--- a/library/core/src/intrinsics/mir.rs
+++ b/library/core/src/intrinsics/mir.rs
@@ -279,6 +279,7 @@ define!("mir_storage_dead", fn StorageDead<T>(local: T));
 define!("mir_deinit", fn Deinit<T>(place: T));
 define!("mir_checked", fn Checked<T>(binop: T) -> (T, bool));
 define!("mir_len", fn Len<T>(place: T) -> usize);
+define!("mir_copy_for_deref", fn CopyForDeref<T>(place: T) -> T);
 define!("mir_retag", fn Retag<T>(place: T));
 define!("mir_move", fn Move<T>(place: T) -> T);
 define!("mir_static", fn Static<T>(s: T) -> &'static T);
diff --git a/tests/mir-opt/building/custom/projections.copy_for_deref.built.after.mir b/tests/mir-opt/building/custom/projections.copy_for_deref.built.after.mir
new file mode 100644
index 00000000000..5233d0489c6
--- /dev/null
+++ b/tests/mir-opt/building/custom/projections.copy_for_deref.built.after.mir
@@ -0,0 +1,12 @@
+// MIR for `copy_for_deref` after built
+
+fn copy_for_deref(_1: (&i32, i32)) -> i32 {
+    let mut _0: i32;                     // return place in scope 0 at $DIR/projections.rs:+0:38: +0:41
+    let mut _2: &i32;                    // in scope 0 at $SRC_DIR/core/src/intrinsics/mir.rs:LL:COL
+
+    bb0: {
+        _2 = deref_copy (_1.0: &i32);    // scope 0 at $DIR/projections.rs:+4:13: +4:37
+        _0 = (*_2);                      // scope 0 at $DIR/projections.rs:+5:13: +5:24
+        return;                          // scope 0 at $DIR/projections.rs:+6:13: +6:21
+    }
+}
diff --git a/tests/mir-opt/building/custom/projections.rs b/tests/mir-opt/building/custom/projections.rs
index 5e472e531c7..bd6db0e5c4c 100644
--- a/tests/mir-opt/building/custom/projections.rs
+++ b/tests/mir-opt/building/custom/projections.rs
@@ -71,6 +71,19 @@ fn simple_index(a: [i32; 10], b: &[i32]) -> i32 {
     })
 }
 
+// EMIT_MIR projections.copy_for_deref.built.after.mir
+#[custom_mir(dialect = "runtime", phase = "initial")]
+fn copy_for_deref(x: (&i32, i32)) -> i32 {
+    mir!(
+        let temp: &i32;
+        {
+            temp = CopyForDeref(x.0);
+            RET = *temp;
+            Return()
+        }
+    )
+}
+
 fn main() {
     assert_eq!(unions(U { a: 5 }), 5);
     assert_eq!(tuples((5, 6)), (5, 6));
@@ -82,4 +95,7 @@ fn main() {
     assert_eq!(o, Some(10));
 
     assert_eq!(simple_index([0; 10], &[0; 10]), 0);
+
+    let one = 1;
+    assert_eq!(copy_for_deref((&one, one)), 1);
 }

From 1bd6e168b24236fdb0b848537861a4b53feb4221 Mon Sep 17 00:00:00 2001
From: Andy Wang <cbeuw.andy@gmail.com>
Date: Mon, 15 May 2023 12:05:17 +0200
Subject: [PATCH 2/3] Address FIXME

---
 tests/mir-opt/building/custom/projections.rs             | 9 +++------
 .../building/custom/projections.tuples.built.after.mir   | 8 +++-----
 2 files changed, 6 insertions(+), 11 deletions(-)

diff --git a/tests/mir-opt/building/custom/projections.rs b/tests/mir-opt/building/custom/projections.rs
index bd6db0e5c4c..3c155deae4b 100644
--- a/tests/mir-opt/building/custom/projections.rs
+++ b/tests/mir-opt/building/custom/projections.rs
@@ -21,13 +21,10 @@ fn unions(u: U) -> i32 {
 #[custom_mir(dialect = "analysis", phase = "post-cleanup")]
 fn tuples(i: (u32, i32)) -> (u32, i32) {
     mir!(
-        // FIXME(JakobDegen): This is necessary because we can't give type hints for `RET`
-        let temp: (u32, i32);
+        type RET = (u32, i32);
         {
-            temp.0 = i.0;
-            temp.1 = i.1;
-
-            RET = temp;
+            RET.0 = i.0;
+            RET.1 = i.1;
             Return()
         }
     )
diff --git a/tests/mir-opt/building/custom/projections.tuples.built.after.mir b/tests/mir-opt/building/custom/projections.tuples.built.after.mir
index 65487d3c9ed..dec575200c6 100644
--- a/tests/mir-opt/building/custom/projections.tuples.built.after.mir
+++ b/tests/mir-opt/building/custom/projections.tuples.built.after.mir
@@ -2,12 +2,10 @@
 
 fn tuples(_1: (u32, i32)) -> (u32, i32) {
     let mut _0: (u32, i32);              // return place in scope 0 at $DIR/projections.rs:+0:29: +0:39
-    let mut _2: (u32, i32);              // in scope 0 at $SRC_DIR/core/src/intrinsics/mir.rs:LL:COL
 
     bb0: {
-        (_2.0: u32) = (_1.0: u32);       // scope 0 at $DIR/projections.rs:+5:13: +5:25
-        (_2.1: i32) = (_1.1: i32);       // scope 0 at $DIR/projections.rs:+6:13: +6:25
-        _0 = _2;                         // scope 0 at $DIR/projections.rs:+8:13: +8:23
-        return;                          // scope 0 at $DIR/projections.rs:+9:13: +9:21
+        (_0.0: u32) = (_1.0: u32);       // scope 0 at $DIR/projections.rs:+4:13: +4:24
+        (_0.1: i32) = (_1.1: i32);       // scope 0 at $DIR/projections.rs:+5:13: +5:24
+        return;                          // scope 0 at $DIR/projections.rs:+6:13: +6:21
     }
 }

From 3d938ddb39467614301e9843af29bbfe843cc7ad Mon Sep 17 00:00:00 2001
From: Andy Wang <cbeuw.andy@gmail.com>
Date: Mon, 15 May 2023 12:08:16 +0200
Subject: [PATCH 3/3] Documentation

---
 library/core/src/intrinsics/mir.rs | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/library/core/src/intrinsics/mir.rs b/library/core/src/intrinsics/mir.rs
index b1a112849d8..5944a0de1a4 100644
--- a/library/core/src/intrinsics/mir.rs
+++ b/library/core/src/intrinsics/mir.rs
@@ -228,7 +228,7 @@
 //!
 //!  - Operands implicitly convert to `Use` rvalues.
 //!  - `&`, `&mut`, `addr_of!`, and `addr_of_mut!` all work to create their associated rvalue.
-//!  - [`Discriminant`] and [`Len`] have associated functions.
+//!  - [`Discriminant`], [`Len`], and [`CopyForDeref`] have associated functions.
 //!  - Unary and binary operations use their normal Rust syntax - `a * b`, `!c`, etc.
 //!  - The binary operation `Offset` can be created via [`Offset`].
 //!  - Checked binary operations are represented by wrapping the associated binop in [`Checked`].