Split smir `Const` into `TyConst` and `MirConst`

This commit is contained in:
Boxy 2024-06-04 00:39:35 +01:00
parent 27529d5c25
commit 7e08f80b34
14 changed files with 285 additions and 134 deletions

View File

@ -5,17 +5,17 @@
// Prefer importing stable_mir over internal rustc constructs to make this file more readable. // Prefer importing stable_mir over internal rustc constructs to make this file more readable.
use crate::rustc_smir::Tables; use crate::rustc_smir::Tables;
use rustc_middle::ty::{self as rustc_ty, Ty as InternalTy, TyCtxt}; use rustc_middle::ty::{self as rustc_ty, Const as InternalConst, Ty as InternalTy, TyCtxt};
use rustc_span::Symbol; use rustc_span::Symbol;
use stable_mir::abi::Layout; use stable_mir::abi::Layout;
use stable_mir::mir::alloc::AllocId; use stable_mir::mir::alloc::AllocId;
use stable_mir::mir::mono::{Instance, MonoItem, StaticDef}; use stable_mir::mir::mono::{Instance, MonoItem, StaticDef};
use stable_mir::mir::{BinOp, Mutability, Place, ProjectionElem, Safety, UnOp}; use stable_mir::mir::{BinOp, Mutability, Place, ProjectionElem, Safety, UnOp};
use stable_mir::ty::{ use stable_mir::ty::{
Abi, AdtDef, Binder, BoundRegionKind, BoundTyKind, BoundVariableKind, ClosureKind, Const, Abi, AdtDef, Binder, BoundRegionKind, BoundTyKind, BoundVariableKind, ClosureKind, DynKind,
DynKind, ExistentialPredicate, ExistentialProjection, ExistentialTraitRef, FloatTy, FnSig, ExistentialPredicate, ExistentialProjection, ExistentialTraitRef, FloatTy, FnSig,
GenericArgKind, GenericArgs, IndexedVal, IntTy, Movability, Pattern, Region, RigidTy, Span, GenericArgKind, GenericArgs, IndexedVal, IntTy, MirConst, Movability, Pattern, Region, RigidTy,
TermKind, TraitRef, Ty, UintTy, VariantDef, VariantIdx, Span, TermKind, TraitRef, Ty, TyConst, UintTy, VariantDef, VariantIdx,
}; };
use stable_mir::{CrateItem, CrateNum, DefId}; use stable_mir::{CrateItem, CrateNum, DefId};
@ -55,7 +55,7 @@ impl RustcInternal for GenericArgKind {
let arg: rustc_ty::GenericArg<'tcx> = match self { let arg: rustc_ty::GenericArg<'tcx> = match self {
GenericArgKind::Lifetime(reg) => reg.internal(tables, tcx).into(), GenericArgKind::Lifetime(reg) => reg.internal(tables, tcx).into(),
GenericArgKind::Type(ty) => ty.internal(tables, tcx).into(), GenericArgKind::Type(ty) => ty.internal(tables, tcx).into(),
GenericArgKind::Const(cnst) => ty_const(cnst, tables, tcx).into(), GenericArgKind::Const(cnst) => cnst.internal(tables, tcx).into(),
}; };
tcx.lift(arg).unwrap() tcx.lift(arg).unwrap()
} }
@ -76,13 +76,20 @@ impl RustcInternal for Ty {
} }
} }
impl RustcInternal for TyConst {
type T<'tcx> = InternalConst<'tcx>;
fn internal<'tcx>(&self, tables: &mut Tables<'_>, tcx: TyCtxt<'tcx>) -> Self::T<'tcx> {
tcx.lift(tables.ty_consts[self.id]).unwrap()
}
}
impl RustcInternal for Pattern { impl RustcInternal for Pattern {
type T<'tcx> = rustc_ty::Pattern<'tcx>; type T<'tcx> = rustc_ty::Pattern<'tcx>;
fn internal<'tcx>(&self, tables: &mut Tables<'_>, tcx: TyCtxt<'tcx>) -> Self::T<'tcx> { fn internal<'tcx>(&self, tables: &mut Tables<'_>, tcx: TyCtxt<'tcx>) -> Self::T<'tcx> {
tcx.mk_pat(match self { tcx.mk_pat(match self {
Pattern::Range { start, end, include_end } => rustc_ty::PatternKind::Range { Pattern::Range { start, end, include_end } => rustc_ty::PatternKind::Range {
start: start.as_ref().map(|c| ty_const(c, tables, tcx)), start: start.as_ref().map(|c| c.internal(tables, tcx)),
end: end.as_ref().map(|c| ty_const(c, tables, tcx)), end: end.as_ref().map(|c| c.internal(tables, tcx)),
include_end: *include_end, include_end: *include_end,
}, },
}) })
@ -101,7 +108,7 @@ impl RustcInternal for RigidTy {
RigidTy::Float(float_ty) => rustc_ty::TyKind::Float(float_ty.internal(tables, tcx)), RigidTy::Float(float_ty) => rustc_ty::TyKind::Float(float_ty.internal(tables, tcx)),
RigidTy::Never => rustc_ty::TyKind::Never, RigidTy::Never => rustc_ty::TyKind::Never,
RigidTy::Array(ty, cnst) => { RigidTy::Array(ty, cnst) => {
rustc_ty::TyKind::Array(ty.internal(tables, tcx), ty_const(cnst, tables, tcx)) rustc_ty::TyKind::Array(ty.internal(tables, tcx), cnst.internal(tables, tcx))
} }
RigidTy::Pat(ty, pat) => { RigidTy::Pat(ty, pat) => {
rustc_ty::TyKind::Pat(ty.internal(tables, tcx), pat.internal(tables, tcx)) rustc_ty::TyKind::Pat(ty.internal(tables, tcx), pat.internal(tables, tcx))
@ -239,23 +246,10 @@ impl RustcInternal for VariantDef {
} }
} }
fn ty_const<'tcx>( impl RustcInternal for MirConst {
constant: &Const,
tables: &mut Tables<'_>,
tcx: TyCtxt<'tcx>,
) -> rustc_ty::Const<'tcx> {
match constant.internal(tables, tcx) {
rustc_middle::mir::Const::Ty(c) => c,
cnst => {
panic!("Trying to convert constant `{constant:?}` to type constant, but found {cnst:?}")
}
}
}
impl RustcInternal for Const {
type T<'tcx> = rustc_middle::mir::Const<'tcx>; type T<'tcx> = rustc_middle::mir::Const<'tcx>;
fn internal<'tcx>(&self, tables: &mut Tables<'_>, tcx: TyCtxt<'tcx>) -> Self::T<'tcx> { fn internal<'tcx>(&self, tables: &mut Tables<'_>, tcx: TyCtxt<'tcx>) -> Self::T<'tcx> {
let constant = tables.constants[self.id]; let constant = tables.mir_consts[self.id];
match constant { match constant {
rustc_middle::mir::Const::Ty(ty) => rustc_middle::mir::Const::Ty(tcx.lift(ty).unwrap()), rustc_middle::mir::Const::Ty(ty) => rustc_middle::mir::Const::Ty(tcx.lift(ty).unwrap()),
rustc_middle::mir::Const::Unevaluated(uneval, ty) => { rustc_middle::mir::Const::Unevaluated(uneval, ty) => {
@ -392,7 +386,7 @@ impl RustcInternal for TermKind {
fn internal<'tcx>(&self, tables: &mut Tables<'_>, tcx: TyCtxt<'tcx>) -> Self::T<'tcx> { fn internal<'tcx>(&self, tables: &mut Tables<'_>, tcx: TyCtxt<'tcx>) -> Self::T<'tcx> {
match self { match self {
TermKind::Type(ty) => ty.internal(tables, tcx).into(), TermKind::Type(ty) => ty.internal(tables, tcx).into(),
TermKind::Const(const_) => ty_const(const_, tables, tcx).into(), TermKind::Const(cnst) => cnst.internal(tables, tcx).into(),
} }
} }
} }

View File

@ -214,7 +214,8 @@ where
spans: IndexMap::default(), spans: IndexMap::default(),
types: IndexMap::default(), types: IndexMap::default(),
instances: IndexMap::default(), instances: IndexMap::default(),
constants: IndexMap::default(), ty_consts: IndexMap::default(),
mir_consts: IndexMap::default(),
layouts: IndexMap::default(), layouts: IndexMap::default(),
})); }));
stable_mir::compiler_interface::run(&tables, || init(&tables, f)) stable_mir::compiler_interface::run(&tables, || init(&tables, f))

View File

@ -6,7 +6,6 @@
#![allow(rustc::usage_of_qualified_ty)] #![allow(rustc::usage_of_qualified_ty)]
use rustc_abi::HasDataLayout; use rustc_abi::HasDataLayout;
use rustc_middle::ty;
use rustc_middle::ty::layout::{ use rustc_middle::ty::layout::{
FnAbiOf, FnAbiOfHelpers, HasParamEnv, HasTyCtxt, LayoutOf, LayoutOfHelpers, FnAbiOf, FnAbiOfHelpers, HasParamEnv, HasTyCtxt, LayoutOf, LayoutOfHelpers,
}; };
@ -14,6 +13,7 @@ use rustc_middle::ty::print::{with_forced_trimmed_paths, with_no_trimmed_paths};
use rustc_middle::ty::{ use rustc_middle::ty::{
GenericPredicates, Instance, List, ParamEnv, ScalarInt, TyCtxt, TypeVisitableExt, ValTree, GenericPredicates, Instance, List, ParamEnv, ScalarInt, TyCtxt, TypeVisitableExt, ValTree,
}; };
use rustc_middle::{mir, ty};
use rustc_span::def_id::LOCAL_CRATE; use rustc_span::def_id::LOCAL_CRATE;
use stable_mir::abi::{FnAbi, Layout, LayoutShape}; use stable_mir::abi::{FnAbi, Layout, LayoutShape};
use stable_mir::compiler_interface::Context; use stable_mir::compiler_interface::Context;
@ -22,9 +22,9 @@ use stable_mir::mir::mono::{InstanceDef, StaticDef};
use stable_mir::mir::{BinOp, Body, Place, UnOp}; use stable_mir::mir::{BinOp, Body, Place, UnOp};
use stable_mir::target::{MachineInfo, MachineSize}; use stable_mir::target::{MachineInfo, MachineSize};
use stable_mir::ty::{ use stable_mir::ty::{
AdtDef, AdtKind, Allocation, ClosureDef, ClosureKind, Const, FieldDef, FnDef, ForeignDef, AdtDef, AdtKind, Allocation, ClosureDef, ClosureKind, FieldDef, FnDef, ForeignDef,
ForeignItemKind, GenericArgs, IntrinsicDef, LineInfo, PolyFnSig, RigidTy, Span, Ty, TyKind, ForeignItemKind, GenericArgs, IntrinsicDef, LineInfo, MirConst, PolyFnSig, RigidTy, Span, Ty,
UintTy, VariantDef, TyConst, TyKind, UintTy, VariantDef,
}; };
use stable_mir::{Crate, CrateDef, CrateItem, CrateNum, DefId, Error, Filename, ItemKind, Symbol}; use stable_mir::{Crate, CrateDef, CrateItem, CrateNum, DefId, Error, Filename, ItemKind, Symbol};
use std::cell::RefCell; use std::cell::RefCell;
@ -360,7 +360,15 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
def.internal(&mut *tables, tcx).fields.iter().map(|f| f.stable(&mut *tables)).collect() def.internal(&mut *tables, tcx).fields.iter().map(|f| f.stable(&mut *tables)).collect()
} }
fn eval_target_usize(&self, cnst: &Const) -> Result<u64, Error> { fn eval_target_usize(&self, cnst: &MirConst) -> Result<u64, Error> {
let mut tables = self.0.borrow_mut();
let tcx = tables.tcx;
let mir_const = cnst.internal(&mut *tables, tcx);
mir_const
.try_eval_target_usize(tables.tcx, ParamEnv::empty())
.ok_or_else(|| Error::new(format!("Const `{cnst:?}` cannot be encoded as u64")))
}
fn eval_target_usize_ty(&self, cnst: &TyConst) -> Result<u64, Error> {
let mut tables = self.0.borrow_mut(); let mut tables = self.0.borrow_mut();
let tcx = tables.tcx; let tcx = tables.tcx;
let mir_const = cnst.internal(&mut *tables, tcx); let mir_const = cnst.internal(&mut *tables, tcx);
@ -369,7 +377,7 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
.ok_or_else(|| Error::new(format!("Const `{cnst:?}` cannot be encoded as u64"))) .ok_or_else(|| Error::new(format!("Const `{cnst:?}` cannot be encoded as u64")))
} }
fn try_new_const_zst(&self, ty: Ty) -> Result<Const, Error> { fn try_new_const_zst(&self, ty: Ty) -> Result<MirConst, Error> {
let mut tables = self.0.borrow_mut(); let mut tables = self.0.borrow_mut();
let tcx = tables.tcx; let tcx = tables.tcx;
let ty_internal = ty.internal(&mut *tables, tcx); let ty_internal = ty.internal(&mut *tables, tcx);
@ -390,25 +398,45 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
))); )));
} }
Ok(ty::Const::zero_sized(tables.tcx, ty_internal).stable(&mut *tables)) Ok(mir::Const::Ty(ty::Const::zero_sized(tables.tcx, ty_internal)).stable(&mut *tables))
} }
fn new_const_str(&self, value: &str) -> Const { fn new_const_str(&self, value: &str) -> MirConst {
let mut tables = self.0.borrow_mut(); let mut tables = self.0.borrow_mut();
let tcx = tables.tcx; let tcx = tables.tcx;
let ty = ty::Ty::new_static_str(tcx); let ty = ty::Ty::new_static_str(tcx);
let bytes = value.as_bytes(); let bytes = value.as_bytes();
let val_tree = ty::ValTree::from_raw_bytes(tcx, bytes); let val_tree = ty::ValTree::from_raw_bytes(tcx, bytes);
ty::Const::new_value(tcx, val_tree, ty).stable(&mut *tables) let ct = ty::Const::new_value(tcx, val_tree, ty);
super::convert::mir_const_from_ty_const(&mut *tables, ct, ty)
} }
fn new_const_bool(&self, value: bool) -> Const { fn new_const_bool(&self, value: bool) -> MirConst {
let mut tables = self.0.borrow_mut(); let mut tables = self.0.borrow_mut();
ty::Const::from_bool(tables.tcx, value).stable(&mut *tables) let ct = ty::Const::from_bool(tables.tcx, value);
let ty = tables.tcx.types.bool;
super::convert::mir_const_from_ty_const(&mut *tables, ct, ty)
} }
fn try_new_const_uint(&self, value: u128, uint_ty: UintTy) -> Result<Const, Error> { fn try_new_const_uint(&self, value: u128, uint_ty: UintTy) -> Result<MirConst, Error> {
let mut tables = self.0.borrow_mut();
let tcx = tables.tcx;
let ty = ty::Ty::new_uint(tcx, uint_ty.internal(&mut *tables, tcx));
let size = tables.tcx.layout_of(ParamEnv::empty().and(ty)).unwrap().size;
// We don't use Const::from_bits since it doesn't have any error checking.
let scalar = ScalarInt::try_from_uint(value, size).ok_or_else(|| {
Error::new(format!("Value overflow: cannot convert `{value}` to `{ty}`."))
})?;
let ct = ty::Const::new_value(tables.tcx, ValTree::from_scalar_int(scalar), ty);
Ok(super::convert::mir_const_from_ty_const(&mut *tables, ct, ty))
}
fn try_new_ty_const_uint(
&self,
value: u128,
uint_ty: UintTy,
) -> Result<stable_mir::ty::TyConst, Error> {
let mut tables = self.0.borrow_mut(); let mut tables = self.0.borrow_mut();
let tcx = tables.tcx; let tcx = tables.tcx;
let ty = ty::Ty::new_uint(tcx, uint_ty.internal(&mut *tables, tcx)); let ty = ty::Ty::new_uint(tcx, uint_ty.internal(&mut *tables, tcx));
@ -453,7 +481,7 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
.stable(&mut *tables) .stable(&mut *tables)
} }
fn const_pretty(&self, cnst: &stable_mir::ty::Const) -> String { fn mir_const_pretty(&self, cnst: &stable_mir::ty::MirConst) -> String {
let mut tables = self.0.borrow_mut(); let mut tables = self.0.borrow_mut();
let tcx = tables.tcx; let tcx = tables.tcx;
cnst.internal(&mut *tables, tcx).to_string() cnst.internal(&mut *tables, tcx).to_string()
@ -474,6 +502,11 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
tables.types[ty].kind().stable(&mut *tables) tables.types[ty].kind().stable(&mut *tables)
} }
fn ty_const_pretty(&self, ct: stable_mir::ty::TyConstId) -> String {
let tables = self.0.borrow_mut();
tables.ty_consts[ct].to_string()
}
fn rigid_ty_discriminant_ty(&self, ty: &RigidTy) -> stable_mir::ty::Ty { fn rigid_ty_discriminant_ty(&self, ty: &RigidTy) -> stable_mir::ty::Ty {
let mut tables = self.0.borrow_mut(); let mut tables = self.0.borrow_mut();
let tcx = tables.tcx; let tcx = tables.tcx;

View File

@ -6,7 +6,7 @@ use rustc_middle::mir::interpret::alloc_range;
use rustc_middle::mir::mono::MonoItem; use rustc_middle::mir::mono::MonoItem;
use stable_mir::mir::alloc::GlobalAlloc; use stable_mir::mir::alloc::GlobalAlloc;
use stable_mir::mir::{ConstOperand, Statement, UserTypeProjection, VarDebugInfoFragment}; use stable_mir::mir::{ConstOperand, Statement, UserTypeProjection, VarDebugInfoFragment};
use stable_mir::ty::{Allocation, Const, ConstantKind}; use stable_mir::ty::{Allocation, ConstantKind, MirConst};
use stable_mir::{opaque, Error}; use stable_mir::{opaque, Error};
use crate::rustc_smir::{alloc, Stable, Tables}; use crate::rustc_smir::{alloc, Stable, Tables};
@ -724,11 +724,16 @@ impl<'tcx> Stable<'tcx> for mir::interpret::GlobalAlloc<'tcx> {
} }
impl<'tcx> Stable<'tcx> for rustc_middle::mir::Const<'tcx> { impl<'tcx> Stable<'tcx> for rustc_middle::mir::Const<'tcx> {
type T = stable_mir::ty::Const; type T = stable_mir::ty::MirConst;
fn stable(&self, tables: &mut Tables<'_>) -> Self::T { fn stable(&self, tables: &mut Tables<'_>) -> Self::T {
let id = tables.intern_mir_const(tables.tcx.lift(*self).unwrap());
match *self { match *self {
mir::Const::Ty(c) => c.stable(tables), mir::Const::Ty(c) => MirConst::new(
stable_mir::ty::ConstantKind::Ty(c.stable(tables)),
c.ty().stable(tables),
id,
),
mir::Const::Unevaluated(unev_const, ty) => { mir::Const::Unevaluated(unev_const, ty) => {
let kind = let kind =
stable_mir::ty::ConstantKind::Unevaluated(stable_mir::ty::UnevaluatedConst { stable_mir::ty::ConstantKind::Unevaluated(stable_mir::ty::UnevaluatedConst {
@ -737,21 +742,18 @@ impl<'tcx> Stable<'tcx> for rustc_middle::mir::Const<'tcx> {
promoted: unev_const.promoted.map(|u| u.as_u32()), promoted: unev_const.promoted.map(|u| u.as_u32()),
}); });
let ty = ty.stable(tables); let ty = ty.stable(tables);
let id = tables.intern_const(tables.tcx.lift(*self).unwrap()); MirConst::new(kind, ty, id)
Const::new(kind, ty, id)
} }
mir::Const::Val(mir::ConstValue::ZeroSized, ty) => { mir::Const::Val(mir::ConstValue::ZeroSized, ty) => {
let ty = ty.stable(tables); let ty = ty.stable(tables);
let id = tables.intern_const(tables.tcx.lift(*self).unwrap()); MirConst::new(ConstantKind::ZeroSized, ty, id)
Const::new(ConstantKind::ZeroSized, ty, id)
} }
mir::Const::Val(val, ty) => { mir::Const::Val(val, ty) => {
let ty = tables.tcx.lift(ty).unwrap(); let ty = tables.tcx.lift(ty).unwrap();
let val = tables.tcx.lift(val).unwrap(); let val = tables.tcx.lift(val).unwrap();
let kind = ConstantKind::Allocated(alloc::new_allocation(ty, val, tables)); let kind = ConstantKind::Allocated(alloc::new_allocation(ty, val, tables));
let ty = ty.stable(tables); let ty = ty.stable(tables);
let id = tables.intern_const(tables.tcx.lift(*self).unwrap()); MirConst::new(kind, ty, id)
Const::new(kind, ty, id)
} }
} }
} }

