Skip to content

Commit fef872a

Browse files
committed
Guard AliasTy creation against passing the wrong number of substs
1 parent 7bdda8f commit fef872a

File tree

9 files changed

+35
-38
lines changed

9 files changed

+35
-38
lines changed

compiler/rustc_hir_analysis/src/astconv/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1146,7 +1146,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
11461146

11471147
debug!(?substs_trait_ref_and_assoc_item);
11481148

1149-
ty::AliasTy { def_id: assoc_item.def_id, substs: substs_trait_ref_and_assoc_item }
1149+
self.tcx().mk_alias_ty(assoc_item.def_id, substs_trait_ref_and_assoc_item)
11501150
});
11511151

11521152
if !speculative {

compiler/rustc_hir_analysis/src/check/compare_method.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1746,10 +1746,7 @@ pub fn check_type_bounds<'tcx>(
17461746
_ => predicates.push(
17471747
ty::Binder::bind_with_vars(
17481748
ty::ProjectionPredicate {
1749-
projection_ty: ty::AliasTy {
1750-
def_id: trait_ty.def_id,
1751-
substs: rebased_substs,
1752-
},
1749+
projection_ty: tcx.mk_alias_ty(trait_ty.def_id, rebased_substs),
17531750
term: impl_ty_value.into(),
17541751
},
17551752
bound_vars,

compiler/rustc_hir_typeck/src/method/suggest.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -557,10 +557,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
557557
.chain(projection_ty.substs.iter().skip(1)),
558558
);
559559

560-
let quiet_projection_ty = ty::AliasTy {
561-
substs: substs_with_infer_self,
562-
def_id: projection_ty.def_id,
563-
};
560+
let quiet_projection_ty =
561+
tcx.mk_alias_ty(projection_ty.def_id, substs_with_infer_self);
564562

565563
let term = pred.skip_binder().term;
566564

compiler/rustc_middle/src/ty/context.rs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use crate::thir::Thir;
1818
use crate::traits;
1919
use crate::ty::query::{self, TyCtxtAt};
2020
use crate::ty::{
21-
self, AdtDef, AdtDefData, AdtKind, AliasTy, Binder, BindingMode, BoundVar, CanonicalPolyFnSig,
21+
self, AdtDef, AdtDefData, AdtKind, Binder, BindingMode, BoundVar, CanonicalPolyFnSig,
2222
ClosureSizeProfileData, Const, ConstS, DefIdTree, FloatTy, FloatVar, FloatVid,
2323
GenericParamDefKind, InferTy, IntTy, IntVar, IntVid, List, ParamConst, ParamTy,
2424
PolyExistentialPredicate, PolyFnSig, Predicate, PredicateKind, Region, RegionKind, ReprOptions,
@@ -2591,12 +2591,7 @@ impl<'tcx> TyCtxt<'tcx> {
25912591

25922592
#[inline]
25932593
pub fn mk_projection(self, item_def_id: DefId, substs: SubstsRef<'tcx>) -> Ty<'tcx> {
2594-
debug_assert_eq!(
2595-
self.generics_of(item_def_id).count(),
2596-
substs.len(),
2597-
"wrong number of generic parameters for {item_def_id:?}: {substs:?}",
2598-
);
2599-
self.mk_ty(Alias(ty::Projection, AliasTy { def_id: item_def_id, substs }))
2594+
self.mk_ty(Alias(ty::Projection, self.mk_alias_ty(item_def_id, substs)))
26002595
}
26012596

26022597
#[inline]
@@ -2867,6 +2862,23 @@ impl<'tcx> TyCtxt<'tcx> {
28672862
ty::TraitRef::new(trait_def_id, substs)
28682863
}
28692864

2865+
pub fn mk_alias_ty(
2866+
self,
2867+
def_id: DefId,
2868+
substs: impl IntoIterator<Item = impl Into<GenericArg<'tcx>>>,
2869+
) -> ty::AliasTy<'tcx> {
2870+
let substs = substs.into_iter().map(Into::into);
2871+
let n = self.generics_of(def_id).count();
2872+
debug_assert_eq!(
2873+
(n, Some(n)),
2874+
substs.size_hint(),
2875+
"wrong number of generic parameters for {def_id:?}: {:?} \nDid you accidentally include the self-type in the params list?",
2876+
substs.collect::<Vec<_>>(),
2877+
);
2878+
let substs = self.mk_substs(substs);
2879+
ty::AliasTy { def_id, substs }
2880+
}
2881+
28702882
pub fn mk_bound_variable_kinds<
28712883
I: InternAs<ty::BoundVariableKind, &'tcx List<ty::BoundVariableKind>>,
28722884
>(

compiler/rustc_middle/src/ty/relate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ impl<'tcx> Relate<'tcx> for ty::AliasTy<'tcx> {
280280
Err(TypeError::ProjectionMismatched(expected_found(relation, a.def_id, b.def_id)))
281281
} else {
282282
let substs = relation.relate(a.substs, b.substs)?;
283-
Ok(ty::AliasTy { def_id: a.def_id, substs: &substs })
283+
Ok(relation.tcx().mk_alias_ty(a.def_id, substs))
284284
}
285285
}
286286
}

compiler/rustc_middle/src/ty/sty.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1449,10 +1449,8 @@ impl<'tcx> ExistentialProjection<'tcx> {
14491449
debug_assert!(!self_ty.has_escaping_bound_vars());
14501450

14511451
ty::ProjectionPredicate {
1452-
projection_ty: ty::AliasTy {
1453-
def_id: self.def_id,
1454-
substs: tcx.mk_substs_trait(self_ty, self.substs),
1455-
},
1452+
projection_ty: tcx
1453+
.mk_alias_ty(self.def_id, [self_ty.into()].into_iter().chain(self.substs)),
14561454
term: self.term,
14571455
}
14581456
}

compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3265,9 +3265,9 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
32653265
// in. For example, this would be what `Iterator::Item` is here.
32663266
let ty_var = self.infcx.next_ty_var(origin);
32673267
// This corresponds to `<ExprTy as Iterator>::Item = _`.
3268-
let trait_ref = ty::Binder::dummy(ty::PredicateKind::Clause(
3268+
let projection = ty::Binder::dummy(ty::PredicateKind::Clause(
32693269
ty::Clause::Projection(ty::ProjectionPredicate {
3270-
projection_ty: ty::AliasTy { substs, def_id: proj.def_id },
3270+
projection_ty: tcx.mk_alias_ty(proj.def_id, substs),
32713271
term: ty_var.into(),
32723272
}),
32733273
));
@@ -3277,7 +3277,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
32773277
span,
32783278
expr.hir_id,
32793279
param_env,
3280-
trait_ref,
3280+
projection,
32813281
));
32823282
if ocx.select_where_possible().is_empty() {
32833283
// `ty_var` now holds the type that `Item` is for `ExprTy`.

compiler/rustc_trait_selection/src/traits/project.rs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1867,10 +1867,7 @@ fn confirm_generator_candidate<'cx, 'tcx>(
18671867
};
18681868

18691869
ty::ProjectionPredicate {
1870-
projection_ty: ty::AliasTy {
1871-
substs: trait_ref.substs,
1872-
def_id: obligation.predicate.def_id,
1873-
},
1870+
projection_ty: tcx.mk_alias_ty(obligation.predicate.def_id, trait_ref.substs),
18741871
term: ty.into(),
18751872
}
18761873
});
@@ -1909,10 +1906,7 @@ fn confirm_future_candidate<'cx, 'tcx>(
19091906
debug_assert_eq!(tcx.associated_item(obligation.predicate.def_id).name, sym::Output);
19101907

19111908
ty::ProjectionPredicate {
1912-
projection_ty: ty::AliasTy {
1913-
substs: trait_ref.substs,
1914-
def_id: obligation.predicate.def_id,
1915-
},
1909+
projection_ty: tcx.mk_alias_ty(obligation.predicate.def_id, trait_ref.substs),
19161910
term: return_ty.into(),
19171911
}
19181912
});
@@ -1965,10 +1959,8 @@ fn confirm_builtin_candidate<'cx, 'tcx>(
19651959
bug!("unexpected builtin trait with associated type: {:?}", obligation.predicate);
19661960
};
19671961

