From 6ab50d53e332190cbebdf709602c16732ba7aab5 Mon Sep 17 00:00:00 2001 From: Waffle Lapkin Date: Mon, 6 Apr 2026 14:50:54 +0200 Subject: [PATCH] `ty::Alias` review comments --- .../src/error_reporting/infer/mod.rs | 7 +-- compiler/rustc_type_ir/src/interner.rs | 1 + compiler/rustc_type_ir/src/predicate.rs | 60 ++++++------------- compiler/rustc_type_ir/src/relate.rs | 2 - compiler/rustc_type_ir/src/ty_kind.rs | 33 +++++----- 5 files changed, 38 insertions(+), 65 deletions(-) diff --git a/compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs b/compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs index a898619b6faa..19a6c5dfe5ee 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs @@ -54,7 +54,6 @@ use rustc_data_structures::fx::{FxIndexMap, FxIndexSet}; use rustc_errors::{Applicability, Diag, DiagStyledString, IntoDiagArg, StringPart, pluralize}; use rustc_hir as hir; -use rustc_hir::def::DefKind; use rustc_hir::def_id::{CRATE_DEF_ID, DefId}; use rustc_hir::intravisit::Visitor; use rustc_hir::lang_items::LangItem; @@ -182,11 +181,7 @@ pub fn report_mismatched_consts( pub fn get_impl_future_output_ty(&self, ty: Ty<'tcx>) -> Option> { let (def_id, args) = match *ty.kind() { - ty::Alias(ty::AliasTy { kind, args, .. }) - if self.tcx.def_kind(kind.def_id()) == DefKind::OpaqueTy => - { - (kind.def_id(), args) - } + ty::Alias(ty::AliasTy { kind: ty::Opaque { def_id }, args, .. }) => (def_id, args), ty::Alias(ty::AliasTy { kind, args, .. }) if self.tcx.is_impl_trait_in_trait(kind.def_id()) => { diff --git a/compiler/rustc_type_ir/src/interner.rs b/compiler/rustc_type_ir/src/interner.rs index 799d57b1875c..baae3f2ebe36 100644 --- a/compiler/rustc_type_ir/src/interner.rs +++ b/compiler/rustc_type_ir/src/interner.rs @@ -196,6 +196,7 @@ fn canonical_param_env_cache_get_or_insert( type VariancesOf: Copy + Debug + SliceLike; fn variances_of(self, def_id: Self::DefId) -> Self::VariancesOf; + // FIXME: remove `def_id` param after `AliasTermKind` contains `def_id` within fn opt_alias_variances( self, kind: impl Into, diff --git a/compiler/rustc_type_ir/src/predicate.rs b/compiler/rustc_type_ir/src/predicate.rs index bfe62be969d3..5277e0a992fc 100644 --- a/compiler/rustc_type_ir/src/predicate.rs +++ b/compiler/rustc_type_ir/src/predicate.rs @@ -644,52 +644,26 @@ pub fn kind(self, interner: I) -> AliasTermKind { } pub fn to_term(self, interner: I) -> I::Term { - match self.kind(interner) { - AliasTermKind::ProjectionTy => Ty::new_alias( - interner, - ty::AliasTy { - kind: ty::AliasTyKind::Projection { def_id: self.def_id }, - args: self.args, - _use_alias_ty_new_instead: (), - }, - ) - .into(), - AliasTermKind::InherentTy => Ty::new_alias( - interner, - ty::AliasTy { - kind: ty::AliasTyKind::Inherent { def_id: self.def_id }, - args: self.args, - _use_alias_ty_new_instead: (), - }, - ) - .into(), - AliasTermKind::OpaqueTy => Ty::new_alias( - interner, - ty::AliasTy { - kind: ty::AliasTyKind::Opaque { def_id: self.def_id }, - args: self.args, - _use_alias_ty_new_instead: (), - }, - ) - .into(), - AliasTermKind::FreeTy => Ty::new_alias( - interner, - ty::AliasTy { - kind: ty::AliasTyKind::Free { def_id: self.def_id }, - args: self.args, - _use_alias_ty_new_instead: (), - }, - ) - .into(), + let alias_ty_kind = match self.kind(interner) { AliasTermKind::FreeConst | AliasTermKind::InherentConst | AliasTermKind::UnevaluatedConst - | AliasTermKind::ProjectionConst => I::Const::new_unevaluated( - interner, - ty::UnevaluatedConst::new(self.def_id.try_into().unwrap(), self.args), - ) - .into(), - } + | AliasTermKind::ProjectionConst => { + return I::Const::new_unevaluated( + interner, + ty::UnevaluatedConst::new(self.def_id.try_into().unwrap(), self.args), + ) + .into(); + } + + AliasTermKind::ProjectionTy => ty::Projection { def_id: self.def_id }, + AliasTermKind::InherentTy => ty::Inherent { def_id: self.def_id }, + AliasTermKind::OpaqueTy => ty::Opaque { def_id: self.def_id }, + AliasTermKind::FreeTy => ty::Free { def_id: self.def_id }, + }; + + Ty::new_alias(interner, ty::AliasTy::new_from_args(interner, alias_ty_kind, self.args)) + .into() } } diff --git a/compiler/rustc_type_ir/src/relate.rs b/compiler/rustc_type_ir/src/relate.rs index 0edfb0aa1644..61095a00d041 100644 --- a/compiler/rustc_type_ir/src/relate.rs +++ b/compiler/rustc_type_ir/src/relate.rs @@ -504,8 +504,6 @@ pub fn structurally_relate_tys>( // Alias tend to mostly already be handled downstream due to normalization. (ty::Alias(a), ty::Alias(b)) => { let alias_ty = relation.relate(a, b)?; - assert_eq!(a.kind, b.kind); - assert_eq!(a.kind, alias_ty.kind); Ok(Ty::new_alias(cx, alias_ty)) } diff --git a/compiler/rustc_type_ir/src/ty_kind.rs b/compiler/rustc_type_ir/src/ty_kind.rs index 6685f3d4fa63..9c57d04159cc 100644 --- a/compiler/rustc_type_ir/src/ty_kind.rs +++ b/compiler/rustc_type_ir/src/ty_kind.rs @@ -31,25 +31,30 @@ pub enum AliasTyKind { /// A projection `::AssocType`. /// /// Can get normalized away if monomorphic enough. - Projection { - /// 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, `interner.type_of(def_id)` can be used to get the type of the - /// underlying type if the type is an opaque. - /// - /// Note that if this is an associated type, this is not the `DefId` of the - /// `TraitRef` containing this associated type, which is in `interner.associated_item(def_id).container`, - /// aka. `interner.parent(def_id)`. - def_id: I::DefId, - }, + /// + /// The `def_id` is the `DefId` of the `TraitItem` for the associated type. + /// + /// Note that the `def_id` is not the `DefId` of the `TraitRef` containing this + /// associated type, which is in `interner.associated_item(def_id).container`, + /// aka. `interner.parent(def_id)`. + Projection { def_id: I::DefId }, + /// An associated type in an inherent `impl` + /// + /// The `def_id` is the `DefId` of the `ImplItem` for the associated type. Inherent { def_id: I::DefId }, + /// An opaque type (usually from `impl Trait` in type aliases or function return types) /// - /// Can only be normalized away in PostAnalysis mode or its defining scope. + /// `def_id` is the `DefId` of the `OpaqueType` item. + /// + /// + /// Can only be normalized away in `PostAnalysis` mode or its defining scope. + /// + /// During codegen, `interner.type_of(def_id)` can be used to get the type of the + /// underlying type if the type is an opaque. Opaque { def_id: I::DefId }, + /// A type alias that actually checks its trait bounds. /// /// Currently only used if the type alias references opaque types.