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

+2-8
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

+10-26
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

+2-7
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

+4-7
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

+2-5
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

+2-3
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

+6-7
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

+1-2
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

+5-8
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

+1-2
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();

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1093,10 +1093,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10931093
self.tcx,
10941094
self.misc(expr.span),
10951095
self.param_env,
1096-
ty::Binder::dummy(ty::TraitRef {
1097-
def_id: into_def_id,
1098-
substs: self.tcx.mk_substs_trait(expr_ty, &[expected_ty.into()]),
1099-
})
1096+
ty::Binder::dummy(self.tcx.mk_trait_ref(
1097+
into_def_id,
1098+
[expr_ty, expected_ty]
1099+
))
11001100
.to_poly_trait_predicate(),
11011101
))
11021102
{

compiler/rustc_hir_typeck/src/method/prelude2021.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use hir::ItemKind;
88
use rustc_ast::Mutability;
99
use rustc_errors::Applicability;
1010
use rustc_hir as hir;
11-
use rustc_middle::ty::subst::InternalSubsts;
11+
use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
1212
use rustc_middle::ty::{Adt, Array, Ref, Ty};
1313
use rustc_session::lint::builtin::RUST_2021_PRELUDE_COLLISIONS;
1414
use rustc_span::symbol::kw::{Empty, Underscore};
@@ -227,14 +227,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
227227
// If we know it does not, we don't need to warn.
228228
if method_name.name == sym::from_iter {
229229
if let Some(trait_def_id) = self.tcx.get_diagnostic_item(sym::FromIterator) {
230+
let any_type = self.infcx.next_ty_var(TypeVariableOrigin {
231+
kind: TypeVariableOriginKind::MiscVariable,
232+
span,
233+
});
230234
if !self
231235
.infcx
232-
.type_implements_trait(
233-
trait_def_id,
234-
self_ty,
235-
InternalSubsts::empty(),
236-
self.param_env,
237-
)
236+
.type_implements_trait(trait_def_id, [self_ty, any_type], self.param_env)
238237
.may_apply()
239238
{
240239
return;

compiler/rustc_hir_typeck/src/method/suggest.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
6868
self.autoderef(span, ty).any(|(ty, _)| {
6969
info!("check deref {:?} impl FnOnce", ty);
7070
self.probe(|_| {
71-
let fn_once_substs = tcx.mk_substs_trait(
72-
ty,
73-
&[self
74-
.next_ty_var(TypeVariableOrigin {
71+
let trait_ref = tcx.mk_trait_ref(
72+
fn_once,
73+
[
74+
ty,
75+
self.next_ty_var(TypeVariableOrigin {
7576
kind: TypeVariableOriginKind::MiscVariable,
7677
span,
77-
})
78-
.into()],
78+
}),
79+
],
7980
);
80-
let trait_ref = ty::TraitRef::new(fn_once, fn_once_substs);
8181
let poly_trait_ref = ty::Binder::dummy(trait_ref);
8282
let obligation = Obligation::misc(
8383
tcx,

compiler/rustc_hir_typeck/src/upvar.rs

+3-19
Original file line numberDiff line numberDiff line change
@@ -970,12 +970,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
970970
check_trait
971971
.map(|check_trait| {
972972
self.infcx
973-
.type_implements_trait(
974-
check_trait,
975-
ty,
976-
self.tcx.mk_substs_trait(ty, &[]),
977-
self.param_env,
978-
)
973+
.type_implements_trait(check_trait, [ty], self.param_env)
979974
.must_apply_modulo_regions()
980975
})
981976
.unwrap_or(false),
@@ -999,12 +994,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
999994
check_trait
1000995
.map(|check_trait| {
1001996
self.infcx
1002-
.type_implements_trait(
1003-
check_trait,
1004-
ty,
1005-
self.tcx.mk_substs_trait(ty, &[]),
1006-
self.param_env,
1007-
)
997+
.type_implements_trait(check_trait, [ty], self.param_env)
1008998
.must_apply_modulo_regions()
1009999
})
10101000
.unwrap_or(false),
@@ -1347,14 +1337,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13471337

13481338
let is_drop_defined_for_ty = |ty: Ty<'tcx>| {
13491339
let drop_trait = self.tcx.require_lang_item(hir::LangItem::Drop, Some(closure_span));
1350-
let ty_params = self.tcx.mk_substs_trait(base_path_ty, &[]);
13511340
self.infcx
1352-
.type_implements_trait(
1353-
drop_trait,
1354-
ty,
1355-
ty_params,
1356-
self.tcx.param_env(closure_def_id),
1357-
)
1341+
.type_implements_trait(drop_trait, [ty], self.tcx.param_env(closure_def_id))
13581342
.must_apply_modulo_regions()
13591343
};
13601344

compiler/rustc_infer/src/traits/engine.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub trait TraitEngine<'tcx>: 'tcx {
2727
def_id: DefId,
2828
cause: ObligationCause<'tcx>,
2929
) {
30-
let trait_ref = ty::TraitRef { def_id, substs: infcx.tcx.mk_substs_trait(ty, &[]) };
30+
let trait_ref = infcx.tcx.mk_trait_ref(def_id, [ty]);
3131
self.register_predicate_obligation(
3232
infcx,
3333
Obligation {

0 commit comments

Comments
 (0)