Skip to content

Commit c3ab4f2

Browse files
committed
Add CopyForDeref to custom MIR
1 parent 0bcfd2d commit c3ab4f2

File tree

4 files changed

+30
-0
lines changed

4 files changed

+30
-0
lines changed

compiler/rustc_mir_build/src/build/custom/parse/instruction.rs

+1
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ impl<'tcx, 'body> ParseCtxt<'tcx, 'body> {
154154
Ok(Rvalue::BinaryOp(BinOp::Offset, Box::new((ptr, offset))))
155155
},
156156
@call("mir_len", args) => Ok(Rvalue::Len(self.parse_place(args[0])?)),
157+
@call("mir_copy_for_deref", args) => Ok(Rvalue::CopyForDeref(self.parse_place(args[0])?)),
157158
ExprKind::Borrow { borrow_kind, arg } => Ok(
158159
Rvalue::Ref(self.tcx.lifetimes.re_erased, *borrow_kind, self.parse_place(*arg)?)
159160
),

library/core/src/intrinsics/mir.rs

+1
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ define!("mir_storage_dead", fn StorageDead<T>(local: T));
279279
define!("mir_deinit", fn Deinit<T>(place: T));
280280
define!("mir_checked", fn Checked<T>(binop: T) -> (T, bool));
281281
define!("mir_len", fn Len<T>(place: T) -> usize);
282+
define!("mir_copy_for_deref", fn CopyForDeref<T>(place: T) -> T);
282283
define!("mir_retag", fn Retag<T>(place: T));
283284
define!("mir_move", fn Move<T>(place: T) -> T);
284285
define!("mir_static", fn Static<T>(s: T) -> &'static T);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// MIR for `copy_for_deref` after built
2+
3+
fn copy_for_deref(_1: (&i32, i32)) -> i32 {
4+
let mut _0: i32; // return place in scope 0 at $DIR/projections.rs:+0:38: +0:41
5+
let mut _2: &i32; // in scope 0 at $SRC_DIR/core/src/intrinsics/mir.rs:LL:COL
6+
7+
bb0: {
8+
_2 = deref_copy (_1.0: &i32); // scope 0 at $DIR/projections.rs:+4:13: +4:37
9+
_0 = (*_2); // scope 0 at $DIR/projections.rs:+5:13: +5:24
10+
return; // scope 0 at $DIR/projections.rs:+6:13: +6:21
11+
}
12+
}

tests/mir-opt/building/custom/projections.rs

+16
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,19 @@ fn simple_index(a: [i32; 10], b: &[i32]) -> i32 {
7171
})
7272
}
7373

74+
// EMIT_MIR projections.copy_for_deref.built.after.mir
75+
#[custom_mir(dialect = "runtime", phase = "initial")]
76+
fn copy_for_deref(x: (&i32, i32)) -> i32 {
77+
mir!(
78+
let temp: &i32;
79+
{
80+
temp = CopyForDeref(x.0);
81+
RET = *temp;
82+
Return()
83+
}
84+
)
85+
}
86+
7487
fn main() {
7588
assert_eq!(unions(U { a: 5 }), 5);
7689
assert_eq!(tuples((5, 6)), (5, 6));
@@ -82,4 +95,7 @@ fn main() {
8295
assert_eq!(o, Some(10));
8396

8497
assert_eq!(simple_index([0; 10], &[0; 10]), 0);
98+
99+
let one = 1;
100+
assert_eq!(copy_for_deref((&one, one)), 1);
85101
}

0 commit comments

Comments
 (0)