1
Fork 0

Add bound_type_of

This commit is contained in:
Jack Huey 2022-05-08 15:12:56 -04:00
parent 319575ae8c
commit c92248ab9f
30 changed files with 90 additions and 83 deletions

View file

@ -10,7 +10,7 @@ use crate::ty::codec::{TyDecoder, TyEncoder};
use crate::ty::fold::{FallibleTypeFolder, TypeFoldable, TypeVisitor};
use crate::ty::print::{FmtPrinter, Printer};
use crate::ty::subst::{GenericArg, InternalSubsts, Subst, SubstsRef};
use crate::ty::{self, EarlyBinder, List, Ty, TyCtxt};
use crate::ty::{self, List, Ty, TyCtxt};
use crate::ty::{AdtDef, InstanceDef, Region, ScalarInt, UserTypeAnnotationIndex};
use rustc_errors::ErrorGuaranteed;
@ -2387,7 +2387,7 @@ impl<'tcx> Operand<'tcx> {
substs: SubstsRef<'tcx>,
span: Span,
) -> Self {
let ty = EarlyBinder(tcx.type_of(def_id)).subst(tcx, substs);
let ty = tcx.bound_type_of(def_id).subst(tcx, substs);
Operand::Constant(Box::new(Constant {
span,
user_ty: None,

View file

@ -5,7 +5,7 @@
use crate::mir::*;
use crate::ty::subst::Subst;
use crate::ty::{self, EarlyBinder, Ty, TyCtxt};
use crate::ty::{self, Ty, TyCtxt};
use rustc_hir as hir;
use rustc_target::abi::VariantIdx;
@ -203,7 +203,7 @@ impl<'tcx> Rvalue<'tcx> {
AggregateKind::Array(ty) => tcx.mk_array(ty, ops.len() as u64),
AggregateKind::Tuple => tcx.mk_tup(ops.iter().map(|op| op.ty(local_decls, tcx))),
AggregateKind::Adt(did, _, substs, _, _) => {
EarlyBinder(tcx.type_of(did)).subst(tcx, substs)
tcx.bound_type_of(did).subst(tcx, substs)
}
AggregateKind::Closure(did, substs) => tcx.mk_closure(did, substs),
AggregateKind::Generator(did, substs, movability) => {

View file

@ -19,11 +19,10 @@ use crate::ty::subst::{GenericArg, GenericArgKind, InternalSubsts, Subst, Substs
use crate::ty::TyKind::*;
use crate::ty::{
self, AdtDef, AdtDefData, AdtKind, Binder, BindingMode, BoundVar, CanonicalPolyFnSig,
ClosureSizeProfileData, Const, ConstS, ConstVid, DefIdTree, EarlyBinder, ExistentialPredicate,
FloatTy, FloatVar, FloatVid, GenericParamDefKind, InferConst, InferTy, IntTy, IntVar, IntVid,
List, ParamConst, ParamTy, PolyFnSig, Predicate, PredicateKind, PredicateS, ProjectionTy,
Region, RegionKind, ReprOptions, TraitObjectVisitor, Ty, TyKind, TyS, TyVar, TyVid, TypeAndMut,
UintTy,
ClosureSizeProfileData, Const, ConstS, ConstVid, DefIdTree, ExistentialPredicate, FloatTy,
FloatVar, FloatVid, GenericParamDefKind, InferConst, InferTy, IntTy, IntVar, IntVid, List,
ParamConst, ParamTy, PolyFnSig, Predicate, PredicateKind, PredicateS, ProjectionTy, Region,
RegionKind, ReprOptions, TraitObjectVisitor, Ty, TyKind, TyS, TyVar, TyVid, TypeAndMut, UintTy,
};
use rustc_ast as ast;
use rustc_data_structures::fingerprint::Fingerprint;
@ -1605,7 +1604,7 @@ impl<'tcx> TyCtxt<'tcx> {
pub fn caller_location_ty(self) -> Ty<'tcx> {
self.mk_imm_ref(
self.lifetimes.re_static,
EarlyBinder(self.type_of(self.require_lang_item(LangItem::PanicLocation, None)))
self.bound_type_of(self.require_lang_item(LangItem::PanicLocation, None))
.subst(self, self.mk_substs([self.lifetimes.re_static.into()].iter())),
)
}
@ -2334,7 +2333,7 @@ impl<'tcx> TyCtxt<'tcx> {
ty_param.into()
} else {
assert!(has_default);
EarlyBinder(self.type_of(param.def_id)).subst(self, substs).into()
self.bound_type_of(param.def_id).subst(self, substs).into()
}
}
});

View file

@ -1932,7 +1932,7 @@ impl<'tcx> FieldDef {
/// Returns the type of this field. The resulting type is not normalized. The `subst` is
/// typically obtained via the second field of [`TyKind::Adt`].
pub fn ty(&self, tcx: TyCtxt<'tcx>, subst: SubstsRef<'tcx>) -> Ty<'tcx> {
EarlyBinder(tcx.type_of(self.did)).subst(tcx, subst)
tcx.bound_type_of(self.did).subst(tcx, subst)
}
/// Computes the `Ident` of this variant by looking up the `Span`

View file

@ -115,12 +115,16 @@ pub trait Printer<'tcx>: Sized {
DefPathData::Impl => {
let generics = self.tcx().generics_of(def_id);
let mut self_ty = self.tcx().type_of(def_id);
let mut impl_trait_ref = self.tcx().impl_trait_ref(def_id);
if substs.len() >= generics.count() {
self_ty = EarlyBinder(self_ty).subst(self.tcx(), substs);
impl_trait_ref = EarlyBinder(impl_trait_ref).subst(self.tcx(), substs);
}
let self_ty = self.tcx().bound_type_of(def_id);
let impl_trait_ref = self.tcx().impl_trait_ref(def_id);
let (self_ty, impl_trait_ref) = if substs.len() >= generics.count() {
(
self_ty.subst(self.tcx(), substs),
EarlyBinder(impl_trait_ref).subst(self.tcx(), substs),
)
} else {
(self_ty.0, impl_trait_ref)
};
self.print_impl_path(def_id, substs, self_ty, impl_trait_ref)
}
@ -203,8 +207,7 @@ pub trait Printer<'tcx>: Sized {
has_default
&& substs[param.index as usize]
== GenericArg::from(
EarlyBinder(self.tcx().type_of(param.def_id))
.subst(self.tcx(), substs),
self.tcx().bound_type_of(param.def_id).subst(self.tcx(), substs),
)
}
ty::GenericParamDefKind::Const { has_default } => {

View file

@ -7,7 +7,7 @@
use crate::mir::interpret::{get_slice_bytes, ConstValue, GlobalAlloc, Scalar};
use crate::ty::error::{ExpectedFound, TypeError};
use crate::ty::subst::{GenericArg, GenericArgKind, Subst, SubstsRef};
use crate::ty::{self, EarlyBinder, ImplSubject, Term, Ty, TyCtxt, TypeFoldable};
use crate::ty::{self, ImplSubject, Term, Ty, TyCtxt, TypeFoldable};
use rustc_hir as ast;
use rustc_hir::def_id::DefId;
use rustc_span::DUMMY_SP;
@ -159,8 +159,8 @@ pub fn relate_substs_with_variances<'tcx, R: TypeRelation<'tcx>>(
let params = iter::zip(a_subst, b_subst).enumerate().map(|(i, (a, b))| {
let variance = variances[i];
let variance_info = if variance == ty::Invariant {
let ty = *cached_ty
.get_or_insert_with(|| EarlyBinder(tcx.type_of(ty_def_id)).subst(tcx, a_subst));
let ty =
*cached_ty.get_or_insert_with(|| tcx.bound_type_of(ty_def_id).subst(tcx, a_subst));
ty::VarianceDiagInfo::Invariant { ty, param_index: i.try_into().unwrap() }
} else {
ty::VarianceDiagInfo::default()

View file

@ -2347,7 +2347,7 @@ impl<'tcx> Ty<'tcx> {
ty::Str | ty::Slice(_) => (tcx.types.usize, false),
ty::Dynamic(..) => {
let dyn_metadata = tcx.lang_items().dyn_metadata().unwrap();
(EarlyBinder(tcx.type_of(dyn_metadata)).subst(tcx, &[tail.into()]), false)
(tcx.bound_type_of(dyn_metadata).subst(tcx, &[tail.into()]), false)
},
// type parameters only have unit metadata if they're sized, so return true

View file

@ -592,6 +592,10 @@ impl<'tcx> TyCtxt<'tcx> {
trace!(?expanded_type);
if visitor.found_recursion { Err(expanded_type) } else { Ok(expanded_type) }
}
pub fn bound_type_of(self, def_id: DefId) -> EarlyBinder<Ty<'tcx>> {
EarlyBinder(self.type_of(def_id))
}
}
struct OpaqueTypeExpander<'tcx> {
@ -623,8 +627,8 @@ impl<'tcx> OpaqueTypeExpander<'tcx> {
let expanded_ty = match self.expanded_cache.get(&(def_id, substs)) {
Some(expanded_ty) => *expanded_ty,
None => {
let generic_ty = self.tcx.type_of(def_id);
let concrete_ty = EarlyBinder(generic_ty).subst(self.tcx, substs);
let generic_ty = self.tcx.bound_type_of(def_id);
let concrete_ty = generic_ty.subst(self.tcx, substs);
let expanded_ty = self.fold_ty(concrete_ty);
self.expanded_cache.insert((def_id, substs), expanded_ty);
expanded_ty