Skip to content

Commit 250dcf4

Browse files
committed
Check that type_implements_trait actually is passed the right amount of generic params
1 parent 0c47dee commit 250dcf4

File tree

4 files changed

+23
-15
lines changed

4 files changed

+23
-15
lines changed

compiler/rustc_hir_typeck/src/cast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ 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, &[]);
501+
let ty_params = fcx.tcx.mk_substs(std::iter::once(ty::GenericArg::from(expr_ty)));
502502
if fcx
503503
.infcx
504504
.type_implements_trait(from_trait, ty, ty_params, fcx.param_env)

compiler/rustc_hir_typeck/src/method/prelude2021.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ 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};
12+
use rustc_middle::ty;
1213
use rustc_middle::ty::{Adt, Array, Ref, Ty};
1314
use rustc_session::lint::builtin::RUST_2021_PRELUDE_COLLISIONS;
1415
use rustc_span::symbol::kw::{Empty, Underscore};
@@ -227,14 +228,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
227228
// If we know it does not, we don't need to warn.
228229
if method_name.name == sym::from_iter {
229230
if let Some(trait_def_id) = self.tcx.get_diagnostic_item(sym::FromIterator) {
231+
let any_type = self.infcx.next_ty_var(TypeVariableOrigin {
232+
kind: TypeVariableOriginKind::MiscVariable,
233+
span,
234+
});
235+
let params = self.tcx.mk_substs(std::iter::once(ty::GenericArg::from(any_type)));
230236
if !self
231237
.infcx
232-
.type_implements_trait(
233-
trait_def_id,
234-
self_ty,
235-
InternalSubsts::empty(),
236-
self.param_env,
237-
)
238+
.type_implements_trait(trait_def_id, self_ty, params, self.param_env)
238239
.may_apply()
239240
{
240241
return;

compiler/rustc_hir_typeck/src/upvar.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -973,7 +973,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
973973
.type_implements_trait(
974974
check_trait,
975975
ty,
976-
self.tcx.mk_substs_trait(ty, &[]),
976+
ty::List::empty(),
977977
self.param_env,
978978
)
979979
.must_apply_modulo_regions()
@@ -1002,7 +1002,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10021002
.type_implements_trait(
10031003
check_trait,
10041004
ty,
1005-
self.tcx.mk_substs_trait(ty, &[]),
1005+
ty::List::empty(),
10061006
self.param_env,
10071007
)
10081008
.must_apply_modulo_regions()
@@ -1347,12 +1347,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13471347

13481348
let is_drop_defined_for_ty = |ty: Ty<'tcx>| {
13491349
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, &[]);
13511350
self.infcx
13521351
.type_implements_trait(
13531352
drop_trait,
13541353
ty,
1355-
ty_params,
1354+
ty::List::empty(),
13561355
self.tcx.param_env(closure_def_id),
13571356
)
13581357
.must_apply_modulo_regions()

compiler/rustc_trait_selection/src/infer.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,20 @@ impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> {
113113
fn type_implements_trait(
114114
&self,
115115
trait_def_id: DefId,
116-
ty: Ty<'tcx>,
116+
self_ty: Ty<'tcx>,
117117
params: SubstsRef<'tcx>,
118118
param_env: ty::ParamEnv<'tcx>,
119119
) -> traits::EvaluationResult {
120-
let trait_ref =
121-
ty::TraitRef { def_id: trait_def_id, substs: self.tcx.mk_substs_trait(ty, params) };
120+
let trait_ref = ty::TraitRef {
121+
def_id: trait_def_id,
122+
substs: self.tcx.mk_substs_trait(self_ty, params),
123+
};
124+
125+
debug_assert_eq!(
126+
self.tcx.generics_of(trait_def_id).count() - 1,
127+
params.len(),
128+
"wrong number of generic parameters for {trait_def_id:?}, did you accidentally include the self-type in the params list?"
129+
);
122130

123131
let obligation = traits::Obligation {
124132
cause: traits::ObligationCause::dummy(),

0 commit comments

Comments
 (0)