Skip to content

Commit

Permalink
Improve code per PR comments
Browse files Browse the repository at this point in the history
 - Simplified DefTy::internal
 - Break down place::ty() method
  • Loading branch information
celinval committed Nov 20, 2023
1 parent d3fa6a0 commit d94df62
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 35 deletions.
6 changes: 1 addition & 5 deletions compiler/rustc_smir/src/rustc_internal/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_smir/src/rustc_smir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
64 changes: 35 additions & 29 deletions compiler/stable_mir/src/mir/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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, Error> {
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<Ty, Error> {
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<Ty, Error> {
let deref_ty = ty
.kind()
.builtin_deref(true)
.ok_or_else(|| error!("Cannot dereference type: {ty:?}"))?;
Ok(deref_ty.ty)
}
}

0 comments on commit d94df62

Please sign in to comment.