View File

@ -9,6 +9,8 @@ mod error;
mod mir; mod mir;
mod ty; mod ty;
pub use ty::mir_const_from_ty_const;
impl<'tcx> Stable<'tcx> for rustc_hir::Safety { impl<'tcx> Stable<'tcx> for rustc_hir::Safety {
type T = stable_mir::mir::Safety; type T = stable_mir::mir::Safety;
fn stable(&self, _: &mut Tables<'_>) -> Self::T { fn stable(&self, _: &mut Tables<'_>) -> Self::T {

View File

@ -3,8 +3,7 @@
use rustc_middle::ty::Ty; use rustc_middle::ty::Ty;
use rustc_middle::{mir, ty}; use rustc_middle::{mir, ty};
use stable_mir::ty::{ use stable_mir::ty::{
AdtKind, Const, ConstantKind, FloatTy, GenericArgs, GenericParamDef, IntTy, Region, RigidTy, AdtKind, FloatTy, GenericArgs, GenericParamDef, IntTy, Region, RigidTy, TyKind, UintTy,
TyKind, UintTy,
}; };
use crate::rustc_smir::{alloc, Stable, Tables}; use crate::rustc_smir::{alloc, Stable, Tables};
@ -410,8 +409,50 @@ impl<'tcx> Stable<'tcx> for ty::Pattern<'tcx> {
} }
} }
pub fn mir_const_from_ty_const<'tcx>(
tables: &mut Tables<'tcx>,
ty_const: ty::Const<'tcx>,
ty: Ty<'tcx>,
) -> stable_mir::ty::MirConst {
let kind = match ty_const.kind() {
ty::Value(val) => {
let val = match val {
ty::ValTree::Leaf(scalar) => ty::ValTree::Leaf(scalar),
ty::ValTree::Branch(branch) => {
ty::ValTree::Branch(tables.tcx.lift(branch).unwrap())
}
};
let ty = tables.tcx.lift(ty).unwrap();
let const_val = tables.tcx.valtree_to_const_val((ty, val));
if matches!(const_val, mir::ConstValue::ZeroSized) {
stable_mir::ty::ConstantKind::ZeroSized
} else {
stable_mir::ty::ConstantKind::Allocated(alloc::new_allocation(
ty, const_val, tables,
))
}
}
ty::ParamCt(param) => stable_mir::ty::ConstantKind::Param(param.stable(tables)),
ty::ErrorCt(_) => unreachable!(),
ty::InferCt(_) => unreachable!(),
ty::BoundCt(_, _) => unimplemented!(),
ty::PlaceholderCt(_) => unimplemented!(),
ty::Unevaluated(uv) => {
stable_mir::ty::ConstantKind::Unevaluated(stable_mir::ty::UnevaluatedConst {
def: tables.const_def(uv.def),
args: uv.args.stable(tables),
promoted: None,
})
}
ty::ExprCt(_) => unimplemented!(),
};
let stable_ty = tables.intern_ty(ty);
let id = tables.intern_mir_const(mir::Const::Ty(ty_const));
stable_mir::ty::MirConst::new(kind, stable_ty, id)
}
impl<'tcx> Stable<'tcx> for ty::Const<'tcx> { impl<'tcx> Stable<'tcx> for ty::Const<'tcx> {
type T = stable_mir::ty::Const; type T = stable_mir::ty::TyConst;
fn stable(&self, tables: &mut Tables<'_>) -> Self::T { fn stable(&self, tables: &mut Tables<'_>) -> Self::T {
let kind = match self.kind() { let kind = match self.kind() {
@ -425,30 +466,27 @@ impl<'tcx> Stable<'tcx> for ty::Const<'tcx> {
let ty = tables.tcx.lift(self.ty()).unwrap(); let ty = tables.tcx.lift(self.ty()).unwrap();
let const_val = tables.tcx.valtree_to_const_val((ty, val)); let const_val = tables.tcx.valtree_to_const_val((ty, val));
if matches!(const_val, mir::ConstValue::ZeroSized) { if matches!(const_val, mir::ConstValue::ZeroSized) {
ConstantKind::ZeroSized stable_mir::ty::TyConstKind::ZSTValue(ty.stable(tables))
} else { } else {
stable_mir::ty::ConstantKind::Allocated(alloc::new_allocation( stable_mir::ty::TyConstKind::Value(
ty, const_val, tables, ty.stable(tables),
)) alloc::new_allocation(ty, const_val, tables),
)
} }
} }
ty::ParamCt(param) => stable_mir::ty::ConstantKind::Param(param.stable(tables)), ty::ParamCt(param) => stable_mir::ty::TyConstKind::Param(param.stable(tables)),
ty::Unevaluated(uv) => stable_mir::ty::TyConstKind::Unevaluated(
tables.const_def(uv.def),
uv.args.stable(tables),
),
ty::ErrorCt(_) => unreachable!(), ty::ErrorCt(_) => unreachable!(),
ty::InferCt(_) => unreachable!(), ty::InferCt(_) => unreachable!(),
ty::BoundCt(_, _) => unimplemented!(), ty::BoundCt(_, _) => unimplemented!(),
ty::PlaceholderCt(_) => unimplemented!(), ty::PlaceholderCt(_) => unimplemented!(),
ty::Unevaluated(uv) => {
stable_mir::ty::ConstantKind::Unevaluated(stable_mir::ty::UnevaluatedConst {
def: tables.const_def(uv.def),
args: uv.args.stable(tables),
promoted: None,
})
}
ty::ExprCt(_) => unimplemented!(), ty::ExprCt(_) => unimplemented!(),
}; };
let ty = self.ty().stable(tables); let id = tables.intern_ty_const(tables.tcx.lift(*self).unwrap());
let id = tables.intern_const(mir::Const::Ty(tables.tcx.lift(*self).unwrap())); stable_mir::ty::TyConst::new(kind, id)
Const::new(kind, ty, id)
} }
} }

