From 1d2a312aace24cbced9171f81b54e714d70bb6eb Mon Sep 17 00:00:00 2001 From: Jose Torres Date: Tue, 14 Apr 2026 22:21:08 -0400 Subject: [PATCH 1/6] refactor `TypeRelativePath::AssocItem` to use `AliasTerm`. `probe_inherent_assoc_item` returns `AliasTerm` based fmease suggestins --- .../src/hir_ty_lowering/mod.rs | 29 ++++++++----------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs b/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs index 5a86e8186a5aa..77a9a48b04e9a 100644 --- a/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs +++ b/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs @@ -299,7 +299,7 @@ pub enum PermitVariants { #[derive(Debug, Clone, Copy)] enum TypeRelativePath<'tcx> { - AssocItem(DefId, GenericArgsRef<'tcx>), + AssocItem(ty::AliasTerm<'tcx>), Variant { adt: Ty<'tcx>, variant_did: DefId }, Ctor { ctor_def_id: DefId, args: GenericArgsRef<'tcx> }, } @@ -1476,15 +1476,10 @@ 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, - ); - let ty = Ty::new_alias(tcx, alias_ty); + TypeRelativePath::AssocItem(alias_term) => { + let ty = alias_term.expect_ty(tcx).to_ty(tcx); let ty = self.check_param_uses_if_mcg(ty, span, false); - Ok((ty, tcx.def_kind(def_id), def_id)) + Ok((ty, tcx.def_kind(alias_term.def_id()), alias_term.def_id())) } TypeRelativePath::Variant { adt, variant_did } => { let adt = self.check_param_uses_if_mcg(adt, span, false); @@ -1516,9 +1511,9 @@ 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)); + TypeRelativePath::AssocItem(alias_term) => { + self.require_type_const_attribute(alias_term.def_id(), span)?; + let ct = Const::new_unevaluated(tcx, alias_term.expect_ct(tcx)); let ct = self.check_param_uses_if_mcg(ct, span, false); Ok(ct) } @@ -1648,7 +1643,7 @@ 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(alias_term) = self.probe_inherent_assoc_item( segment, adt_def.did(), self_ty, @@ -1656,7 +1651,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { span, mode.assoc_tag(), )? { - return Ok(TypeRelativePath::AssocItem(did, args)); + return Ok(TypeRelativePath::AssocItem(alias_term)); } } @@ -1690,7 +1685,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { ); } - Ok(TypeRelativePath::AssocItem(item_def_id, args)) + Ok(TypeRelativePath::AssocItem(ty::AliasTerm::new_from_def_id(tcx, item_def_id, args))) } /// Resolve a [type-relative](hir::QPath::TypeRelative) (and type-level) path. @@ -1770,7 +1765,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { block: HirId, span: Span, assoc_tag: ty::AssocTag, - ) -> Result)>, ErrorGuaranteed> { + ) -> Result>, ErrorGuaranteed> { let tcx = self.tcx(); if !tcx.features().inherent_associated_types() { @@ -1853,7 +1848,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { .chain(args.into_iter().skip(parent_args.len())), ); - Ok(Some((assoc_item, args))) + Ok(Some(ty::AliasTerm::new_from_def_id(tcx, assoc_item, args))) } /// Given name and kind search for the assoc item in the provided scope and check if it's accessible[^1]. From be1ff079a3f3fd63ddbb050ea92b549b233bdf03 Mon Sep 17 00:00:00 2001 From: Jose Torres Date: Tue, 12 May 2026 23:15:19 -0400 Subject: [PATCH 2/6] guarding on inherent check --- .../rustc_hir_analysis/src/hir_ty_lowering/mod.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs b/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs index 77a9a48b04e9a..6b5fc86cde9d4 100644 --- a/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs +++ b/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs @@ -1848,7 +1848,18 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { .chain(args.into_iter().skip(parent_args.len())), ); - Ok(Some(ty::AliasTerm::new_from_def_id(tcx, assoc_item, args))) + let kind = match assoc_tag { + ty::AssocTag::Type => ty::AliasTermKind::InherentTy { def_id: assoc_item }, + ty::AssocTag::Const => { + // surface the type_const error here before we construct an AliasTerm with + // mismatched args. related PR #155341 and semi-related issue #156181 + self.require_type_const_attribute(assoc_item, span)?; + ty::AliasTermKind::InherentConst { def_id: assoc_item } + } + ty::AssocTag::Fn => unreachable!(), + }; + + Ok(Some(ty::AliasTerm::new_from_args(tcx, kind, args))) } /// Given name and kind search for the assoc item in the provided scope and check if it's accessible[^1]. From e6fee51ac54a059e097f7593f66204b69f66e8f8 Mon Sep 17 00:00:00 2001 From: Jose Torres Date: Thu, 14 May 2026 22:10:06 -0400 Subject: [PATCH 3/6] remove alias_ty_kind_from_def_id as I noticied now unused --- .../rustc_middle/src/ty/context/impl_interner.rs | 15 --------------- compiler/rustc_type_ir/src/interner.rs | 2 -- 2 files changed, 17 deletions(-) diff --git a/compiler/rustc_middle/src/ty/context/impl_interner.rs b/compiler/rustc_middle/src/ty/context/impl_interner.rs index 19668a4c0f20e..a5ad0e36bff5d 100644 --- a/compiler/rustc_middle/src/ty/context/impl_interner.rs +++ b/compiler/rustc_middle/src/ty/context/impl_interner.rs @@ -204,21 +204,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_from_def_id(self, def_id: DefId) -> ty::AliasTermKind<'tcx> { match self.def_kind(def_id) { DefKind::AssocTy => { diff --git a/compiler/rustc_type_ir/src/interner.rs b/compiler/rustc_type_ir/src/interner.rs index 7cdd82fe1a807..76df263babcf6 100644 --- a/compiler/rustc_type_ir/src/interner.rs +++ b/compiler/rustc_type_ir/src/interner.rs @@ -247,8 +247,6 @@ pub trait Interner: type AdtDef: AdtDef; 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; - // FIXME: remove in favor of explicit construction fn alias_term_kind_from_def_id(self, def_id: Self::DefId) -> ty::AliasTermKind; From 9986acae5f5fa17bc9905c19e651a39ad90197af Mon Sep 17 00:00:00 2001 From: Jose Torres Date: Thu, 14 May 2026 22:11:02 -0400 Subject: [PATCH 4/6] remove new_from_def_id as noticied unused --- compiler/rustc_type_ir/src/ty_kind.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/compiler/rustc_type_ir/src/ty_kind.rs b/compiler/rustc_type_ir/src/ty_kind.rs index 0fc39660015de..96bea0c8cc14f 100644 --- a/compiler/rustc_type_ir/src/ty_kind.rs +++ b/compiler/rustc_type_ir/src/ty_kind.rs @@ -65,9 +65,6 @@ pub enum AliasTyKind { } impl AliasTyKind { - 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 { From 947120814f10ccf477b6c0e8044fcf570a75da92 Mon Sep 17 00:00:00 2001 From: Jose Torres Date: Thu, 14 May 2026 22:11:57 -0400 Subject: [PATCH 5/6] update comment --- compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs | 4 ++-- compiler/rustc_type_ir/src/ty_kind.rs | 1 - 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs b/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs index 6b5fc86cde9d4..fa0530b1d4825 100644 --- a/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs +++ b/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs @@ -1851,8 +1851,8 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { let kind = match assoc_tag { ty::AssocTag::Type => ty::AliasTermKind::InherentTy { def_id: assoc_item }, ty::AssocTag::Const => { - // surface the type_const error here before we construct an AliasTerm with - // mismatched args. related PR #155341 and semi-related issue #156181 + // drop once `InherentConst` accepts IAC-shaped args (issue #156181) + // without this, `new_from_args` errors (#155341). self.require_type_const_attribute(assoc_item, span)?; ty::AliasTermKind::InherentConst { def_id: assoc_item } } diff --git a/compiler/rustc_type_ir/src/ty_kind.rs b/compiler/rustc_type_ir/src/ty_kind.rs index 96bea0c8cc14f..feb6228d98e3a 100644 --- a/compiler/rustc_type_ir/src/ty_kind.rs +++ b/compiler/rustc_type_ir/src/ty_kind.rs @@ -65,7 +65,6 @@ pub enum AliasTyKind { } impl AliasTyKind { - pub fn descr(self) -> &'static str { match self { AliasTyKind::Projection { .. } => "associated type", From 08738f11d6839cfabaa93218195290482beb645a Mon Sep 17 00:00:00 2001 From: Jose Torres Date: Thu, 14 May 2026 22:32:26 -0400 Subject: [PATCH 6/6] inlining --- src/tools/clippy/clippy_utils/src/ty/mod.rs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/tools/clippy/clippy_utils/src/ty/mod.rs b/src/tools/clippy/clippy_utils/src/ty/mod.rs index 4ab21f34de60d..aaaac6ad4c088 100644 --- a/src/tools/clippy/clippy_utils/src/ty/mod.rs +++ b/src/tools/clippy/clippy_utils/src/ty/mod.rs @@ -1048,11 +1048,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 kind = if let DefKind::Impl { of_trait: false } = tcx.def_kind(tcx.parent(assoc_item.def_id)) { + ty::AliasTyKind::Inherent { def_id: assoc_item.def_id } + } else { + ty::AliasTyKind::Projection { def_id: assoc_item.def_id } + }; + + Some(AliasTy::new_from_args(tcx, kind, args)) } helper( tcx,