1968-
let predicate = ty::ProjectionPredicate {
1969-
projection_ty: ty::AliasTy { substs, def_id: item_def_id },
1970-
term,
1971-
};
1962+
let predicate =
1963+
ty::ProjectionPredicate { projection_ty: tcx.mk_alias_ty(item_def_id, substs), term };
19721964

19731965
confirm_param_env_candidate(selcx, obligation, ty::Binder::dummy(predicate), false)
19741966
.with_addl_obligations(obligations)
@@ -2037,7 +2029,7 @@ fn confirm_callable_candidate<'cx, 'tcx>(
20372029
flag,
20382030
)
20392031
.map_bound(|(trait_ref, ret_type)| ty::ProjectionPredicate {
2040-
projection_ty: ty::AliasTy { substs: trait_ref.substs, def_id: fn_once_output_def_id },
2032+
projection_ty: tcx.mk_alias_ty(fn_once_output_def_id, trait_ref.substs),
20412033
term: ret_type.into(),
20422034
});
20432035

compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
536536
let ty = traits::normalize_projection_type(
537537
self,
538538
param_env,
539-
ty::AliasTy { def_id: tcx.lang_items().deref_target()?, substs: trait_ref.substs },
539+
tcx.mk_alias_ty(tcx.lang_items().deref_target()?, trait_ref.substs),
540540
cause.clone(),
541541
0,
542542
// We're *intentionally* throwing these away,

0 commit comments

Comments
 (0)