Skip to content

Commit 28a53cd

Browse files
committed
Auto merge of #104533 - oli-obk:method_callee, r=lcnr
Clean up and harden various methods around trait substs r? `@lcnr`
2 parents b7bc90f + c2ecd8f commit 28a53cd

File tree

61 files changed

+309
-350
lines changed

Some content is hidden

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

61 files changed

+309
-350
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -489,12 +489,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
489489
// but the type has region variables, so erase those.
490490
tcx.infer_ctxt()
491491
.build()
492-
.type_implements_trait(
493-
default_trait,
494-
tcx.erase_regions(ty),
495-
ty::List::empty(),
496-
param_env,
497-
)
492+
.type_implements_trait(default_trait, [tcx.erase_regions(ty)], param_env)
498493
.must_apply_modulo_regions()
499494
};
500495

@@ -1707,7 +1702,6 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
17071702
err.span_label(borrow_span, note);
17081703

17091704
let tcx = self.infcx.tcx;
1710-
let ty_params = ty::List::empty();
17111705

17121706
let return_ty = self.regioncx.universal_regions().unnormalized_output_ty;
17131707
let return_ty = tcx.erase_regions(return_ty);
@@ -1716,7 +1710,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
17161710
if let Some(iter_trait) = tcx.get_diagnostic_item(sym::Iterator)
17171711
&& self
17181712
.infcx
1719-
.type_implements_trait(iter_trait, return_ty, ty_params, self.param_env)
1713+
.type_implements_trait(iter_trait, [return_ty], self.param_env)
17201714
.must_apply_modulo_regions()
17211715
{
17221716
err.span_suggestion_hidden(

compiler/rustc_borrowck/src/type_check/mod.rs

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -547,10 +547,7 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {
547547

548548
if let PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy) = context {
549549
let tcx = self.tcx();
550-
let trait_ref = ty::TraitRef {
551-
def_id: tcx.require_lang_item(LangItem::Copy, Some(self.last_span)),
552-
substs: tcx.mk_substs_trait(place_ty.ty, &[]),
553-
};
550+
let trait_ref = tcx.at(self.last_span).mk_trait_ref(LangItem::Copy, [place_ty.ty]);
554551

555552
// To have a `Copy` operand, the type `T` of the
556553
// value must be `Copy`. Note that we prove that `T: Copy`,
@@ -1273,10 +1270,8 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
12731270

12741271
self.check_rvalue(body, rv, location);
12751272
if !self.unsized_feature_enabled() {
1276-
let trait_ref = ty::TraitRef {
1277-
def_id: tcx.require_lang_item(LangItem::Sized, Some(self.last_span)),
1278-
substs: tcx.mk_substs_trait(place_ty, &[]),
1279-
};
1273+
let trait_ref =
1274+
tcx.at(self.last_span).mk_trait_ref(LangItem::Sized, [place_ty]);
12801275
self.prove_trait_ref(
12811276
trait_ref,
12821277
location.to_locations(),
@@ -1840,6 +1835,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
18401835
#[instrument(skip(self, body), level = "debug")]
18411836
fn check_rvalue(&mut self, body: &Body<'tcx>, rvalue: &Rvalue<'tcx>, location: Location) {
18421837
let tcx = self.tcx();
1838+
let span = body.source_info(location).span;
18431839

18441840
match rvalue {
18451841
Rvalue::Aggregate(ak, ops) => {
@@ -1863,12 +1859,8 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
18631859
}
18641860
Operand::Move(place) => {
18651861
// Make sure that repeated elements implement `Copy`.
1866-
let span = body.source_info(location).span;
18671862
let ty = place.ty(body, tcx).ty;
1868-
let trait_ref = ty::TraitRef::new(
1869-
tcx.require_lang_item(LangItem::Copy, Some(span)),
1870-
tcx.mk_substs_trait(ty, &[]),
1871-
);
1863+
let trait_ref = tcx.at(span).mk_trait_ref(LangItem::Copy, [ty]);
18721864

18731865
self.prove_trait_ref(
18741866
trait_ref,
@@ -1881,10 +1873,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
18811873
}
18821874

18831875
&Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf, ty) => {
1884-
let trait_ref = ty::TraitRef {
1885-
def_id: tcx.require_lang_item(LangItem::Sized, Some(self.last_span)),
1886-
substs: tcx.mk_substs_trait(ty, &[]),
1887-
};
1876+
let trait_ref = tcx.at(span).mk_trait_ref(LangItem::Sized, [ty]);
18881877

18891878
self.prove_trait_ref(
18901879
trait_ref,
@@ -1896,10 +1885,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
18961885
Rvalue::ShallowInitBox(operand, ty) => {
18971886
self.check_operand(operand, location);
18981887

1899-
let trait_ref = ty::TraitRef {
1900-
def_id: tcx.require_lang_item(LangItem::Sized, Some(self.last_span)),
1901-
substs: tcx.mk_substs_trait(*ty, &[]),
1902-
};
1888+
let trait_ref = tcx.at(span).mk_trait_ref(LangItem::Sized, [*ty]);
19031889

19041890
self.prove_trait_ref(
19051891
trait_ref,
@@ -1996,11 +1982,9 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
19961982

19971983
CastKind::Pointer(PointerCast::Unsize) => {
19981984
let &ty = ty;
1999-
let trait_ref = ty::TraitRef {
2000-
def_id: tcx
2001-
.require_lang_item(LangItem::CoerceUnsized, Some(self.last_span)),
2002-
substs: tcx.mk_substs_trait(op.ty(body, tcx), &[ty.into()]),
2003-
};
1985+
let trait_ref = tcx
1986+
.at(span)
1987+
.mk_trait_ref(LangItem::CoerceUnsized, [op.ty(body, tcx), ty]);
20041988

20051989
self.prove_trait_ref(
20061990
trait_ref,

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -153,17 +153,12 @@ impl Qualif for NeedsNonConstDrop {
153153
return false;
154154
}
155155

156-
let destruct = cx.tcx.require_lang_item(LangItem::Destruct, None);
157-
158156
let obligation = Obligation::new(
159157
cx.tcx,
160-
ObligationCause::dummy(),
158+
ObligationCause::dummy_with_span(cx.body.span),
161159
cx.param_env,
162160
ty::Binder::dummy(ty::TraitPredicate {
163-
trait_ref: ty::TraitRef {
164-
def_id: destruct,
165-
substs: cx.tcx.mk_substs_trait(ty, &[]),
166-
},
161+
trait_ref: cx.tcx.at(cx.body.span).mk_trait_ref(LangItem::Destruct, [ty]),
167162
constness: ty::BoundConstness::ConstIfConst,
168163
polarity: ty::ImplPolarity::Positive,
169164
}),

compiler/rustc_hir_analysis/src/bounds.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,10 @@ impl<'tcx> Bounds<'tcx> {
6060
{
6161
// If it could be sized, and is, add the `Sized` predicate.
6262
let sized_predicate = self.implicitly_sized.and_then(|span| {
63-
tcx.lang_items().sized_trait().map(move |sized| {
64-
let trait_ref = ty::Binder::dummy(ty::TraitRef {
65-
def_id: sized,
66-
substs: tcx.mk_substs_trait(param_ty, &[]),
67-
});
68-
(trait_ref.without_const().to_predicate(tcx), span)
69-
})
63+
// FIXME: use tcx.at(span).mk_trait_ref(LangItem::Sized) here? This may make no-core code harder to write.
64+
let sized = tcx.lang_items().sized_trait()?;
65+
let trait_ref = ty::Binder::dummy(tcx.mk_trait_ref(sized, [param_ty]));
66+
Some((trait_ref.without_const().to_predicate(tcx), span))
7067
});
7168

7269
let region_preds = self.region_bounds.iter().map(move |&(region_bound, span)| {

compiler/rustc_hir_analysis/src/check/wfcheck.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1722,7 +1722,7 @@ fn receiver_is_valid<'tcx>(
17221722
// The first type is `receiver_ty`, which we know its not equal to `self_ty`; skip it.
17231723
autoderef.next();
17241724

1725-
let receiver_trait_def_id = tcx.require_lang_item(LangItem::Receiver, None);
1725+
let receiver_trait_def_id = tcx.require_lang_item(LangItem::Receiver, Some(span));
17261726

17271727
// Keep dereferencing `receiver_ty` until we get to `self_ty`.
17281728
loop {
@@ -1782,10 +1782,7 @@ fn receiver_is_implemented<'tcx>(
17821782
receiver_ty: Ty<'tcx>,
17831783
) -> bool {
17841784
let tcx = wfcx.tcx();
1785-
let trait_ref = ty::Binder::dummy(ty::TraitRef {
1786-
def_id: receiver_trait_def_id,
1787-
substs: tcx.mk_substs_trait(receiver_ty, &[]),
1788-
});
1785+
let trait_ref = ty::Binder::dummy(tcx.mk_trait_ref(receiver_trait_def_id, [receiver_ty]));
17891786

17901787
let obligation = traits::Obligation::new(tcx, cause, wfcx.param_env, trait_ref.without_const());
17911788

compiler/rustc_hir_analysis/src/coherence/builtin.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -315,8 +315,7 @@ fn visit_implementation_of_dispatch_from_dyn<'tcx>(tcx: TyCtxt<'tcx>, impl_did:
315315
cause.clone(),
316316
dispatch_from_dyn_trait,
317317
0,
318-
field.ty(tcx, substs_a),
319-
&[field.ty(tcx, substs_b).into()],
318+
[field.ty(tcx, substs_a), field.ty(tcx, substs_b)],
320319
)
321320
}),
322321
);
@@ -558,7 +557,7 @@ pub fn coerce_unsized_info<'tcx>(tcx: TyCtxt<'tcx>, impl_did: DefId) -> CoerceUn
558557
// Register an obligation for `A: Trait<B>`.
559558
let cause = traits::ObligationCause::misc(span, impl_hir_id);
560559
let predicate =
561-
predicate_for_trait_def(tcx, param_env, cause, trait_def_id, 0, source, &[target.into()]);
560+
predicate_for_trait_def(tcx, param_env, cause, trait_def_id, 0, [source, target]);
562561
let errors = traits::fully_solve_obligation(&infcx, predicate);
563562
if !errors.is_empty() {
564563
infcx.err_ctxt().report_fulfillment_errors(&errors, None);

compiler/rustc_hir_typeck/src/_match.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -539,17 +539,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
539539
.subst_iter_copied(self.tcx, substs)
540540
{
541541
let pred = pred.kind().rebind(match pred.kind().skip_binder() {
542-
ty::PredicateKind::Trait(mut trait_pred) => {
542+
ty::PredicateKind::Trait(trait_pred) => {
543543
assert_eq!(trait_pred.trait_ref.self_ty(), opaque_ty);
544-
trait_pred.trait_ref.substs =
545-
self.tcx.mk_substs_trait(ty, &trait_pred.trait_ref.substs[1..]);
546-
ty::PredicateKind::Trait(trait_pred)
544+
ty::PredicateKind::Trait(trait_pred.with_self_type(self.tcx, ty))
547545
}
548546
ty::PredicateKind::Projection(mut proj_pred) => {
549547
assert_eq!(proj_pred.projection_ty.self_ty(), opaque_ty);
550-
proj_pred.projection_ty.substs = self
551-
.tcx
552-
.mk_substs_trait(ty, &proj_pred.projection_ty.substs[1..]);
548+
proj_pred.projection_ty.substs = self.tcx.mk_substs_trait(
549+
ty,
550+
proj_pred.projection_ty.substs.iter().skip(1),
551+
);
553552
ty::PredicateKind::Projection(proj_pred)
554553
}
555554
_ => continue,

compiler/rustc_hir_typeck/src/cast.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -498,10 +498,9 @@ impl<'a, 'tcx> CastCheck<'tcx> {
498498
let ty = fcx.tcx.erase_regions(ty);
499499
let expr_ty = fcx.resolve_vars_if_possible(self.expr_ty);
500500
let expr_ty = fcx.tcx.erase_regions(expr_ty);
501-
let ty_params = fcx.tcx.mk_substs_trait(expr_ty, &[]);
502501
if fcx
503502
.infcx
504-
.type_implements_trait(from_trait, ty, ty_params, fcx.param_env)
503+
.type_implements_trait(from_trait, [ty, expr_ty], fcx.param_env)
505504
.must_apply_modulo_regions()
506505
{
507506
label = false;

compiler/rustc_hir_typeck/src/coercion.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -630,8 +630,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
630630
cause,
631631
coerce_unsized_did,
632632
0,
633-
coerce_source,
634-
&[coerce_target.into()]
633+
[coerce_source, coerce_target]
635634
)];
636635

637636
let mut has_unsized_tuple_coercion = false;
@@ -805,10 +804,9 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
805804
self.tcx,
806805
self.cause.clone(),
807806
self.param_env,
808-
ty::Binder::dummy(ty::TraitRef::new(
809-
self.tcx.require_lang_item(hir::LangItem::PointerSized, Some(self.cause.span)),
810-
self.tcx.mk_substs_trait(a, &[]),
811-
))
807+
ty::Binder::dummy(
808+
self.tcx.at(self.cause.span).mk_trait_ref(hir::LangItem::PointerSized, [a]),
809+
)
812810
.to_poly_trait_predicate(),
813811
));
814812
}
@@ -1086,8 +1084,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10861084
self.infcx
10871085
.type_implements_trait(
10881086
self.tcx.lang_items().deref_mut_trait()?,
1089-
expr_ty,
1090-
ty::List::empty(),
1087+
[expr_ty],
10911088
self.param_env,
10921089
)
10931090
.may_apply()

compiler/rustc_hir_typeck/src/expr.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,8 +1119,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11191119
.infcx
11201120
.type_implements_trait(
11211121
self.tcx.lang_items().sized_trait().unwrap(),
1122-
lhs_deref_ty,
1123-
ty::List::empty(),
1122+
[lhs_deref_ty],
11241123
self.param_env,
11251124
)
11261125
.may_apply();

0 commit comments

Comments
 (0)