View File

@ -14,7 +14,7 @@ use rustc_middle::ty::{self, Instance, Ty, TyCtxt};
use rustc_span::def_id::{CrateNum, DefId, LOCAL_CRATE}; use rustc_span::def_id::{CrateNum, DefId, LOCAL_CRATE};
use stable_mir::abi::Layout; use stable_mir::abi::Layout;
use stable_mir::mir::mono::InstanceDef; use stable_mir::mir::mono::InstanceDef;
use stable_mir::ty::{ConstId, Span}; use stable_mir::ty::{MirConstId, Span, TyConstId};
use stable_mir::{CtorKind, ItemKind}; use stable_mir::{CtorKind, ItemKind};
use std::ops::RangeInclusive; use std::ops::RangeInclusive;
use tracing::debug; use tracing::debug;
@ -33,7 +33,8 @@ pub struct Tables<'tcx> {
pub(crate) spans: IndexMap<rustc_span::Span, Span>, pub(crate) spans: IndexMap<rustc_span::Span, Span>,
pub(crate) types: IndexMap<Ty<'tcx>, stable_mir::ty::Ty>, pub(crate) types: IndexMap<Ty<'tcx>, stable_mir::ty::Ty>,
pub(crate) instances: IndexMap<ty::Instance<'tcx>, InstanceDef>, pub(crate) instances: IndexMap<ty::Instance<'tcx>, InstanceDef>,
pub(crate) constants: IndexMap<mir::Const<'tcx>, ConstId>, pub(crate) ty_consts: IndexMap<ty::Const<'tcx>, TyConstId>,
pub(crate) mir_consts: IndexMap<mir::Const<'tcx>, MirConstId>,
pub(crate) layouts: IndexMap<rustc_target::abi::Layout<'tcx>, Layout>, pub(crate) layouts: IndexMap<rustc_target::abi::Layout<'tcx>, Layout>,
} }
@ -42,8 +43,12 @@ impl<'tcx> Tables<'tcx> {
self.types.create_or_fetch(ty) self.types.create_or_fetch(ty)
} }
pub(crate) fn intern_const(&mut self, constant: mir::Const<'tcx>) -> ConstId { pub(crate) fn intern_ty_const(&mut self, ct: ty::Const<'tcx>) -> TyConstId {
self.constants.create_or_fetch(constant) self.ty_consts.create_or_fetch(ct)
}
pub(crate) fn intern_mir_const(&mut self, constant: mir::Const<'tcx>) -> MirConstId {
self.mir_consts.create_or_fetch(constant)
} }
pub(crate) fn has_body(&self, instance: Instance<'tcx>) -> bool { pub(crate) fn has_body(&self, instance: Instance<'tcx>) -> bool {

View File

@ -11,10 +11,10 @@ use crate::mir::mono::{Instance, InstanceDef, StaticDef};
use crate::mir::{BinOp, Body, Place, UnOp}; use crate::mir::{BinOp, Body, Place, UnOp};
use crate::target::MachineInfo; use crate::target::MachineInfo;
use crate::ty::{ use crate::ty::{
AdtDef, AdtKind, Allocation, ClosureDef, ClosureKind, Const, FieldDef, FnDef, ForeignDef, AdtDef, AdtKind, Allocation, ClosureDef, ClosureKind, FieldDef, FnDef, ForeignDef,
ForeignItemKind, ForeignModule, ForeignModuleDef, GenericArgs, GenericPredicates, Generics, ForeignItemKind, ForeignModule, ForeignModuleDef, GenericArgs, GenericPredicates, Generics,
ImplDef, ImplTrait, IntrinsicDef, LineInfo, PolyFnSig, RigidTy, Span, TraitDecl, TraitDef, Ty, ImplDef, ImplTrait, IntrinsicDef, LineInfo, MirConst, PolyFnSig, RigidTy, Span, TraitDecl,
TyKind, UintTy, VariantDef, TraitDef, Ty, TyConst, TyConstId, TyKind, UintTy, VariantDef,
}; };
use crate::{ use crate::{
mir, Crate, CrateItem, CrateItems, CrateNum, DefId, Error, Filename, ImplTraitDecls, ItemKind, mir, Crate, CrateItem, CrateItems, CrateNum, DefId, Error, Filename, ImplTraitDecls, ItemKind,
@ -109,19 +109,21 @@ pub trait Context {
fn variant_fields(&self, def: VariantDef) -> Vec<FieldDef>; fn variant_fields(&self, def: VariantDef) -> Vec<FieldDef>;
/// Evaluate constant as a target usize. /// Evaluate constant as a target usize.
fn eval_target_usize(&self, cnst: &Const) -> Result<u64, Error>; fn eval_target_usize(&self, cnst: &MirConst) -> Result<u64, Error>;
fn eval_target_usize_ty(&self, cnst: &TyConst) -> Result<u64, Error>;
/// Create a new zero-sized constant. /// Create a new zero-sized constant.
fn try_new_const_zst(&self, ty: Ty) -> Result<Const, Error>; fn try_new_const_zst(&self, ty: Ty) -> Result<MirConst, Error>;
/// Create a new constant that represents the given string value. /// Create a new constant that represents the given string value.
fn new_const_str(&self, value: &str) -> Const; fn new_const_str(&self, value: &str) -> MirConst;
/// Create a new constant that represents the given boolean value. /// Create a new constant that represents the given boolean value.
fn new_const_bool(&self, value: bool) -> Const; fn new_const_bool(&self, value: bool) -> MirConst;
/// Create a new constant that represents the given value. /// Create a new constant that represents the given value.
fn try_new_const_uint(&self, value: u128, uint_ty: UintTy) -> Result<Const, Error>; fn try_new_const_uint(&self, value: u128, uint_ty: UintTy) -> Result<MirConst, Error>;
fn try_new_ty_const_uint(&self, value: u128, uint_ty: UintTy) -> Result<TyConst, Error>;
/// Create a new type from the given kind. /// Create a new type from the given kind.
fn new_rigid_ty(&self, kind: RigidTy) -> Ty; fn new_rigid_ty(&self, kind: RigidTy) -> Ty;
@ -136,11 +138,13 @@ pub trait Context {
fn def_ty_with_args(&self, item: DefId, args: &GenericArgs) -> Ty; fn def_ty_with_args(&self, item: DefId, args: &GenericArgs) -> Ty;
/// Returns literal value of a const as a string. /// Returns literal value of a const as a string.
fn const_pretty(&self, cnst: &Const) -> String; fn mir_const_pretty(&self, cnst: &MirConst) -> String;
/// `Span` of an item /// `Span` of an item
fn span_of_an_item(&self, def_id: DefId) -> Span; fn span_of_an_item(&self, def_id: DefId) -> Span;
fn ty_const_pretty(&self, ct: TyConstId) -> String;
/// Obtain the representation of a type. /// Obtain the representation of a type.
fn ty_pretty(&self, ty: Ty) -> String; fn ty_pretty(&self, ty: Ty) -> String;

View File

@ -1,8 +1,8 @@
use crate::compiler_interface::with; use crate::compiler_interface::with;
use crate::mir::pretty::function_body; use crate::mir::pretty::function_body;
use crate::ty::{ use crate::ty::{
AdtDef, ClosureDef, Const, CoroutineDef, GenericArgs, Movability, Region, RigidTy, Ty, TyKind, AdtDef, ClosureDef, CoroutineDef, GenericArgs, MirConst, Movability, Region, RigidTy, Ty,
VariantIdx, TyConst, TyKind, VariantIdx,
}; };
use crate::{Error, Opaque, Span, Symbol}; use crate::{Error, Opaque, Span, Symbol};
use std::io; use std::io;
@ -524,7 +524,7 @@ pub enum Rvalue {
/// Corresponds to source code like `[x; 32]`. /// Corresponds to source code like `[x; 32]`.
/// ///
/// [#74836]: https://github.com/rust-lang/rust/issues/74836 /// [#74836]: https://github.com/rust-lang/rust/issues/74836
Repeat(Operand, Const), Repeat(Operand, TyConst),
/// Transmutes a `*mut u8` into shallow-initialized `Box<T>`. /// Transmutes a `*mut u8` into shallow-initialized `Box<T>`.
/// ///
@ -718,7 +718,7 @@ pub enum VarDebugInfoContents {
pub struct ConstOperand { pub struct ConstOperand {
pub span: Span, pub span: Span,
pub user_ty: Option<UserTypeAnnotationIndex>, pub user_ty: Option<UserTypeAnnotationIndex>,
pub const_: Const, pub const_: MirConst,
} }
// In MIR ProjectionElem is parameterized on the second Field argument and the Index argument. This // In MIR ProjectionElem is parameterized on the second Field argument and the Index argument. This
@ -833,7 +833,7 @@ type UserTypeAnnotationIndex = usize;
pub struct Constant { pub struct Constant {
pub span: Span, pub span: Span,
pub user_ty: Option<UserTypeAnnotationIndex>, pub user_ty: Option<UserTypeAnnotationIndex>,
pub literal: Const, pub literal: MirConst,
} }
/// The possible branch sites of a [TerminatorKind::SwitchInt]. /// The possible branch sites of a [TerminatorKind::SwitchInt].

View File

@ -1,5 +1,5 @@
use crate::mir::{Operand, Place, Rvalue, StatementKind, UnwindAction, VarDebugInfoContents}; use crate::mir::{Operand, Place, Rvalue, StatementKind, UnwindAction, VarDebugInfoContents};
use crate::ty::{Const, IndexedVal, Ty}; use crate::ty::{IndexedVal, MirConst, Ty, TyConst};
use crate::{with, Body, Mutability}; use crate::{with, Body, Mutability};
use fmt::{Display, Formatter}; use fmt::{Display, Formatter};
use std::fmt::Debug; use std::fmt::Debug;
@ -46,7 +46,7 @@ pub(crate) fn function_body<W: Write>(writer: &mut W, body: &Body, name: &str) -
VarDebugInfoContents::Place(place) => { VarDebugInfoContents::Place(place) => {
format!("{place:?}") format!("{place:?}")
} }
VarDebugInfoContents::Const(constant) => pretty_const(&constant.const_), VarDebugInfoContents::Const(constant) => pretty_mir_const(&constant.const_),
}; };
writeln!(writer, " debug {} => {};", info.name, content) writeln!(writer, " debug {} => {};", info.name, content)
})?; })?;
@ -310,12 +310,16 @@ fn pretty_operand(operand: &Operand) -> String {
Operand::Move(mv) => { Operand::Move(mv) => {
format!("move {:?}", mv) format!("move {:?}", mv)
} }
Operand::Constant(cnst) => pretty_const(&cnst.literal), Operand::Constant(cnst) => pretty_mir_const(&cnst.literal),
} }
} }
fn pretty_const(literal: &Const) -> String { fn pretty_mir_const(literal: &MirConst) -> String {
with(|cx| cx.const_pretty(literal)) with(|cx| cx.mir_const_pretty(literal))
}
fn pretty_ty_const(ct: &TyConst) -> String {
with(|cx| cx.ty_const_pretty(ct.id))
} }
fn pretty_rvalue<W: Write>(writer: &mut W, rval: &Rvalue) -> io::Result<()> { fn pretty_rvalue<W: Write>(writer: &mut W, rval: &Rvalue) -> io::Result<()> {
@ -359,7 +363,7 @@ fn pretty_rvalue<W: Write>(writer: &mut W, rval: &Rvalue) -> io::Result<()> {
write!(writer, "{kind}{:?}", place) write!(writer, "{kind}{:?}", place)
} }
Rvalue::Repeat(op, cnst) => { Rvalue::Repeat(op, cnst) => {
write!(writer, "{} \" \" {}", &pretty_operand(op), cnst.ty()) write!(writer, "{} \" \" {}", &pretty_operand(op), &pretty_ty_const(cnst))
} }
Rvalue::ShallowInitBox(_, _) => Ok(()), Rvalue::ShallowInitBox(_, _) => Ok(()),
Rvalue::ThreadLocalRef(item) => { Rvalue::ThreadLocalRef(item) => {

View File

@ -36,7 +36,7 @@
//! variant argument) that does not require visiting. //! variant argument) that does not require visiting.
use crate::mir::*; use crate::mir::*;
use crate::ty::{Const, GenericArgs, Region, Ty}; use crate::ty::{GenericArgs, MirConst, Region, Ty, TyConst};
use crate::{Error, Opaque, Span}; use crate::{Error, Opaque, Span};
pub trait MirVisitor { pub trait MirVisitor {
@ -112,8 +112,13 @@ pub trait MirVisitor {
self.super_constant(constant, location) self.super_constant(constant, location)
} }
fn visit_const(&mut self, constant: &Const, location: Location) { fn visit_mir_const(&mut self, constant: &MirConst, location: Location) {
self.super_const(constant, location) self.super_mir_const(constant, location)
}
fn visit_ty_const(&mut self, constant: &TyConst, location: Location) {
let _ = location;
self.super_ty_const(constant)
} }
fn visit_region(&mut self, region: &Region, location: Location) { fn visit_region(&mut self, region: &Region, location: Location) {
@ -339,7 +344,7 @@ pub trait MirVisitor {
} }
Rvalue::Repeat(op, constant) => { Rvalue::Repeat(op, constant) => {
self.visit_operand(op, location); self.visit_operand(op, location);
self.visit_const(constant, location); self.visit_ty_const(constant, location);
} }
Rvalue::ShallowInitBox(op, ty) => { Rvalue::ShallowInitBox(op, ty) => {
self.visit_ty(ty, location); self.visit_ty(ty, location);
@ -378,14 +383,18 @@ pub trait MirVisitor {
fn super_constant(&mut self, constant: &Constant, location: Location) { fn super_constant(&mut self, constant: &Constant, location: Location) {
let Constant { span, user_ty: _, literal } = constant; let Constant { span, user_ty: _, literal } = constant;
self.visit_span(span); self.visit_span(span);
self.visit_const(literal, location); self.visit_mir_const(literal, location);
} }
fn super_const(&mut self, constant: &Const, location: Location) { fn super_mir_const(&mut self, constant: &MirConst, location: Location) {
let Const { kind: _, ty, id: _ } = constant; let MirConst { kind: _, ty, id: _ } = constant;
self.visit_ty(ty, location); self.visit_ty(ty, location);
} }
fn super_ty_const(&mut self, constant: &TyConst) {
let _ = constant;
}
fn super_region(&mut self, region: &Region) { fn super_region(&mut self, region: &Region) {
let _ = region; let _ = region;
} }
@ -407,7 +416,7 @@ pub trait MirVisitor {
self.visit_place(place, PlaceContext::NON_USE, location); self.visit_place(place, PlaceContext::NON_USE, location);
} }
VarDebugInfoContents::Const(constant) => { VarDebugInfoContents::Const(constant) => {
self.visit_const(&constant.const_, location); self.visit_mir_const(&constant.const_, location);
} }
} }
} }

View File

@ -28,11 +28,11 @@ impl Ty {
/// Create a new array type. /// Create a new array type.
pub fn try_new_array(elem_ty: Ty, size: u64) -> Result<Ty, Error> { pub fn try_new_array(elem_ty: Ty, size: u64) -> Result<Ty, Error> {
Ok(Ty::from_rigid_kind(RigidTy::Array(elem_ty, Const::try_from_target_usize(size)?))) Ok(Ty::from_rigid_kind(RigidTy::Array(elem_ty, TyConst::try_from_target_usize(size)?)))
} }
/// Create a new array type from Const length. /// Create a new array type from Const length.
pub fn new_array_with_const_len(elem_ty: Ty, len: Const) -> Ty { pub fn new_array_with_const_len(elem_ty: Ty, len: TyConst) -> Ty {
Ty::from_rigid_kind(RigidTy::Array(elem_ty, len)) Ty::from_rigid_kind(RigidTy::Array(elem_ty, len))
} }
@ -101,24 +101,66 @@ impl Ty {
/// Represents a pattern in the type system /// Represents a pattern in the type system
#[derive(Clone, Debug, Eq, PartialEq)] #[derive(Clone, Debug, Eq, PartialEq)]
pub enum Pattern { pub enum Pattern {
Range { start: Option<Const>, end: Option<Const>, include_end: bool }, Range { start: Option<TyConst>, end: Option<TyConst>, include_end: bool },
} }
/// Represents a constant in MIR or from the Type system. /// Represents a constant in the type system
#[derive(Clone, Debug, Eq, PartialEq)] #[derive(Clone, Debug, Eq, PartialEq)]
pub struct Const { pub struct TyConst {
pub(crate) kind: TyConstKind,
pub id: TyConstId,
}
impl TyConst {
pub fn new(kind: TyConstKind, id: TyConstId) -> TyConst {
Self { kind, id }
}
/// Retrieve the constant kind.
pub fn kind(&self) -> &TyConstKind {
&self.kind
}
/// Creates an interned usize constant.
fn try_from_target_usize(val: u64) -> Result<Self, Error> {
with(|cx| cx.try_new_ty_const_uint(val.into(), UintTy::Usize))
}
/// Try to evaluate to a target `usize`.
pub fn eval_target_usize(&self) -> Result<u64, Error> {
with(|cx| cx.eval_target_usize_ty(self))
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum TyConstKind {
Param(ParamConst),
Bound(DebruijnIndex, BoundVar),
Unevaluated(ConstDef, GenericArgs),
// FIXME: These should be a valtree
Value(Ty, Allocation),
ZSTValue(Ty),
}
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct TyConstId(usize);
/// Represents a constant in MIR
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct MirConst {
/// The constant kind. /// The constant kind.
pub(crate) kind: ConstantKind, pub(crate) kind: ConstantKind,
/// The constant type. /// The constant type.
pub(crate) ty: Ty, pub(crate) ty: Ty,
/// Used for internal tracking of the internal constant. /// Used for internal tracking of the internal constant.
pub id: ConstId, pub id: MirConstId,
} }
impl Const { impl MirConst {
/// Build a constant. Note that this should only be used by the compiler. /// Build a constant. Note that this should only be used by the compiler.
pub fn new(kind: ConstantKind, ty: Ty, id: ConstId) -> Const { pub fn new(kind: ConstantKind, ty: Ty, id: MirConstId) -> MirConst {
Const { kind, ty, id } MirConst { kind, ty, id }
} }
/// Retrieve the constant kind. /// Retrieve the constant kind.
@ -131,11 +173,6 @@ impl Const {
self.ty self.ty
} }
/// Creates an interned usize constant.
fn try_from_target_usize(val: u64) -> Result<Self, Error> {
with(|cx| cx.try_new_const_uint(val.into(), UintTy::Usize))
}
/// Try to evaluate to a target `usize`. /// Try to evaluate to a target `usize`.
pub fn eval_target_usize(&self) -> Result<u64, Error> { pub fn eval_target_usize(&self) -> Result<u64, Error> {
with(|cx| cx.eval_target_usize(self)) with(|cx| cx.eval_target_usize(self))
@ -143,7 +180,7 @@ impl Const {
/// Create a constant that represents a new zero-sized constant of type T. /// Create a constant that represents a new zero-sized constant of type T.
/// Fails if the type is not a ZST or if it doesn't have a known size. /// Fails if the type is not a ZST or if it doesn't have a known size.
pub fn try_new_zero_sized(ty: Ty) -> Result<Const, Error> { pub fn try_new_zero_sized(ty: Ty) -> Result<MirConst, Error> {
with(|cx| cx.try_new_const_zst(ty)) with(|cx| cx.try_new_const_zst(ty))
} }
@ -152,23 +189,23 @@ impl Const {
/// Note that there is no guarantee today about duplication of the same constant. /// Note that there is no guarantee today about duplication of the same constant.
/// I.e.: Calling this function multiple times with the same argument may or may not return /// I.e.: Calling this function multiple times with the same argument may or may not return
/// the same allocation. /// the same allocation.
pub fn from_str(value: &str) -> Const { pub fn from_str(value: &str) -> MirConst {
with(|cx| cx.new_const_str(value)) with(|cx| cx.new_const_str(value))
} }
/// Build a new constant that represents the given boolean value. /// Build a new constant that represents the given boolean value.
pub fn from_bool(value: bool) -> Const { pub fn from_bool(value: bool) -> MirConst {
with(|cx| cx.new_const_bool(value)) with(|cx| cx.new_const_bool(value))
} }
/// Build a new constant that represents the given unsigned integer. /// Build a new constant that represents the given unsigned integer.
pub fn try_from_uint(value: u128, uint_ty: UintTy) -> Result<Const, Error> { pub fn try_from_uint(value: u128, uint_ty: UintTy) -> Result<MirConst, Error> {
with(|cx| cx.try_new_const_uint(value, uint_ty)) with(|cx| cx.try_new_const_uint(value, uint_ty))
} }
} }
#[derive(Clone, Copy, Debug, PartialEq, Eq)] #[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ConstId(usize); pub struct MirConstId(usize);
type Ident = Opaque; type Ident = Opaque;
@ -484,7 +521,7 @@ pub enum RigidTy {
Adt(AdtDef, GenericArgs), Adt(AdtDef, GenericArgs),
Foreign(ForeignDef), Foreign(ForeignDef),
Str, Str,
Array(Ty, Const), Array(Ty, TyConst),
Pat(Ty, Pattern), Pat(Ty, Pattern),
Slice(Ty), Slice(Ty),
RawPtr(Ty, Mutability), RawPtr(Ty, Mutability),
@ -866,7 +903,7 @@ impl std::ops::Index<ParamTy> for GenericArgs {
} }
impl std::ops::Index<ParamConst> for GenericArgs { impl std::ops::Index<ParamConst> for GenericArgs {
type Output = Const; type Output = TyConst;
fn index(&self, index: ParamConst) -> &Self::Output { fn index(&self, index: ParamConst) -> &Self::Output {
self.0[index.index as usize].expect_const() self.0[index.index as usize].expect_const()
@ -877,7 +914,7 @@ impl std::ops::Index<ParamConst> for GenericArgs {
pub enum GenericArgKind { pub enum GenericArgKind {
Lifetime(Region), Lifetime(Region),
Type(Ty), Type(Ty),
Const(Const), Const(TyConst),
} }
impl GenericArgKind { impl GenericArgKind {
@ -894,7 +931,7 @@ impl GenericArgKind {
/// Panic if this generic argument is not a const, otherwise /// Panic if this generic argument is not a const, otherwise
/// return the const. /// return the const.
#[track_caller] #[track_caller]
pub fn expect_const(&self) -> &Const { pub fn expect_const(&self) -> &TyConst {
match self { match self {
GenericArgKind::Const(c) => c, GenericArgKind::Const(c) => c,
_ => panic!("{self:?}"), _ => panic!("{self:?}"),
@ -913,7 +950,7 @@ impl GenericArgKind {
#[derive(Clone, Debug, Eq, PartialEq)] #[derive(Clone, Debug, Eq, PartialEq)]
pub enum TermKind { pub enum TermKind {
Type(Ty), Type(Ty),
Const(Const), Const(TyConst),
} }
#[derive(Clone, Debug, Eq, PartialEq)] #[derive(Clone, Debug, Eq, PartialEq)]
@ -1202,6 +1239,7 @@ impl Allocation {
#[derive(Clone, Debug, Eq, PartialEq)] #[derive(Clone, Debug, Eq, PartialEq)]
pub enum ConstantKind { pub enum ConstantKind {
Ty(TyConst),
Allocated(Allocation), Allocated(Allocation),
Unevaluated(UnevaluatedConst), Unevaluated(UnevaluatedConst),
Param(ParamConst), Param(ParamConst),
@ -1335,7 +1373,7 @@ pub enum PredicateKind {
ObjectSafe(TraitDef), ObjectSafe(TraitDef),
SubType(SubtypePredicate), SubType(SubtypePredicate),
Coerce(CoercePredicate), Coerce(CoercePredicate),
ConstEquate(Const, Const), ConstEquate(TyConst, TyConst),
Ambiguous, Ambiguous,
AliasRelate(TermKind, TermKind, AliasRelationDirection), AliasRelate(TermKind, TermKind, AliasRelationDirection),
} }
@ -1346,9 +1384,9 @@ pub enum ClauseKind {
RegionOutlives(RegionOutlivesPredicate), RegionOutlives(RegionOutlivesPredicate),
TypeOutlives(TypeOutlivesPredicate), TypeOutlives(TypeOutlivesPredicate),
Projection(ProjectionPredicate), Projection(ProjectionPredicate),
ConstArgHasType(Const, Ty), ConstArgHasType(TyConst, Ty),
WellFormed(GenericArgKind), WellFormed(GenericArgKind),
ConstEvaluatable(Const), ConstEvaluatable(TyConst),
} }
#[derive(Clone, Debug, Eq, PartialEq)] #[derive(Clone, Debug, Eq, PartialEq)]
@ -1426,7 +1464,8 @@ macro_rules! index_impl {
}; };
} }
index_impl!(ConstId); index_impl!(TyConstId);
index_impl!(MirConstId);
index_impl!(Ty); index_impl!(Ty);
index_impl!(Span); index_impl!(Span);

View File

@ -1,10 +1,10 @@
use std::ops::ControlFlow; use std::ops::ControlFlow;
use crate::Opaque; use crate::{ty::TyConst, Opaque};
use super::ty::{ use super::ty::{
Allocation, Binder, Const, ConstDef, ExistentialPredicate, FnSig, GenericArgKind, GenericArgs, Allocation, Binder, ConstDef, ExistentialPredicate, FnSig, GenericArgKind, GenericArgs,
Promoted, Region, RigidTy, TermKind, Ty, UnevaluatedConst, MirConst, Promoted, Region, RigidTy, TermKind, Ty, UnevaluatedConst,
}; };
pub trait Visitor: Sized { pub trait Visitor: Sized {
@ -12,7 +12,7 @@ pub trait Visitor: Sized {
fn visit_ty(&mut self, ty: &Ty) -> ControlFlow<Self::Break> { fn visit_ty(&mut self, ty: &Ty) -> ControlFlow<Self::Break> {
ty.super_visit(self) ty.super_visit(self)
} }
fn visit_const(&mut self, c: &Const) -> ControlFlow<Self::Break> { fn visit_const(&mut self, c: &TyConst) -> ControlFlow<Self::Break> {
c.super_visit(self) c.super_visit(self)
} }
fn visit_reg(&mut self, reg: &Region) -> ControlFlow<Self::Break> { fn visit_reg(&mut self, reg: &Region) -> ControlFlow<Self::Break> {
@ -42,12 +42,32 @@ impl Visitable for Ty {
} }
} }
impl Visitable for Const { impl Visitable for TyConst {
fn visit<V: Visitor>(&self, visitor: &mut V) -> ControlFlow<V::Break> { fn visit<V: Visitor>(&self, visitor: &mut V) -> ControlFlow<V::Break> {
visitor.visit_const(self) visitor.visit_const(self)
} }
fn super_visit<V: Visitor>(&self, visitor: &mut V) -> ControlFlow<V::Break> {
match &self.kind {
crate::ty::TyConstKind::Param(_) => {}
crate::ty::TyConstKind::Bound(_, _) => {}
crate::ty::TyConstKind::Unevaluated(_, args) => args.visit(visitor)?,
crate::ty::TyConstKind::Value(ty, alloc) => {
alloc.visit(visitor)?;
ty.visit(visitor)?;
}
crate::ty::TyConstKind::ZSTValue(ty) => ty.visit(visitor)?,
}
ControlFlow::Continue(())
}
}
impl Visitable for MirConst {
fn visit<V: Visitor>(&self, visitor: &mut V) -> ControlFlow<V::Break> {
self.super_visit(visitor)
}
fn super_visit<V: Visitor>(&self, visitor: &mut V) -> ControlFlow<V::Break> { fn super_visit<V: Visitor>(&self, visitor: &mut V) -> ControlFlow<V::Break> {
match &self.kind() { match &self.kind() {
super::ty::ConstantKind::Ty(ct) => ct.visit(visitor)?,
super::ty::ConstantKind::Allocated(alloc) => alloc.visit(visitor)?, super::ty::ConstantKind::Allocated(alloc) => alloc.visit(visitor)?,
super::ty::ConstantKind::Unevaluated(uv) => uv.visit(visitor)?, super::ty::ConstantKind::Unevaluated(uv) => uv.visit(visitor)?,
super::ty::ConstantKind::Param(_) | super::ty::ConstantKind::ZeroSized => {} super::ty::ConstantKind::Param(_) | super::ty::ConstantKind::ZeroSized => {}

View File

@ -22,7 +22,7 @@ use rustc_smir::rustc_internal;
use stable_mir::mir::alloc::GlobalAlloc; use stable_mir::mir::alloc::GlobalAlloc;
use stable_mir::mir::mono::Instance; use stable_mir::mir::mono::Instance;
use stable_mir::mir::{Body, Constant, Operand, Rvalue, StatementKind, TerminatorKind}; use stable_mir::mir::{Body, Constant, Operand, Rvalue, StatementKind, TerminatorKind};
use stable_mir::ty::{Const, ConstantKind}; use stable_mir::ty::{ConstantKind, MirConst};
use stable_mir::{CrateDef, CrateItems, ItemKind}; use stable_mir::{CrateDef, CrateItems, ItemKind};
use std::convert::TryFrom; use std::convert::TryFrom;
use std::io::Write; use std::io::Write;
@ -77,7 +77,7 @@ fn check_msg(body: &Body, expected: &str) {
}; };
assert_eq!(alloc.provenance.ptrs.len(), 1); assert_eq!(alloc.provenance.ptrs.len(), 1);
let alloc_prov_id = alloc.provenance.ptrs[0].1 .0; let alloc_prov_id = alloc.provenance.ptrs[0].1.0;
let GlobalAlloc::Memory(val) = GlobalAlloc::from(alloc_prov_id) else { let GlobalAlloc::Memory(val) = GlobalAlloc::from(alloc_prov_id) else {
unreachable!() unreachable!()
}; };
@ -95,7 +95,7 @@ fn change_panic_msg(mut body: Body, new_msg: &str) -> Body {
for bb in &mut body.blocks { for bb in &mut body.blocks {
match &mut bb.terminator.kind { match &mut bb.terminator.kind {
TerminatorKind::Call { args, .. } => { TerminatorKind::Call { args, .. } => {
let new_const = Const::from_str(new_msg); let new_const = MirConst::from_str(new_msg);
args[0] = Operand::Constant(Constant { args[0] = Operand::Constant(Constant {
literal: new_const, literal: new_const,
span: bb.terminator.span, span: bb.terminator.span,