diff --git a/compiler/rustc_borrowck/src/region_infer/opaque_types/member_constraints.rs b/compiler/rustc_borrowck/src/region_infer/opaque_types/member_constraints.rs index d331ef6a6df51..922e01a92a29c 100644 --- a/compiler/rustc_borrowck/src/region_infer/opaque_types/member_constraints.rs +++ b/compiler/rustc_borrowck/src/region_infer/opaque_types/member_constraints.rs @@ -177,7 +177,7 @@ impl<'tcx> TypeVisitor> for CollectMemberConstraintsVisitor<'_, '_, | ty::Coroutine(def_id, args) => self.visit_closure_args(def_id, args), ty::Alias(ty::AliasTy { kind, args, .. }) - if let Some(variances) = self.cx().opt_alias_variances(kind, kind.def_id()) => + if let Some(variances) = self.cx().opt_alias_variances(kind) => { // Skip lifetime parameters that are not captured, since they do // not need member constraints registered for them; we'll erase diff --git a/compiler/rustc_borrowck/src/region_infer/opaque_types/mod.rs b/compiler/rustc_borrowck/src/region_infer/opaque_types/mod.rs index 49b3e75b25e39..ce402681fbdf8 100644 --- a/compiler/rustc_borrowck/src/region_infer/opaque_types/mod.rs +++ b/compiler/rustc_borrowck/src/region_infer/opaque_types/mod.rs @@ -501,7 +501,7 @@ impl<'tcx> FallibleTypeFolder> for ToArgRegionsFolder<'_, 'tcx> { } ty::Alias(ty::AliasTy { kind, args, .. }) - if let Some(variances) = tcx.opt_alias_variances(kind, kind.def_id()) => + if let Some(variances) = tcx.opt_alias_variances(kind) => { let args = tcx.mk_args_from_iter(std::iter::zip(variances, args.iter()).map( |(&v, s)| { diff --git a/compiler/rustc_hir_analysis/src/check/compare_impl_item.rs b/compiler/rustc_hir_analysis/src/check/compare_impl_item.rs index ffc4e41d3e3a1..234231ed37fed 100644 --- a/compiler/rustc_hir_analysis/src/check/compare_impl_item.rs +++ b/compiler/rustc_hir_analysis/src/check/compare_impl_item.rs @@ -2735,7 +2735,7 @@ fn param_env_with_gat_bounds<'tcx>( _ => predicates.push( ty::Binder::bind_with_vars( ty::ProjectionPredicate { - projection_term: ty::AliasTerm::new_from_args( + projection_term: ty::AliasTerm::new_from_def_id( tcx, trait_ty.def_id, rebased_args, diff --git a/compiler/rustc_hir_analysis/src/check/mod.rs b/compiler/rustc_hir_analysis/src/check/mod.rs index 6862b6fe863f8..fcdbb11b297c1 100644 --- a/compiler/rustc_hir_analysis/src/check/mod.rs +++ b/compiler/rustc_hir_analysis/src/check/mod.rs @@ -370,10 +370,10 @@ fn bounds_from_generic_predicates<'tcx>( let mut projections_str = vec![]; for projection in &projections { let p = projection.skip_binder(); - if bound == tcx.parent(p.projection_term.def_id) + if bound == tcx.parent(p.projection_term.def_id()) && p.projection_term.self_ty() == ty { - let name = tcx.item_name(p.projection_term.def_id); + let name = tcx.item_name(p.projection_term.def_id()); projections_str.push(format!("{} = {}", name, p.term)); } } diff --git a/compiler/rustc_hir_analysis/src/check/wfcheck.rs b/compiler/rustc_hir_analysis/src/check/wfcheck.rs index 07ad2a79dc7ad..32610619fe713 100644 --- a/compiler/rustc_hir_analysis/src/check/wfcheck.rs +++ b/compiler/rustc_hir_analysis/src/check/wfcheck.rs @@ -1655,7 +1655,7 @@ pub(super) fn check_where_clauses<'tcx>(wfcx: &WfCheckingCtxt<'_, 'tcx>, def_id: .map_bound(|pred| { pred.term.as_const().map(|ct| { let assoc_const_ty = tcx - .type_of(pred.projection_term.def_id) + .type_of(pred.projection_term.def_id()) .instantiate(tcx, pred.projection_term.args) .skip_norm_wip(); ty::ClauseKind::ConstArgHasType(ct, assoc_const_ty) diff --git a/compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs b/compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs index bf6b7e24f14e3..6e2c7a82af3b1 100644 --- a/compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs +++ b/compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs @@ -526,7 +526,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { ); debug!(?alias_args); - ty::AliasTerm::new_from_args(tcx, assoc_item.def_id, alias_args) + ty::AliasTerm::new_from_def_id(tcx, assoc_item.def_id, alias_args) }) }; @@ -543,7 +543,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { hir::Term::Ty(ty) => self.lower_ty(ty).into(), hir::Term::Const(ct) => { let ty = projection_term.map_bound(|alias| { - tcx.type_of(alias.def_id).instantiate(tcx, alias.args).skip_norm_wip() + tcx.type_of(alias.def_id()).instantiate(tcx, alias.args).skip_norm_wip() }); let ty = check_assoc_const_binding_type( self, 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 a027c552d92b9..a43e373415ab7 100644 --- a/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs +++ b/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs @@ -1364,7 +1364,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { &item_segment, trait_ref.args, ); - ty::AliasTerm::new_from_args( + ty::AliasTerm::new_from_def_id( tcx, assoc_item.def_id, alias_args, @@ -1374,7 +1374,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { // FIXME(mgca): code duplication with other places we lower // the rhs' of associated const bindings let ty = projection_term.map_bound(|alias| { - tcx.type_of(alias.def_id) + tcx.type_of(alias.def_id()) .instantiate(tcx, alias.args) .skip_norm_wip() }); diff --git a/compiler/rustc_hir_typeck/src/closure.rs b/compiler/rustc_hir_typeck/src/closure.rs index d90cbaf935a71..d8240a0c9d70e 100644 --- a/compiler/rustc_hir_typeck/src/closure.rs +++ b/compiler/rustc_hir_typeck/src/closure.rs @@ -1057,11 +1057,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { // The `Future` trait has only one associated item, `Output`, // so check that this is what we see. let output_assoc_item = self.tcx.associated_item_def_ids(trait_def_id)[0]; - if output_assoc_item != predicate.projection_term.def_id { + if output_assoc_item != predicate.projection_term.def_id() { span_bug!( cause_span, "projecting associated item `{:?}` from future, which is not Output `{:?}`", - predicate.projection_term.def_id, + predicate.projection_term.kind, output_assoc_item, ); } diff --git a/compiler/rustc_infer/src/infer/outlives/for_liveness.rs b/compiler/rustc_infer/src/infer/outlives/for_liveness.rs index a550621a82d60..a1991039e2a1e 100644 --- a/compiler/rustc_infer/src/infer/outlives/for_liveness.rs +++ b/compiler/rustc_infer/src/infer/outlives/for_liveness.rs @@ -95,7 +95,7 @@ where } else { // Skip lifetime parameters that are not captured, since they do // not need to be live. - let variances = tcx.opt_alias_variances(kind, kind.def_id()); + let variances = tcx.opt_alias_variances(kind); for (idx, s) in args.iter().enumerate() { if variances.map(|variances| variances[idx]) != Some(ty::Bivariant) { diff --git a/compiler/rustc_infer/src/infer/outlives/obligations.rs b/compiler/rustc_infer/src/infer/outlives/obligations.rs index 28e1b07182457..e98bee3b12cde 100644 --- a/compiler/rustc_infer/src/infer/outlives/obligations.rs +++ b/compiler/rustc_infer/src/infer/outlives/obligations.rs @@ -485,7 +485,7 @@ where && (alias_ty.has_infer_regions() || matches!(kind, ty::Opaque { .. })) { debug!("no declared bounds"); - let opt_variances = self.tcx.opt_alias_variances(kind, kind.def_id()); + let opt_variances = self.tcx.opt_alias_variances(kind); self.args_must_outlive(alias_ty.args, origin, region, opt_variances); return; } diff --git a/compiler/rustc_infer/src/infer/projection.rs b/compiler/rustc_infer/src/infer/projection.rs index 2a4f9db8963c8..81bb5b42401b2 100644 --- a/compiler/rustc_infer/src/infer/projection.rs +++ b/compiler/rustc_infer/src/infer/projection.rs @@ -22,7 +22,7 @@ impl<'tcx> InferCtxt<'tcx> { ) -> Term<'tcx> { debug_assert!(!self.next_trait_solver()); - let span = self.tcx.def_span(alias_term.def_id); + let span = self.tcx.def_span(alias_term.def_id()); let infer_var = if alias_term.kind(self.tcx).is_type() { self.next_ty_var(span).into() } else { diff --git a/compiler/rustc_infer/src/infer/relate/generalize.rs b/compiler/rustc_infer/src/infer/relate/generalize.rs index 0229af53f2a67..5ec56b3e9dc4a 100644 --- a/compiler/rustc_infer/src/infer/relate/generalize.rs +++ b/compiler/rustc_infer/src/infer/relate/generalize.rs @@ -184,11 +184,12 @@ impl<'tcx> InferCtxt<'tcx> { relation.register_predicates([ty::PredicateKind::AliasRelate(lhs, rhs, direction)]); } else { - let Some(source_alias) = source_term.to_alias_term() else { + let Some(source_alias) = source_term.to_alias_term(self.tcx) else { bug!("generalized `{source_term:?} to infer, not an alias"); }; match source_alias.kind(self.tcx) { - ty::AliasTermKind::ProjectionTy | ty::AliasTermKind::ProjectionConst => { + ty::AliasTermKind::ProjectionTy { .. } + | ty::AliasTermKind::ProjectionConst { .. } => { // FIXME: This does not handle subtyping correctly, we could // instead create a new inference variable `?normalized_source`, emitting // `Projection(normalized_source, ?ty_normalized)` and @@ -199,14 +200,14 @@ impl<'tcx> InferCtxt<'tcx> { }]); } // The old solver only accepts projection predicates for associated types. - ty::AliasTermKind::InherentTy - | ty::AliasTermKind::FreeTy - | ty::AliasTermKind::OpaqueTy => { + ty::AliasTermKind::InherentTy { .. } + | ty::AliasTermKind::FreeTy { .. } + | ty::AliasTermKind::OpaqueTy { .. } => { return Err(TypeError::CyclicTy(source_term.expect_type())); } - ty::AliasTermKind::InherentConst - | ty::AliasTermKind::FreeConst - | ty::AliasTermKind::UnevaluatedConst => { + ty::AliasTermKind::InherentConst { .. } + | ty::AliasTermKind::FreeConst { .. } + | ty::AliasTermKind::UnevaluatedConst { .. } => { return Err(TypeError::CyclicConst(source_term.expect_const())); } } @@ -755,7 +756,8 @@ impl<'tcx> TypeRelation> for Generalizer<'_, 'tcx> { // path), as doing this new No path breaks some GCE things. I expect GCE to be // ripped out soon so this shouldn't matter soon. StructurallyRelateAliases::No if !self.cx().features().generic_const_exprs() => { - self.generalize_alias_term(uv.into()).map(|v| v.expect_const()) + self.generalize_alias_term(ty::AliasTerm::from_unevaluated_const(self.cx(), uv)) + .map(|v| v.expect_const()) } _ => { let ty::UnevaluatedConst { def, args } = uv; diff --git a/compiler/rustc_lint/src/opaque_hidden_inferred_bound.rs b/compiler/rustc_lint/src/opaque_hidden_inferred_bound.rs index 6947ff9d2c40b..97df4ebc8ae13 100644 --- a/compiler/rustc_lint/src/opaque_hidden_inferred_bound.rs +++ b/compiler/rustc_lint/src/opaque_hidden_inferred_bound.rs @@ -132,7 +132,7 @@ impl<'tcx> LateLintPass<'tcx> for OpaqueHiddenInferredBound { let proj_ty = Ty::new_projection_from_args( cx.tcx, - proj.projection_term.def_id, + proj.projection_term.def_id(), proj.projection_term.args, ); // For every instance of the projection type in the bounds, @@ -149,7 +149,7 @@ impl<'tcx> LateLintPass<'tcx> for OpaqueHiddenInferredBound { // with `impl Send: OtherTrait`. for (assoc_pred, assoc_pred_span) in cx .tcx - .explicit_item_bounds(proj.projection_term.def_id) + .explicit_item_bounds(proj.projection_term.def_id()) .iter_instantiated_copied(cx.tcx, proj.projection_term.args) .map(Unnormalized::skip_norm_wip) { diff --git a/compiler/rustc_middle/src/ty/context/impl_interner.rs b/compiler/rustc_middle/src/ty/context/impl_interner.rs index 733985c606e22..ccda7aeb910ca 100644 --- a/compiler/rustc_middle/src/ty/context/impl_interner.rs +++ b/compiler/rustc_middle/src/ty/context/impl_interner.rs @@ -166,10 +166,9 @@ impl<'tcx> Interner for TyCtxt<'tcx> { fn opt_alias_variances( self, - kind: impl Into, - def_id: DefId, + kind: impl Into>, ) -> Option<&'tcx [ty::Variance]> { - self.opt_alias_variances(kind, def_id) + self.opt_alias_variances(kind) } fn type_of(self, def_id: DefId) -> ty::EarlyBinder<'tcx, Ty<'tcx>> { @@ -205,29 +204,27 @@ impl<'tcx> Interner for TyCtxt<'tcx> { } } - fn alias_term_kind(self, alias: ty::AliasTerm<'tcx>) -> ty::AliasTermKind { - match self.def_kind(alias.def_id) { + fn alias_term_kind_from_def_id(self, def_id: DefId) -> ty::AliasTermKind<'tcx> { + match self.def_kind(def_id) { DefKind::AssocTy => { - if let DefKind::Impl { of_trait: false } = self.def_kind(self.parent(alias.def_id)) - { - ty::AliasTermKind::InherentTy + if let DefKind::Impl { of_trait: false } = self.def_kind(self.parent(def_id)) { + ty::AliasTermKind::InherentTy { def_id } } else { - ty::AliasTermKind::ProjectionTy + ty::AliasTermKind::ProjectionTy { def_id } } } DefKind::AssocConst { .. } => { - if let DefKind::Impl { of_trait: false } = self.def_kind(self.parent(alias.def_id)) - { - ty::AliasTermKind::InherentConst + if let DefKind::Impl { of_trait: false } = self.def_kind(self.parent(def_id)) { + ty::AliasTermKind::InherentConst { def_id } } else { - ty::AliasTermKind::ProjectionConst + ty::AliasTermKind::ProjectionConst { def_id } } } - DefKind::OpaqueTy => ty::AliasTermKind::OpaqueTy, - DefKind::TyAlias => ty::AliasTermKind::FreeTy, - DefKind::Const { .. } => ty::AliasTermKind::FreeConst, + DefKind::OpaqueTy => ty::AliasTermKind::OpaqueTy { def_id }, + DefKind::TyAlias => ty::AliasTermKind::FreeTy { def_id }, + DefKind::Const { .. } => ty::AliasTermKind::FreeConst { def_id }, DefKind::AnonConst | DefKind::Ctor(_, CtorKind::Const) => { - ty::AliasTermKind::UnevaluatedConst + ty::AliasTermKind::UnevaluatedConst { def_id } } kind => bug!("unexpected DefKind in AliasTy: {kind:?}"), } diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index 56a7abfac6635..b935f996a464d 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -91,9 +91,9 @@ pub use self::list::{List, ListWithCachedTypeInfo}; pub use self::opaque_types::OpaqueTypeKey; pub use self::pattern::{Pattern, PatternKind}; pub use self::predicate::{ - AliasTerm, ArgOutlivesPredicate, Clause, ClauseKind, CoercePredicate, ExistentialPredicate, - ExistentialPredicateStableCmpExt, ExistentialProjection, ExistentialTraitRef, - HostEffectPredicate, NormalizesTo, OutlivesPredicate, PolyCoercePredicate, + AliasTerm, AliasTermKind, ArgOutlivesPredicate, Clause, ClauseKind, CoercePredicate, + ExistentialPredicate, ExistentialPredicateStableCmpExt, ExistentialProjection, + ExistentialTraitRef, HostEffectPredicate, NormalizesTo, OutlivesPredicate, PolyCoercePredicate, PolyExistentialPredicate, PolyExistentialProjection, PolyExistentialTraitRef, PolyProjectionPredicate, PolyRegionOutlivesPredicate, PolySubtypePredicate, PolyTraitPredicate, PolyTraitRef, PolyTypeOutlivesPredicate, Predicate, PredicateKind, ProjectionPredicate, @@ -607,14 +607,14 @@ impl<'tcx> Term<'tcx> { } } - pub fn to_alias_term(self) -> Option> { + pub fn to_alias_term(self, tcx: TyCtxt<'tcx>) -> Option> { match self.kind() { TermKind::Ty(ty) => match *ty.kind() { ty::Alias(alias_ty) => Some(alias_ty.into()), _ => None, }, TermKind::Const(ct) => match ct.kind() { - ConstKind::Unevaluated(uv) => Some(uv.into()), + ConstKind::Unevaluated(uv) => Some(AliasTerm::from_unevaluated_const(tcx, uv)), _ => None, }, } diff --git a/compiler/rustc_middle/src/ty/predicate.rs b/compiler/rustc_middle/src/ty/predicate.rs index 47175f404cf34..d77d5c99f0009 100644 --- a/compiler/rustc_middle/src/ty/predicate.rs +++ b/compiler/rustc_middle/src/ty/predicate.rs @@ -11,6 +11,7 @@ use crate::ty::{ pub type TraitRef<'tcx> = ir::TraitRef>; pub type AliasTerm<'tcx> = ir::AliasTerm>; +pub type AliasTermKind<'tcx> = ir::AliasTermKind>; pub type ProjectionPredicate<'tcx> = ir::ProjectionPredicate>; pub type ExistentialPredicate<'tcx> = ir::ExistentialPredicate>; pub type ExistentialTraitRef<'tcx> = ir::ExistentialTraitRef>; @@ -656,7 +657,7 @@ mod size_asserts { use super::*; // tidy-alphabetical-start - static_assert_size!(PredicateKind<'_>, 32); - static_assert_size!(WithCachedTypeInfo>, 56); + static_assert_size!(PredicateKind<'_>, 40); + static_assert_size!(WithCachedTypeInfo>, 64); // tidy-alphabetical-end } diff --git a/compiler/rustc_middle/src/ty/print/pretty.rs b/compiler/rustc_middle/src/ty/print/pretty.rs index d3132d3f65780..1b2332098665a 100644 --- a/compiler/rustc_middle/src/ty/print/pretty.rs +++ b/compiler/rustc_middle/src/ty/print/pretty.rs @@ -1346,7 +1346,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write { &mut self, alias_ty: ty::AliasTerm<'tcx>, ) -> Result<(), PrintError> { - let def_key = self.tcx().def_key(alias_ty.def_id); + let def_key = self.tcx().def_key(alias_ty.def_id()); self.print_path_with_generic_args( |p| { p.print_path_with_simple( @@ -3158,22 +3158,22 @@ define_print! { ty::AliasTerm<'tcx> { match self.kind(p.tcx()) { - ty::AliasTermKind::InherentTy | ty::AliasTermKind::InherentConst => p.pretty_print_inherent_projection(*self)?, - ty::AliasTermKind::ProjectionTy => { + ty::AliasTermKind::InherentTy {..} | ty::AliasTermKind::InherentConst {..} => p.pretty_print_inherent_projection(*self)?, + ty::AliasTermKind::ProjectionTy { def_id } => { if !(p.should_print_verbose() || with_reduced_queries()) - && p.tcx().is_impl_trait_in_trait(self.def_id) + && p.tcx().is_impl_trait_in_trait(def_id) { - p.pretty_print_rpitit(self.def_id, self.args)?; + p.pretty_print_rpitit(def_id, self.args)?; } else { - p.print_def_path(self.def_id, self.args)?; + p.print_def_path(def_id, self.args)?; } } - ty::AliasTermKind::FreeTy - | ty::AliasTermKind::FreeConst - | ty::AliasTermKind::OpaqueTy - | ty::AliasTermKind::UnevaluatedConst - | ty::AliasTermKind::ProjectionConst => { - p.print_def_path(self.def_id, self.args)?; + ty::AliasTermKind::FreeTy { def_id } + | ty::AliasTermKind::FreeConst { def_id } + | ty::AliasTermKind::OpaqueTy { def_id } + | ty::AliasTermKind::UnevaluatedConst { def_id } + | ty::AliasTermKind::ProjectionConst { def_id } => { + p.print_def_path(def_id, self.args)?; } } } diff --git a/compiler/rustc_middle/src/ty/util.rs b/compiler/rustc_middle/src/ty/util.rs index 07baf5b49d659..775d33bdebd34 100644 --- a/compiler/rustc_middle/src/ty/util.rs +++ b/compiler/rustc_middle/src/ty/util.rs @@ -939,24 +939,23 @@ impl<'tcx> TyCtxt<'tcx> { // its (un)captured regions. pub fn opt_alias_variances( self, - kind: impl Into, - def_id: DefId, + kind: impl Into>, ) -> Option<&'tcx [ty::Variance]> { match kind.into() { - ty::AliasTermKind::ProjectionTy => { + ty::AliasTermKind::ProjectionTy { def_id } => { if self.is_impl_trait_in_trait(def_id) { Some(self.variances_of(def_id)) } else { None } } - ty::AliasTermKind::OpaqueTy => Some(self.variances_of(def_id)), - ty::AliasTermKind::InherentTy - | ty::AliasTermKind::InherentConst - | ty::AliasTermKind::FreeTy - | ty::AliasTermKind::FreeConst - | ty::AliasTermKind::UnevaluatedConst - | ty::AliasTermKind::ProjectionConst => None, + ty::AliasTermKind::OpaqueTy { def_id } => Some(self.variances_of(def_id)), + ty::AliasTermKind::InherentTy { .. } + | ty::AliasTermKind::InherentConst { .. } + | ty::AliasTermKind::FreeTy { .. } + | ty::AliasTermKind::FreeConst { .. } + | ty::AliasTermKind::UnevaluatedConst { .. } + | ty::AliasTermKind::ProjectionConst { .. } => None, } } } diff --git a/compiler/rustc_next_trait_solver/src/solve/alias_relate.rs b/compiler/rustc_next_trait_solver/src/solve/alias_relate.rs index f7bd460094328..7c79dba61a6d1 100644 --- a/compiler/rustc_next_trait_solver/src/solve/alias_relate.rs +++ b/compiler/rustc_next_trait_solver/src/solve/alias_relate.rs @@ -41,14 +41,14 @@ where // `{type error}` if the alias still contains infer vars, so we also // accept alias-relate goals where one of the terms is an error. debug_assert!( - lhs.to_alias_term().is_some() - || rhs.to_alias_term().is_some() + lhs.to_alias_term(self.cx()).is_some() + || rhs.to_alias_term(self.cx()).is_some() || lhs.is_error() || rhs.is_error() ); // Structurally normalize the lhs. - let lhs = if let Some(alias) = lhs.to_alias_term() { + let lhs = if let Some(alias) = lhs.to_alias_term(self.cx()) { let term = self.next_term_infer_of_kind(lhs); self.add_goal( GoalSource::TypeRelating, @@ -60,7 +60,7 @@ where }; // Structurally normalize the rhs. - let rhs = if let Some(alias) = rhs.to_alias_term() { + let rhs = if let Some(alias) = rhs.to_alias_term(self.cx()) { let term = self.next_term_infer_of_kind(rhs); self.add_goal( GoalSource::TypeRelating, @@ -87,7 +87,7 @@ where ty::AliasRelationDirection::Equate => ty::Invariant, ty::AliasRelationDirection::Subtype => ty::Covariant, }; - match (lhs.to_alias_term(), rhs.to_alias_term()) { + match (lhs.to_alias_term(self.cx()), rhs.to_alias_term(self.cx())) { (None, None) => { self.relate(param_env, lhs, variance, rhs)?; self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes) diff --git a/compiler/rustc_next_trait_solver/src/solve/assembly/structural_traits.rs b/compiler/rustc_next_trait_solver/src/solve/assembly/structural_traits.rs index c3bffb5475c76..0bbc6f483104f 100644 --- a/compiler/rustc_next_trait_solver/src/solve/assembly/structural_traits.rs +++ b/compiler/rustc_next_trait_solver/src/solve/assembly/structural_traits.rs @@ -958,7 +958,7 @@ where source_projection: ty::Binder>, target_projection: ty::AliasTerm, ) -> bool { - source_projection.item_def_id() == target_projection.def_id + source_projection.item_def_id() == target_projection.def_id() && self .ecx .probe(|_| ProbeKind::ProjectionCompatibility) @@ -982,7 +982,7 @@ where return Ok(None); } - let Some(replacements) = self.mapping.get(&alias_term.def_id) else { + let Some(replacements) = self.mapping.get(&alias_term.def_id()) else { return Ok(None); }; diff --git a/compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs b/compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs index 8933ac16b2b10..e224febd3c927 100644 --- a/compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs +++ b/compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs @@ -964,8 +964,8 @@ where // // Alternatively we could modify `Equate` for this case by adding another // variant to `StructurallyRelateAliases`. - let identity_args = self.fresh_args_for_item(alias.def_id); - let rigid_ctor = ty::AliasTerm::new_from_args(cx, alias.def_id, identity_args); + let identity_args = self.fresh_args_for_item(alias.def_id()); + let rigid_ctor = alias.with_args(cx, identity_args); let ctor_term = rigid_ctor.to_term(cx); let obligations = self.delegate.eq_structurally_relating_aliases( param_env, diff --git a/compiler/rustc_next_trait_solver/src/solve/mod.rs b/compiler/rustc_next_trait_solver/src/solve/mod.rs index d37b328920ce1..793d45f22d39b 100644 --- a/compiler/rustc_next_trait_solver/src/solve/mod.rs +++ b/compiler/rustc_next_trait_solver/src/solve/mod.rs @@ -344,7 +344,7 @@ where param_env: I::ParamEnv, term: I::Term, ) -> Result { - if let Some(_) = term.to_alias_term() { + if let Some(_) = term.to_alias_term(self.cx()) { let normalized_term = self.next_term_infer_of_kind(term); let alias_relate_goal = Goal::new( self.cx(), diff --git a/compiler/rustc_next_trait_solver/src/solve/normalizes_to/anon_const.rs b/compiler/rustc_next_trait_solver/src/solve/normalizes_to/anon_const.rs index 46312be5ea9a9..b3703639d99ef 100644 --- a/compiler/rustc_next_trait_solver/src/solve/normalizes_to/anon_const.rs +++ b/compiler/rustc_next_trait_solver/src/solve/normalizes_to/anon_const.rs @@ -17,7 +17,7 @@ where if let Some(normalized_const) = self.evaluate_const( goal.param_env, ty::UnevaluatedConst::new( - goal.predicate.alias.def_id.try_into().unwrap(), + goal.predicate.alias.def_id().try_into().unwrap(), goal.predicate.alias.args, ), ) { diff --git a/compiler/rustc_next_trait_solver/src/solve/normalizes_to/free_alias.rs b/compiler/rustc_next_trait_solver/src/solve/normalizes_to/free_alias.rs index c827494dd2906..f3004e4c8105b 100644 --- a/compiler/rustc_next_trait_solver/src/solve/normalizes_to/free_alias.rs +++ b/compiler/rustc_next_trait_solver/src/solve/normalizes_to/free_alias.rs @@ -24,16 +24,16 @@ where // Check where clauses self.add_goals( GoalSource::Misc, - cx.predicates_of(free_alias.def_id) + cx.predicates_of(free_alias.def_id()) .iter_instantiated(cx, free_alias.args) .map(Unnormalized::skip_norm_wip) .map(|pred| goal.with(cx, pred)), ); let actual = if free_alias.kind(cx).is_type() { - cx.type_of(free_alias.def_id).instantiate(cx, free_alias.args).skip_norm_wip().into() + cx.type_of(free_alias.def_id()).instantiate(cx, free_alias.args).skip_norm_wip().into() } else { - cx.const_of_item(free_alias.def_id) + cx.const_of_item(free_alias.def_id()) .instantiate(cx, free_alias.args) .skip_norm_wip() .into() diff --git a/compiler/rustc_next_trait_solver/src/solve/normalizes_to/inherent.rs b/compiler/rustc_next_trait_solver/src/solve/normalizes_to/inherent.rs index 7b228d4c7b46e..4d62407fe2995 100644 --- a/compiler/rustc_next_trait_solver/src/solve/normalizes_to/inherent.rs +++ b/compiler/rustc_next_trait_solver/src/solve/normalizes_to/inherent.rs @@ -22,7 +22,7 @@ where let cx = self.cx(); let inherent = goal.predicate.alias; - let impl_def_id = cx.parent(inherent.def_id); + let impl_def_id = cx.parent(inherent.def_id()); let impl_args = self.fresh_args_for_item(impl_def_id); // Equate impl header and add impl where clauses @@ -46,16 +46,19 @@ where // to be very careful when changing the impl where-clauses to be productive. self.add_goals( GoalSource::Misc, - cx.predicates_of(inherent.def_id) + cx.predicates_of(inherent.def_id()) .iter_instantiated(cx, inherent_args) .map(Unnormalized::skip_norm_wip) .map(|pred| goal.with(cx, pred)), ); let normalized = if inherent.kind(cx).is_type() { - cx.type_of(inherent.def_id).instantiate(cx, inherent_args).skip_norm_wip().into() + cx.type_of(inherent.def_id()).instantiate(cx, inherent_args).skip_norm_wip().into() } else { - cx.const_of_item(inherent.def_id).instantiate(cx, inherent_args).skip_norm_wip().into() + cx.const_of_item(inherent.def_id()) + .instantiate(cx, inherent_args) + .skip_norm_wip() + .into() }; self.instantiate_normalizes_to_term(goal, normalized); self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes) diff --git a/compiler/rustc_next_trait_solver/src/solve/normalizes_to/mod.rs b/compiler/rustc_next_trait_solver/src/solve/normalizes_to/mod.rs index bbc5739268ef7..1d74f1efe9136 100644 --- a/compiler/rustc_next_trait_solver/src/solve/normalizes_to/mod.rs +++ b/compiler/rustc_next_trait_solver/src/solve/normalizes_to/mod.rs @@ -34,7 +34,7 @@ where debug_assert!(self.term_is_fully_unconstrained(goal)); let cx = self.cx(); match goal.predicate.alias.kind(cx) { - ty::AliasTermKind::ProjectionTy | ty::AliasTermKind::ProjectionConst => { + ty::AliasTermKind::ProjectionTy { .. } | ty::AliasTermKind::ProjectionConst { .. } => { let trait_ref = goal.predicate.alias.trait_ref(cx); let (_, proven_via) = self.probe(|_| ProbeKind::ShadowedEnvProbing).enter(|ecx| { @@ -85,14 +85,14 @@ where }, ) } - ty::AliasTermKind::InherentTy | ty::AliasTermKind::InherentConst => { + ty::AliasTermKind::InherentTy { .. } | ty::AliasTermKind::InherentConst { .. } => { self.normalize_inherent_associated_term(goal) } - ty::AliasTermKind::OpaqueTy => self.normalize_opaque_type(goal), - ty::AliasTermKind::FreeTy | ty::AliasTermKind::FreeConst => { + ty::AliasTermKind::OpaqueTy { .. } => self.normalize_opaque_type(goal), + ty::AliasTermKind::FreeTy { .. } | ty::AliasTermKind::FreeConst { .. } => { self.normalize_free_alias(goal) } - ty::AliasTermKind::UnevaluatedConst => self.normalize_anon_const(goal), + ty::AliasTermKind::UnevaluatedConst { .. } => self.normalize_anon_const(goal), } } @@ -268,8 +268,8 @@ where let error_response = |ecx: &mut EvalCtxt<'_, D>, guar| { let error_term = match goal.predicate.alias.kind(cx) { - ty::AliasTermKind::ProjectionTy => Ty::new_error(cx, guar).into(), - ty::AliasTermKind::ProjectionConst => Const::new_error(cx, guar).into(), + ty::AliasTermKind::ProjectionTy { .. } => Ty::new_error(cx, guar).into(), + ty::AliasTermKind::ProjectionConst { .. } => Const::new_error(cx, guar).into(), kind => panic!("expected projection, found {kind:?}"), }; ecx.instantiate_normalizes_to_term(goal, error_term); @@ -384,10 +384,10 @@ where // Finally we construct the actual value of the associated type. let term = match goal.predicate.alias.kind(cx) { - ty::AliasTermKind::ProjectionTy => { + ty::AliasTermKind::ProjectionTy { .. } => { cx.type_of(target_item_def_id).map_bound(|ty| ty.into()) } - ty::AliasTermKind::ProjectionConst => { + ty::AliasTermKind::ProjectionConst { .. } => { cx.const_of_item(target_item_def_id).map_bound(|ct| ct.into()) } kind => panic!("expected projection, found {kind:?}"), @@ -472,7 +472,7 @@ where let pred = ty::ProjectionPredicate { projection_term: ty::AliasTerm::new( cx, - goal.predicate.def_id(), + cx.alias_term_kind_from_def_id(goal.predicate.def_id()), [goal.predicate.self_ty(), inputs], ), term: output.into(), @@ -526,7 +526,7 @@ where ( ty::AliasTerm::new( cx, - goal.predicate.def_id(), + cx.alias_term_kind_from_def_id(goal.predicate.def_id()), [goal.predicate.self_ty(), tupled_inputs_ty], ), output_coroutine_ty.into(), @@ -535,7 +535,7 @@ where ( ty::AliasTerm::new( cx, - goal.predicate.def_id(), + cx.alias_term_kind_from_def_id(goal.predicate.def_id()), [ I::GenericArg::from(goal.predicate.self_ty()), tupled_inputs_ty.into(), @@ -548,7 +548,7 @@ where ( ty::AliasTerm::new( cx, - goal.predicate.def_id(), + cx.alias_term_kind_from_def_id(goal.predicate.def_id()), [goal.predicate.self_ty(), tupled_inputs_ty], ), coroutine_return_ty.into(), @@ -746,7 +746,11 @@ where CandidateSource::BuiltinImpl(BuiltinImplSource::Misc), goal, ty::ProjectionPredicate { - projection_term: ty::AliasTerm::new(ecx.cx(), goal.predicate.def_id(), [self_ty]), + projection_term: ty::AliasTerm::new( + ecx.cx(), + cx.alias_term_kind_from_def_id(goal.predicate.def_id()), + [self_ty], + ), term, } .upcast(cx), @@ -778,7 +782,11 @@ where CandidateSource::BuiltinImpl(BuiltinImplSource::Misc), goal, ty::ProjectionPredicate { - projection_term: ty::AliasTerm::new(ecx.cx(), goal.predicate.def_id(), [self_ty]), + projection_term: ty::AliasTerm::new( + ecx.cx(), + cx.alias_term_kind_from_def_id(goal.predicate.def_id()), + [self_ty], + ), term, } .upcast(cx), @@ -863,7 +871,7 @@ where ty::ProjectionPredicate { projection_term: ty::AliasTerm::new( ecx.cx(), - goal.predicate.def_id(), + cx.alias_term_kind_from_def_id(goal.predicate.def_id()), [self_ty, coroutine.resume_ty()], ), term, diff --git a/compiler/rustc_next_trait_solver/src/solve/normalizes_to/opaque_types.rs b/compiler/rustc_next_trait_solver/src/solve/normalizes_to/opaque_types.rs index 931dd293973a1..6e21366955d82 100644 --- a/compiler/rustc_next_trait_solver/src/solve/normalizes_to/opaque_types.rs +++ b/compiler/rustc_next_trait_solver/src/solve/normalizes_to/opaque_types.rs @@ -26,7 +26,7 @@ where // An impossible opaque type bound is the only way this goal will fail // e.g. assigning `impl Copy := NotCopy` self.add_item_bounds_for_hidden_type( - opaque_ty.def_id, + opaque_ty.def_id(), opaque_ty.args, goal.param_env, expected, @@ -43,7 +43,7 @@ where } | TypingMode::Borrowck { defining_opaque_types } => { let Some(def_id) = opaque_ty - .def_id + .def_id() .as_local() .filter(|&def_id| defining_opaque_types.contains(&def_id)) else { @@ -110,7 +110,7 @@ where } TypingMode::PostBorrowckAnalysis { defined_opaque_types } => { let Some(def_id) = opaque_ty - .def_id + .def_id() .as_local() .filter(|&def_id| defined_opaque_types.contains(&def_id)) else { @@ -133,7 +133,7 @@ where TypingMode::PostAnalysis => { // FIXME: Add an assertion that opaque type storage is empty. let actual = - cx.type_of(opaque_ty.def_id).instantiate(cx, opaque_ty.args).skip_norm_wip(); + cx.type_of(opaque_ty.def_id()).instantiate(cx, opaque_ty.args).skip_norm_wip(); self.eq(goal.param_env, expected, actual)?; self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes) } diff --git a/compiler/rustc_public/src/unstable/convert/stable/ty.rs b/compiler/rustc_public/src/unstable/convert/stable/ty.rs index 9a9576d47efd2..e45cf377d2fcb 100644 --- a/compiler/rustc_public/src/unstable/convert/stable/ty.rs +++ b/compiler/rustc_public/src/unstable/convert/stable/ty.rs @@ -46,8 +46,11 @@ impl<'tcx> Stable<'tcx> for ty::AliasTerm<'tcx> { tables: &mut Tables<'cx, BridgeTys>, cx: &CompilerCtxt<'cx, BridgeTys>, ) -> Self::T { - let ty::AliasTerm { args, def_id, .. } = self; - crate::ty::AliasTerm { def_id: tables.alias_def(*def_id), args: args.stable(tables, cx) } + let ty::AliasTerm { args, kind, .. } = self; + crate::ty::AliasTerm { + def_id: tables.alias_def(kind.def_id()), + args: args.stable(tables, cx), + } } } diff --git a/compiler/rustc_sanitizers/src/cfi/typeid/itanium_cxx_abi/transform.rs b/compiler/rustc_sanitizers/src/cfi/typeid/itanium_cxx_abi/transform.rs index 6a36d6b3e1f65..8b7d906a27a16 100644 --- a/compiler/rustc_sanitizers/src/cfi/typeid/itanium_cxx_abi/transform.rs +++ b/compiler/rustc_sanitizers/src/cfi/typeid/itanium_cxx_abi/transform.rs @@ -245,7 +245,7 @@ fn trait_object_ty<'tcx>(tcx: TyCtxt<'tcx>, poly_trait_ref: ty::PolyTraitRef<'tc .filter(|item| !tcx.generics_require_sized_self(item.def_id)) .map(move |assoc_item| { super_poly_trait_ref.map_bound(|super_trait_ref| { - let projection_term = ty::AliasTerm::new_from_args( + let projection_term = ty::AliasTerm::new_from_def_id( tcx, assoc_item.def_id, super_trait_ref.args, 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 86fd705e68aea..21951cee6d5ab 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs @@ -202,7 +202,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { .kind() .map_bound(|kind| match kind { ty::ClauseKind::Projection(projection_predicate) - if projection_predicate.projection_term.def_id == item_def_id => + if projection_predicate.projection_term.def_id() == item_def_id => { projection_predicate.term.as_type() } @@ -1468,7 +1468,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { } ValuePairs::TraitRefs(_) => (false, Mismatch::Fixed("trait")), ValuePairs::Aliases(ExpectedFound { expected, .. }) => { - (false, Mismatch::Fixed(self.tcx.def_descr(expected.def_id))) + (false, Mismatch::Fixed(self.tcx.def_descr(expected.def_id()))) } ValuePairs::Regions(_) => (false, Mismatch::Fixed("lifetime")), ValuePairs::ExistentialTraitRef(_) => { diff --git a/compiler/rustc_trait_selection/src/error_reporting/traits/ambiguity.rs b/compiler/rustc_trait_selection/src/error_reporting/traits/ambiguity.rs index 78c8c824f30fd..e6597a67d0ba0 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/traits/ambiguity.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/traits/ambiguity.rs @@ -579,7 +579,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { if let Err(guar) = self .tcx .ensure_result() - .coherent_trait(self.tcx.parent(data.projection_term.def_id)) + .coherent_trait(self.tcx.parent(data.projection_term.def_id())) { // Avoid bogus "type annotations needed `Foo: Bar`" errors on `impl Bar for Foo` in case // other `Foo` impls are incoherent. diff --git a/compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs b/compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs index bf4c4287c433a..4a36cab363363 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs @@ -1536,8 +1536,11 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { let unnormalized_term = data.projection_term.to_term(self.tcx); // FIXME(-Znext-solver): For diagnostic purposes, it would be nice // to deeply normalize this type. - let normalized_term = - ocx.normalize(&obligation.cause, obligation.param_env, Unnormalized::new_wip(unnormalized_term)); + let normalized_term = ocx.normalize( + &obligation.cause, + obligation.param_env, + Unnormalized::new_wip(unnormalized_term), + ); // constrain inference variables a bit more to nested obligations from normalize so // we can have more helpful errors. @@ -1586,8 +1589,9 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { } }; - if let Some(lhs) = lhs.to_alias_term() - && let ty::AliasTermKind::ProjectionTy | ty::AliasTermKind::ProjectionConst = lhs.kind(self.tcx) + if let Some(lhs) = lhs.to_alias_term(self.tcx) + && let ty::AliasTermKind::ProjectionTy { .. } + | ty::AliasTermKind::ProjectionConst { .. } = lhs.kind(self.tcx) && let Some((better_type_err, expected_term)) = derive_better_type_error(lhs, rhs) { @@ -1595,8 +1599,9 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { Some((lhs, self.resolve_vars_if_possible(expected_term), rhs)), better_type_err, ) - } else if let Some(rhs) = rhs.to_alias_term() - && let ty::AliasTermKind::ProjectionTy | ty::AliasTermKind::ProjectionConst = rhs.kind(self.tcx) + } else if let Some(rhs) = rhs.to_alias_term(self.tcx) + && let ty::AliasTermKind::ProjectionTy { .. } + | ty::AliasTermKind::ProjectionConst { .. } = rhs.kind(self.tcx) && let Some((better_type_err, expected_term)) = derive_better_type_error(rhs, lhs) { @@ -1741,7 +1746,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { let self_ty = projection_term.self_ty(); with_forced_trimmed_paths! { - if self.tcx.is_lang_item(projection_term.def_id, LangItem::FnOnceOutput) { + if self.tcx.is_lang_item(projection_term.def_id(), LangItem::FnOnceOutput) { let (span, closure_span) = if let ty::Closure(def_id, _) = *self_ty.kind() { let def_span = self.tcx.def_span(def_id); if let Some(local_def_id) = def_id.as_local() diff --git a/compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs b/compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs index 330bae4e1bc53..fcd886d679dff 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs @@ -1445,7 +1445,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { if let ty::ClauseKind::Projection(proj) = pred.kind().skip_binder() && self .tcx - .is_lang_item(proj.projection_term.def_id, LangItem::FnOnceOutput) + .is_lang_item(proj.projection_term.def_id(), LangItem::FnOnceOutput) // args tuple will always be args[1] && let ty::Tuple(args) = proj.projection_term.args.type_at(1).kind() { @@ -1489,7 +1489,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { if let ty::ClauseKind::Projection(proj) = pred.kind().skip_binder() && self .tcx - .is_lang_item(proj.projection_term.def_id, LangItem::FnOnceOutput) + .is_lang_item(proj.projection_term.def_id(), LangItem::FnOnceOutput) && proj.projection_term.self_ty() == found // args tuple will always be args[1] && let ty::Tuple(args) = proj.projection_term.args.type_at(1).kind() @@ -5256,7 +5256,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { let TypeError::Sorts(expected_found) = diff else { continue; }; - let ty::Alias(ty::AliasTy { kind: ty::Projection { def_id }, .. }) = + let &ty::Alias(ty::AliasTy { kind: kind @ ty::Projection { def_id }, .. }) = expected_found.expected.kind() else { continue; @@ -5265,7 +5265,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { // Make `Self` be equivalent to the type of the call chain // expression we're looking at now, so that we can tell what // for example `Iterator::Item` is at this point in the chain. - let args = GenericArgs::for_item(self.tcx, *def_id, |param, _| { + let args = GenericArgs::for_item(self.tcx, def_id, |param, _| { if param.index == 0 { debug_assert_matches!(param.kind, ty::GenericParamDefKind::Type { .. }); return prev_ty.into(); @@ -5279,7 +5279,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { // This corresponds to `::Item = _`. let projection = ty::Binder::dummy(ty::PredicateKind::Clause( ty::ClauseKind::Projection(ty::ProjectionPredicate { - projection_term: ty::AliasTerm::new_from_args(self.tcx, *def_id, args), + projection_term: ty::AliasTerm::new_from_args(self.tcx, kind.into(), args), term: ty.into(), }), )); @@ -5296,7 +5296,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { && let ty = self.resolve_vars_if_possible(ty) && !ty.is_ty_var() { - assocs_in_this_method.push(Some((span, (*def_id, ty)))); + assocs_in_this_method.push(Some((span, (def_id, ty)))); } else { // `` didn't select, so likely we've // reached the end of the iterator chain, like the originating @@ -6344,12 +6344,12 @@ fn point_at_assoc_type_restriction( return; }; let Some(name) = tcx - .opt_rpitit_info(proj.projection_term.def_id) + .opt_rpitit_info(proj.projection_term.def_id()) .and_then(|data| match data { ty::ImplTraitInTraitData::Trait { fn_def_id, .. } => Some(tcx.item_name(fn_def_id)), ty::ImplTraitInTraitData::Impl { .. } => None, }) - .or_else(|| tcx.opt_item_name(proj.projection_term.def_id)) + .or_else(|| tcx.opt_item_name(proj.projection_term.def_id())) else { return; }; diff --git a/compiler/rustc_trait_selection/src/solve/fulfill/derive_errors.rs b/compiler/rustc_trait_selection/src/solve/fulfill/derive_errors.rs index df410cd3fb1b2..0cd374aedb6f8 100644 --- a/compiler/rustc_trait_selection/src/solve/fulfill/derive_errors.rs +++ b/compiler/rustc_trait_selection/src/solve/fulfill/derive_errors.rs @@ -388,8 +388,8 @@ impl<'tcx> BestObligation<'tcx> { self.detect_error_in_self_ty_normalization(goal, pred.self_ty())?; } Some(ty::PredicateKind::NormalizesTo(pred)) - if let ty::AliasTermKind::ProjectionTy | ty::AliasTermKind::ProjectionConst = - pred.alias.kind(tcx) => + if let ty::AliasTermKind::ProjectionTy { .. } + | ty::AliasTermKind::ProjectionConst { .. } = pred.alias.kind(tcx) => { self.detect_error_in_self_ty_normalization(goal, pred.alias.self_ty())?; self.detect_non_well_formed_assoc_item(goal, pred.alias)?; @@ -450,7 +450,8 @@ impl<'tcx> ProofTreeVisitor<'tcx> for BestObligation<'tcx> { ty::PredicateKind::NormalizesTo(normalizes_to) if matches!( normalizes_to.alias.kind(tcx), - ty::AliasTermKind::ProjectionTy | ty::AliasTermKind::ProjectionConst + ty::AliasTermKind::ProjectionTy { .. } + | ty::AliasTermKind::ProjectionConst { .. } ) => { ChildMode::Trait(pred.kind().rebind(ty::TraitPredicate { diff --git a/compiler/rustc_trait_selection/src/solve/normalize.rs b/compiler/rustc_trait_selection/src/solve/normalize.rs index 7d4bf731454e2..7a25b38866068 100644 --- a/compiler/rustc_trait_selection/src/solve/normalize.rs +++ b/compiler/rustc_trait_selection/src/solve/normalize.rs @@ -107,7 +107,7 @@ where let tcx = infcx.tcx; let recursion_limit = tcx.recursion_limit(); if !recursion_limit.value_within_limit(self.depth) { - let term = alias_term.to_alias_term().unwrap(); + let term = alias_term.to_alias_term(tcx).unwrap(); self.at.infcx.err_ctxt().report_overflow_error( OverflowCause::DeeplyNormalize(term), diff --git a/compiler/rustc_trait_selection/src/traits/coherence.rs b/compiler/rustc_trait_selection/src/traits/coherence.rs index 61ed05d873dd5..689b96779dca8 100644 --- a/compiler/rustc_trait_selection/src/traits/coherence.rs +++ b/compiler/rustc_trait_selection/src/traits/coherence.rs @@ -753,7 +753,7 @@ impl<'a, 'tcx> ProofTreeVisitor<'tcx> for AmbiguityCausesVisitor<'a, 'tcx> { ty::PredicateKind::Clause(ty::ClauseKind::Trait(tr)) => tr.trait_ref, ty::PredicateKind::Clause(ty::ClauseKind::Projection(proj)) if matches!( - infcx.tcx.def_kind(proj.projection_term.def_id), + infcx.tcx.def_kind(proj.projection_term.def_id()), DefKind::AssocTy | DefKind::AssocConst { .. } ) => { diff --git a/compiler/rustc_trait_selection/src/traits/fulfill.rs b/compiler/rustc_trait_selection/src/traits/fulfill.rs index 88cb18e5d3678..11ff9911469e1 100644 --- a/compiler/rustc_trait_selection/src/traits/fulfill.rs +++ b/compiler/rustc_trait_selection/src/traits/fulfill.rs @@ -699,8 +699,8 @@ impl<'a, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'tcx> { // `generic_const_exprs` .eq( DefineOpaqueTypes::Yes, - ty::AliasTerm::from(a), - ty::AliasTerm::from(b), + ty::AliasTerm::from_unevaluated_const(tcx, a), + ty::AliasTerm::from_unevaluated_const(tcx, b), ) { return ProcessResult::Changed(mk_pending( diff --git a/compiler/rustc_trait_selection/src/traits/normalize.rs b/compiler/rustc_trait_selection/src/traits/normalize.rs index 6e900606edb74..55f1f861921ea 100644 --- a/compiler/rustc_trait_selection/src/traits/normalize.rs +++ b/compiler/rustc_trait_selection/src/traits/normalize.rs @@ -321,7 +321,7 @@ impl<'a, 'b, 'tcx> AssocTypeNormalizer<'a, 'b, 'tcx> { self.obligations.extend( infcx .tcx - .predicates_of(free.def_id) + .predicates_of(free.def_id()) .instantiate_own(infcx.tcx, free.args) .map(|(pred, span)| (pred.skip_norm_wip(), span)) .map(|(mut predicate, span)| { @@ -333,7 +333,8 @@ impl<'a, 'b, 'tcx> AssocTypeNormalizer<'a, 'b, 'tcx> { ); } let mut cause = self.cause.clone(); - cause.map_code(|code| ObligationCauseCode::TypeAlias(code, span, free.def_id)); + cause + .map_code(|code| ObligationCauseCode::TypeAlias(code, span, free.def_id())); Obligation::new(infcx.tcx, cause, self.param_env, predicate) }), ); @@ -341,7 +342,7 @@ impl<'a, 'b, 'tcx> AssocTypeNormalizer<'a, 'b, 'tcx> { let res = if free.kind(infcx.tcx).is_type() { infcx .tcx - .type_of(free.def_id) + .type_of(free.def_id()) .instantiate(infcx.tcx, free.args) .skip_norm_wip() .fold_with(self) @@ -349,7 +350,7 @@ impl<'a, 'b, 'tcx> AssocTypeNormalizer<'a, 'b, 'tcx> { } else { infcx .tcx - .const_of_item(free.def_id) + .const_of_item(free.def_id()) .instantiate(infcx.tcx, free.args) .skip_norm_wip() .fold_with(self) @@ -468,16 +469,20 @@ impl<'a, 'b, 'tcx> TypeFolder> for AssocTypeNormalizer<'a, 'b, 'tcx // feature gate causes a parse error. let ct = match tcx.def_kind(uv.def) { DefKind::AssocConst { .. } => match tcx.def_kind(tcx.parent(uv.def)) { - DefKind::Trait => self.normalize_trait_projection(uv.into()).expect_const(), - DefKind::Impl { of_trait: false } => { - self.normalize_inherent_projection(uv.into()).expect_const() - } + DefKind::Trait => self + .normalize_trait_projection(ty::AliasTerm::from_unevaluated_const(tcx, uv)) + .expect_const(), + DefKind::Impl { of_trait: false } => self + .normalize_inherent_projection(ty::AliasTerm::from_unevaluated_const(tcx, uv)) + .expect_const(), kind => unreachable!( "unexpected `DefKind` for const alias' resolution's parent def: {:?}", kind ), }, - DefKind::Const { .. } => self.normalize_free_alias(uv.into()).expect_const(), + DefKind::Const { .. } => self + .normalize_free_alias(ty::AliasTerm::from_unevaluated_const(tcx, uv)) + .expect_const(), DefKind::AnonConst => { let ct = ct.super_fold_with(self); super::with_replaced_escaping_bound_vars( diff --git a/compiler/rustc_trait_selection/src/traits/project.rs b/compiler/rustc_trait_selection/src/traits/project.rs index 5014a59d1f0f9..5423e394119c4 100644 --- a/compiler/rustc_trait_selection/src/traits/project.rs +++ b/compiler/rustc_trait_selection/src/traits/project.rs @@ -463,14 +463,16 @@ fn normalize_to_error<'a, 'tcx>( ) -> NormalizedTerm<'tcx> { let trait_ref = ty::Binder::dummy(projection_term.trait_ref(selcx.tcx())); let new_value = match projection_term.kind(selcx.tcx()) { - ty::AliasTermKind::ProjectionTy - | ty::AliasTermKind::InherentTy - | ty::AliasTermKind::OpaqueTy - | ty::AliasTermKind::FreeTy => selcx.infcx.next_ty_var(cause.span).into(), - ty::AliasTermKind::FreeConst - | ty::AliasTermKind::InherentConst - | ty::AliasTermKind::UnevaluatedConst - | ty::AliasTermKind::ProjectionConst => selcx.infcx.next_const_var(cause.span).into(), + ty::AliasTermKind::ProjectionTy { .. } + | ty::AliasTermKind::InherentTy { .. } + | ty::AliasTermKind::OpaqueTy { .. } + | ty::AliasTermKind::FreeTy { .. } => selcx.infcx.next_ty_var(cause.span).into(), + ty::AliasTermKind::FreeConst { .. } + | ty::AliasTermKind::InherentConst { .. } + | ty::AliasTermKind::UnevaluatedConst { .. } + | ty::AliasTermKind::ProjectionConst { .. } => { + selcx.infcx.next_const_var(cause.span).into() + } }; let mut obligations = PredicateObligations::new(); obligations.push(Obligation { @@ -513,7 +515,7 @@ pub fn normalize_inherent_projection<'a, 'b, 'tcx>( ); // Register the obligations arising from the impl and from the associated type itself. - let predicates = tcx.predicates_of(alias_term.def_id).instantiate(tcx, args); + let predicates = tcx.predicates_of(alias_term.def_id()).instantiate(tcx, args); for (predicate, span) in predicates { let predicate = normalize_with_depth_to( selcx, @@ -531,7 +533,7 @@ pub fn normalize_inherent_projection<'a, 'b, 'tcx>( // cause code, inherent projections will be printed with identity instantiation in // diagnostics which is not ideal. // Consider creating separate cause codes for this specific situation. - ObligationCauseCode::WhereClause(alias_term.def_id, span), + ObligationCauseCode::WhereClause(alias_term.def_id(), span), ); obligations.push(Obligation::with_depth( @@ -544,9 +546,9 @@ pub fn normalize_inherent_projection<'a, 'b, 'tcx>( } let term: Term<'tcx> = if alias_term.kind(tcx).is_type() { - tcx.type_of(alias_term.def_id).instantiate(tcx, args).skip_norm_wip().into() + tcx.type_of(alias_term.def_id()).instantiate(tcx, args).skip_norm_wip().into() } else { - tcx.const_of_item(alias_term.def_id).instantiate(tcx, args).skip_norm_wip().into() + tcx.const_of_item(alias_term.def_id()).instantiate(tcx, args).skip_norm_wip().into() }; let mut term = selcx.infcx.resolve_vars_if_possible(term); @@ -569,7 +571,7 @@ pub fn compute_inherent_assoc_term_args<'a, 'b, 'tcx>( ) -> ty::GenericArgsRef<'tcx> { let tcx = selcx.tcx(); - let impl_def_id = tcx.parent(alias_term.def_id); + let impl_def_id = tcx.parent(alias_term.def_id()); let impl_args = selcx.infcx.fresh_args_for_item(cause.span, impl_def_id); let mut impl_ty = tcx.type_of(impl_def_id).instantiate(tcx, impl_args).skip_norm_wip(); @@ -747,7 +749,7 @@ fn assemble_candidates_from_trait_def<'cx, 'tcx>( let Some(clause) = clause.as_projection_clause() else { return ControlFlow::Continue(()); }; - if clause.item_def_id() != obligation.predicate.def_id { + if clause.item_def_id() != obligation.predicate.def_id() { return ControlFlow::Continue(()); } @@ -815,7 +817,7 @@ fn assemble_candidates_from_object_ty<'cx, 'tcx>( }; let env_predicates = data .projection_bounds() - .filter(|bound| bound.item_def_id() == obligation.predicate.def_id) + .filter(|bound| bound.item_def_id() == obligation.predicate.def_id()) .map(|p| p.with_self_ty(tcx, object_ty).upcast(tcx)); assemble_candidates_from_predicates( @@ -846,7 +848,7 @@ fn assemble_candidates_from_predicates<'cx, 'tcx>( let bound_predicate = predicate.kind(); if let ty::ClauseKind::Projection(data) = predicate.kind().skip_binder() { let data = bound_predicate.rebind(data); - if data.item_def_id() != obligation.predicate.def_id { + if data.item_def_id() != obligation.predicate.def_id() { continue; } @@ -937,7 +939,7 @@ fn assemble_candidates_from_impls<'cx, 'tcx>( match specialization_graph::assoc_def( selcx.tcx(), impl_data.impl_def_id, - obligation.predicate.def_id, + obligation.predicate.def_id(), ) { Ok(node_item) => { if node_item.is_final() { @@ -1143,7 +1145,7 @@ fn assemble_candidates_from_impls<'cx, 'tcx>( } _ if tcx.trait_is_auto(trait_ref.def_id) => { tcx.dcx().span_delayed_bug( - tcx.def_span(obligation.predicate.def_id), + tcx.def_span(obligation.predicate.def_id()), "associated types not allowed on auto traits", ); false @@ -1325,24 +1327,20 @@ fn confirm_coroutine_candidate<'cx, 'tcx>( coroutine_sig, ); - let ty = if tcx.is_lang_item(obligation.predicate.def_id, LangItem::CoroutineReturn) { + let ty = if tcx.is_lang_item(obligation.predicate.def_id(), LangItem::CoroutineReturn) { return_ty - } else if tcx.is_lang_item(obligation.predicate.def_id, LangItem::CoroutineYield) { + } else if tcx.is_lang_item(obligation.predicate.def_id(), LangItem::CoroutineYield) { yield_ty } else { span_bug!( - tcx.def_span(obligation.predicate.def_id), + tcx.def_span(obligation.predicate.def_id()), "unexpected associated type: `Coroutine::{}`", - tcx.item_name(obligation.predicate.def_id), + tcx.item_name(obligation.predicate.def_id()), ); }; let predicate = ty::ProjectionPredicate { - projection_term: ty::AliasTerm::new_from_args( - tcx, - obligation.predicate.def_id, - trait_ref.args, - ), + projection_term: obligation.predicate.with_args(tcx, trait_ref.args), term: ty.into(), }; @@ -1383,14 +1381,10 @@ fn confirm_future_candidate<'cx, 'tcx>( coroutine_sig, ); - debug_assert_eq!(tcx.associated_item(obligation.predicate.def_id).name(), sym::Output); + debug_assert_eq!(tcx.associated_item(obligation.predicate.def_id()).name(), sym::Output); let predicate = ty::ProjectionPredicate { - projection_term: ty::AliasTerm::new_from_args( - tcx, - obligation.predicate.def_id, - trait_ref.args, - ), + projection_term: obligation.predicate.with_args(tcx, trait_ref.args), term: return_ty.into(), }; @@ -1429,14 +1423,10 @@ fn confirm_iterator_candidate<'cx, 'tcx>( gen_sig, ); - debug_assert_eq!(tcx.associated_item(obligation.predicate.def_id).name(), sym::Item); + debug_assert_eq!(tcx.associated_item(obligation.predicate.def_id()).name(), sym::Item); let predicate = ty::ProjectionPredicate { - projection_term: ty::AliasTerm::new_from_args( - tcx, - obligation.predicate.def_id, - trait_ref.args, - ), + projection_term: obligation.predicate.with_args(tcx, trait_ref.args), term: yield_ty.into(), }; @@ -1475,7 +1465,7 @@ fn confirm_async_iterator_candidate<'cx, 'tcx>( gen_sig, ); - debug_assert_eq!(tcx.associated_item(obligation.predicate.def_id).name(), sym::Item); + debug_assert_eq!(tcx.associated_item(obligation.predicate.def_id()).name(), sym::Item); let ty::Adt(_poll_adt, args) = *yield_ty.kind() else { bug!(); @@ -1486,11 +1476,7 @@ fn confirm_async_iterator_candidate<'cx, 'tcx>( let item_ty = args.type_at(0); let predicate = ty::ProjectionPredicate { - projection_term: ty::AliasTerm::new_from_args( - tcx, - obligation.predicate.def_id, - trait_ref.args, - ), + projection_term: obligation.predicate.with_args(tcx, trait_ref.args), term: item_ty.into(), }; @@ -1506,7 +1492,7 @@ fn confirm_builtin_candidate<'cx, 'tcx>( ) -> Progress<'tcx> { let tcx = selcx.tcx(); let self_ty = obligation.predicate.self_ty(); - let item_def_id = obligation.predicate.def_id; + let item_def_id = obligation.predicate.def_id(); let trait_def_id = tcx.parent(item_def_id); let args = tcx.mk_args(&[self_ty.into()]); let (term, obligations) = if tcx.is_lang_item(trait_def_id, LangItem::DiscriminantKind) { @@ -1569,7 +1555,11 @@ fn confirm_builtin_candidate<'cx, 'tcx>( }; let predicate = ty::ProjectionPredicate { - projection_term: ty::AliasTerm::new_from_args(tcx, item_def_id, args), + projection_term: ty::AliasTerm::new_from_args( + tcx, + ty::AliasTermKind::ProjectionTy { def_id: item_def_id }, + args, + ), term, }; @@ -1702,7 +1692,11 @@ fn confirm_callable_candidate<'cx, 'tcx>( flag, ) .map_bound(|(trait_ref, ret_type)| ty::ProjectionPredicate { - projection_term: ty::AliasTerm::new_from_args(tcx, fn_once_output_def_id, trait_ref.args), + projection_term: ty::AliasTerm::new_from_args( + tcx, + ty::AliasTermKind::ProjectionTy { def_id: fn_once_output_def_id }, + trait_ref.args, + ), term: ret_type.into(), }); @@ -1723,7 +1717,7 @@ fn confirm_async_closure_candidate<'cx, 'tcx>( ty::ClosureKind::Fn | ty::ClosureKind::FnMut => obligation.predicate.args.region_at(2), ty::ClosureKind::FnOnce => tcx.lifetimes.re_static, }; - let item_name = tcx.item_name(obligation.predicate.def_id); + let item_name = tcx.item_name(obligation.predicate.def_id()); let poly_cache_entry = match *self_ty.kind() { ty::CoroutineClosure(def_id, args) => { @@ -1787,12 +1781,12 @@ fn confirm_async_closure_candidate<'cx, 'tcx>( let projection_term = match item_name { sym::CallOnceFuture | sym::Output => ty::AliasTerm::new( tcx, - obligation.predicate.def_id, + obligation.predicate.kind, [self_ty, sig.tupled_inputs_ty], ), sym::CallRefFuture => ty::AliasTerm::new( tcx, - obligation.predicate.def_id, + obligation.predicate.kind, [ty::GenericArg::from(self_ty), sig.tupled_inputs_ty.into(), env_region.into()], ), name => bug!("no such associated type: {name}"), @@ -1817,12 +1811,12 @@ fn confirm_async_closure_candidate<'cx, 'tcx>( let projection_term = match item_name { sym::CallOnceFuture | sym::Output => ty::AliasTerm::new( tcx, - obligation.predicate.def_id, + obligation.predicate.kind, [self_ty, Ty::new_tup(tcx, sig.inputs())], ), sym::CallRefFuture => ty::AliasTerm::new( tcx, - obligation.predicate.def_id, + obligation.predicate.kind, [ ty::GenericArg::from(self_ty), Ty::new_tup(tcx, sig.inputs()).into(), @@ -1850,11 +1844,11 @@ fn confirm_async_closure_candidate<'cx, 'tcx>( }; let projection_term = match item_name { sym::CallOnceFuture | sym::Output => { - ty::AliasTerm::new(tcx, obligation.predicate.def_id, [self_ty, sig.inputs()[0]]) + ty::AliasTerm::new(tcx, obligation.predicate.kind, [self_ty, sig.inputs()[0]]) } sym::CallRefFuture => ty::AliasTerm::new( tcx, - obligation.predicate.def_id, + obligation.predicate.kind, [ty::GenericArg::from(self_ty), sig.inputs()[0].into(), env_region.into()], ), name => bug!("no such associated type: {name}"), @@ -1888,11 +1882,7 @@ fn confirm_async_fn_kind_helper_candidate<'cx, 'tcx>( }; let predicate = ty::ProjectionPredicate { - projection_term: ty::AliasTerm::new_from_args( - selcx.tcx(), - obligation.predicate.def_id, - obligation.predicate.args, - ), + projection_term: obligation.predicate.with_args(selcx.tcx(), obligation.predicate.args), term: ty::CoroutineClosureSignature::tupled_upvars_by_closure_kind( selcx.tcx(), goal_kind_ty.expect_ty().to_opt_closure_kind().unwrap(), @@ -1986,7 +1976,7 @@ fn confirm_impl_candidate<'cx, 'tcx>( let ImplSourceUserDefinedData { impl_def_id, args, mut nested } = impl_impl_source; - let assoc_item_id = obligation.predicate.def_id; + let assoc_item_id = obligation.predicate.def_id(); let trait_def_id = tcx.impl_trait_id(impl_def_id); let param_env = obligation.param_env; @@ -2074,7 +2064,7 @@ fn assoc_term_own_obligations<'cx, 'tcx>( ) { let tcx = selcx.tcx(); let predicates = tcx - .predicates_of(obligation.predicate.def_id) + .predicates_of(obligation.predicate.def_id()) .instantiate_own(tcx, obligation.predicate.args); for (predicate, span) in predicates { let normalized = normalize_with_depth_to( @@ -2097,7 +2087,7 @@ fn assoc_term_own_obligations<'cx, 'tcx>( ObligationCause::new( obligation.cause.span, obligation.cause.body_id, - ObligationCauseCode::WhereClause(obligation.predicate.def_id, span), + ObligationCauseCode::WhereClause(obligation.predicate.def_id(), span), ) }; nested.push(Obligation::with_depth( diff --git a/compiler/rustc_trait_selection/src/traits/query/normalize.rs b/compiler/rustc_trait_selection/src/traits/query/normalize.rs index 2aef690e61035..21d437a4a72c7 100644 --- a/compiler/rustc_trait_selection/src/traits/query/normalize.rs +++ b/compiler/rustc_trait_selection/src/traits/query/normalize.rs @@ -256,8 +256,8 @@ impl<'a, 'tcx> FallibleTypeFolder> for QueryNormalizer<'a, 'tcx> { } } - ty::Projection { def_id } | ty::Inherent { def_id } | ty::Free { def_id } => self - .try_fold_free_or_assoc(ty::AliasTerm::new(self.cx(), def_id, data.args))? + kind @ (ty::Projection { .. } | ty::Inherent { .. } | ty::Free { .. }) => self + .try_fold_free_or_assoc(ty::AliasTerm::new(self.cx(), kind.into(), data.args))? .expect_type(), }; @@ -286,7 +286,7 @@ impl<'a, 'tcx> FallibleTypeFolder> for QueryNormalizer<'a, 'tcx> { |constant| crate::traits::evaluate_const(&self.infcx, constant, self.param_env), ), _ => self - .try_fold_free_or_assoc(ty::AliasTerm::new(self.cx(), uv.def, uv.args))? + .try_fold_free_or_assoc(ty::AliasTerm::from_unevaluated_const(self.cx(), uv))? .expect_const(), }; debug!(?constant, ?self.param_env); @@ -329,16 +329,17 @@ impl<'a, 'tcx> QueryNormalizer<'a, 'tcx> { debug!("QueryNormalizer: c_term = {:#?}", c_term); debug!("QueryNormalizer: orig_values = {:#?}", orig_values); let result = match term.kind(tcx) { - ty::AliasTermKind::ProjectionTy | ty::AliasTermKind::ProjectionConst => { + ty::AliasTermKind::ProjectionTy { .. } | ty::AliasTermKind::ProjectionConst { .. } => { tcx.normalize_canonicalized_projection(c_term) } - ty::AliasTermKind::FreeTy | ty::AliasTermKind::FreeConst => { + ty::AliasTermKind::FreeTy { .. } | ty::AliasTermKind::FreeConst { .. } => { tcx.normalize_canonicalized_free_alias(c_term) } - ty::AliasTermKind::InherentTy | ty::AliasTermKind::InherentConst => { + ty::AliasTermKind::InherentTy { .. } | ty::AliasTermKind::InherentConst { .. } => { tcx.normalize_canonicalized_inherent_projection(c_term) } - kind @ (ty::AliasTermKind::OpaqueTy | ty::AliasTermKind::UnevaluatedConst) => { + kind @ (ty::AliasTermKind::OpaqueTy { .. } + | ty::AliasTermKind::UnevaluatedConst { .. }) => { unreachable!("did not expect {kind:?} due to match arm above") } }?; @@ -382,7 +383,7 @@ impl<'a, 'tcx> QueryNormalizer<'a, 'tcx> { && (res.has_type_flags(ty::TypeFlags::HAS_CT_PROJECTION) || matches!( term.kind(tcx), - ty::AliasTermKind::FreeTy | ty::AliasTermKind::FreeConst + ty::AliasTermKind::FreeTy { .. } | ty::AliasTermKind::FreeConst { .. } )) { res.try_fold_with(self) diff --git a/compiler/rustc_trait_selection/src/traits/select/mod.rs b/compiler/rustc_trait_selection/src/traits/select/mod.rs index 17a5f14767eec..28b9ce24178d9 100644 --- a/compiler/rustc_trait_selection/src/traits/select/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/select/mod.rs @@ -891,8 +891,8 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { // `generic_const_exprs` .eq( DefineOpaqueTypes::Yes, - ty::AliasTerm::from(a), - ty::AliasTerm::from(b), + ty::AliasTerm::from_unevaluated_const(tcx, a), + ty::AliasTerm::from_unevaluated_const(tcx, b), ) { return self.evaluate_predicates_recursively( @@ -1761,7 +1761,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { env_predicate: PolyProjectionPredicate<'tcx>, potentially_unnormalized_candidates: bool, ) -> ProjectionMatchesProjection { - debug_assert_eq!(obligation.predicate.def_id, env_predicate.item_def_id()); + debug_assert_eq!(obligation.predicate.def_id(), env_predicate.item_def_id()); let mut nested_obligations = PredicateObligations::new(); let infer_predicate = self.infcx.instantiate_binder_with_fresh_vars( @@ -1797,7 +1797,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { }); if is_match { - let generics = self.tcx().generics_of(obligation.predicate.def_id); + let generics = self.tcx().generics_of(obligation.predicate.def_id()); // FIXME(generic_associated_types): Addresses aggressive inference in #92917. // If this type is a GAT, and of the GAT args resolve to something new, // that means that we must have newly inferred something about the GAT. diff --git a/compiler/rustc_trait_selection/src/traits/structural_normalize.rs b/compiler/rustc_trait_selection/src/traits/structural_normalize.rs index 03edfeb06d8c4..fe00e9b4f8426 100644 --- a/compiler/rustc_trait_selection/src/traits/structural_normalize.rs +++ b/compiler/rustc_trait_selection/src/traits/structural_normalize.rs @@ -41,7 +41,7 @@ impl<'tcx> At<'_, 'tcx> { if self.infcx.next_trait_solver() { let term = term.skip_normalization(); - if let None = term.to_alias_term() { + if let None = term.to_alias_term(self.infcx.tcx) { return Ok(term); } diff --git a/compiler/rustc_trait_selection/src/traits/wf.rs b/compiler/rustc_trait_selection/src/traits/wf.rs index c9c274990eb97..66a2fcaa562d1 100644 --- a/compiler/rustc_trait_selection/src/traits/wf.rs +++ b/compiler/rustc_trait_selection/src/traits/wf.rs @@ -478,7 +478,7 @@ impl<'a, 'tcx> WfPredicates<'a, 'tcx> { // `i32: Clone` // `i32: Copy` // ] - let obligations = self.nominal_obligations(data.def_id, data.args); + let obligations = self.nominal_obligations(data.def_id(), data.args); self.out.extend(obligations); self.add_wf_preds_for_projection_args(data.args); @@ -507,7 +507,7 @@ impl<'a, 'tcx> WfPredicates<'a, 'tcx> { self.recursion_depth, &mut self.out, ); - let obligations = self.nominal_obligations(data.def_id, args); + let obligations = self.nominal_obligations(data.def_id(), args); self.out.extend(obligations); } @@ -1001,7 +1001,7 @@ impl<'a, 'tcx> TypeVisitor> for WfPredicates<'a, 'tcx> { .map_bound(|p| { p.term.as_const().map(|ct| { let assoc_const_ty = tcx - .type_of(p.projection_term.def_id) + .type_of(p.projection_term.def_id()) .instantiate(tcx, p.projection_term.args) .skip_norm_wip(); ty::PredicateKind::Clause(ty::ClauseKind::ConstArgHasType( @@ -1077,7 +1077,9 @@ impl<'a, 'tcx> TypeVisitor> for WfPredicates<'a, 'tcx> { if matches!(tcx.def_kind(uv.def), DefKind::AssocConst { .. }) && tcx.def_kind(tcx.parent(uv.def)) == (DefKind::Impl { of_trait: false }) { - self.add_wf_preds_for_inherent_projection(uv.into()); + self.add_wf_preds_for_inherent_projection( + ty::AliasTerm::from_unevaluated_const(tcx, uv), + ); return; // Subtree is handled by above function } else { let obligations = self.nominal_obligations(uv.def, uv.args); diff --git a/compiler/rustc_traits/src/normalize_projection_ty.rs b/compiler/rustc_traits/src/normalize_projection_ty.rs index ebea483a77cf0..16d5c20c87fe2 100644 --- a/compiler/rustc_traits/src/normalize_projection_ty.rs +++ b/compiler/rustc_traits/src/normalize_projection_ty.rs @@ -75,7 +75,7 @@ fn normalize_canonicalized_free_alias<'tcx>( tcx.infer_ctxt().enter_canonical_trait_query( &goal, |ocx, ParamEnvAnd { param_env, value: goal }| { - let obligations = tcx.predicates_of(goal.def_id).instantiate_own(tcx, goal.args).map( + let obligations = tcx.predicates_of(goal.def_id()).instantiate_own(tcx, goal.args).map( |(predicate, span)| { traits::Obligation::new( tcx, @@ -87,9 +87,9 @@ fn normalize_canonicalized_free_alias<'tcx>( ); ocx.register_obligations(obligations); let normalized_term = if goal.kind(tcx).is_type() { - tcx.type_of(goal.def_id).instantiate(tcx, goal.args).skip_norm_wip().into() + tcx.type_of(goal.def_id()).instantiate(tcx, goal.args).skip_norm_wip().into() } else { - tcx.const_of_item(goal.def_id).instantiate(tcx, goal.args).skip_norm_wip().into() + tcx.const_of_item(goal.def_id()).instantiate(tcx, goal.args).skip_norm_wip().into() }; Ok(NormalizationResult { normalized_term }) }, diff --git a/compiler/rustc_type_ir/src/inherent.rs b/compiler/rustc_type_ir/src/inherent.rs index a336313e90b8f..a8757e045d170 100644 --- a/compiler/rustc_type_ir/src/inherent.rs +++ b/compiler/rustc_type_ir/src/inherent.rs @@ -432,14 +432,16 @@ pub trait Term>: } } - fn to_alias_term(self) -> Option> { + fn to_alias_term(self, interner: I) -> Option> { match self.kind() { ty::TermKind::Ty(ty) => match ty.kind() { ty::Alias(alias_ty) => Some(alias_ty.into()), _ => None, }, ty::TermKind::Const(ct) => match ct.kind() { - ty::ConstKind::Unevaluated(uv) => Some(uv.into()), + ty::ConstKind::Unevaluated(uv) => { + Some(ty::AliasTerm::from_unevaluated_const(interner, uv)) + } _ => None, }, } diff --git a/compiler/rustc_type_ir/src/interner.rs b/compiler/rustc_type_ir/src/interner.rs index f71f7c7c1ab38..f350e5da9654c 100644 --- a/compiler/rustc_type_ir/src/interner.rs +++ b/compiler/rustc_type_ir/src/interner.rs @@ -197,11 +197,9 @@ pub trait Interner: 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, - def_id: Self::DefId, + kind: impl Into>, ) -> Option; fn type_of(self, def_id: Self::DefId) -> ty::EarlyBinder; @@ -215,7 +213,8 @@ pub trait Interner: fn alias_ty_kind_from_def_id(self, def_id: Self::DefId) -> ty::AliasTyKind; - fn alias_term_kind(self, alias: ty::AliasTerm) -> ty::AliasTermKind; + // FIXME: remove in favor of explicit construction + fn alias_term_kind_from_def_id(self, def_id: Self::DefId) -> ty::AliasTermKind; fn trait_ref_and_own_args_for_alias( self, diff --git a/compiler/rustc_type_ir/src/outlives.rs b/compiler/rustc_type_ir/src/outlives.rs index 5b4e44dc89ebe..56f40d3f78288 100644 --- a/compiler/rustc_type_ir/src/outlives.rs +++ b/compiler/rustc_type_ir/src/outlives.rs @@ -226,7 +226,7 @@ pub fn compute_alias_components_recursive( alias_ty: ty::AliasTy, out: &mut SmallVec<[Component; 4]>, ) { - let opt_variances = cx.opt_alias_variances(alias_ty.kind, alias_ty.kind.def_id()); + let opt_variances = cx.opt_alias_variances(alias_ty.kind); let mut visitor = OutlivesCollector { cx, out, visited: Default::default() }; diff --git a/compiler/rustc_type_ir/src/predicate.rs b/compiler/rustc_type_ir/src/predicate.rs index e480b089b655c..0545fbfda6e41 100644 --- a/compiler/rustc_type_ir/src/predicate.rs +++ b/compiler/rustc_type_ir/src/predicate.rs @@ -3,9 +3,7 @@ use std::{fmt, iter}; use derive_where::derive_where; #[cfg(feature = "nightly")] -use rustc_macros::{ - Decodable, Decodable_NoContext, Encodable, Encodable_NoContext, HashStable_NoContext, -}; +use rustc_macros::{Decodable_NoContext, Encodable_NoContext, HashStable_NoContext}; use rustc_type_ir_macros::{ GenericTypeVisitable, Lift_Generic, TypeFoldable_Generic, TypeVisitable_Generic, }; @@ -532,7 +530,7 @@ impl ExistentialProjection { ProjectionPredicate { projection_term: AliasTerm::new( interner, - self.def_id, + interner.alias_term_kind_from_def_id(self.def_id), [self_ty.into()].iter().chain(self.args.iter()), ), term: self.term, @@ -544,7 +542,7 @@ impl ExistentialProjection { projection_predicate.projection_term.args.type_at(0); Self { - def_id: projection_predicate.projection_term.def_id, + def_id: projection_predicate.projection_term.def_id(), args: interner.mk_args(&projection_predicate.projection_term.args.as_slice()[1..]), term: projection_predicate.term, use_existential_projection_new_instead: (), @@ -562,71 +560,104 @@ impl ty::Binder> { } } -#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] -#[cfg_attr(feature = "nightly", derive(Encodable, Decodable, HashStable_NoContext))] -pub enum AliasTermKind { +#[derive_where(Clone, Copy, PartialEq, Eq, Hash, Debug; I: Interner)] +#[derive(Lift_Generic)] +#[cfg_attr( + feature = "nightly", + derive(Encodable_NoContext, Decodable_NoContext, HashStable_NoContext) +)] +pub enum AliasTermKind { /// A projection `::AssocType`. /// /// Can get normalized away if monomorphic enough. - ProjectionTy, + /// + /// 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)`. + ProjectionTy { def_id: I::DefId }, + /// An associated type in an inherent `impl` - InherentTy, + /// + /// The `def_id` is the `DefId` of the `ImplItem` for the associated type. + InherentTy { 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. - OpaqueTy, - /// A free type alias that actually checks its trait bounds. + /// `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. + OpaqueTy { def_id: I::DefId }, + + /// A type alias that actually checks its trait bounds. /// /// Currently only used if the type alias references opaque types. /// Can always be normalized away. - FreeTy, + FreeTy { def_id: I::DefId }, /// An unevaluated anonymous constants. - UnevaluatedConst, + UnevaluatedConst { def_id: I::DefId }, /// An unevaluated const coming from an associated const. - ProjectionConst, + ProjectionConst { def_id: I::DefId }, /// A top level const item not part of a trait or impl. - FreeConst, + FreeConst { def_id: I::DefId }, /// An associated const in an inherent `impl` - InherentConst, + InherentConst { def_id: I::DefId }, } -impl AliasTermKind { +impl AliasTermKind { pub fn descr(self) -> &'static str { match self { - AliasTermKind::ProjectionTy => "associated type", - AliasTermKind::ProjectionConst => "associated const", - AliasTermKind::InherentTy => "inherent associated type", - AliasTermKind::InherentConst => "inherent associated const", - AliasTermKind::OpaqueTy => "opaque type", - AliasTermKind::FreeTy => "type alias", - AliasTermKind::FreeConst => "unevaluated constant", - AliasTermKind::UnevaluatedConst => "unevaluated constant", + AliasTermKind::ProjectionTy { .. } => "associated type", + AliasTermKind::ProjectionConst { .. } => "associated const", + AliasTermKind::InherentTy { .. } => "inherent associated type", + AliasTermKind::InherentConst { .. } => "inherent associated const", + AliasTermKind::OpaqueTy { .. } => "opaque type", + AliasTermKind::FreeTy { .. } => "type alias", + AliasTermKind::FreeConst { .. } => "unevaluated constant", + AliasTermKind::UnevaluatedConst { .. } => "unevaluated constant", } } pub fn is_type(self) -> bool { match self { - AliasTermKind::ProjectionTy - | AliasTermKind::InherentTy - | AliasTermKind::OpaqueTy - | AliasTermKind::FreeTy => true, - - AliasTermKind::UnevaluatedConst - | AliasTermKind::ProjectionConst - | AliasTermKind::InherentConst - | AliasTermKind::FreeConst => false, + AliasTermKind::ProjectionTy { .. } + | AliasTermKind::InherentTy { .. } + | AliasTermKind::OpaqueTy { .. } + | AliasTermKind::FreeTy { .. } => true, + + AliasTermKind::UnevaluatedConst { .. } + | AliasTermKind::ProjectionConst { .. } + | AliasTermKind::InherentConst { .. } + | AliasTermKind::FreeConst { .. } => false, } } + + // FIXME: replace with explicit matches + pub fn def_id(self) -> I::DefId { + let (AliasTermKind::ProjectionTy { def_id } + | AliasTermKind::InherentTy { def_id } + | AliasTermKind::OpaqueTy { def_id } + | AliasTermKind::FreeTy { def_id } + | AliasTermKind::UnevaluatedConst { def_id } + | AliasTermKind::ProjectionConst { def_id } + | AliasTermKind::FreeConst { def_id } + | AliasTermKind::InherentConst { def_id }) = self; + def_id + } } -impl From> for AliasTermKind { +impl From> for AliasTermKind { fn from(value: ty::AliasTyKind) -> Self { match value { - ty::Projection { .. } => AliasTermKind::ProjectionTy, - ty::Opaque { .. } => AliasTermKind::OpaqueTy, - ty::Free { .. } => AliasTermKind::FreeTy, - ty::Inherent { .. } => AliasTermKind::InherentTy, + ty::Projection { def_id } => AliasTermKind::ProjectionTy { def_id }, + ty::Opaque { def_id } => AliasTermKind::OpaqueTy { def_id }, + ty::Free { def_id } => AliasTermKind::FreeTy { def_id }, + ty::Inherent { def_id } => AliasTermKind::InherentTy { def_id }, } } } @@ -655,17 +686,9 @@ pub struct AliasTerm { /// while for TAIT it is used for the generic parameters of the alias. pub args: I::GenericArgs, - /// 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)`. - pub def_id: I::DefId, + #[type_foldable(identity)] + #[type_visitable(ignore)] + pub kind: AliasTermKind, /// This field exists to prevent the creation of `AliasTerm` without using [`AliasTerm::new_from_args`]. #[derive_where(skip(Debug))] @@ -675,62 +698,86 @@ pub struct AliasTerm { impl Eq for AliasTerm {} impl AliasTerm { - pub fn new_from_args(interner: I, def_id: I::DefId, args: I::GenericArgs) -> AliasTerm { - interner.debug_assert_args_compatible(def_id, args); - AliasTerm { def_id, args, _use_alias_term_new_instead: () } + pub fn new_from_args( + interner: I, + kind: AliasTermKind, + args: I::GenericArgs, + ) -> AliasTerm { + interner.debug_assert_args_compatible(kind.def_id(), args); + AliasTerm { kind, args, _use_alias_term_new_instead: () } } pub fn new( interner: I, - def_id: I::DefId, + kind: AliasTermKind, args: impl IntoIterator>, ) -> AliasTerm { let args = interner.mk_args_from_iter(args.into_iter().map(Into::into)); - Self::new_from_args(interner, def_id, args) + Self::new_from_args(interner, kind, args) + } + + pub fn new_from_def_id(interner: I, def_id: I::DefId, args: I::GenericArgs) -> AliasTerm { + let kind = interner.alias_term_kind_from_def_id(def_id); + Self::new_from_args(interner, kind, args) + } + + pub fn from_unevaluated_const(interner: I, ct: ty::UnevaluatedConst) -> Self { + let kind = interner.alias_term_kind_from_def_id(ct.def.into()); + AliasTerm::new_from_args(interner, kind, ct.args) } pub fn expect_ty(self, interner: I) -> ty::AliasTy { let kind = match self.kind(interner) { - AliasTermKind::ProjectionTy => AliasTyKind::Projection { def_id: self.def_id }, - AliasTermKind::InherentTy => AliasTyKind::Inherent { def_id: self.def_id }, - AliasTermKind::OpaqueTy => AliasTyKind::Opaque { def_id: self.def_id }, - AliasTermKind::FreeTy => AliasTyKind::Free { def_id: self.def_id }, - AliasTermKind::InherentConst - | AliasTermKind::FreeConst - | AliasTermKind::UnevaluatedConst - | AliasTermKind::ProjectionConst => { + AliasTermKind::ProjectionTy { def_id } => AliasTyKind::Projection { def_id }, + AliasTermKind::InherentTy { def_id } => AliasTyKind::Inherent { def_id }, + AliasTermKind::OpaqueTy { def_id } => AliasTyKind::Opaque { def_id }, + AliasTermKind::FreeTy { def_id } => AliasTyKind::Free { def_id }, + AliasTermKind::InherentConst { .. } + | AliasTermKind::FreeConst { .. } + | AliasTermKind::UnevaluatedConst { .. } + | AliasTermKind::ProjectionConst { .. } => { panic!("Cannot turn `UnevaluatedConst` into `AliasTy`") } }; ty::AliasTy { kind, args: self.args, _use_alias_ty_new_instead: () } } - pub fn kind(self, interner: I) -> AliasTermKind { - interner.alias_term_kind(self) + // FIXME: remove this function (access the field instead) + pub fn kind(self, _interner: I) -> AliasTermKind { + self.kind + } + + // FIXME: replace with explicit matches + pub fn def_id(self) -> I::DefId { + self.kind.def_id() } pub fn to_term(self, interner: I) -> I::Term { let alias_ty_kind = match self.kind(interner) { - AliasTermKind::FreeConst - | AliasTermKind::InherentConst - | AliasTermKind::UnevaluatedConst - | AliasTermKind::ProjectionConst => { + AliasTermKind::FreeConst { def_id } + | AliasTermKind::InherentConst { def_id } + | AliasTermKind::UnevaluatedConst { def_id } + | AliasTermKind::ProjectionConst { def_id } => { return I::Const::new_unevaluated( interner, - ty::UnevaluatedConst::new(self.def_id.try_into().unwrap(), self.args), + ty::UnevaluatedConst::new(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 }, + AliasTermKind::ProjectionTy { def_id } => ty::Projection { def_id }, + AliasTermKind::InherentTy { def_id } => ty::Inherent { def_id }, + AliasTermKind::OpaqueTy { def_id } => ty::Opaque { def_id }, + AliasTermKind::FreeTy { def_id } => ty::Free { def_id }, }; Ty::new_alias(interner, ty::AliasTy::new_from_args(interner, alias_ty_kind, self.args)) .into() } + + pub fn with_args(self, interner: I, args: I::GenericArgs) -> Self { + Self::new_from_args(interner, self.kind, args) + } } /// The following methods work only with (trait) associated term projections. @@ -742,7 +789,7 @@ impl AliasTerm { pub fn with_replaced_self_ty(self, interner: I, self_ty: I::Ty) -> Self { AliasTerm::new( interner, - self.def_id, + self.kind, [self_ty.into()].into_iter().chain(self.args.iter().skip(1)), ) } @@ -751,11 +798,11 @@ impl AliasTerm { assert!( matches!( self.kind(interner), - AliasTermKind::ProjectionTy | AliasTermKind::ProjectionConst + AliasTermKind::ProjectionTy { .. } | AliasTermKind::ProjectionConst { .. } ), "expected a projection" ); - interner.parent(self.def_id).try_into().unwrap() + interner.parent(self.def_id()).try_into().unwrap() } /// Extracts the underlying trait reference and own args from this projection. @@ -763,7 +810,7 @@ impl AliasTerm { /// then this function would return a `T: StreamingIterator` trait reference and /// `['a]` as the own args. pub fn trait_ref_and_own_args(self, interner: I) -> (TraitRef, I::GenericArgsSlice) { - interner.trait_ref_and_own_args_for_alias(self.def_id, self.args) + interner.trait_ref_and_own_args_for_alias(self.def_id(), self.args) } /// Extracts the underlying trait reference from this projection. @@ -804,7 +851,7 @@ impl AliasTerm { ) -> I::GenericArgs { debug_assert!(matches!( self.kind(interner), - AliasTermKind::InherentTy | AliasTermKind::InherentConst + AliasTermKind::InherentTy { .. } | AliasTermKind::InherentConst { .. } )); interner.mk_args_from_iter(impl_args.iter().chain(self.args.iter().skip(1))) } @@ -812,13 +859,11 @@ impl AliasTerm { impl From> for AliasTerm { fn from(ty: ty::AliasTy) -> Self { - AliasTerm { args: ty.args, def_id: ty.kind.def_id(), _use_alias_term_new_instead: () } - } -} - -impl From> for AliasTerm { - fn from(ct: ty::UnevaluatedConst) -> Self { - AliasTerm { args: ct.args, def_id: ct.def.into(), _use_alias_term_new_instead: () } + AliasTerm { + args: ty.args, + kind: AliasTermKind::from(ty.kind), + _use_alias_term_new_instead: (), + } } } @@ -864,7 +909,7 @@ impl ProjectionPredicate { } pub fn def_id(self) -> I::DefId { - self.projection_term.def_id + self.projection_term.def_id() } } @@ -885,7 +930,7 @@ impl ty::Binder> { /// associated type, which is in `tcx.associated_item(projection_def_id()).container`. pub fn item_def_id(&self) -> I::DefId { // Ok to skip binder since trait `DefId` does not care about regions. - self.skip_binder().projection_term.def_id + self.skip_binder().projection_term.def_id() } } @@ -924,7 +969,7 @@ impl NormalizesTo { } pub fn def_id(self) -> I::DefId { - self.alias.def_id + self.alias.def_id() } } diff --git a/compiler/rustc_type_ir/src/relate.rs b/compiler/rustc_type_ir/src/relate.rs index 2ae8d52db6f5e..425436dabafb2 100644 --- a/compiler/rustc_type_ir/src/relate.rs +++ b/compiler/rustc_type_ir/src/relate.rs @@ -220,7 +220,7 @@ impl Relate for ty::AliasTy { ))) } else { let cx = relation.cx(); - let args = if let Some(variances) = cx.opt_alias_variances(a.kind, a.kind.def_id()) { + let args = if let Some(variances) = cx.opt_alias_variances(a.kind) { relate_args_with_variances(relation, variances, a.args, b.args)? } else { relate_args_invariantly(relation, a.args, b.args)? @@ -236,27 +236,27 @@ impl Relate for ty::AliasTerm { a: ty::AliasTerm, b: ty::AliasTerm, ) -> RelateResult> { - if a.def_id != b.def_id { - Err(TypeError::ProjectionMismatched(ExpectedFound::new(a.def_id, b.def_id))) + if a.def_id() != b.def_id() { + Err(TypeError::ProjectionMismatched(ExpectedFound::new(a.def_id(), b.def_id()))) } else { let args = match a.kind(relation.cx()) { - ty::AliasTermKind::OpaqueTy => relate_args_with_variances( + ty::AliasTermKind::OpaqueTy { .. } => relate_args_with_variances( relation, - relation.cx().variances_of(a.def_id), + relation.cx().variances_of(a.def_id()), a.args, b.args, )?, - ty::AliasTermKind::ProjectionTy - | ty::AliasTermKind::FreeConst - | ty::AliasTermKind::FreeTy - | ty::AliasTermKind::InherentTy - | ty::AliasTermKind::InherentConst - | ty::AliasTermKind::UnevaluatedConst - | ty::AliasTermKind::ProjectionConst => { + ty::AliasTermKind::ProjectionTy { .. } + | ty::AliasTermKind::FreeConst { .. } + | ty::AliasTermKind::FreeTy { .. } + | ty::AliasTermKind::InherentTy { .. } + | ty::AliasTermKind::InherentConst { .. } + | ty::AliasTermKind::UnevaluatedConst { .. } + | ty::AliasTermKind::ProjectionConst { .. } => { relate_args_invariantly(relation, a.args, b.args)? } }; - Ok(ty::AliasTerm::new_from_args(relation.cx(), a.def_id, args)) + Ok(a.with_args(relation.cx(), args)) } } } diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index 345f13dae3224..f2ea85a4bf8ee 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -563,7 +563,7 @@ fn projection_to_path_segment<'tcx>( proj: ty::Binder<'tcx, ty::AliasTerm<'tcx>>, cx: &mut DocContext<'tcx>, ) -> PathSegment { - let def_id = proj.skip_binder().def_id; + let def_id = proj.skip_binder().def_id(); let generics = cx.tcx.generics_of(def_id); PathSegment { name: cx.tcx.item_name(def_id), diff --git a/src/tools/clippy/clippy_utils/src/ty/mod.rs b/src/tools/clippy/clippy_utils/src/ty/mod.rs index 0c261d21c1ec5..5c6fecde238a4 100644 --- a/src/tools/clippy/clippy_utils/src/ty/mod.rs +++ b/src/tools/clippy/clippy_utils/src/ty/mod.rs @@ -696,7 +696,7 @@ fn sig_from_bounds<'tcx>( inputs = Some(i); }, ty::ClauseKind::Projection(p) - if Some(p.projection_term.def_id) == lang_items.fn_once_output() + if Some(p.projection_term.def_id()) == lang_items.fn_once_output() && p.projection_term.self_ty() == ty => { if output.is_some() { @@ -737,7 +737,7 @@ fn sig_for_projection<'tcx>(cx: &LateContext<'tcx>, ty: AliasTy<'tcx>) -> Option } inputs = Some(i); }, - ty::ClauseKind::Projection(p) if Some(p.projection_term.def_id) == lang_items.fn_once_output() => { + ty::ClauseKind::Projection(p) if Some(p.projection_term.def_id()) == lang_items.fn_once_output() => { if output.is_some() { // Multiple different fn trait impls. Is this even allowed? return None; diff --git a/tests/ui/attributes/dump-preds.stderr b/tests/ui/attributes/dump-preds.stderr index dd2453342f7f5..12d9308431734 100644 --- a/tests/ui/attributes/dump-preds.stderr +++ b/tests/ui/attributes/dump-preds.stderr @@ -33,7 +33,7 @@ error: rustc_dump_item_bounds LL | type Assoc: std::ops::Deref | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | - = note: Binder { value: ProjectionPredicate(AliasTerm { args: [Alias(AliasTy { args: [Self/#0, T/#1, P/#2], kind: Projection { def_id: DefId(..) }, .. })], def_id: DefId(..), .. }, Term::Ty(())), bound_vars: [] } + = note: Binder { value: ProjectionPredicate(AliasTerm { args: [Alias(AliasTy { args: [Self/#0, T/#1, P/#2], kind: Projection { def_id: DefId(..) }, .. })], kind: ProjectionTy { def_id: DefId(..) }, .. }, Term::Ty(())), bound_vars: [] } = note: Binder { value: TraitPredicate(<>::Assoc

as std::ops::Deref>, polarity:Positive), bound_vars: [] } = note: Binder { value: TraitPredicate(<>::Assoc

as std::marker::Sized>, polarity:Positive), bound_vars: [] } diff --git a/tests/ui/coherence/occurs-check/associated-type.next.stderr b/tests/ui/coherence/occurs-check/associated-type.next.stderr index 4f02a91ae8ee3..38fb75483f3b3 100644 --- a/tests/ui/coherence/occurs-check/associated-type.next.stderr +++ b/tests/ui/coherence/occurs-check/associated-type.next.stderr @@ -1,5 +1,5 @@ - WARN rustc_infer::infer::relate::generalize may incompletely handle alias type: AliasTerm { args: [*const ?1t, '^0.Named(DefId(0:27 ~ associated_type[f554]::{impl#3}::'a#1))], def_id: DefId(0:5 ~ associated_type[f554]::ToUnit::Unit), .. } - WARN rustc_infer::infer::relate::generalize may incompletely handle alias type: AliasTerm { args: [*const ?1t, '^0.Named(DefId(0:27 ~ associated_type[f554]::{impl#3}::'a#1))], def_id: DefId(0:5 ~ associated_type[f554]::ToUnit::Unit), .. } + WARN rustc_infer::infer::relate::generalize may incompletely handle alias type: AliasTerm { args: [*const ?1t, '^0.Named(DefId(0:27 ~ associated_type[f554]::{impl#3}::'a#1))], kind: ProjectionTy { def_id: DefId(0:5 ~ associated_type[f554]::ToUnit::Unit) }, .. } + WARN rustc_infer::infer::relate::generalize may incompletely handle alias type: AliasTerm { args: [*const ?1t, '^0.Named(DefId(0:27 ~ associated_type[f554]::{impl#3}::'a#1))], kind: ProjectionTy { def_id: DefId(0:5 ~ associated_type[f554]::ToUnit::Unit) }, .. } error[E0119]: conflicting implementations of trait `Overlap fn(&'a (), ())>` for type `for<'a> fn(&'a (), ())` --> $DIR/associated-type.rs:32:1 | diff --git a/tests/ui/coherence/occurs-check/associated-type.old.stderr b/tests/ui/coherence/occurs-check/associated-type.old.stderr index 40811a67dd180..1dac3ecea2687 100644 --- a/tests/ui/coherence/occurs-check/associated-type.old.stderr +++ b/tests/ui/coherence/occurs-check/associated-type.old.stderr @@ -1,5 +1,5 @@ - WARN rustc_infer::infer::relate::generalize may incompletely handle alias type: AliasTerm { args: [*const ?1t, '^0.Named(DefId(0:27 ~ associated_type[f554]::{impl#3}::'a#1))], def_id: DefId(0:5 ~ associated_type[f554]::ToUnit::Unit), .. } - WARN rustc_infer::infer::relate::generalize may incompletely handle alias type: AliasTerm { args: [*const ?1t, '^0.Named(DefId(0:27 ~ associated_type[f554]::{impl#3}::'a#1))], def_id: DefId(0:5 ~ associated_type[f554]::ToUnit::Unit), .. } + WARN rustc_infer::infer::relate::generalize may incompletely handle alias type: AliasTerm { args: [*const ?1t, '^0.Named(DefId(0:27 ~ associated_type[f554]::{impl#3}::'a#1))], kind: ProjectionTy { def_id: DefId(0:5 ~ associated_type[f554]::ToUnit::Unit) }, .. } + WARN rustc_infer::infer::relate::generalize may incompletely handle alias type: AliasTerm { args: [*const ?1t, '^0.Named(DefId(0:27 ~ associated_type[f554]::{impl#3}::'a#1))], kind: ProjectionTy { def_id: DefId(0:5 ~ associated_type[f554]::ToUnit::Unit) }, .. } error[E0119]: conflicting implementations of trait `Overlap fn(&'a (), ())>` for type `for<'a> fn(&'a (), ())` --> $DIR/associated-type.rs:32:1 | diff --git a/tests/ui/higher-ranked/structually-relate-aliases.stderr b/tests/ui/higher-ranked/structually-relate-aliases.stderr index f8fcb73db4478..4649aac47cdbf 100644 --- a/tests/ui/higher-ranked/structually-relate-aliases.stderr +++ b/tests/ui/higher-ranked/structually-relate-aliases.stderr @@ -1,4 +1,4 @@ - WARN rustc_infer::infer::relate::generalize may incompletely handle alias type: AliasTerm { args: [?1t, '^0.Named(DefId(0:15 ~ structually_relate_aliases[de75]::{impl#1}::'a))], def_id: DefId(0:5 ~ structually_relate_aliases[de75]::ToUnit::Unit), .. } + WARN rustc_infer::infer::relate::generalize may incompletely handle alias type: AliasTerm { args: [?1t, '^0.Named(DefId(0:15 ~ structually_relate_aliases[de75]::{impl#1}::'a))], kind: ProjectionTy { def_id: DefId(0:5 ~ structually_relate_aliases[de75]::ToUnit::Unit) }, .. } error[E0277]: the trait bound `for<'a> T: ToUnit<'a>` is not satisfied --> $DIR/structually-relate-aliases.rs:13:36 | diff --git a/tests/ui/traits/next-solver/issue-118950-root-region.stderr b/tests/ui/traits/next-solver/issue-118950-root-region.stderr index a4c8d259406b0..32adbb491bee7 100644 --- a/tests/ui/traits/next-solver/issue-118950-root-region.stderr +++ b/tests/ui/traits/next-solver/issue-118950-root-region.stderr @@ -16,7 +16,7 @@ help: this trait has no implementations, consider adding one LL | trait ToUnit<'a> { | ^^^^^^^^^^^^^^^^ - WARN rustc_infer::infer::relate::generalize may incompletely handle alias type: AliasTerm { args: ['^0.Named(DefId(0:15 ~ issue_118950_root_region[d54f]::{impl#1}::'a)), ?1t], def_id: DefId(0:8 ~ issue_118950_root_region[d54f]::Assoc), .. } + WARN rustc_infer::infer::relate::generalize may incompletely handle alias type: AliasTerm { args: ['^0.Named(DefId(0:15 ~ issue_118950_root_region[d54f]::{impl#1}::'a)), ?1t], kind: FreeTy { def_id: DefId(0:8 ~ issue_118950_root_region[d54f]::Assoc) }, .. } error: aborting due to 2 previous errors Some errors have detailed explanations: E0277, E0425.