Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 54 additions & 16 deletions compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,20 @@ impl LowerTypeRelativePathMode {
Self::Const => PermitVariants::No,
}
}

fn inherent_alias_term_kind(self) -> ty::AliasTermKind {
match self {
Self::Type(_) => ty::AliasTermKind::InherentTy,
Self::Const => ty::AliasTermKind::InherentConst,
}
}

fn projection_alias_term_kind(self) -> ty::AliasTermKind {
match self {
Self::Type(_) => ty::AliasTermKind::ProjectionTy,
Self::Const => ty::AliasTermKind::ProjectionConst,
}
}
}

/// Whether to permit a path to resolve to an enum variant.
Expand All @@ -298,7 +312,7 @@ pub enum PermitVariants {

#[derive(Debug, Clone, Copy)]
enum TypeRelativePath<'tcx> {
AssocItem(DefId, GenericArgsRef<'tcx>),
AssocItem(ty::AliasTermKind, DefId, GenericArgsRef<'tcx>),
Variant { adt: Ty<'tcx>, variant_did: DefId },
Ctor { ctor_def_id: DefId, args: GenericArgsRef<'tcx> },
}
Expand Down Expand Up @@ -1400,12 +1414,20 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
span,
LowerTypeRelativePathMode::Type(permit_variants),
)? {
TypeRelativePath::AssocItem(def_id, args) => {
let alias_ty = ty::AliasTy::new_from_args(
tcx,
ty::AliasTyKind::new_from_def_id(tcx, def_id),
args,
);
TypeRelativePath::AssocItem(kind, def_id, args) => {
let alias_ty_kind = match kind {
ty::AliasTermKind::ProjectionTy => ty::Projection { def_id },
ty::AliasTermKind::InherentTy => ty::Inherent { def_id },
ty::AliasTermKind::OpaqueTy
| ty::AliasTermKind::FreeTy
| ty::AliasTermKind::ProjectionConst
| ty::AliasTermKind::InherentConst
| ty::AliasTermKind::FreeConst
| ty::AliasTermKind::UnevaluatedConst => {
unreachable!()
Copy link
Copy Markdown
Contributor Author

@josetorrs josetorrs Apr 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

admittedly not sure if unreachable!() or something else is desired

View changes since the review

}
};
let alias_ty = ty::AliasTy::new_from_args(tcx, alias_ty_kind, args);
let ty = Ty::new_alias(tcx, alias_ty);
let ty = self.check_param_uses_if_mcg(ty, span, false);
Ok((ty, tcx.def_kind(def_id), def_id))
Expand Down Expand Up @@ -1440,12 +1462,24 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
span,
LowerTypeRelativePathMode::Const,
)? {
TypeRelativePath::AssocItem(def_id, args) => {
self.require_type_const_attribute(def_id, span)?;
let ct = Const::new_unevaluated(tcx, ty::UnevaluatedConst::new(def_id, args));
let ct = self.check_param_uses_if_mcg(ct, span, false);
Ok(ct)
}
TypeRelativePath::AssocItem(kind, def_id, args) => match kind {
ty::AliasTermKind::ProjectionConst | ty::AliasTermKind::InherentConst => {
self.require_type_const_attribute(def_id, span)?;
let ct = Const::new_unevaluated(tcx, ty::UnevaluatedConst::new(def_id, args));
let ct = self.check_param_uses_if_mcg(ct, span, false);
Ok(ct)
}
ty::AliasTermKind::ProjectionTy
| ty::AliasTermKind::InherentTy
| ty::AliasTermKind::OpaqueTy
| ty::AliasTermKind::FreeTy
| ty::AliasTermKind::UnevaluatedConst
| ty::AliasTermKind::FreeConst => span_bug!(
span,
"type-relative const path should only produce \
ProjectionConst or InherentConst, got {kind:?}"
),
},
TypeRelativePath::Ctor { ctor_def_id, args } => match tcx.def_kind(ctor_def_id) {
DefKind::Ctor(_, CtorKind::Fn) => {
Ok(ty::Const::zero_sized(tcx, Ty::new_fn_def(tcx, ctor_def_id, args)))
Expand Down Expand Up @@ -1572,15 +1606,19 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
}

// FIXME(inherent_associated_types, #106719): Support self types other than ADTs.
if let Some((did, args)) = self.probe_inherent_assoc_item(
if let Some((def_id, args)) = self.probe_inherent_assoc_item(
segment,
adt_def.did(),
self_ty,
qpath_hir_id,
span,
mode.assoc_tag(),
)? {
return Ok(TypeRelativePath::AssocItem(did, args));
return Ok(TypeRelativePath::AssocItem(
mode.inherent_alias_term_kind(),
def_id,
args,
));
}
}

Expand Down Expand Up @@ -1614,7 +1652,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
);
}

Ok(TypeRelativePath::AssocItem(item_def_id, args))
Ok(TypeRelativePath::AssocItem(mode.projection_alias_term_kind(), item_def_id, args))
}

/// Resolve a [type-relative](hir::QPath::TypeRelative) (and type-level) path.
Expand Down
15 changes: 0 additions & 15 deletions compiler/rustc_middle/src/ty/context/impl_interner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,21 +187,6 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
self.adt_def(adt_def_id)
}

fn alias_ty_kind_from_def_id(self, def_id: DefId) -> ty::AliasTyKind<'tcx> {
match self.def_kind(def_id) {
DefKind::AssocTy
if let DefKind::Impl { of_trait: false } = self.def_kind(self.parent(def_id)) =>
{
ty::Inherent { def_id }
}
DefKind::AssocTy => ty::Projection { def_id },

DefKind::OpaqueTy => ty::Opaque { def_id },
DefKind::TyAlias => ty::Free { def_id },
kind => bug!("unexpected DefKind in AliasTy: {kind:?}"),
}
}

fn alias_term_kind(self, alias: ty::AliasTerm<'tcx>) -> ty::AliasTermKind {
match self.def_kind(alias.def_id) {
DefKind::AssocTy => {
Expand Down
2 changes: 0 additions & 2 deletions compiler/rustc_type_ir/src/interner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,6 @@ pub trait Interner:
type AdtDef: AdtDef<Self>;
fn adt_def(self, adt_def_id: Self::AdtId) -> Self::AdtDef;

fn alias_ty_kind_from_def_id(self, def_id: Self::DefId) -> ty::AliasTyKind<Self>;

fn alias_term_kind(self, alias: ty::AliasTerm<Self>) -> ty::AliasTermKind;

fn trait_ref_and_own_args_for_alias(
Expand Down
4 changes: 0 additions & 4 deletions compiler/rustc_type_ir/src/ty_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,6 @@ pub enum AliasTyKind<I: Interner> {
}

impl<I: Interner> AliasTyKind<I> {
pub fn new_from_def_id(interner: I, def_id: I::DefId) -> Self {
interner.alias_ty_kind_from_def_id(def_id)
}

pub fn descr(self) -> &'static str {
match self {
AliasTyKind::Projection { .. } => "associated type",
Expand Down
18 changes: 10 additions & 8 deletions src/tools/clippy/clippy_utils/src/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ use rustc_middle::traits::EvaluationResult;
use rustc_middle::ty::adjustment::{Adjust, Adjustment, DerefAdjustKind};
use rustc_middle::ty::layout::ValidityRequirement;
use rustc_middle::ty::{
self, AdtDef, AliasTy, AssocItem, AssocTag, Binder, BoundRegion, BoundVarIndexKind, FnSig, GenericArg,
GenericArgKind, GenericArgsRef, IntTy, Region, RegionKind, TraitRef, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable,
TypeVisitableExt, TypeVisitor, UintTy, Upcast, VariantDef, VariantDiscr,
self, AdtDef, AliasTy, AssocContainer, AssocItem, AssocTag, Binder, BoundRegion, BoundVarIndexKind, FnSig,
GenericArg, GenericArgKind, GenericArgsRef, IntTy, Region, RegionKind, TraitRef, Ty, TyCtxt,
TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor, UintTy, Upcast, VariantDef, VariantDiscr,
};
use rustc_span::symbol::Ident;
use rustc_span::{DUMMY_SP, Span, Symbol};
Expand Down Expand Up @@ -1023,11 +1023,13 @@ pub fn make_projection<'tcx>(
#[cfg(debug_assertions)]
assert_generic_args_match(tcx, assoc_item.def_id, args);

Some(AliasTy::new_from_args(
tcx,
ty::AliasTyKind::new_from_def_id(tcx, assoc_item.def_id),
args,
))
let alias_kind = match assoc_item.container {
AssocContainer::Trait | AssocContainer::TraitImpl(_) => {
ty::Projection { def_id: assoc_item.def_id }
}
AssocContainer::InherentImpl => ty::Inherent { def_id: assoc_item.def_id },
};
Some(AliasTy::new_from_args(tcx, alias_kind, args))
}
helper(
tcx,
Expand Down
Loading