diff --git a/compiler/rustc_hir_analysis/src/check/check.rs b/compiler/rustc_hir_analysis/src/check/check.rs index 3b53c253195..e13ea1a1935 100644 --- a/compiler/rustc_hir_analysis/src/check/check.rs +++ b/compiler/rustc_hir_analysis/src/check/check.rs @@ -719,7 +719,7 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) { tcx, assoc_item, assoc_item, - ty::TraitRef::new(tcx, def_id.to_def_id(), trait_args), + ty::TraitRef::new_from_args(tcx, def_id.to_def_id(), trait_args), ); } _ => {} 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 550f38af8b5..7fa5c96bc9a 100644 --- a/compiler/rustc_hir_analysis/src/check/compare_impl_item.rs +++ b/compiler/rustc_hir_analysis/src/check/compare_impl_item.rs @@ -2032,7 +2032,7 @@ pub(super) fn check_type_bounds<'tcx>( // to its definition type. This should be the param-env we use to *prove* the // predicate too, but we don't do that because of performance issues. // See . - let trait_projection_ty = Ty::new_projection(tcx, trait_ty.def_id, rebased_args); + let trait_projection_ty = Ty::new_projection_from_args(tcx, trait_ty.def_id, rebased_args); let impl_identity_ty = tcx.type_of(impl_ty.def_id).instantiate_identity(); let normalize_param_env = param_env_with_gat_bounds(tcx, impl_ty, impl_trait_ref); for mut obligation in util::elaborate(tcx, obligations) { @@ -2230,7 +2230,11 @@ fn param_env_with_gat_bounds<'tcx>( _ => predicates.push( ty::Binder::bind_with_vars( ty::ProjectionPredicate { - projection_term: ty::AliasTerm::new(tcx, trait_ty.def_id, rebased_args), + projection_term: ty::AliasTerm::new_from_args( + tcx, + trait_ty.def_id, + rebased_args, + ), term: normalize_impl_ty.into(), }, bound_vars, diff --git a/compiler/rustc_hir_analysis/src/check/intrinsic.rs b/compiler/rustc_hir_analysis/src/check/intrinsic.rs index 683709f43f2..f21aeb4c0b9 100644 --- a/compiler/rustc_hir_analysis/src/check/intrinsic.rs +++ b/compiler/rustc_hir_analysis/src/check/intrinsic.rs @@ -504,7 +504,11 @@ pub fn check_intrinsic_type( ty::Region::new_bound(tcx, ty::INNERMOST, br), param(0), )], - Ty::new_projection(tcx, discriminant_def_id, tcx.mk_args(&[param(0).into()])), + Ty::new_projection_from_args( + tcx, + discriminant_def_id, + tcx.mk_args(&[param(0).into()]), + ), ) } diff --git a/compiler/rustc_hir_analysis/src/collect.rs b/compiler/rustc_hir_analysis/src/collect.rs index e5bd147352d..e7892f17660 100644 --- a/compiler/rustc_hir_analysis/src/collect.rs +++ b/compiler/rustc_hir_analysis/src/collect.rs @@ -423,7 +423,7 @@ impl<'tcx> HirTyLowerer<'tcx> for ItemCtxt<'tcx> { item_segment, trait_ref.args, ); - Ty::new_projection(self.tcx(), item_def_id, item_args) + Ty::new_projection_from_args(self.tcx(), item_def_id, item_args) } else { // There are no late-bound regions; we can just ignore the binder. let (mut mpart_sugg, mut inferred_sugg) = (None, None); @@ -1607,7 +1607,7 @@ pub fn suggest_impl_trait<'tcx>( let item_ty = ocx.normalize( &ObligationCause::dummy(), param_env, - Ty::new_projection(infcx.tcx, assoc_item_def_id, args), + Ty::new_projection_from_args(infcx.tcx, assoc_item_def_id, args), ); // FIXME(compiler-errors): We may benefit from resolving regions here. if ocx.select_where_possible().is_empty() diff --git a/compiler/rustc_hir_analysis/src/collect/item_bounds.rs b/compiler/rustc_hir_analysis/src/collect/item_bounds.rs index 9f198933dee..d084d3aefeb 100644 --- a/compiler/rustc_hir_analysis/src/collect/item_bounds.rs +++ b/compiler/rustc_hir_analysis/src/collect/item_bounds.rs @@ -23,7 +23,7 @@ fn associated_type_bounds<'tcx>( span: Span, filter: PredicateFilter, ) -> &'tcx [(ty::Clause<'tcx>, Span)] { - let item_ty = Ty::new_projection( + let item_ty = Ty::new_projection_from_args( tcx, assoc_item_def_id.to_def_id(), GenericArgs::identity_for_item(tcx, assoc_item_def_id), @@ -108,7 +108,7 @@ pub(super) fn explicit_item_bounds_with_filter( tcx, opaque_def_id.expect_local(), opaque_ty.bounds, - Ty::new_projection( + Ty::new_projection_from_args( tcx, def_id.to_def_id(), ty::GenericArgs::identity_for_item(tcx, def_id), 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 c7699b0b310..802215b2843 100644 --- a/compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs +++ b/compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs @@ -409,7 +409,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { ); debug!(?alias_args); - ty::AliasTerm::new(tcx, assoc_item.def_id, alias_args) + ty::AliasTerm::new_from_args(tcx, assoc_item.def_id, alias_args) }); // Provide the resolved type of the associated constant to `type_of(AnonConst)`. 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 5911d5bb4e1..24ea328889c 100644 --- a/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs +++ b/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs @@ -693,7 +693,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { debug!(?bound_vars); let poly_trait_ref = ty::Binder::bind_with_vars( - ty::TraitRef::new(tcx, trait_def_id, generic_args), + ty::TraitRef::new_from_args(tcx, trait_def_id, generic_args), bound_vars, ); @@ -759,7 +759,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { Some((trait_def_id, trait_segment, span)), ); } - ty::TraitRef::new(self.tcx(), trait_def_id, generic_args) + ty::TraitRef::new_from_args(self.tcx(), trait_def_id, generic_args) } fn probe_trait_that_defines_assoc_item( @@ -789,7 +789,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { // Type aliases defined in crates that have the // feature `lazy_type_alias` enabled get encoded as a type alias that normalization will // then actually instantiate the where bounds of. - let alias_ty = ty::AliasTy::new(tcx, did, args); + let alias_ty = ty::AliasTy::new_from_args(tcx, did, args); Ty::new_alias(tcx, ty::Weak, alias_ty) } else { tcx.at(span).type_of(did).instantiate(tcx, args) @@ -1267,7 +1267,8 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { .chain(args.into_iter().skip(parent_args.len())), ); - let ty = Ty::new_alias(tcx, ty::Inherent, ty::AliasTy::new(tcx, assoc_item, args)); + let ty = + Ty::new_alias(tcx, ty::Inherent, ty::AliasTy::new_from_args(tcx, assoc_item, args)); Ok(Some((ty, assoc_item))) } @@ -1534,7 +1535,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { let item_args = self.lower_generic_args_of_assoc_item(span, item_def_id, item_segment, trait_ref.args); - Ty::new_projection(tcx, item_def_id, item_args) + Ty::new_projection_from_args(tcx, item_def_id, item_args) } pub fn prohibit_generic_args<'a>( @@ -2302,7 +2303,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { debug!(?args); if in_trait { - Ty::new_projection(tcx, def_id, args) + Ty::new_projection_from_args(tcx, def_id, args) } else { Ty::new_opaque(tcx, def_id, args) } diff --git a/compiler/rustc_hir_typeck/src/expr.rs b/compiler/rustc_hir_typeck/src/expr.rs index 233dc2afa9b..f4e1e461953 100644 --- a/compiler/rustc_hir_typeck/src/expr.rs +++ b/compiler/rustc_hir_typeck/src/expr.rs @@ -3108,7 +3108,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let element_ty = ocx.normalize( &cause, self.param_env, - Ty::new_projection(self.tcx, index_trait_output_def_id, impl_trait_ref.args), + Ty::new_projection_from_args( + self.tcx, + index_trait_output_def_id, + impl_trait_ref.args, + ), ); let true_errors = ocx.select_where_possible(); diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/adjust_fulfillment_errors.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/adjust_fulfillment_errors.rs index 4edc11d7ab1..f7abba35706 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/adjust_fulfillment_errors.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/adjust_fulfillment_errors.rs @@ -569,7 +569,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { // For the purposes of this function, we hope that it is a `struct` type, and that our current `expr` is a literal of // that struct type. let impl_trait_self_ref = if self.tcx.is_trait_alias(obligation.impl_or_alias_def_id) { - ty::TraitRef::new( + ty::TraitRef::new_from_args( self.tcx, obligation.impl_or_alias_def_id, ty::GenericArgs::identity_for_item(self.tcx, obligation.impl_or_alias_def_id), diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/mod.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/mod.rs index 1713d75092e..c0d60477967 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/mod.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/mod.rs @@ -297,7 +297,7 @@ impl<'tcx> HirTyLowerer<'tcx> for FnCtxt<'_, 'tcx> { trait_ref.args, ); - Ty::new_projection(self.tcx(), item_def_id, item_args) + Ty::new_projection_from_args(self.tcx(), item_def_id, item_args) } fn probe_adt(&self, span: Span, ty: Ty<'tcx>) -> Option> { diff --git a/compiler/rustc_hir_typeck/src/method/mod.rs b/compiler/rustc_hir_typeck/src/method/mod.rs index 1f90d5e4c88..e1223307b53 100644 --- a/compiler/rustc_hir_typeck/src/method/mod.rs +++ b/compiler/rustc_hir_typeck/src/method/mod.rs @@ -333,7 +333,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { self.var_for_def(cause.span, param) }); - let trait_ref = ty::TraitRef::new(self.tcx, trait_def_id, args); + let trait_ref = ty::TraitRef::new_from_args(self.tcx, trait_def_id, args); // Construct an obligation let poly_trait_ref = ty::Binder::dummy(trait_ref); diff --git a/compiler/rustc_hir_typeck/src/method/probe.rs b/compiler/rustc_hir_typeck/src/method/probe.rs index 47ea221d1a1..9747a91ccbf 100644 --- a/compiler/rustc_hir_typeck/src/method/probe.rs +++ b/compiler/rustc_hir_typeck/src/method/probe.rs @@ -870,7 +870,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> { trait_def_id: DefId, ) { let trait_args = self.fresh_args_for_item(self.span, trait_def_id); - let trait_ref = ty::TraitRef::new(self.tcx, trait_def_id, trait_args); + let trait_ref = ty::TraitRef::new_from_args(self.tcx, trait_def_id, trait_args); if self.tcx.is_trait_alias(trait_def_id) { // For trait aliases, recursively assume all explicitly named traits are relevant diff --git a/compiler/rustc_hir_typeck/src/method/suggest.rs b/compiler/rustc_hir_typeck/src/method/suggest.rs index a5e955a986b..4f46320fa02 100644 --- a/compiler/rustc_hir_typeck/src/method/suggest.rs +++ b/compiler/rustc_hir_typeck/src/method/suggest.rs @@ -1978,7 +1978,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { err, self_source, args, - ty::TraitRef::new( + ty::TraitRef::new_from_args( self.tcx, trait_did, self.fresh_args_for_item(sugg_span, trait_did), diff --git a/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/placeholder_error.rs b/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/placeholder_error.rs index e125f1858dd..80b7e3b4fa5 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/placeholder_error.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/placeholder_error.rs @@ -256,12 +256,12 @@ impl<'tcx> NiceRegionError<'_, 'tcx> { (false, None, None, Some(span), String::new()) }; - let expected_trait_ref = self.cx.resolve_vars_if_possible(ty::TraitRef::new( + let expected_trait_ref = self.cx.resolve_vars_if_possible(ty::TraitRef::new_from_args( self.cx.tcx, trait_def_id, expected_args, )); - let actual_trait_ref = self.cx.resolve_vars_if_possible(ty::TraitRef::new( + let actual_trait_ref = self.cx.resolve_vars_if_possible(ty::TraitRef::new_from_args( self.cx.tcx, trait_def_id, actual_args, diff --git a/compiler/rustc_lint/src/opaque_hidden_inferred_bound.rs b/compiler/rustc_lint/src/opaque_hidden_inferred_bound.rs index 7aef6321eeb..8b669bcc13f 100644 --- a/compiler/rustc_lint/src/opaque_hidden_inferred_bound.rs +++ b/compiler/rustc_lint/src/opaque_hidden_inferred_bound.rs @@ -109,7 +109,7 @@ impl<'tcx> LateLintPass<'tcx> for OpaqueHiddenInferredBound { return; } - let proj_ty = Ty::new_projection( + let proj_ty = Ty::new_projection_from_args( cx.tcx, proj.projection_term.def_id, proj.projection_term.args, diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index 7848aa21eac..4bac9396e59 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -240,7 +240,7 @@ impl<'tcx> Interner for TyCtxt<'tcx> { assert_matches!(self.def_kind(trait_def_id), DefKind::Trait); let trait_generics = self.generics_of(trait_def_id); ( - ty::TraitRef::new(self, trait_def_id, args.truncate_to(self, trait_generics)), + ty::TraitRef::new_from_args(self, trait_def_id, args.truncate_to(self, trait_generics)), &args[trait_generics.count()..], ) } @@ -261,12 +261,8 @@ impl<'tcx> Interner for TyCtxt<'tcx> { self.check_args_compatible(def_id, args) } - fn check_and_mk_args( - self, - def_id: DefId, - args: impl IntoIterator>>, - ) -> ty::GenericArgsRef<'tcx> { - self.check_and_mk_args(def_id, args) + fn debug_assert_args_compatible(self, def_id: DefId, args: ty::GenericArgsRef<'tcx>) { + self.debug_assert_args_compatible(def_id, args); } fn intern_canonical_goal_evaluation_step( diff --git a/compiler/rustc_middle/src/ty/sty.rs b/compiler/rustc_middle/src/ty/sty.rs index 9c8a3484aa5..a3cb84d561d 100644 --- a/compiler/rustc_middle/src/ty/sty.rs +++ b/compiler/rustc_middle/src/ty/sty.rs @@ -499,7 +499,7 @@ impl<'tcx> Ty<'tcx> { #[inline] pub fn new_opaque(tcx: TyCtxt<'tcx>, def_id: DefId, args: GenericArgsRef<'tcx>) -> Ty<'tcx> { - Ty::new_alias(tcx, ty::Opaque, AliasTy::new(tcx, def_id, args)) + Ty::new_alias(tcx, ty::Opaque, AliasTy::new_from_args(tcx, def_id, args)) } /// Constructs a `TyKind::Error` type with current `ErrorGuaranteed` @@ -669,6 +669,15 @@ impl<'tcx> Ty<'tcx> { Ty::new(tcx, Dynamic(obj, reg, repr)) } + #[inline] + pub fn new_projection_from_args( + tcx: TyCtxt<'tcx>, + item_def_id: DefId, + args: ty::GenericArgsRef<'tcx>, + ) -> Ty<'tcx> { + Ty::new_alias(tcx, ty::Projection, AliasTy::new_from_args(tcx, item_def_id, args)) + } + #[inline] pub fn new_projection( tcx: TyCtxt<'tcx>, @@ -1409,7 +1418,7 @@ impl<'tcx> Ty<'tcx> { let assoc_items = tcx.associated_item_def_ids( tcx.require_lang_item(hir::LangItem::DiscriminantKind, None), ); - Ty::new_projection(tcx, assoc_items[0], tcx.mk_args(&[self.into()])) + Ty::new_projection_from_args(tcx, assoc_items[0], tcx.mk_args(&[self.into()])) } ty::Pat(ty, _) => ty.discriminant_ty(tcx), diff --git a/compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs b/compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs index dda6c88008b..8c6c9e10cdf 100644 --- a/compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs +++ b/compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs @@ -220,7 +220,7 @@ impl<'tcx> ConstToPat<'tcx> { tcx, ObligationCause::dummy(), self.param_env, - ty::TraitRef::new( + ty::TraitRef::new_from_args( tcx, partial_eq_trait_id, tcx.with_opt_host_effect_param( 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 6644fff2140..687623d4408 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 @@ -787,7 +787,7 @@ 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(tcx, alias.def_id, identity_args); + let rigid_ctor = ty::AliasTerm::new_from_args(tcx, alias.def_id, identity_args); let ctor_term = rigid_ctor.to_term(tcx); let obligations = self.delegate.eq_structurally_relating_aliases(param_env, term, ctor_term)?; 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 44b31f2659c..f0f2d1fefd2 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 @@ -232,7 +232,8 @@ fn trait_object_ty<'tcx>(tcx: TyCtxt<'tcx>, poly_trait_ref: ty::PolyTraitRef<'tc .filter(|item| item.kind == ty::AssocKind::Type) .map(move |assoc_ty| { super_poly_trait_ref.map_bound(|super_trait_ref| { - let alias_ty = ty::AliasTy::new(tcx, assoc_ty.def_id, super_trait_ref.args); + let alias_ty = + ty::AliasTy::new_from_args(tcx, assoc_ty.def_id, super_trait_ref.args); let resolved = tcx.normalize_erasing_regions( ty::ParamEnv::reveal_all(), alias_ty.to_ty(tcx), @@ -351,7 +352,7 @@ pub fn transform_instance<'tcx>( // Adjust the type ids of VTableShims to the type id expected in the call sites for the // entry in the vtable (i.e., by using the signature of the closure passed as an argument // to the shim, or by just removing self). - let trait_ref = ty::TraitRef::new(tcx, trait_id, instance.args); + let trait_ref = ty::TraitRef::new_from_args(tcx, trait_id, instance.args); let invoke_ty = trait_object_ty(tcx, ty::Binder::dummy(trait_ref)); instance.args = tcx.mk_args_trait(invoke_ty, trait_ref.args.into_iter().skip(1)); } @@ -432,7 +433,7 @@ pub fn transform_instance<'tcx>( x => bug!("Unexpected type kind for closure-like: {x:?}"), }; let concrete_args = tcx.mk_args_trait(closure_ty, inputs.map(Into::into)); - let trait_ref = ty::TraitRef::new(tcx, trait_id, concrete_args); + let trait_ref = ty::TraitRef::new_from_args(tcx, trait_id, concrete_args); let invoke_ty = trait_object_ty(tcx, ty::Binder::dummy(trait_ref)); let abstract_args = tcx.mk_args_trait(invoke_ty, trait_ref.args.into_iter().skip(1)); // There should be exactly one method on this trait, and it should be the one we're diff --git a/compiler/rustc_smir/src/rustc_internal/internal.rs b/compiler/rustc_smir/src/rustc_internal/internal.rs index c33a52f4a7a..c9c1570a29a 100644 --- a/compiler/rustc_smir/src/rustc_internal/internal.rs +++ b/compiler/rustc_smir/src/rustc_internal/internal.rs @@ -408,7 +408,7 @@ impl RustcInternal for TraitRef { type T<'tcx> = rustc_ty::TraitRef<'tcx>; fn internal<'tcx>(&self, tables: &mut Tables<'_>, tcx: TyCtxt<'tcx>) -> Self::T<'tcx> { - rustc_ty::TraitRef::new( + rustc_ty::TraitRef::new_from_args( tcx, self.def_id.0.internal(tables, tcx), self.args().internal(tables, tcx), diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs index b2fa3489dda..87d88312b4c 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs @@ -630,7 +630,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> { let trait_pred_and_ty = trait_pred.map_bound(|inner| { ( ty::TraitPredicate { - trait_ref: ty::TraitRef::new( + trait_ref: ty::TraitRef::new_from_args( self.tcx, inner.trait_ref.def_id, self.tcx.mk_args( @@ -3955,7 +3955,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> { // Extract `::Target` assoc type and check that it is `T` && let Some(deref_target_did) = tcx.lang_items().deref_target() - && let projection = Ty::new_projection(tcx,deref_target_did, tcx.mk_args(&[ty::GenericArg::from(found_ty)])) + && let projection = Ty::new_projection_from_args(tcx,deref_target_did, tcx.mk_args(&[ty::GenericArg::from(found_ty)])) && let InferOk { value: deref_target, obligations } = infcx.at(&ObligationCause::dummy(), param_env).normalize(projection) && obligations.iter().all(|obligation| infcx.predicate_must_hold_modulo_regions(obligation)) && infcx.can_eq(param_env, deref_target, target_ty) @@ -4290,7 +4290,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> { // This corresponds to `::Item = _`. let projection = ty::Binder::dummy(ty::PredicateKind::Clause( ty::ClauseKind::Projection(ty::ProjectionPredicate { - projection_term: ty::AliasTerm::new(self.tcx, proj.def_id, args), + projection_term: ty::AliasTerm::new_from_args(self.tcx, proj.def_id, args), term: ty.into(), }), )); diff --git a/compiler/rustc_trait_selection/src/traits/object_safety.rs b/compiler/rustc_trait_selection/src/traits/object_safety.rs index fc5a2875b67..4d10d33fa6e 100644 --- a/compiler/rustc_trait_selection/src/traits/object_safety.rs +++ b/compiler/rustc_trait_selection/src/traits/object_safety.rs @@ -689,7 +689,7 @@ fn receiver_is_dispatchable<'tcx>( if param.index == 0 { unsized_self_ty.into() } else { tcx.mk_param_from_def(param) } }); - ty::TraitRef::new(tcx, trait_def_id, args).upcast(tcx) + ty::TraitRef::new_from_args(tcx, trait_def_id, args).upcast(tcx) }; let caller_bounds = diff --git a/compiler/rustc_trait_selection/src/traits/project.rs b/compiler/rustc_trait_selection/src/traits/project.rs index 8ab324e6601..1d7a0515044 100644 --- a/compiler/rustc_trait_selection/src/traits/project.rs +++ b/compiler/rustc_trait_selection/src/traits/project.rs @@ -727,10 +727,12 @@ fn project<'cx, 'tcx>( ProjectionCandidateSet::None => { let tcx = selcx.tcx(); let term = match tcx.def_kind(obligation.predicate.def_id) { - DefKind::AssocTy => { - Ty::new_projection(tcx, obligation.predicate.def_id, obligation.predicate.args) - .into() - } + DefKind::AssocTy => Ty::new_projection_from_args( + tcx, + obligation.predicate.def_id, + obligation.predicate.args, + ) + .into(), DefKind::AssocConst => ty::Const::new_unevaluated( tcx, ty::UnevaluatedConst::new( @@ -1387,7 +1389,11 @@ fn confirm_coroutine_candidate<'cx, 'tcx>( }; let predicate = ty::ProjectionPredicate { - projection_term: ty::AliasTerm::new(tcx, obligation.predicate.def_id, trait_ref.args), + projection_term: ty::AliasTerm::new_from_args( + tcx, + obligation.predicate.def_id, + trait_ref.args, + ), term: ty.into(), }; @@ -1431,7 +1437,11 @@ fn confirm_future_candidate<'cx, 'tcx>( debug_assert_eq!(tcx.associated_item(obligation.predicate.def_id).name, sym::Output); let predicate = ty::ProjectionPredicate { - projection_term: ty::AliasTerm::new(tcx, obligation.predicate.def_id, trait_ref.args), + projection_term: ty::AliasTerm::new_from_args( + tcx, + obligation.predicate.def_id, + trait_ref.args, + ), term: return_ty.into(), }; @@ -1473,7 +1483,11 @@ fn confirm_iterator_candidate<'cx, 'tcx>( debug_assert_eq!(tcx.associated_item(obligation.predicate.def_id).name, sym::Item); let predicate = ty::ProjectionPredicate { - projection_term: ty::AliasTerm::new(tcx, obligation.predicate.def_id, trait_ref.args), + projection_term: ty::AliasTerm::new_from_args( + tcx, + obligation.predicate.def_id, + trait_ref.args, + ), term: yield_ty.into(), }; @@ -1523,7 +1537,11 @@ fn confirm_async_iterator_candidate<'cx, 'tcx>( let item_ty = args.type_at(0); let predicate = ty::ProjectionPredicate { - projection_term: ty::AliasTerm::new(tcx, obligation.predicate.def_id, trait_ref.args), + projection_term: ty::AliasTerm::new_from_args( + tcx, + obligation.predicate.def_id, + trait_ref.args, + ), term: item_ty.into(), }; @@ -1592,7 +1610,7 @@ fn confirm_builtin_candidate<'cx, 'tcx>( }; let predicate = ty::ProjectionPredicate { - projection_term: ty::AliasTerm::new(tcx, item_def_id, args), + projection_term: ty::AliasTerm::new_from_args(tcx, item_def_id, args), term, }; @@ -1753,7 +1771,7 @@ fn confirm_callable_candidate<'cx, 'tcx>( fn_host_effect, ) .map_bound(|(trait_ref, ret_type)| ty::ProjectionPredicate { - projection_term: ty::AliasTerm::new(tcx, fn_once_output_def_id, trait_ref.args), + projection_term: ty::AliasTerm::new_from_args(tcx, fn_once_output_def_id, trait_ref.args), term: ret_type.into(), }); @@ -1937,7 +1955,7 @@ fn confirm_async_fn_kind_helper_candidate<'cx, 'tcx>( }; let predicate = ty::ProjectionPredicate { - projection_term: ty::AliasTerm::new( + projection_term: ty::AliasTerm::new_from_args( selcx.tcx(), obligation.predicate.def_id, obligation.predicate.args, diff --git a/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs b/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs index c53939bfe60..e36a9ca8bd1 100644 --- a/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs +++ b/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs @@ -940,7 +940,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { let ty = traits::normalize_projection_ty( self, param_env, - ty::AliasTy::new(tcx, tcx.lang_items().deref_target()?, trait_ref.args), + ty::AliasTy::new_from_args(tcx, tcx.lang_items().deref_target()?, trait_ref.args), cause.clone(), 0, // We're *intentionally* throwing these away, diff --git a/compiler/rustc_trait_selection/src/traits/select/mod.rs b/compiler/rustc_trait_selection/src/traits/select/mod.rs index fe047f9966f..68cc04bc8e6 100644 --- a/compiler/rustc_trait_selection/src/traits/select/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/select/mod.rs @@ -2487,7 +2487,7 @@ impl<'tcx> SelectionContext<'_, 'tcx> { trait_def_id, &[normalized_ty.into()], ); - ty::TraitRef::new(tcx, trait_def_id, err_args) + ty::TraitRef::new_from_args(tcx, trait_def_id, err_args) }; let obligation = Obligation::new(self.tcx(), cause.clone(), param_env, trait_ref); diff --git a/compiler/rustc_type_ir/src/inherent.rs b/compiler/rustc_type_ir/src/inherent.rs index be6deee011b..93ca7ab9280 100644 --- a/compiler/rustc_type_ir/src/inherent.rs +++ b/compiler/rustc_type_ir/src/inherent.rs @@ -47,6 +47,14 @@ pub trait Ty>: fn new_alias(interner: I, kind: ty::AliasTyKind, alias_ty: ty::AliasTy) -> Self; + fn new_projection_from_args(interner: I, def_id: I::DefId, args: I::GenericArgs) -> Self { + Ty::new_alias( + interner, + ty::AliasTyKind::Projection, + ty::AliasTy::new_from_args(interner, def_id, args), + ) + } + fn new_projection( interner: I, def_id: I::DefId, diff --git a/compiler/rustc_type_ir/src/interner.rs b/compiler/rustc_type_ir/src/interner.rs index 59ca95c09cd..69693043fa7 100644 --- a/compiler/rustc_type_ir/src/interner.rs +++ b/compiler/rustc_type_ir/src/interner.rs @@ -169,11 +169,7 @@ pub trait Interner: fn check_args_compatible(self, def_id: Self::DefId, args: Self::GenericArgs) -> bool; - fn check_and_mk_args( - self, - def_id: Self::DefId, - args: impl IntoIterator>, - ) -> Self::GenericArgs; + fn debug_assert_args_compatible(self, def_id: Self::DefId, args: Self::GenericArgs); fn intern_canonical_goal_evaluation_step( self, diff --git a/compiler/rustc_type_ir/src/predicate.rs b/compiler/rustc_type_ir/src/predicate.rs index 7a3376c7218..392b758eea1 100644 --- a/compiler/rustc_type_ir/src/predicate.rs +++ b/compiler/rustc_type_ir/src/predicate.rs @@ -64,18 +64,23 @@ pub struct TraitRef { pub def_id: I::DefId, pub args: I::GenericArgs, /// This field exists to prevent the creation of `TraitRef` without - /// calling [`TraitRef::new`]. + /// calling [`TraitRef::new_from_args`]. _use_trait_ref_new_instead: (), } impl TraitRef { + pub fn new_from_args(interner: I, trait_def_id: I::DefId, args: I::GenericArgs) -> Self { + interner.debug_assert_args_compatible(trait_def_id, args); + Self { def_id: trait_def_id, args, _use_trait_ref_new_instead: () } + } + pub fn new( interner: I, trait_def_id: I::DefId, args: impl IntoIterator>, ) -> Self { - let args = interner.check_and_mk_args(trait_def_id, args); - Self { def_id: trait_def_id, args, _use_trait_ref_new_instead: () } + let args = interner.mk_args_from_iter(args.into_iter().map(Into::into)); + Self::new_from_args(interner, trait_def_id, args) } pub fn from_method(interner: I, trait_id: I::DefId, args: I::GenericArgs) -> TraitRef { @@ -86,7 +91,11 @@ impl TraitRef { /// Returns a `TraitRef` of the form `P0: Foo` where `Pi` /// are the parameters defined on trait. pub fn identity(interner: I, def_id: I::DefId) -> TraitRef { - TraitRef::new(interner, def_id, I::GenericArgs::identity_for_item(interner, def_id)) + TraitRef::new_from_args( + interner, + def_id, + I::GenericArgs::identity_for_item(interner, def_id), + ) } pub fn with_self_ty(self, interner: I, self_ty: I::Ty) -> Self { @@ -274,7 +283,7 @@ impl ty::Binder> { // If this is an ill-formed auto trait, then synthesize // new error args for the missing generics. let err_args = GenericArgs::extend_with_error(tcx, did, &[self_ty.into()]); - ty::TraitRef::new(tcx, did, err_args) + ty::TraitRef::new_from_args(tcx, did, err_args) }; self.rebind(trait_ref).upcast(tcx) } @@ -485,19 +494,24 @@ pub struct AliasTerm { /// aka. `interner.parent(def_id)`. pub def_id: I::DefId, - /// This field exists to prevent the creation of `AliasTerm` without using [`AliasTerm::new`]. + /// This field exists to prevent the creation of `AliasTerm` without using [`AliasTerm::new_from_args`]. #[derivative(Debug = "ignore")] _use_alias_term_new_instead: (), } 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( interner: I, def_id: I::DefId, args: impl IntoIterator>, ) -> AliasTerm { - let args = interner.check_and_mk_args(def_id, args); - AliasTerm { def_id, args, _use_alias_term_new_instead: () } + let args = interner.mk_args_from_iter(args.into_iter().map(Into::into)); + Self::new_from_args(interner, def_id, args) } pub fn expect_ty(self, interner: I) -> ty::AliasTy { diff --git a/compiler/rustc_type_ir/src/relate.rs b/compiler/rustc_type_ir/src/relate.rs index 429bc3197d4..350d8093b86 100644 --- a/compiler/rustc_type_ir/src/relate.rs +++ b/compiler/rustc_type_ir/src/relate.rs @@ -258,7 +258,7 @@ impl Relate for ty::AliasTy { relate_args_invariantly(relation, a.args, b.args)? } }; - Ok(ty::AliasTy::new(relation.tcx(), a.def_id, args)) + Ok(ty::AliasTy::new_from_args(relation.tcx(), a.def_id, args)) } } } @@ -293,7 +293,7 @@ impl Relate for ty::AliasTerm { relate_args_invariantly(relation, a.args, b.args)? } }; - Ok(ty::AliasTerm::new(relation.tcx(), a.def_id, args)) + Ok(ty::AliasTerm::new_from_args(relation.tcx(), a.def_id, args)) } } } @@ -343,7 +343,7 @@ impl Relate for ty::TraitRef { })) } else { let args = relate_args_invariantly(relation, a.args, b.args)?; - Ok(ty::TraitRef::new(relation.tcx(), a.def_id, args)) + Ok(ty::TraitRef::new_from_args(relation.tcx(), a.def_id, args)) } } } diff --git a/compiler/rustc_type_ir/src/ty_kind.rs b/compiler/rustc_type_ir/src/ty_kind.rs index 915888ab59b..1c29fafc3cc 100644 --- a/compiler/rustc_type_ir/src/ty_kind.rs +++ b/compiler/rustc_type_ir/src/ty_kind.rs @@ -352,7 +352,7 @@ impl fmt::Debug for TyKind { Float(float) => write!(f, "{float:?}"), Adt(d, s) => { write!(f, "{d:?}")?; - let mut s = s.into_iter(); + let mut s = s.iter(); let first = s.next(); match first { Some(first) => write!(f, "<{:?}", first)?, @@ -452,19 +452,24 @@ pub struct AliasTy { /// aka. `interner.parent(def_id)`. pub def_id: I::DefId, - /// This field exists to prevent the creation of `AliasTy` without using [`AliasTy::new`]. + /// This field exists to prevent the creation of `AliasTy` without using [`AliasTy::new_from_args`]. #[derivative(Debug = "ignore")] pub(crate) _use_alias_ty_new_instead: (), } impl AliasTy { + pub fn new_from_args(interner: I, def_id: I::DefId, args: I::GenericArgs) -> AliasTy { + interner.debug_assert_args_compatible(def_id, args); + AliasTy { def_id, args, _use_alias_ty_new_instead: () } + } + pub fn new( interner: I, def_id: I::DefId, args: impl IntoIterator>, ) -> AliasTy { - let args = interner.check_and_mk_args(def_id, args); - AliasTy { def_id, args, _use_alias_ty_new_instead: () } + let args = interner.mk_args_from_iter(args.into_iter().map(Into::into)); + Self::new_from_args(interner, def_id, args) } pub fn kind(self, interner: I) -> AliasTyKind { diff --git a/src/tools/clippy/clippy_lints/src/bool_assert_comparison.rs b/src/tools/clippy/clippy_lints/src/bool_assert_comparison.rs index 58c1a2f2706..db5792188dd 100644 --- a/src/tools/clippy/clippy_lints/src/bool_assert_comparison.rs +++ b/src/tools/clippy/clippy_lints/src/bool_assert_comparison.rs @@ -61,7 +61,7 @@ fn is_impl_not_trait_with_bool_out<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) - ) }) .map_or(false, |assoc_item| { - let proj = Ty::new_projection(cx.tcx, assoc_item.def_id, cx.tcx.mk_args_trait(ty, [])); + let proj = Ty::new_projection_from_args(cx.tcx, assoc_item.def_id, cx.tcx.mk_args_trait(ty, [])); let nty = cx.tcx.normalize_erasing_regions(cx.param_env, proj); nty.is_bool() diff --git a/src/tools/clippy/clippy_lints/src/methods/needless_collect.rs b/src/tools/clippy/clippy_lints/src/methods/needless_collect.rs index f26f164fa54..46b457daf70 100644 --- a/src/tools/clippy/clippy_lints/src/methods/needless_collect.rs +++ b/src/tools/clippy/clippy_lints/src/methods/needless_collect.rs @@ -206,7 +206,7 @@ fn iterates_same_ty<'tcx>(cx: &LateContext<'tcx>, iter_ty: Ty<'tcx>, collect_ty: && let Some(into_iter_item_proj) = make_projection(cx.tcx, into_iter_trait, sym::Item, [collect_ty]) && let Ok(into_iter_item_ty) = cx.tcx.try_normalize_erasing_regions( cx.param_env, - Ty::new_projection(cx.tcx, into_iter_item_proj.def_id, into_iter_item_proj.args), + Ty::new_projection_from_args(cx.tcx, into_iter_item_proj.def_id, into_iter_item_proj.args), ) { iter_item_ty == into_iter_item_ty @@ -235,7 +235,7 @@ fn is_contains_sig(cx: &LateContext<'_>, call_id: HirId, iter_expr: &Expr<'_>) - iter_trait, ) && let args = cx.tcx.mk_args(&[GenericArg::from(typeck.expr_ty_adjusted(iter_expr))]) - && let proj_ty = Ty::new_projection(cx.tcx, iter_item.def_id, args) + && let proj_ty = Ty::new_projection_from_args(cx.tcx, iter_item.def_id, args) && let Ok(item_ty) = cx.tcx.try_normalize_erasing_regions(cx.param_env, proj_ty) { item_ty == EarlyBinder::bind(search_ty).instantiate(cx.tcx, cx.typeck_results().node_args(call_id)) diff --git a/src/tools/clippy/clippy_lints/src/redundant_slicing.rs b/src/tools/clippy/clippy_lints/src/redundant_slicing.rs index 7f87d18e502..82f22ad693d 100644 --- a/src/tools/clippy/clippy_lints/src/redundant_slicing.rs +++ b/src/tools/clippy/clippy_lints/src/redundant_slicing.rs @@ -133,7 +133,7 @@ impl<'tcx> LateLintPass<'tcx> for RedundantSlicing { } else if let Some(target_id) = cx.tcx.lang_items().deref_target() { if let Ok(deref_ty) = cx.tcx.try_normalize_erasing_regions( cx.param_env, - Ty::new_projection(cx.tcx, target_id, cx.tcx.mk_args(&[GenericArg::from(indexed_ty)])), + Ty::new_projection_from_args(cx.tcx, target_id, cx.tcx.mk_args(&[GenericArg::from(indexed_ty)])), ) { if deref_ty == expr_ty { let snip = snippet_with_context(cx, indexed.span, ctxt, "..", &mut app).0; diff --git a/src/tools/clippy/clippy_utils/src/ty.rs b/src/tools/clippy/clippy_utils/src/ty.rs index 7d4332a3d9d..3790a852f7e 100644 --- a/src/tools/clippy/clippy_utils/src/ty.rs +++ b/src/tools/clippy/clippy_utils/src/ty.rs @@ -292,7 +292,7 @@ pub fn implements_trait_with_env_from_iter<'tcx>( let trait_ref = TraitRef::new( tcx, trait_id, - Some(GenericArg::from(ty)).into_iter().chain(args).chain(effect_arg), + [GenericArg::from(ty)].into_iter().chain(args).chain(effect_arg), ); debug_assert_matches!( @@ -1126,7 +1126,7 @@ pub fn make_projection<'tcx>( #[cfg(debug_assertions)] assert_generic_args_match(tcx, assoc_item.def_id, args); - Some(AliasTy::new(tcx, assoc_item.def_id, args)) + Some(AliasTy::new_from_args(tcx, assoc_item.def_id, args)) } helper( tcx, @@ -1165,7 +1165,7 @@ pub fn make_normalized_projection<'tcx>( ); return None; } - match tcx.try_normalize_erasing_regions(param_env, Ty::new_projection(tcx, ty.def_id, ty.args)) { + match tcx.try_normalize_erasing_regions(param_env, Ty::new_projection_from_args(tcx, ty.def_id, ty.args)) { Ok(ty) => Some(ty), Err(e) => { debug_assert!(false, "failed to normalize type `{ty}`: {e:#?}"); @@ -1289,7 +1289,7 @@ pub fn make_normalized_projection_with_regions<'tcx>( .infer_ctxt() .build() .at(&cause, param_env) - .query_normalize(Ty::new_projection(tcx, ty.def_id, ty.args)) + .query_normalize(Ty::new_projection_from_args(tcx, ty.def_id, ty.args)) { Ok(ty) => Some(ty.value), Err(e) => {