Skip to content

Commit 42e1789

Browse files
committed
more smir
1 parent f972570 commit 42e1789

File tree

5 files changed

+59
-11
lines changed

5 files changed

+59
-11
lines changed

compiler/rustc_smir/src/rustc_smir/context.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -409,13 +409,15 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
409409
let bytes = value.as_bytes();
410410
let val_tree = ty::ValTree::from_raw_bytes(tcx, bytes);
411411

412-
mir::Const::Ty(ty, ty::Const::new_value(tcx, val_tree, ty)).stable(&mut *tables)
412+
let ct = ty::Const::new_value(tcx, val_tree, ty);
413+
super::convert::mir_const_from_ty_const(&mut *tables, ct, ty)
413414
}
414415

415416
fn new_const_bool(&self, value: bool) -> MirConst {
416417
let mut tables = self.0.borrow_mut();
417-
mir::Const::Ty(tables.tcx.types.bool, ty::Const::from_bool(tables.tcx, value))
418-
.stable(&mut *tables)
418+
let ct = ty::Const::from_bool(tables.tcx, value);
419+
let ty = tables.tcx.types.bool;
420+
super::convert::mir_const_from_ty_const(&mut *tables, ct, ty)
419421
}
420422

421423
fn try_new_const_uint(&self, value: u128, uint_ty: UintTy) -> Result<MirConst, Error> {
@@ -428,11 +430,8 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
428430
let scalar = ScalarInt::try_from_uint(value, size).ok_or_else(|| {
429431
Error::new(format!("Value overflow: cannot convert `{value}` to `{ty}`."))
430432
})?;
431-
Ok(mir::Const::Ty(
432-
ty,
433-
ty::Const::new_value(tables.tcx, ValTree::from_scalar_int(scalar), ty),
434-
)
435-
.stable(&mut *tables))
433+
let ct = ty::Const::new_value(tables.tcx, ValTree::from_scalar_int(scalar), ty);
434+
Ok(super::convert::mir_const_from_ty_const(&mut *tables, ct, ty))
436435
}
437436
fn try_new_ty_const_uint(
438437
&self,

compiler/rustc_smir/src/rustc_smir/convert/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ mod error;
99
mod mir;
1010
mod ty;
1111

12+
pub use ty::mir_const_from_ty_const;
13+
1214
impl<'tcx> Stable<'tcx> for rustc_hir::Safety {
1315
type T = stable_mir::mir::Safety;
1416
fn stable(&self, _: &mut Tables<'_>) -> Self::T {

compiler/rustc_smir/src/rustc_smir/convert/ty.rs

+42
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,48 @@ impl<'tcx> Stable<'tcx> for ty::Pattern<'tcx> {
409409
}
410410
}
411411

412+
pub fn mir_const_from_ty_const<'tcx>(
413+
tables: &mut Tables<'tcx>,
414+
ty_const: ty::Const<'tcx>,
415+
ty: Ty<'tcx>,
416+
) -> stable_mir::ty::MirConst {
417+
let kind = match ty_const.kind() {
418+
ty::Value(ty, val) => {
419+
let val = match val {
420+
ty::ValTree::Leaf(scalar) => ty::ValTree::Leaf(scalar),
421+
ty::ValTree::Branch(branch) => {
422+
ty::ValTree::Branch(tables.tcx.lift(branch).unwrap())
423+
}
424+
};
425+
let ty = tables.tcx.lift(ty).unwrap();
426+
let const_val = tables.tcx.valtree_to_const_val((ty, val));
427+
if matches!(const_val, mir::ConstValue::ZeroSized) {
428+
stable_mir::ty::ConstantKind::ZeroSized
429+
} else {
430+
stable_mir::ty::ConstantKind::Allocated(alloc::new_allocation(
431+
ty, const_val, tables,
432+
))
433+
}
434+
}
435+
ty::ParamCt(param) => stable_mir::ty::ConstantKind::Param(param.stable(tables)),
436+
ty::ErrorCt(_) => unreachable!(),
437+
ty::InferCt(_) => unreachable!(),
438+
ty::BoundCt(_, _) => unimplemented!(),
439+
ty::PlaceholderCt(_) => unimplemented!(),
440+
ty::Unevaluated(uv) => {
441+
stable_mir::ty::ConstantKind::Unevaluated(stable_mir::ty::UnevaluatedConst {
442+
def: tables.const_def(uv.def),
443+
args: uv.args.stable(tables),
444+
promoted: None,
445+
})
446+
}
447+
ty::ExprCt(_) => unimplemented!(),
448+
};
449+
let stable_ty = tables.intern_ty(ty);
450+
let id = tables.intern_mir_const(mir::Const::Ty(ty, ty_const));
451+
stable_mir::ty::MirConst::new(kind, stable_ty, id)
452+
}
453+
412454
impl<'tcx> Stable<'tcx> for ty::Const<'tcx> {
413455
type T = stable_mir::ty::TyConst;
414456

compiler/stable_mir/src/ty.rs

+5
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@ impl TyConst {
116116
Self { kind, id }
117117
}
118118

119+
/// Retrieve the constant kind.
120+
pub fn kind(&self) -> &TyConstKind {
121+
&self.kind
122+
}
123+
119124
/// Creates an interned usize constant.
120125
fn try_from_target_usize(val: u64) -> Result<Self, Error> {
121126
with(|cx| cx.try_new_ty_const_uint(val.into(), UintTy::Usize))

tests/ui-fulldeps/stable-mir/check_transform.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use rustc_smir::rustc_internal;
2222
use stable_mir::mir::alloc::GlobalAlloc;
2323
use stable_mir::mir::mono::Instance;
2424
use stable_mir::mir::{Body, Constant, Operand, Rvalue, StatementKind, TerminatorKind};
25-
use stable_mir::ty::{Const, ConstantKind};
25+
use stable_mir::ty::{ConstantKind, MirConst};
2626
use stable_mir::{CrateDef, CrateItems, ItemKind};
2727
use std::convert::TryFrom;
2828
use std::io::Write;
@@ -77,7 +77,7 @@ fn check_msg(body: &Body, expected: &str) {
7777
};
7878
assert_eq!(alloc.provenance.ptrs.len(), 1);
7979

80-
let alloc_prov_id = alloc.provenance.ptrs[0].1 .0;
80+
let alloc_prov_id = alloc.provenance.ptrs[0].1.0;
8181
let GlobalAlloc::Memory(val) = GlobalAlloc::from(alloc_prov_id) else {
8282
unreachable!()
8383
};
@@ -95,7 +95,7 @@ fn change_panic_msg(mut body: Body, new_msg: &str) -> Body {
9595
for bb in &mut body.blocks {
9696
match &mut bb.terminator.kind {
9797
TerminatorKind::Call { args, .. } => {
98-
let new_const = Const::from_str(new_msg);
98+
let new_const = MirConst::from_str(new_msg);
9999
args[0] = Operand::Constant(Constant {
100100
literal: new_const,
101101
span: bb.terminator.span,

0 commit comments

Comments
 (0)