1
Fork 0

Change ty_with_args to return Ty instead of Result

Although, we would like to avoid crashes whenever
possible, and that's why I wanted to make this API fallible. It's
looking pretty hard to do proper validation.

I think many of our APIs will unfortunately depend on the user doing
the correct thing since at the MIR level we are working on,
we expect types to have been checked already.
This commit is contained in:
Celina G. Val 2023-12-05 12:01:09 -08:00
parent 1720b108f7
commit 326fea0fb8
8 changed files with 148 additions and 33 deletions

View file

@ -255,16 +255,11 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
tables.tcx.type_of(item.internal(&mut *tables)).instantiate_identity().stable(&mut *tables)
}
fn def_ty_with_args(
&self,
item: stable_mir::DefId,
args: &GenericArgs,
) -> Result<stable_mir::ty::Ty, Error> {
fn def_ty_with_args(&self, item: stable_mir::DefId, args: &GenericArgs) -> stable_mir::ty::Ty {
let mut tables = self.0.borrow_mut();
let args = args.internal(&mut *tables);
let def_ty = tables.tcx.type_of(item.internal(&mut *tables));
// FIXME(celinval): use try_fold instead to avoid crashing.
Ok(def_ty.instantiate(tables.tcx, args).stable(&mut *tables))
def_ty.instantiate(tables.tcx, args).stable(&mut *tables)
}
fn const_literal(&self, cnst: &stable_mir::ty::Const) -> String {

View file

@ -75,24 +75,3 @@ impl<'tcx> Stable<'tcx> for rustc_span::Span {
tables.create_span(*self)
}
}
impl<'tcx, T> Stable<'tcx> for &[T]
where
T: Stable<'tcx>,
{
type T = Vec<T::T>;
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
self.iter().map(|e| e.stable(tables)).collect()
}
}
impl<'tcx, T, U> Stable<'tcx> for (T, U)
where
T: Stable<'tcx>,
U: Stable<'tcx>,
{
type T = (T::T, U::T);
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
(self.0.stable(tables), self.1.stable(tables))
}
}

View file

@ -141,3 +141,24 @@ where
}
}
}
impl<'tcx, T> Stable<'tcx> for &[T]
where
T: Stable<'tcx>,
{
type T = Vec<T::T>;
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
self.iter().map(|e| e.stable(tables)).collect()
}
}
impl<'tcx, T, U> Stable<'tcx> for (T, U)
where
T: Stable<'tcx>,
U: Stable<'tcx>,
{
type T = (T::T, U::T);
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
(self.0.stable(tables), self.1.stable(tables))
}
}

View file

@ -91,7 +91,7 @@ pub trait Context {
fn def_ty(&self, item: DefId) -> Ty;
/// Returns the type of given definition instantiated with the given arguments.
fn def_ty_with_args(&self, item: DefId, args: &GenericArgs) -> Result<Ty, Error>;
fn def_ty_with_args(&self, item: DefId, args: &GenericArgs) -> Ty;
/// Returns literal value of a const as a string.
fn const_literal(&self, cnst: &Const) -> String;

View file

@ -28,7 +28,7 @@ pub enum CompilerError<T> {
}
/// A generic error to represent an API request that cannot be fulfilled.
#[derive(Debug)]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Error(pub(crate) String);
impl Error {

View file

@ -452,7 +452,7 @@ impl Location {
}
/// Information about a place's usage.
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct PlaceContext {
/// Whether the access is mutable or not. Keep this private so we can increment the type in a
/// backward compatible manner.

View file

@ -382,7 +382,9 @@ impl AdtDef {
}
/// Retrieve the type of this Adt instantiating the type with the given arguments.
pub fn ty_with_args(&self, args: &GenericArgs) -> Result<Ty, Error> {
///
/// This will assume the type can be instantiated with these arguments.
pub fn ty_with_args(&self, args: &GenericArgs) -> Ty {
with(|cx| cx.def_ty_with_args(self.0, args))
}
@ -441,6 +443,7 @@ impl VariantDef {
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct FieldDef {
/// The field definition.
///
@ -454,7 +457,9 @@ pub struct FieldDef {
impl FieldDef {
/// Retrieve the type of this field instantiating the type with the given arguments.
pub fn ty_with_args(&self, args: &GenericArgs) -> Result<Ty, Error> {
///
/// This will assume the type can be instantiated with these arguments.
pub fn ty_with_args(&self, args: &GenericArgs) -> Ty {
with(|cx| cx.def_ty_with_args(self.def, args))
}