diff options
Diffstat (limited to 'compiler/rustc_middle/src')
| -rw-r--r-- | compiler/rustc_middle/src/query/mod.rs | 10 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/ty/context.rs | 14 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/ty/error.rs | 4 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/ty/flags.rs | 14 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/ty/inhabitedness/mod.rs | 6 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/ty/layout.rs | 2 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/ty/mod.rs | 2 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/ty/print/pretty.rs | 2 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/ty/relate.rs | 5 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/ty/sty.rs | 51 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/ty/visit.rs | 3 |
11 files changed, 89 insertions, 24 deletions
diff --git a/compiler/rustc_middle/src/query/mod.rs b/compiler/rustc_middle/src/query/mod.rs index be2657d25a6..ab73832ae68 100644 --- a/compiler/rustc_middle/src/query/mod.rs +++ b/compiler/rustc_middle/src/query/mod.rs @@ -1821,6 +1821,16 @@ rustc_queries! { desc { "normalizing `{}`", goal.value.value } } + /// Do not call this query directly: invoke `normalize` instead. + query normalize_inherent_projection_ty( + goal: CanonicalProjectionGoal<'tcx> + ) -> Result< + &'tcx Canonical<'tcx, canonical::QueryResponse<'tcx, NormalizationResult<'tcx>>>, + NoSolution, + > { + desc { "normalizing `{}`", goal.value.value } + } + /// Do not call this query directly: invoke `try_normalize_erasing_regions` instead. query try_normalize_generic_arg_after_erasing_regions( goal: ParamEnvAnd<'tcx, GenericArg<'tcx>> diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index c62254cd79c..212720f3b69 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -1846,7 +1846,17 @@ impl<'tcx> TyCtxt<'tcx> { let substs = substs.into_iter().map(Into::into); #[cfg(debug_assertions)] { - let n = self.generics_of(_def_id).count(); + let generics = self.generics_of(_def_id); + + let n = if let DefKind::AssocTy = self.def_kind(_def_id) + && let DefKind::Impl { of_trait: false } = self.def_kind(self.parent(_def_id)) + { + // If this is an inherent projection. + + generics.params.len() + 1 + } else { + generics.count() + }; assert_eq!( (n, Some(n)), substs.size_hint(), @@ -2007,7 +2017,7 @@ impl<'tcx> TyCtxt<'tcx> { debug_assert_matches!( (kind, self.def_kind(alias_ty.def_id)), (ty::Opaque, DefKind::OpaqueTy) - | (ty::Projection, DefKind::AssocTy) + | (ty::Projection | ty::Inherent, DefKind::AssocTy) | (ty::Opaque | ty::Projection, DefKind::ImplTraitPlaceholder) ); self.mk_ty_from_kind(Alias(kind, alias_ty)) diff --git a/compiler/rustc_middle/src/ty/error.rs b/compiler/rustc_middle/src/ty/error.rs index 1be61e16dbe..49ab9b79e96 100644 --- a/compiler/rustc_middle/src/ty/error.rs +++ b/compiler/rustc_middle/src/ty/error.rs @@ -265,7 +265,7 @@ impl<'tcx> Ty<'tcx> { ty::Infer(ty::FreshTy(_)) => "fresh type".into(), ty::Infer(ty::FreshIntTy(_)) => "fresh integral type".into(), ty::Infer(ty::FreshFloatTy(_)) => "fresh floating-point type".into(), - ty::Alias(ty::Projection, _) => "associated type".into(), + ty::Alias(ty::Projection | ty::Inherent, _) => "associated type".into(), ty::Param(p) => format!("type parameter `{p}`").into(), ty::Alias(ty::Opaque, ..) => if tcx.ty_is_opaque_future(self) { "future".into() } else { "opaque type".into() }, ty::Error(_) => "type error".into(), @@ -312,7 +312,7 @@ impl<'tcx> Ty<'tcx> { ty::Tuple(..) => "tuple".into(), ty::Placeholder(..) => "higher-ranked type".into(), ty::Bound(..) => "bound type variable".into(), - ty::Alias(ty::Projection, _) => "associated type".into(), + ty::Alias(ty::Projection | ty::Inherent, _) => "associated type".into(), ty::Param(_) => "type parameter".into(), ty::Alias(ty::Opaque, ..) => "opaque type".into(), } diff --git a/compiler/rustc_middle/src/ty/flags.rs b/compiler/rustc_middle/src/ty/flags.rs index 68002bfcfbd..d64875a9f00 100644 --- a/compiler/rustc_middle/src/ty/flags.rs +++ b/compiler/rustc_middle/src/ty/flags.rs @@ -176,14 +176,14 @@ impl FlagComputation { self.add_substs(substs); } - &ty::Alias(ty::Projection, data) => { - self.add_flags(TypeFlags::HAS_TY_PROJECTION); - self.add_alias_ty(data); - } + &ty::Alias(kind, data) => { + self.add_flags(match kind { + ty::Projection => TypeFlags::HAS_TY_PROJECTION, + ty::Inherent => TypeFlags::HAS_TY_INHERENT, + ty::Opaque => TypeFlags::HAS_TY_OPAQUE, + }); - &ty::Alias(ty::Opaque, ty::AliasTy { substs, .. }) => { - self.add_flags(TypeFlags::HAS_TY_OPAQUE); - self.add_substs(substs); + self.add_alias_ty(data); } &ty::Dynamic(obj, r, _) => { diff --git a/compiler/rustc_middle/src/ty/inhabitedness/mod.rs b/compiler/rustc_middle/src/ty/inhabitedness/mod.rs index 92a040068dd..9e672004cf9 100644 --- a/compiler/rustc_middle/src/ty/inhabitedness/mod.rs +++ b/compiler/rustc_middle/src/ty/inhabitedness/mod.rs @@ -113,6 +113,12 @@ impl<'tcx> Ty<'tcx> { } Never => InhabitedPredicate::False, Param(_) | Alias(ty::Projection, _) => InhabitedPredicate::GenericType(self), + // FIXME(inherent_associated_types): Most likely we can just map to `GenericType` like above. + // However it's unclear if the substs passed to `InhabitedPredicate::subst` are of the correct + // format, i.e. don't contain parent substs. If you hit this case, please verify this beforehand. + Alias(ty::Inherent, _) => { + bug!("unimplemented: inhabitedness checking for inherent projections") + } Tuple(tys) if tys.is_empty() => InhabitedPredicate::True, // use a query for more complex cases Adt(..) | Array(..) | Tuple(_) => tcx.inhabited_predicate_type(self), diff --git a/compiler/rustc_middle/src/ty/layout.rs b/compiler/rustc_middle/src/ty/layout.rs index f2a2e67cf82..47cf48f46cf 100644 --- a/compiler/rustc_middle/src/ty/layout.rs +++ b/compiler/rustc_middle/src/ty/layout.rs @@ -324,7 +324,7 @@ impl<'tcx> SizeSkeleton<'tcx> { let non_zero = !ty.is_unsafe_ptr(); let tail = tcx.struct_tail_erasing_lifetimes(pointee, param_env); match tail.kind() { - ty::Param(_) | ty::Alias(ty::Projection, _) => { + ty::Param(_) | ty::Alias(ty::Projection | ty::Inherent, _) => { debug_assert!(tail.has_non_region_param()); Ok(SizeSkeleton::Pointer { non_zero, tail: tcx.erase_regions(tail) }) } diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index 23d6242899f..9fc0110b354 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -1004,7 +1004,7 @@ impl<'tcx> Term<'tcx> { match self.unpack() { TermKind::Ty(ty) => match ty.kind() { ty::Alias(kind, alias_ty) => match kind { - AliasKind::Projection => Some(*alias_ty), + AliasKind::Projection | AliasKind::Inherent => Some(*alias_ty), AliasKind::Opaque => None, }, _ => None, diff --git a/compiler/rustc_middle/src/ty/print/pretty.rs b/compiler/rustc_middle/src/ty/print/pretty.rs index 6ac9f950450..8a4d3820d7b 100644 --- a/compiler/rustc_middle/src/ty/print/pretty.rs +++ b/compiler/rustc_middle/src/ty/print/pretty.rs @@ -729,7 +729,7 @@ pub trait PrettyPrinter<'tcx>: ty::Foreign(def_id) => { p!(print_def_path(def_id, &[])); } - ty::Alias(ty::Projection, ref data) => { + ty::Alias(ty::Projection | ty::Inherent, ref data) => { if !(self.should_print_verbose() || NO_QUERIES.with(|q| q.get())) && self.tcx().is_impl_trait_in_trait(data.def_id) { diff --git a/compiler/rustc_middle/src/ty/relate.rs b/compiler/rustc_middle/src/ty/relate.rs index 7f28ed6c263..da43475941e 100644 --- a/compiler/rustc_middle/src/ty/relate.rs +++ b/compiler/rustc_middle/src/ty/relate.rs @@ -550,6 +550,11 @@ pub fn super_relate_tys<'tcx, R: TypeRelation<'tcx>>( Ok(tcx.mk_projection(projection_ty.def_id, projection_ty.substs)) } + (&ty::Alias(ty::Inherent, a_data), &ty::Alias(ty::Inherent, b_data)) => { + let alias_ty = relation.relate(a_data, b_data)?; + Ok(tcx.mk_alias(ty::Inherent, tcx.mk_alias_ty(alias_ty.def_id, alias_ty.substs))) + } + ( &ty::Alias(ty::Opaque, ty::AliasTy { def_id: a_def_id, substs: a_substs, .. }), &ty::Alias(ty::Opaque, ty::AliasTy { def_id: b_def_id, substs: b_substs, .. }), diff --git a/compiler/rustc_middle/src/ty/sty.rs b/compiler/rustc_middle/src/ty/sty.rs index 646384eebc8..8d0737e1eee 100644 --- a/compiler/rustc_middle/src/ty/sty.rs +++ b/compiler/rustc_middle/src/ty/sty.rs @@ -1190,9 +1190,9 @@ where /// Represents the projection of an associated type. /// -/// For a projection, this would be `<Ty as Trait<...>>::N`. -/// -/// For an opaque type, there is no explicit syntax. +/// * For a projection, this would be `<Ty as Trait<...>>::N<...>`. +/// * For an inherent projection, this would be `Ty::N<...>`. +/// * For an opaque type, there is no explicit syntax. #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, TyEncodable, TyDecodable)] #[derive(HashStable, TypeFoldable, TypeVisitable, Lift)] pub struct AliasTy<'tcx> { @@ -1201,12 +1201,16 @@ pub struct AliasTy<'tcx> { /// For a projection, these are the substitutions for the trait and the /// GAT substitutions, if there are any. /// + /// For an inherent projection, they consist of the self type and the GAT substitutions, + /// if there are any. + /// /// For RPIT the substitutions are for the generics of the function, /// while for TAIT it is used for the generic parameters of the alias. pub substs: SubstsRef<'tcx>, - /// The `DefId` of the `TraitItem` for the associated type `N` if this is a projection, - /// or the `OpaqueType` item if this is an opaque. + /// The `DefId` of the `TraitItem` or `ImplItem` for the associated type `N` depending on whether + /// this is a projection or an inherent projection or the `DefId` of the `OpaqueType` item if + /// this is an opaque. /// /// During codegen, `tcx.type_of(def_id)` can be used to get the type of the /// underlying type if the type is an opaque. @@ -1224,6 +1228,7 @@ pub struct AliasTy<'tcx> { impl<'tcx> AliasTy<'tcx> { pub fn kind(self, tcx: TyCtxt<'tcx>) -> ty::AliasKind { match tcx.def_kind(self.def_id) { + DefKind::AssocTy if let DefKind::Impl { of_trait: false } = tcx.def_kind(tcx.parent(self.def_id)) => ty::Inherent, DefKind::AssocTy | DefKind::ImplTraitPlaceholder => ty::Projection, DefKind::OpaqueTy => ty::Opaque, kind => bug!("unexpected DefKind in AliasTy: {kind:?}"), @@ -1237,6 +1242,17 @@ impl<'tcx> AliasTy<'tcx> { /// The following methods work only with associated type projections. impl<'tcx> AliasTy<'tcx> { + pub fn self_ty(self) -> Ty<'tcx> { + self.substs.type_at(0) + } + + pub fn with_self_ty(self, tcx: TyCtxt<'tcx>, self_ty: Ty<'tcx>) -> Self { + tcx.mk_alias_ty(self.def_id, [self_ty.into()].into_iter().chain(self.substs.iter().skip(1))) + } +} + +/// The following methods work only with trait associated type projections. +impl<'tcx> AliasTy<'tcx> { pub fn trait_def_id(self, tcx: TyCtxt<'tcx>) -> DefId { match tcx.def_kind(self.def_id) { DefKind::AssocTy | DefKind::AssocConst => tcx.parent(self.def_id), @@ -1274,13 +1290,28 @@ impl<'tcx> AliasTy<'tcx> { let def_id = self.trait_def_id(tcx); ty::TraitRef::new(tcx, def_id, self.substs.truncate_to(tcx, tcx.generics_of(def_id))) } +} - pub fn self_ty(self) -> Ty<'tcx> { - self.substs.type_at(0) - } +/// The following methods work only with inherent associated type projections. +impl<'tcx> AliasTy<'tcx> { + /// Transform the substitutions to have the given `impl` substs as the base and the GAT substs on top of that. + /// + /// Does the following transformation: + /// + /// ```text + /// [Self, P_0...P_m] -> [I_0...I_n, P_0...P_m] + /// + /// I_i impl subst + /// P_j GAT subst + /// ``` + pub fn rebase_substs_onto_impl( + self, + impl_substs: ty::SubstsRef<'tcx>, + tcx: TyCtxt<'tcx>, + ) -> ty::SubstsRef<'tcx> { + debug_assert_eq!(self.kind(tcx), ty::Inherent); - pub fn with_self_ty(self, tcx: TyCtxt<'tcx>, self_ty: Ty<'tcx>) -> Self { - tcx.mk_alias_ty(self.def_id, [self_ty.into()].into_iter().chain(self.substs.iter().skip(1))) + tcx.mk_substs_from_iter(impl_substs.into_iter().chain(self.substs.into_iter().skip(1))) } } diff --git a/compiler/rustc_middle/src/ty/visit.rs b/compiler/rustc_middle/src/ty/visit.rs index 5eaa58d69ed..520bb55e031 100644 --- a/compiler/rustc_middle/src/ty/visit.rs +++ b/compiler/rustc_middle/src/ty/visit.rs @@ -49,6 +49,9 @@ pub trait TypeVisitableExt<'tcx>: TypeVisitable<TyCtxt<'tcx>> { fn has_projections(&self) -> bool { self.has_type_flags(TypeFlags::HAS_PROJECTION) } + fn has_inherent_projections(&self) -> bool { + self.has_type_flags(TypeFlags::HAS_TY_INHERENT) + } fn has_opaque_types(&self) -> bool { self.has_type_flags(TypeFlags::HAS_TY_OPAQUE) } |
