Skip to content

Commit 6f8c055

Browse files
committed
Auto merge of rust-lang#110806 - WaffleLapkin:unmkI, r=lcnr
Replace `tcx.mk_trait_ref` with `TraitRef::new` First step in implementing rust-lang/compiler-team#616 r? `@lcnr`
2 parents 129195f + 689721d commit 6f8c055

File tree

49 files changed

+245
-180
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+245
-180
lines changed

compiler/rustc_borrowck/src/diagnostics/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1120,7 +1120,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
11201120
});
11211121
}
11221122
if let Some(clone_trait) = tcx.lang_items().clone_trait()
1123-
&& let trait_ref = tcx.mk_trait_ref(clone_trait, [ty])
1123+
&& let trait_ref = ty::TraitRef::new(tcx, clone_trait, [ty])
11241124
&& let o = Obligation::new(
11251125
tcx,
11261126
ObligationCause::dummy(),

compiler/rustc_borrowck/src/type_check/mod.rs

+18-9
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,8 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {
538538

539539
if let PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy) = context {
540540
let tcx = self.tcx();
541-
let trait_ref = tcx.at(self.last_span).mk_trait_ref(LangItem::Copy, [place_ty.ty]);
541+
let trait_ref =
542+
ty::TraitRef::from_lang_item(tcx, LangItem::Copy, self.last_span, [place_ty.ty]);
542543

543544
// To have a `Copy` operand, the type `T` of the
544545
// value must be `Copy`. Note that we prove that `T: Copy`,
@@ -1237,8 +1238,12 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
12371238

12381239
self.check_rvalue(body, rv, location);
12391240
if !self.unsized_feature_enabled() {
1240-
let trait_ref =
1241-
tcx.at(self.last_span).mk_trait_ref(LangItem::Sized, [place_ty]);
1241+
let trait_ref = ty::TraitRef::from_lang_item(
1242+
tcx,
1243+
LangItem::Sized,
1244+
self.last_span,
1245+
[place_ty],
1246+
);
12421247
self.prove_trait_ref(
12431248
trait_ref,
12441249
location.to_locations(),
@@ -1810,7 +1815,8 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
18101815
Operand::Move(place) => {
18111816
// Make sure that repeated elements implement `Copy`.
18121817
let ty = place.ty(body, tcx).ty;
1813-
let trait_ref = tcx.at(span).mk_trait_ref(LangItem::Copy, [ty]);
1818+
let trait_ref =
1819+
ty::TraitRef::from_lang_item(tcx, LangItem::Copy, span, [ty]);
18141820

18151821
self.prove_trait_ref(
18161822
trait_ref,
@@ -1823,7 +1829,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
18231829
}
18241830

18251831
&Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf, ty) => {
1826-
let trait_ref = tcx.at(span).mk_trait_ref(LangItem::Sized, [ty]);
1832+
let trait_ref = ty::TraitRef::from_lang_item(tcx, LangItem::Sized, span, [ty]);
18271833

18281834
self.prove_trait_ref(
18291835
trait_ref,
@@ -1835,7 +1841,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
18351841
Rvalue::ShallowInitBox(operand, ty) => {
18361842
self.check_operand(operand, location);
18371843

1838-
let trait_ref = tcx.at(span).mk_trait_ref(LangItem::Sized, [*ty]);
1844+
let trait_ref = ty::TraitRef::from_lang_item(tcx, LangItem::Sized, span, [*ty]);
18391845

18401846
self.prove_trait_ref(
18411847
trait_ref,
@@ -1932,9 +1938,12 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
19321938

19331939
CastKind::Pointer(PointerCast::Unsize) => {
19341940
let &ty = ty;
1935-
let trait_ref = tcx
1936-
.at(span)
1937-
.mk_trait_ref(LangItem::CoerceUnsized, [op.ty(body, tcx), ty]);
1941+
let trait_ref = ty::TraitRef::from_lang_item(
1942+
tcx,
1943+
LangItem::CoerceUnsized,
1944+
span,
1945+
[op.ty(body, tcx), ty],
1946+
);
19381947

19391948
self.prove_trait_ref(
19401949
trait_ref,

compiler/rustc_const_eval/src/transform/check_consts/qualifs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ impl Qualif for NeedsNonConstDrop {
157157
cx.tcx,
158158
ObligationCause::dummy_with_span(cx.body.span),
159159
cx.param_env,
160-
ty::Binder::dummy(cx.tcx.at(cx.body.span).mk_trait_ref(LangItem::Destruct, [ty]))
160+
ty::TraitRef::from_lang_item(cx.tcx, LangItem::Destruct, cx.body.span, [ty])
161161
.with_constness(ty::BoundConstness::ConstIfConst),
162162
);
163163

compiler/rustc_hir_analysis/src/astconv/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
694694
let assoc_bindings = self.create_assoc_bindings_for_generic_args(args);
695695

696696
let poly_trait_ref =
697-
ty::Binder::bind_with_vars(tcx.mk_trait_ref(trait_def_id, substs), bound_vars);
697+
ty::Binder::bind_with_vars(ty::TraitRef::new(tcx, trait_def_id, substs), bound_vars);
698698

699699
debug!(?poly_trait_ref, ?assoc_bindings);
700700
bounds.push_trait_bound(tcx, poly_trait_ref, span, constness, polarity);
@@ -846,7 +846,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
846846
if let Some(b) = trait_segment.args().bindings.first() {
847847
prohibit_assoc_ty_binding(self.tcx(), b.span, Some((trait_segment, span)));
848848
}
849-
self.tcx().mk_trait_ref(trait_def_id, substs)
849+
ty::TraitRef::new(self.tcx(), trait_def_id, substs)
850850
}
851851

852852
#[instrument(level = "debug", skip(self, span))]

compiler/rustc_hir_analysis/src/autoderef.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ impl<'a, 'tcx> Autoderef<'a, 'tcx> {
123123
let tcx = self.infcx.tcx;
124124

125125
// <ty as Deref>
126-
let trait_ref = tcx.mk_trait_ref(tcx.lang_items().deref_trait()?, [ty]);
126+
let trait_ref = ty::TraitRef::new(tcx, tcx.lang_items().deref_trait()?, [ty]);
127127

128128
let cause = traits::ObligationCause::misc(self.span, self.body_id);
129129

compiler/rustc_hir_analysis/src/bounds.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ impl<'tcx> Bounds<'tcx> {
6363

6464
pub fn push_sized(&mut self, tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, span: Span) {
6565
let sized_def_id = tcx.require_lang_item(LangItem::Sized, Some(span));
66-
let trait_ref = ty::Binder::dummy(tcx.mk_trait_ref(sized_def_id, [ty]));
66+
let trait_ref = ty::TraitRef::new(tcx, sized_def_id, [ty]);
6767
// Preferable to put this obligation first, since we report better errors for sized ambiguity.
6868
self.predicates.insert(0, (trait_ref.without_const().to_predicate(tcx), span));
6969
}

compiler/rustc_hir_analysis/src/check/check.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ fn check_item_type(tcx: TyCtxt<'_>, id: hir::ItemId) {
536536
tcx,
537537
assoc_item,
538538
assoc_item,
539-
tcx.mk_trait_ref(id.owner_id.to_def_id(), trait_substs),
539+
ty::TraitRef::new(tcx, id.owner_id.to_def_id(), trait_substs),
540540
);
541541
}
542542
_ => {}

compiler/rustc_hir_analysis/src/check/wfcheck.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1779,7 +1779,7 @@ fn receiver_is_implemented<'tcx>(
17791779
receiver_ty: Ty<'tcx>,
17801780
) -> bool {
17811781
let tcx = wfcx.tcx();
1782-
let trait_ref = ty::Binder::dummy(tcx.mk_trait_ref(receiver_trait_def_id, [receiver_ty]));
1782+
let trait_ref = ty::TraitRef::new(tcx, receiver_trait_def_id, [receiver_ty]);
17831783

17841784
let obligation = traits::Obligation::new(tcx, cause, wfcx.param_env, trait_ref);
17851785

compiler/rustc_hir_analysis/src/coherence/builtin.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -265,10 +265,11 @@ fn visit_implementation_of_dispatch_from_dyn(tcx: TyCtxt<'_>, impl_did: LocalDef
265265
tcx,
266266
cause.clone(),
267267
param_env,
268-
ty::Binder::dummy(tcx.mk_trait_ref(
268+
ty::TraitRef::new(
269+
tcx,
269270
dispatch_from_dyn_trait,
270271
[field.ty(tcx, substs_a), field.ty(tcx, substs_b)],
271-
)),
272+
),
272273
));
273274
}
274275
let errors = ocx.select_all_or_error();
@@ -504,8 +505,12 @@ pub fn coerce_unsized_info<'tcx>(tcx: TyCtxt<'tcx>, impl_did: LocalDefId) -> Coe
504505
// Register an obligation for `A: Trait<B>`.
505506
let ocx = ObligationCtxt::new(&infcx);
506507
let cause = traits::ObligationCause::misc(span, impl_did);
507-
let obligation =
508-
Obligation::new(tcx, cause, param_env, tcx.mk_trait_ref(trait_def_id, [source, target]));
508+
let obligation = Obligation::new(
509+
tcx,
510+
cause,
511+
param_env,
512+
ty::TraitRef::new(tcx, trait_def_id, [source, target]),
513+
);
509514
ocx.register_obligation(obligation);
510515
let errors = ocx.select_all_or_error();
511516
if !errors.is_empty() {

compiler/rustc_hir_typeck/src/coercion.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
601601
self.tcx,
602602
cause,
603603
self.fcx.param_env,
604-
self.tcx.mk_trait_ref(coerce_unsized_did, [coerce_source, coerce_target])
604+
ty::TraitRef::new(self.tcx, coerce_unsized_did, [coerce_source, coerce_target])
605605
)];
606606

607607
let mut has_unsized_tuple_coercion = false;
@@ -764,8 +764,11 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
764764
self.tcx,
765765
self.cause.clone(),
766766
self.param_env,
767-
ty::Binder::dummy(
768-
self.tcx.at(self.cause.span).mk_trait_ref(hir::LangItem::PointerLike, [a]),
767+
ty::TraitRef::from_lang_item(
768+
self.tcx,
769+
hir::LangItem::PointerLike,
770+
self.cause.span,
771+
[a],
769772
),
770773
));
771774

compiler/rustc_hir_typeck/src/fn_ctxt/adjust_fulfillment_errors.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
481481
// For the purposes of this function, we hope that it is a `struct` type, and that our current `expr` is a literal of
482482
// that struct type.
483483
let impl_trait_self_ref = if self.tcx.is_trait_alias(obligation.impl_or_alias_def_id) {
484-
self.tcx.mk_trait_ref(
484+
ty::TraitRef::new(
485+
self.tcx,
485486
obligation.impl_or_alias_def_id,
486487
ty::InternalSubsts::identity_for_item(self.tcx, obligation.impl_or_alias_def_id),
487488
)

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1895,7 +1895,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
18951895
_ => {
18961896
// Look for a user-provided impl of a `Fn` trait, and point to it.
18971897
let new_def_id = self.probe(|_| {
1898-
let trait_ref = self.tcx.mk_trait_ref(
1898+
let trait_ref = ty::TraitRef::new(self.tcx,
18991899
call_kind.to_def_id(self.tcx),
19001900
[
19011901
callee_ty,

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1096,10 +1096,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10961096
self.tcx,
10971097
self.misc(expr.span),
10981098
self.param_env,
1099-
ty::Binder::dummy(self.tcx.mk_trait_ref(
1099+
ty::TraitRef::new(self.tcx,
11001100
into_def_id,
11011101
[expr_ty, expected_ty]
1102-
)),
1102+
),
11031103
))
11041104
{
11051105
let sugg = if expr.precedence().order() >= PREC_POSTFIX {
@@ -1438,7 +1438,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
14381438
&& !results.expr_adjustments(callee_expr).iter().any(|adj| matches!(adj.kind, ty::adjustment::Adjust::Deref(..)))
14391439
// Check that we're in fact trying to clone into the expected type
14401440
&& self.can_coerce(*pointee_ty, expected_ty)
1441-
&& let trait_ref = ty::Binder::dummy(self.tcx.mk_trait_ref(clone_trait_did, [expected_ty]))
1441+
&& let trait_ref = ty::TraitRef::new(self.tcx, clone_trait_did, [expected_ty])
14421442
// And the expected type doesn't implement `Clone`
14431443
&& !self.predicate_must_hold_considering_regions(&traits::Obligation::new(
14441444
self.tcx,

compiler/rustc_hir_typeck/src/method/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
316316
self.var_for_def(cause.span, param)
317317
});
318318

319-
let trait_ref = self.tcx.mk_trait_ref(trait_def_id, substs);
319+
let trait_ref = ty::TraitRef::new(self.tcx, trait_def_id, substs);
320320

321321
// Construct an obligation
322322
let poly_trait_ref = ty::Binder::dummy(trait_ref);

compiler/rustc_hir_typeck/src/method/probe.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -954,7 +954,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
954954
) {
955955
debug!("assemble_extension_candidates_for_trait(trait_def_id={:?})", trait_def_id);
956956
let trait_substs = self.fresh_item_substs(trait_def_id);
957-
let trait_ref = self.tcx.mk_trait_ref(trait_def_id, trait_substs);
957+
let trait_ref = ty::TraitRef::new(self.tcx, trait_def_id, trait_substs);
958958

959959
if self.tcx.is_trait_alias(trait_def_id) {
960960
// For trait aliases, recursively assume all explicitly named traits are relevant

compiler/rustc_hir_typeck/src/method/suggest.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
7272
self.autoderef(span, ty).any(|(ty, _)| {
7373
info!("check deref {:?} impl FnOnce", ty);
7474
self.probe(|_| {
75-
let trait_ref = tcx.mk_trait_ref(
75+
let trait_ref = ty::TraitRef::new(
76+
tcx,
7677
fn_once,
7778
[
7879
ty,

compiler/rustc_infer/src/infer/error_reporting/nice_region_error/placeholder_error.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -261,11 +261,16 @@ impl<'tcx> NiceRegionError<'_, 'tcx> {
261261
(false, None, None, Some(span), String::new())
262262
};
263263

264-
let expected_trait_ref = self
265-
.cx
266-
.resolve_vars_if_possible(self.cx.tcx.mk_trait_ref(trait_def_id, expected_substs));
267-
let actual_trait_ref =
268-
self.cx.resolve_vars_if_possible(self.cx.tcx.mk_trait_ref(trait_def_id, actual_substs));
264+
let expected_trait_ref = self.cx.resolve_vars_if_possible(ty::TraitRef::new(
265+
self.cx.tcx,
266+
trait_def_id,
267+
expected_substs,
268+
));
269+
let actual_trait_ref = self.cx.resolve_vars_if_possible(ty::TraitRef::new(
270+
self.cx.tcx,
271+
trait_def_id,
272+
actual_substs,
273+
));
269274

270275
// Search the expected and actual trait references to see (a)
271276
// whether the sub/sup placeholders appear in them (sometimes

compiler/rustc_infer/src/traits/engine.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub trait TraitEngine<'tcx>: 'tcx {
1818
def_id: DefId,
1919
cause: ObligationCause<'tcx>,
2020
) {
21-
let trait_ref = infcx.tcx.mk_trait_ref(def_id, [ty]);
21+
let trait_ref = ty::TraitRef::new(infcx.tcx, def_id, [ty]);
2222
self.register_predicate_obligation(
2323
infcx,
2424
Obligation {

compiler/rustc_middle/src/ty/context.rs

+1-19
Original file line numberDiff line numberDiff line change
@@ -1838,7 +1838,7 @@ impl<'tcx> TyCtxt<'tcx> {
18381838
}
18391839

18401840
#[inline(always)]
1841-
fn check_and_mk_substs(
1841+
pub(crate) fn check_and_mk_substs(
18421842
self,
18431843
_def_id: DefId,
18441844
substs: impl IntoIterator<Item: Into<GenericArg<'tcx>>>,
@@ -2238,15 +2238,6 @@ impl<'tcx> TyCtxt<'tcx> {
22382238
self.mk_substs_from_iter(iter::once(self_ty.into()).chain(rest))
22392239
}
22402240

2241-
pub fn mk_trait_ref(
2242-
self,
2243-
trait_def_id: DefId,
2244-
substs: impl IntoIterator<Item: Into<GenericArg<'tcx>>>,
2245-
) -> ty::TraitRef<'tcx> {
2246-
let substs = self.check_and_mk_substs(trait_def_id, substs);
2247-
ty::TraitRef { def_id: trait_def_id, substs, _use_mk_trait_ref_instead: () }
2248-
}
2249-
22502241
pub fn mk_alias_ty(
22512242
self,
22522243
def_id: DefId,
@@ -2441,15 +2432,6 @@ impl<'tcx> TyCtxtAt<'tcx> {
24412432
pub fn ty_error_with_message(self, msg: &str) -> Ty<'tcx> {
24422433
self.tcx.ty_error_with_message(self.span, msg)
24432434
}
2444-
2445-
pub fn mk_trait_ref(
2446-
self,
2447-
trait_lang_item: LangItem,
2448-
substs: impl IntoIterator<Item: Into<ty::GenericArg<'tcx>>>,
2449-
) -> ty::TraitRef<'tcx> {
2450-
let trait_def_id = self.require_lang_item(trait_lang_item, Some(self.span));
2451-
self.tcx.mk_trait_ref(trait_def_id, substs)
2452-
}
24532435
}
24542436

24552437
/// Parameter attributes that can only be determined by examining the body of a function instead

compiler/rustc_middle/src/ty/mod.rs

+18
Original file line numberDiff line numberDiff line change
@@ -1210,6 +1210,18 @@ impl<'tcx> ToPredicate<'tcx, PolyTraitPredicate<'tcx>> for Binder<'tcx, TraitRef
12101210
}
12111211
}
12121212

1213+
impl<'tcx> ToPredicate<'tcx, PolyTraitPredicate<'tcx>> for TraitRef<'tcx> {
1214+
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> PolyTraitPredicate<'tcx> {
1215+
ty::Binder::dummy(self).to_predicate(tcx)
1216+
}
1217+
}
1218+
1219+
impl<'tcx> ToPredicate<'tcx, PolyTraitPredicate<'tcx>> for TraitPredicate<'tcx> {
1220+
fn to_predicate(self, _tcx: TyCtxt<'tcx>) -> PolyTraitPredicate<'tcx> {
1221+
ty::Binder::dummy(self)
1222+
}
1223+
}
1224+
12131225
impl<'tcx> ToPredicate<'tcx> for PolyTraitPredicate<'tcx> {
12141226
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
12151227
self.map_bound(|p| PredicateKind::Clause(Clause::Trait(p))).to_predicate(tcx)
@@ -1234,6 +1246,12 @@ impl<'tcx> ToPredicate<'tcx> for PolyProjectionPredicate<'tcx> {
12341246
}
12351247
}
12361248

1249+
impl<'tcx> ToPredicate<'tcx> for TraitPredicate<'tcx> {
1250+
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
1251+
PredicateKind::Clause(Clause::Trait(self)).to_predicate(tcx)
1252+
}
1253+
}
1254+
12371255
impl<'tcx> Predicate<'tcx> {
12381256
pub fn to_opt_poly_trait_pred(self) -> Option<PolyTraitPredicate<'tcx>> {
12391257
let predicate = self.kind();

compiler/rustc_middle/src/ty/print/mod.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,11 @@ pub trait Printer<'tcx>: Sized {
169169
self.path_append(
170170
|cx: Self| {
171171
if trait_qualify_parent {
172-
let trait_ref =
173-
cx.tcx().mk_trait_ref(parent_def_id, parent_substs.iter().copied());
172+
let trait_ref = ty::TraitRef::new(
173+
cx.tcx(),
174+
parent_def_id,
175+
parent_substs.iter().copied(),
176+
);
174177
cx.path_qualified(trait_ref.self_ty(), Some(trait_ref))
175178
} else {
176179
cx.print_def_path(parent_def_id, parent_substs)

compiler/rustc_middle/src/ty/relate.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ impl<'tcx> Relate<'tcx> for ty::TraitRef<'tcx> {
315315
Err(TypeError::Traits(expected_found(relation, a.def_id, b.def_id)))
316316
} else {
317317
let substs = relate_substs(relation, a.substs, b.substs)?;
318-
Ok(relation.tcx().mk_trait_ref(a.def_id, substs))
318+
Ok(ty::TraitRef::new(relation.tcx(), a.def_id, substs))
319319
}
320320
}
321321
}

0 commit comments

Comments
 (0)