Skip to content

Commit 6f77c97

Browse files
committed
Assert that various types have the right amount of generic args and fix the sites that used the wrong amount
1 parent d9a02b0 commit 6f77c97

File tree

27 files changed

+153
-144
lines changed

27 files changed

+153
-144
lines changed

compiler/rustc_borrowck/src/type_check/mod.rs

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -547,10 +547,11 @@ 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.mk_trait_ref(
551+
tcx.require_lang_item(LangItem::Copy, Some(self.last_span)),
552+
place_ty.ty,
553+
&[],
554+
);
554555

555556
// To have a `Copy` operand, the type `T` of the
556557
// value must be `Copy`. Note that we prove that `T: Copy`,
@@ -1273,10 +1274,11 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
12731274

12741275
self.check_rvalue(body, rv, location);
12751276
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-
};
1277+
let trait_ref = tcx.mk_trait_ref(
1278+
tcx.require_lang_item(LangItem::Sized, Some(self.last_span)),
1279+
place_ty,
1280+
&[],
1281+
);
12801282
self.prove_trait_ref(
12811283
trait_ref,
12821284
location.to_locations(),
@@ -1865,9 +1867,10 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
18651867
// Make sure that repeated elements implement `Copy`.
18661868
let span = body.source_info(location).span;
18671869
let ty = place.ty(body, tcx).ty;
1868-
let trait_ref = ty::TraitRef::new(
1870+
let trait_ref = tcx.mk_trait_ref(
18691871
tcx.require_lang_item(LangItem::Copy, Some(span)),
1870-
tcx.mk_substs_trait(ty, &[]),
1872+
ty,
1873+
&[],
18711874
);
18721875

18731876
self.prove_trait_ref(
@@ -1881,10 +1884,11 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
18811884
}
18821885

18831886
&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-
};
1887+
let trait_ref = tcx.mk_trait_ref(
1888+
tcx.require_lang_item(LangItem::Sized, Some(self.last_span)),
1889+
ty,
1890+
&[],
1891+
);
18881892

18891893
self.prove_trait_ref(
18901894
trait_ref,
@@ -1896,10 +1900,11 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
18961900
Rvalue::ShallowInitBox(operand, ty) => {
18971901
self.check_operand(operand, location);
18981902

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-
};
1903+
let trait_ref = tcx.mk_trait_ref(
1904+
tcx.require_lang_item(LangItem::Sized, Some(self.last_span)),
1905+
*ty,
1906+
&[],
1907+
);
19031908

19041909
self.prove_trait_ref(
19051910
trait_ref,
@@ -1996,11 +2001,11 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
19962001

19972002
CastKind::Pointer(PointerCast::Unsize) => {
19982003
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-
};
2004+
let trait_ref = tcx.mk_trait_ref(
2005+
tcx.require_lang_item(LangItem::CoerceUnsized, Some(self.last_span)),
2006+
op.ty(body, tcx),
2007+
&[ty.into()],
2008+
);
20042009

20052010
self.prove_trait_ref(
20062011
trait_ref,

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,7 @@ impl Qualif for NeedsNonConstDrop {
160160
ObligationCause::dummy(),
161161
cx.param_env,
162162
ty::Binder::dummy(ty::TraitPredicate {
163-
trait_ref: ty::TraitRef {
164-
def_id: destruct,
165-
substs: cx.tcx.mk_substs_trait(ty, &[]),
166-
},
163+
trait_ref: cx.tcx.mk_trait_ref(destruct, ty, &[]),
167164
constness: ty::BoundConstness::ConstIfConst,
168165
polarity: ty::ImplPolarity::Positive,
169166
}),

compiler/rustc_hir_analysis/src/bounds.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,7 @@ impl<'tcx> Bounds<'tcx> {
6161
// If it could be sized, and is, add the `Sized` predicate.
6262
let sized_predicate = self.implicitly_sized.and_then(|span| {
6363
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-
});
64+
let trait_ref = ty::Binder::dummy(tcx.mk_trait_ref(sized, param_ty, &[]));
6865
(trait_ref.without_const().to_predicate(tcx), span)
6966
})
7067
});

compiler/rustc_hir_analysis/src/check/wfcheck.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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_typeck/src/fn_ctxt/suggestions.rs

Lines changed: 4 additions & 4 deletions
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.into()]
1099+
))
11001100
.to_poly_trait_predicate(),
11011101
))
11021102
{

compiler/rustc_hir_typeck/src/method/suggest.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ 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(
71+
let trait_ref = tcx.mk_trait_ref(
72+
fn_once,
7273
ty,
7374
&[self
7475
.next_ty_var(TypeVariableOrigin {
@@ -77,7 +78,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
7778
})
7879
.into()],
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_infer/src/traits/engine.rs

Lines changed: 1 addition & 1 deletion
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 {

compiler/rustc_middle/src/ty/context.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2532,6 +2532,11 @@ impl<'tcx> TyCtxt<'tcx> {
25322532

25332533
#[inline]
25342534
pub fn mk_fn_def(self, def_id: DefId, substs: SubstsRef<'tcx>) -> Ty<'tcx> {
2535+
debug_assert_eq!(
2536+
self.generics_of(def_id).count(),
2537+
substs.len(),
2538+
"wrong number of generic parameters for {def_id:?}: {substs:?}",
2539+
);
25352540
self.mk_ty(FnDef(def_id, substs))
25362541
}
25372542

@@ -2552,6 +2557,11 @@ impl<'tcx> TyCtxt<'tcx> {
25522557

25532558
#[inline]
25542559
pub fn mk_projection(self, item_def_id: DefId, substs: SubstsRef<'tcx>) -> Ty<'tcx> {
2560+
debug_assert_eq!(
2561+
self.generics_of(item_def_id).count(),
2562+
substs.len(),
2563+
"wrong number of generic parameters for {item_def_id:?}: {substs:?}",
2564+
);
25552565
self.mk_ty(Projection(ProjectionTy { item_def_id, substs }))
25562566
}
25572567

@@ -2803,6 +2813,21 @@ impl<'tcx> TyCtxt<'tcx> {
28032813
self.mk_substs(iter::once(self_ty.into()).chain(rest.iter().cloned()))
28042814
}
28052815

2816+
pub fn mk_trait_ref(
2817+
self,
2818+
trait_def_id: DefId,
2819+
self_ty: Ty<'tcx>,
2820+
rest: &[GenericArg<'tcx>],
2821+
) -> ty::TraitRef<'tcx> {
2822+
debug_assert_eq!(
2823+
self.generics_of(trait_def_id).count() - 1,
2824+
rest.len(),
2825+
"wrong number of generic parameters for {trait_def_id:?} on self type {self_ty:?}: {rest:?} \nDid you accidentally include the self-type in the params list?"
2826+
);
2827+
let substs = self.mk_substs_trait(self_ty, rest);
2828+
ty::TraitRef::new(trait_def_id, substs)
2829+
}
2830+
28062831
pub fn mk_bound_variable_kinds<
28072832
I: InternAs<[ty::BoundVariableKind], &'tcx List<ty::BoundVariableKind>>,
28082833
>(

compiler/rustc_middle/src/ty/sty.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -719,10 +719,7 @@ impl<'tcx> PolyExistentialPredicate<'tcx> {
719719
self.rebind(p.with_self_ty(tcx, self_ty)).to_predicate(tcx)
720720
}
721721
ExistentialPredicate::AutoTrait(did) => {
722-
let trait_ref = self.rebind(ty::TraitRef {
723-
def_id: did,
724-
substs: tcx.mk_substs_trait(self_ty, &[]),
725-
});
722+
let trait_ref = self.rebind(tcx.mk_trait_ref(did, self_ty, &[]));
726723
trait_ref.without_const().to_predicate(tcx)
727724
}
728725
}
@@ -909,7 +906,7 @@ impl<'tcx> ExistentialTraitRef<'tcx> {
909906
// otherwise the escaping vars would be captured by the binder
910907
// debug_assert!(!self_ty.has_escaping_bound_vars());
911908

912-
ty::TraitRef { def_id: self.def_id, substs: tcx.mk_substs_trait(self_ty, self.substs) }
909+
tcx.mk_trait_ref(self.def_id, self_ty, self.substs)
913910
}
914911
}
915912

compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use rustc_errors::DelayDm;
22
use rustc_hir as hir;
33
use rustc_index::vec::Idx;
4+
use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
45
use rustc_infer::infer::{InferCtxt, TyCtxtInferExt};
56
use rustc_middle::mir::{self, Field};
67
use rustc_middle::thir::{FieldPat, Pat, PatKind};
@@ -226,14 +227,21 @@ impl<'tcx> ConstToPat<'tcx> {
226227
// using `PartialEq::eq` in this scenario in the past.)
227228
let partial_eq_trait_id =
228229
self.tcx().require_lang_item(hir::LangItem::PartialEq, Some(self.span));
230+
let any_ty = self
231+
.infcx
232+
.next_ty_var(TypeVariableOrigin {
233+
kind: TypeVariableOriginKind::MiscVariable,
234+
span: self.span,
235+
})
236+
.into();
229237
let obligation: PredicateObligation<'_> = predicate_for_trait_def(
230238
self.tcx(),
231239
self.param_env,
232240
ObligationCause::misc(self.span, self.id),
233241
partial_eq_trait_id,
234242
0,
235243
ty,
236-
&[],
244+
&[any_ty],
237245
);
238246
// FIXME: should this call a `predicate_must_hold` variant instead?
239247

0 commit comments

Comments
 (0)