Skip to content

Commit fddd861

Browse files
authored
Rollup merge of rust-lang#108855 - cbeuw:mir-cast, r=tmiasko
Custom MIR: Support `as` casts Small changes to support this low hanging fruit r? `@oli-obk` or `@tmiasko` or `@JakobDegen`
2 parents b866e1e + 7281cd0 commit fddd861

File tree

5 files changed

+81
-1
lines changed

5 files changed

+81
-1
lines changed

Diff for: compiler/rustc_mir_build/src/build/custom/parse/instruction.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use rustc_middle::mir::interpret::{ConstValue, Scalar};
22
use rustc_middle::mir::tcx::PlaceTy;
3+
use rustc_middle::ty::cast::mir_cast_kind;
34
use rustc_middle::{mir::*, thir::*, ty};
45
use rustc_span::Span;
56
use rustc_target::abi::VariantIdx;
@@ -142,7 +143,7 @@ impl<'tcx, 'body> ParseCtxt<'tcx, 'body> {
142143
}
143144

144145
fn parse_rvalue(&self, expr_id: ExprId) -> PResult<Rvalue<'tcx>> {
145-
parse_by_kind!(self, expr_id, _, "rvalue",
146+
parse_by_kind!(self, expr_id, expr, "rvalue",
146147
@call("mir_discriminant", args) => self.parse_place(args[0]).map(Rvalue::Discriminant),
147148
@call("mir_checked", args) => {
148149
parse_by_kind!(self, args[0], _, "binary op",
@@ -167,6 +168,12 @@ impl<'tcx, 'body> ParseCtxt<'tcx, 'body> {
167168
ExprKind::Repeat { value, count } => Ok(
168169
Rvalue::Repeat(self.parse_operand(*value)?, *count)
169170
),
171+
ExprKind::Cast { source } => {
172+
let source = self.parse_operand(*source)?;
173+
let source_ty = source.ty(self.body.local_decls(), self.tcx);
174+
let cast_kind = mir_cast_kind(source_ty, expr.ty);
175+
Ok(Rvalue::Cast(cast_kind, source, expr.ty))
176+
},
170177
_ => self.parse_operand(expr_id).map(Rvalue::Use),
171178
)
172179
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// MIR for `float_to_int` after built
2+
3+
fn float_to_int(_1: f32) -> i32 {
4+
let mut _0: i32; // return place in scope 0 at $DIR/as_cast.rs:+0:28: +0:31
5+
6+
bb0: {
7+
_0 = _1 as i32 (FloatToInt); // scope 0 at $DIR/as_cast.rs:+3:13: +3:27
8+
return; // scope 0 at $DIR/as_cast.rs:+4:13: +4:21
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// MIR for `int_to_int` after built
2+
3+
fn int_to_int(_1: u32) -> i32 {
4+
let mut _0: i32; // return place in scope 0 at $DIR/as_cast.rs:+0:26: +0:29
5+
6+
bb0: {
7+
_0 = _1 as i32 (IntToInt); // scope 0 at $DIR/as_cast.rs:+3:13: +3:27
8+
return; // scope 0 at $DIR/as_cast.rs:+4:13: +4:21
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// MIR for `int_to_ptr` after built
2+
3+
fn int_to_ptr(_1: usize) -> *const i32 {
4+
let mut _0: *const i32; // return place in scope 0 at $DIR/as_cast.rs:+0:28: +0:38
5+
6+
bb0: {
7+
_0 = _1 as *const i32 (PointerFromExposedAddress); // scope 0 at $DIR/as_cast.rs:+3:13: +3:34
8+
return; // scope 0 at $DIR/as_cast.rs:+4:13: +4:21
9+
}
10+
}

Diff for: tests/mir-opt/building/custom/as_cast.rs

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#![feature(custom_mir, core_intrinsics)]
2+
3+
extern crate core;
4+
use core::intrinsics::mir::*;
5+
6+
// EMIT_MIR as_cast.int_to_int.built.after.mir
7+
#[custom_mir(dialect = "built")]
8+
fn int_to_int(x: u32) -> i32 {
9+
mir!(
10+
{
11+
RET = x as i32;
12+
Return()
13+
}
14+
)
15+
}
16+
17+
// EMIT_MIR as_cast.float_to_int.built.after.mir
18+
#[custom_mir(dialect = "built")]
19+
fn float_to_int(x: f32) -> i32 {
20+
mir!(
21+
{
22+
RET = x as i32;
23+
Return()
24+
}
25+
)
26+
}
27+
28+
// EMIT_MIR as_cast.int_to_ptr.built.after.mir
29+
#[custom_mir(dialect = "built")]
30+
fn int_to_ptr(x: usize) -> *const i32 {
31+
mir!(
32+
{
33+
RET = x as *const i32;
34+
Return()
35+
}
36+
)
37+
}
38+
39+
fn main() {
40+
assert_eq!(int_to_int(5), 5);
41+
assert_eq!(float_to_int(5.), 5);
42+
assert_eq!(int_to_ptr(0), std::ptr::null());
43+
}

0 commit comments

Comments
 (0)