From 0ae3da34c358150f53fd201461e52820ea75fc85 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Tue, 13 Dec 2022 11:25:31 +0000 Subject: [PATCH] Remove TraitRef::new --- .../rustc_hir_analysis/src/astconv/mod.rs | 4 ++-- .../rustc_hir_typeck/src/fn_ctxt/checks.rs | 20 ++++++++----------- compiler/rustc_hir_typeck/src/method/mod.rs | 4 ++-- compiler/rustc_hir_typeck/src/method/probe.rs | 2 +- compiler/rustc_middle/src/ty/context.rs | 2 +- compiler/rustc_middle/src/ty/print/mod.rs | 6 ++---- compiler/rustc_middle/src/ty/sty.rs | 18 +++-------------- src/tools/clippy/clippy_lints/src/derive.rs | 6 +++--- 8 files changed, 22 insertions(+), 40 deletions(-) diff --git a/compiler/rustc_hir_analysis/src/astconv/mod.rs b/compiler/rustc_hir_analysis/src/astconv/mod.rs index babf2ef1af4..71f26eb60c9 100644 --- a/compiler/rustc_hir_analysis/src/astconv/mod.rs +++ b/compiler/rustc_hir_analysis/src/astconv/mod.rs @@ -680,7 +680,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { let assoc_bindings = self.create_assoc_bindings_for_generic_args(args); let poly_trait_ref = - ty::Binder::bind_with_vars(ty::TraitRef::new(trait_def_id, substs), bound_vars); + ty::Binder::bind_with_vars(tcx.mk_trait_ref(trait_def_id, substs), bound_vars); debug!(?poly_trait_ref, ?assoc_bindings); bounds.trait_bounds.push((poly_trait_ref, span, constness)); @@ -813,7 +813,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { if let Some(b) = trait_segment.args().bindings.first() { Self::prohibit_assoc_ty_binding(self.tcx(), b.span); } - ty::TraitRef::new(trait_def_id, substs) + self.tcx().mk_trait_ref(trait_def_id, substs) } #[instrument(level = "debug", skip(self, span))] diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs index 6f0e8eb9039..8e520e563ff 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs @@ -2132,19 +2132,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { _ => { // Look for a user-provided impl of a `Fn` trait, and point to it. let new_def_id = self.probe(|_| { - let trait_ref = ty::TraitRef::new( + let trait_ref = self.tcx.mk_trait_ref( call_kind.to_def_id(self.tcx), - self.tcx.mk_substs( - [ - ty::GenericArg::from(callee_ty), - self.next_ty_var(TypeVariableOrigin { - kind: TypeVariableOriginKind::MiscVariable, - span: rustc_span::DUMMY_SP, - }) - .into(), - ] - .into_iter(), - ), + [ + callee_ty, + self.next_ty_var(TypeVariableOrigin { + kind: TypeVariableOriginKind::MiscVariable, + span: rustc_span::DUMMY_SP, + }), + ], ); let obligation = traits::Obligation::new( self.tcx, diff --git a/compiler/rustc_hir_typeck/src/method/mod.rs b/compiler/rustc_hir_typeck/src/method/mod.rs index a7eae392de1..3f3af53d199 100644 --- a/compiler/rustc_hir_typeck/src/method/mod.rs +++ b/compiler/rustc_hir_typeck/src/method/mod.rs @@ -285,7 +285,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { self.var_for_def(span, param) }); - let trait_ref = ty::TraitRef::new(trait_def_id, substs); + let trait_ref = self.tcx.mk_trait_ref(trait_def_id, substs); // Construct an obligation let poly_trait_ref = ty::Binder::dummy(trait_ref); @@ -326,7 +326,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { self.var_for_def(span, param) }); - let trait_ref = ty::TraitRef::new(trait_def_id, substs); + let trait_ref = self.tcx.mk_trait_ref(trait_def_id, substs); // 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 070359e71be..b9e7830bf07 100644 --- a/compiler/rustc_hir_typeck/src/method/probe.rs +++ b/compiler/rustc_hir_typeck/src/method/probe.rs @@ -920,7 +920,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> { ) { debug!("assemble_extension_candidates_for_trait(trait_def_id={:?})", trait_def_id); let trait_substs = self.fresh_item_substs(trait_def_id); - let trait_ref = ty::TraitRef::new(trait_def_id, trait_substs); + let trait_ref = self.tcx.mk_trait_ref(trait_def_id, trait_substs); if self.tcx.is_trait_alias(trait_def_id) { // For trait aliases, assume all supertraits are relevant. diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index c220814d4fd..ae913c6a367 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -2871,7 +2871,7 @@ impl<'tcx> TyCtxt<'tcx> { substs.collect::>(), ); let substs = self.mk_substs(substs); - ty::TraitRef::new(trait_def_id, substs) + ty::TraitRef { def_id: trait_def_id, substs, _use_mk_trait_ref_instead: () } } pub fn mk_alias_ty( diff --git a/compiler/rustc_middle/src/ty/print/mod.rs b/compiler/rustc_middle/src/ty/print/mod.rs index 3fad349bff8..29bad33e4bc 100644 --- a/compiler/rustc_middle/src/ty/print/mod.rs +++ b/compiler/rustc_middle/src/ty/print/mod.rs @@ -169,10 +169,8 @@ pub trait Printer<'tcx>: Sized { self.path_append( |cx: Self| { if trait_qualify_parent { - let trait_ref = ty::TraitRef::new( - parent_def_id, - cx.tcx().intern_substs(parent_substs), - ); + let trait_ref = + cx.tcx().mk_trait_ref(parent_def_id, parent_substs.iter().copied()); cx.path_qualified(trait_ref.self_ty(), Some(trait_ref)) } else { cx.print_def_path(parent_def_id, parent_substs) diff --git a/compiler/rustc_middle/src/ty/sty.rs b/compiler/rustc_middle/src/ty/sty.rs index 28f04c83a9b..e073637402f 100644 --- a/compiler/rustc_middle/src/ty/sty.rs +++ b/compiler/rustc_middle/src/ty/sty.rs @@ -818,14 +818,10 @@ pub struct TraitRef<'tcx> { pub substs: SubstsRef<'tcx>, /// This field exists to prevent the creation of `TraitRef` without /// calling [TyCtxt::mk_trait_ref]. - _use_mk_trait_ref_instead: (), + pub(super) _use_mk_trait_ref_instead: (), } impl<'tcx> TraitRef<'tcx> { - pub fn new(def_id: DefId, substs: SubstsRef<'tcx>) -> TraitRef<'tcx> { - TraitRef { def_id, substs, _use_mk_trait_ref_instead: () } - } - pub fn with_self_ty(self, tcx: TyCtxt<'tcx>, self_ty: Ty<'tcx>) -> Self { tcx.mk_trait_ref( self.def_id, @@ -836,11 +832,7 @@ impl<'tcx> TraitRef<'tcx> { /// Returns a `TraitRef` of the form `P0: Foo` where `Pi` /// are the parameters defined on trait. pub fn identity(tcx: TyCtxt<'tcx>, def_id: DefId) -> Binder<'tcx, TraitRef<'tcx>> { - ty::Binder::dummy(TraitRef { - def_id, - substs: InternalSubsts::identity_for_item(tcx, def_id), - _use_mk_trait_ref_instead: (), - }) + ty::Binder::dummy(tcx.mk_trait_ref(def_id, InternalSubsts::identity_for_item(tcx, def_id))) } #[inline] @@ -854,11 +846,7 @@ impl<'tcx> TraitRef<'tcx> { substs: SubstsRef<'tcx>, ) -> ty::TraitRef<'tcx> { let defs = tcx.generics_of(trait_id); - ty::TraitRef { - def_id: trait_id, - substs: tcx.intern_substs(&substs[..defs.params.len()]), - _use_mk_trait_ref_instead: (), - } + tcx.mk_trait_ref(trait_id, tcx.intern_substs(&substs[..defs.params.len()])) } } diff --git a/src/tools/clippy/clippy_lints/src/derive.rs b/src/tools/clippy/clippy_lints/src/derive.rs index 9e596ca8157..3f0b165f2b6 100644 --- a/src/tools/clippy/clippy_lints/src/derive.rs +++ b/src/tools/clippy/clippy_lints/src/derive.rs @@ -15,7 +15,7 @@ use rustc_middle::hir::nested_filter; use rustc_middle::traits::Reveal; use rustc_middle::ty::{ self, Binder, BoundConstness, Clause, GenericParamDefKind, ImplPolarity, ParamEnv, PredicateKind, TraitPredicate, - TraitRef, Ty, TyCtxt, + Ty, TyCtxt, }; use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_span::source_map::Span; @@ -513,9 +513,9 @@ fn param_env_for_derived_eq(tcx: TyCtxt<'_>, did: DefId, eq_trait_id: DefId) -> tcx.mk_predicates(ty_predicates.iter().map(|&(p, _)| p).chain( params.iter().filter(|&&(_, needs_eq)| needs_eq).map(|&(param, _)| { tcx.mk_predicate(Binder::dummy(PredicateKind::Clause(Clause::Trait(TraitPredicate { - trait_ref: TraitRef::new( + trait_ref: tcx.mk_trait_ref( eq_trait_id, - tcx.mk_substs(std::iter::once(tcx.mk_param_from_def(param))), + [tcx.mk_param_from_def(param)], ), constness: BoundConstness::NotConst, polarity: ImplPolarity::Positive,