Skip to content

Commit 0e6768a

Browse files
committed
Improve code per PR comments
- Simplified DefTy::internal - Break down place::ty() method
1 parent 857eb17 commit 0e6768a

File tree

3 files changed

+36
-34
lines changed

3 files changed

+36
-34
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
@@ -319,7 +319,7 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
319319
let size = tables.tcx.layout_of(ParamEnv::empty().and(ty)).unwrap().size;
320320

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

compiler/stable_mir/src/mir/body.rs

+34-28
Original file line numberDiff line numberDiff line change
@@ -661,40 +661,46 @@ impl Place {
661661
self.projection.iter().fold(Ok(start_ty), |place_ty, elem| {
662662
let ty = place_ty?;
663663
match elem {
664-
ProjectionElem::Deref => {
665-
let deref_ty = ty
666-
.kind()
667-
.builtin_deref(true)
668-
.ok_or_else(|| error!("Cannot dereference type: {ty:?}"))?;
669-
Ok(deref_ty.ty)
670-
}
664+
ProjectionElem::Deref => Self::deref_ty(ty),
671665
ProjectionElem::Field(_idx, fty) => Ok(*fty),
672-
ProjectionElem::Index(_) | ProjectionElem::ConstantIndex { .. } => ty
673-
.kind()
674-
.builtin_index()
675-
.ok_or_else(|| error!("Cannot index non-array type: {ty:?}")),
666+
ProjectionElem::Index(_) | ProjectionElem::ConstantIndex { .. } => {
667+
Self::index_ty(ty)
668+
}
676669
ProjectionElem::Subslice { from, to, from_end } => {
677-
let ty_kind = ty.kind();
678-
match ty_kind {
679-
TyKind::RigidTy(RigidTy::Slice(..)) => Ok(ty),
680-
TyKind::RigidTy(RigidTy::Array(inner, _)) if !from_end => {
681-
Ty::try_new_array(
682-
inner,
683-
to.checked_sub(*from)
684-
.ok_or_else(|| error!("Subslice overflow: {from}..{to}"))?,
685-
)
686-
}
687-
TyKind::RigidTy(RigidTy::Array(inner, size)) => {
688-
let size = size.eval_target_usize()?;
689-
let len = size - from - to;
690-
Ty::try_new_array(inner, len)
691-
}
692-
_ => Err(Error(format!("Cannot subslice non-array type: `{ty_kind:?}`"))),
693-
}
670+
Self::subslice_ty(ty, from, to, from_end)
694671
}
695672
ProjectionElem::Downcast(_) => Ok(ty),
696673
ProjectionElem::OpaqueCast(ty) | ProjectionElem::Subtype(ty) => Ok(*ty),
697674
}
698675
})
699676
}
677+
678+
fn index_ty(ty: Ty) -> Result<Ty, Error> {
679+
ty.kind().builtin_index().ok_or_else(|| error!("Cannot index non-array type: {ty:?}"))
680+
}
681+
682+
fn subslice_ty(ty: Ty, from: &u64, to: &u64, from_end: &bool) -> Result<Ty, Error> {
683+
let ty_kind = ty.kind();
684+
match ty_kind {
685+
TyKind::RigidTy(RigidTy::Slice(..)) => Ok(ty),
686+
TyKind::RigidTy(RigidTy::Array(inner, _)) if !from_end => Ty::try_new_array(
687+
inner,
688+
to.checked_sub(*from).ok_or_else(|| error!("Subslice overflow: {from}..{to}"))?,
689+
),
690+
TyKind::RigidTy(RigidTy::Array(inner, size)) => {
691+
let size = size.eval_target_usize()?;
692+
let len = size - from - to;
693+
Ty::try_new_array(inner, len)
694+
}
695+
_ => Err(Error(format!("Cannot subslice non-array type: `{ty_kind:?}`"))),
696+
}
697+
}
698+
699+
fn deref_ty(ty: Ty) -> Result<Ty, Error> {
700+
let deref_ty = ty
701+
.kind()
702+
.builtin_deref(true)
703+
.ok_or_else(|| error!("Cannot dereference type: {ty:?}"))?;
704+
Ok(deref_ty.ty)
705+
}
700706
}

0 commit comments

Comments
 (0)