diff --git a/compiler/rustc_smir/src/rustc_internal/internal.rs b/compiler/rustc_smir/src/rustc_internal/internal.rs index 954b292a3b9..147d683b51f 100644 --- a/compiler/rustc_smir/src/rustc_internal/internal.rs +++ b/compiler/rustc_smir/src/rustc_internal/internal.rs @@ -262,11 +262,7 @@ impl<'tcx> RustcInternal<'tcx> for ClosureKind { impl<'tcx> RustcInternal<'tcx> for AdtDef { type T = rustc_ty::AdtDef<'tcx>; fn internal(&self, tables: &mut Tables<'tcx>) -> Self::T { - let ty = tables.tcx.type_of(self.0.internal(&mut *tables)).instantiate_identity().kind(); - let rustc_ty::TyKind::Adt(def, _) = ty else { - panic!("Expected an ADT definition, but found: {ty:?}") - }; - *def + tables.tcx.adt_def(self.0.internal(&mut *tables)) } } diff --git a/compiler/rustc_smir/src/rustc_smir/mod.rs b/compiler/rustc_smir/src/rustc_smir/mod.rs index f72cd9a9255..167a01ab799 100644 --- a/compiler/rustc_smir/src/rustc_smir/mod.rs +++ b/compiler/rustc_smir/src/rustc_smir/mod.rs @@ -323,7 +323,7 @@ impl<'tcx> Context for TablesWrapper<'tcx> { let size = tables.tcx.layout_of(ParamEnv::empty().and(ty)).unwrap().size; let scalar = ScalarInt::try_from_uint(val, size).ok_or_else(|| { - Error::new(format!("Value overflow: cannot covert `{val}` to usize.")) + Error::new(format!("Value overflow: cannot convert `{val}` to usize.")) })?; Ok(ty::Const::new_value(tables.tcx, ty::ValTree::from_scalar_int(scalar), ty) .stable(&mut *tables)) diff --git a/compiler/stable_mir/src/mir/body.rs b/compiler/stable_mir/src/mir/body.rs index 8cdb054de9a..ac07246dfd3 100644 --- a/compiler/stable_mir/src/mir/body.rs +++ b/compiler/stable_mir/src/mir/body.rs @@ -2,7 +2,7 @@ use crate::mir::pretty::{function_body, pretty_statement}; use crate::ty::{ AdtDef, ClosureDef, Const, CoroutineDef, GenericArgs, Movability, Region, RigidTy, Ty, TyKind, }; -use crate::{Error, Span, Opaque}; +use crate::{Error, Opaque, Span}; use std::io; /// The SMIR representation of a single function. @@ -685,40 +685,46 @@ impl Place { self.projection.iter().fold(Ok(start_ty), |place_ty, elem| { let ty = place_ty?; match elem { - ProjectionElem::Deref => { - let deref_ty = ty - .kind() - .builtin_deref(true) - .ok_or_else(|| error!("Cannot dereference type: {ty:?}"))?; - Ok(deref_ty.ty) - } + ProjectionElem::Deref => Self::deref_ty(ty), ProjectionElem::Field(_idx, fty) => Ok(*fty), - ProjectionElem::Index(_) | ProjectionElem::ConstantIndex { .. } => ty - .kind() - .builtin_index() - .ok_or_else(|| error!("Cannot index non-array type: {ty:?}")), + ProjectionElem::Index(_) | ProjectionElem::ConstantIndex { .. } => { + Self::index_ty(ty) + } ProjectionElem::Subslice { from, to, from_end } => { - let ty_kind = ty.kind(); - match ty_kind { - TyKind::RigidTy(RigidTy::Slice(..)) => Ok(ty), - TyKind::RigidTy(RigidTy::Array(inner, _)) if !from_end => { - Ty::try_new_array( - inner, - to.checked_sub(*from) - .ok_or_else(|| error!("Subslice overflow: {from}..{to}"))?, - ) - } - TyKind::RigidTy(RigidTy::Array(inner, size)) => { - let size = size.eval_target_usize()?; - let len = size - from - to; - Ty::try_new_array(inner, len) - } - _ => Err(Error(format!("Cannot subslice non-array type: `{ty_kind:?}`"))), - } + Self::subslice_ty(ty, from, to, from_end) } ProjectionElem::Downcast(_) => Ok(ty), ProjectionElem::OpaqueCast(ty) | ProjectionElem::Subtype(ty) => Ok(*ty), } }) } + + fn index_ty(ty: Ty) -> Result { + ty.kind().builtin_index().ok_or_else(|| error!("Cannot index non-array type: {ty:?}")) + } + + fn subslice_ty(ty: Ty, from: &u64, to: &u64, from_end: &bool) -> Result { + let ty_kind = ty.kind(); + match ty_kind { + TyKind::RigidTy(RigidTy::Slice(..)) => Ok(ty), + TyKind::RigidTy(RigidTy::Array(inner, _)) if !from_end => Ty::try_new_array( + inner, + to.checked_sub(*from).ok_or_else(|| error!("Subslice overflow: {from}..{to}"))?, + ), + TyKind::RigidTy(RigidTy::Array(inner, size)) => { + let size = size.eval_target_usize()?; + let len = size - from - to; + Ty::try_new_array(inner, len) + } + _ => Err(Error(format!("Cannot subslice non-array type: `{ty_kind:?}`"))), + } + } + + fn deref_ty(ty: Ty) -> Result { + let deref_ty = ty + .kind() + .builtin_deref(true) + .ok_or_else(|| error!("Cannot dereference type: {ty:?}"))?; + Ok(deref_ty.ty) + } }