1
Fork 0

Auto merge of #135753 - compiler-errors:from-ty-const, r=oli-obk

Get rid of `mir::Const::from_ty_const`

This function is strange, because it turns valtrees into `mir::Const::Value`, but the rest of the const variants stay as type system consts.

All of the callsites except for one in `instsimplify` (array length simplification of `ptr_metadata` call) just go through the valtree arm of the function, so it's easier to just create a `mir::Const` directly for those.

For the instsimplify case, if we have a type system const we should *keep* having a type system const, rather than turning it into a `mir::Const::Value`; it doesn't really matter in practice, though, bc `usize` has no padding, but it feels more principled.
This commit is contained in:
bors 2025-01-26 09:26:34 +00:00
commit d9b4598d7e
10 changed files with 21 additions and 98 deletions

View file

@ -451,16 +451,13 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
let ty = ty::Ty::new_static_str(tcx);
let bytes = value.as_bytes();
let val_tree = ty::ValTree::from_raw_bytes(tcx, bytes);
let ct = ty::Const::new_value(tcx, val_tree, ty);
super::convert::mir_const_from_ty_const(&mut *tables, ct, ty)
let val = tcx.valtree_to_const_val((ty, val_tree));
mir::Const::from_value(val, ty).stable(&mut tables)
}
fn new_const_bool(&self, value: bool) -> MirConst {
let mut tables = self.0.borrow_mut();
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)
mir::Const::from_bool(tables.tcx, value).stable(&mut tables)
}
fn try_new_const_uint(&self, value: u128, uint_ty: UintTy) -> Result<MirConst, Error> {
@ -472,13 +469,11 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
.layout_of(ty::TypingEnv::fully_monomorphized().as_query_input(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))
Ok(mir::Const::from_scalar(tcx, mir::interpret::Scalar::Int(scalar), ty)
.stable(&mut tables))
}
fn try_new_ty_const_uint(
&self,

View file

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

View file

@ -414,48 +414,6 @@ impl<'tcx> Stable<'tcx> for ty::Pattern<'tcx> {
}
}
pub(crate) 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::ConstKind::Value(ty, 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::ConstKind::Param(param) => stable_mir::ty::ConstantKind::Param(param.stable(tables)),
ty::ConstKind::Error(_) => unreachable!(),
ty::ConstKind::Infer(_) => unreachable!(),
ty::ConstKind::Bound(_, _) => unimplemented!(),
ty::ConstKind::Placeholder(_) => unimplemented!(),
ty::ConstKind::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::ConstKind::Expr(_) => unimplemented!(),
};
let stable_ty = tables.intern_ty(ty);
let id = tables.intern_mir_const(mir::Const::Ty(ty, ty_const));
stable_mir::ty::MirConst::new(kind, stable_ty, id)
}
impl<'tcx> Stable<'tcx> for ty::Const<'tcx> {
type T = stable_mir::ty::TyConst;