Skip to content

Commit d94df62

Browse files
committed
Improve code per PR comments
- Simplified DefTy::internal - Break down place::ty() method
1 parent d3fa6a0 commit d94df62

File tree

3 files changed

+37
-35
lines changed

3 files changed

+37
-35
lines changed

compiler/rustc_smir/src/rustc_internal/internal.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -262,11 +262,7 @@ impl<'tcx> RustcInternal<'tcx> for ClosureKind {
262262
impl<'tcx> RustcInternal<'tcx> for AdtDef {
263263
type T = rustc_ty::AdtDef<'tcx>;
264264
fn internal(&self, tables: &mut Tables<'tcx>) -> Self::T {
265-
let ty = tables.tcx.type_of(self.0.internal(&mut *tables)).instantiate_identity().kind();
266-
let rustc_ty::TyKind::Adt(def, _) = ty else {
267-
panic!("Expected an ADT definition, but found: {ty:?}")
268-
};
269-
*def
265+
tables.tcx.adt_def(self.0.internal(&mut *tables))
270266
}
271267
}
272268

compiler/rustc_smir/src/rustc_smir/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
323323
let size = tables.tcx.layout_of(ParamEnv::empty().and(ty)).unwrap().size;
324324

325325
let scalar = ScalarInt::try_from_uint(val, size).ok_or_else(|| {
326-
Error::new(format!("Value overflow: cannot covert `{val}` to usize."))
326+
Error::new(format!("Value overflow: cannot convert `{val}` to usize."))
327327
})?;
328328
Ok(ty::Const::new_value(tables.tcx, ty::ValTree::from_scalar_int(scalar), ty)
329329
.stable(&mut *tables))

compiler/stable_mir/src/mir/body.rs

+35-29
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::mir::pretty::{function_body, pretty_statement};
22
use crate::ty::{
33
AdtDef, ClosureDef, Const, CoroutineDef, GenericArgs, Movability, Region, RigidTy, Ty, TyKind,
44
};
5-
use crate::{Error, Span, Opaque};
5+
use crate::{Error, Opaque, Span};
66
use std::io;
77

88
/// The SMIR representation of a single function.
@@ -685,40 +685,46 @@ impl Place {
685685
self.projection.iter().fold(Ok(start_ty), |place_ty, elem| {
686686
let ty = place_ty?;
687687
match elem {
688-
ProjectionElem::Deref => {
689-
let deref_ty = ty
690-
.kind()
691-
.builtin_deref(true)
692-
.ok_or_else(|| error!("Cannot dereference type: {ty:?}"))?;
693-
Ok(deref_ty.ty)
694-
}
688+
ProjectionElem::Deref => Self::deref_ty(ty),
695689
ProjectionElem::Field(_idx, fty) => Ok(*fty),
696-
ProjectionElem::Index(_) | ProjectionElem::ConstantIndex { .. } => ty
697-
.kind()
698-
.builtin_index()
699-
.ok_or_else(|| error!("Cannot index non-array type: {ty:?}")),
690+
ProjectionElem::Index(_) | ProjectionElem::ConstantIndex { .. } => {
691+
Self::index_ty(ty)
692+
}
700693
ProjectionElem::Subslice { from, to, from_end } => {
701-
let ty_kind = ty.kind();
702-
match ty_kind {
703-
TyKind::RigidTy(RigidTy::Slice(..)) => Ok(ty),
704-
TyKind::RigidTy(RigidTy::Array(inner, _)) if !from_end => {
705-
Ty::try_new_array(
706-
inner,
707-
to.checked_sub(*from)
708-
.ok_or_else(|| error!("Subslice overflow: {from}..{to}"))?,
709-
)
710-
}
711-
TyKind::RigidTy(RigidTy::Array(inner, size)) => {
712-
let size = size.eval_target_usize()?;
713-
let len = size - from - to;
714-
Ty::try_new_array(inner, len)
715-
}
716-
_ => Err(Error(format!("Cannot subslice non-array type: `{ty_kind:?}`"))),
717-
}
694+
Self::subslice_ty(ty, from, to, from_end)
718695
}
719696
ProjectionElem::Downcast(_) => Ok(ty),
720697
ProjectionElem::OpaqueCast(ty) | ProjectionElem::Subtype(ty) => Ok(*ty),
721698
}
722699
})
723700
}
701+
702+
fn index_ty(ty: Ty) -> Result<Ty, Error> {
703+
ty.kind().builtin_index().ok_or_else(|| error!("Cannot index non-array type: {ty:?}"))
704+
}
705+
706+
fn subslice_ty(ty: Ty, from: &u64, to: &u64, from_end: &bool) -> Result<Ty, Error> {
707+
let ty_kind = ty.kind();
708+
match ty_kind {
709+
TyKind::RigidTy(RigidTy::Slice(..)) => Ok(ty),
710+
TyKind::RigidTy(RigidTy::Array(inner, _)) if !from_end => Ty::try_new_array(
711+
inner,
712+
to.checked_sub(*from).ok_or_else(|| error!("Subslice overflow: {from}..{to}"))?,
713+
),
714+
TyKind::RigidTy(RigidTy::Array(inner, size)) => {
715+
let size = size.eval_target_usize()?;
716+
let len = size - from - to;
717+
Ty::try_new_array(inner, len)
718+
}
719+
_ => Err(Error(format!("Cannot subslice non-array type: `{ty_kind:?}`"))),
720+
}
721+
}
722+
723+
fn deref_ty(ty: Ty) -> Result<Ty, Error> {
724+
let deref_ty = ty
725+
.kind()
726+
.builtin_deref(true)
727+
.ok_or_else(|| error!("Cannot dereference type: {ty:?}"))?;
728+
Ok(deref_ty.ty)
729+
}
724730
}

0 commit comments

Comments
 (0)