]> git.lizzy.rs Git - rust.git/commitdiff
Guard `AliasTy` creation against passing the wrong number of substs
authorOli Scherer <git-spam-no-reply9815368754983@oli-obk.de>
Tue, 13 Dec 2022 10:25:21 +0000 (10:25 +0000)
committerOli Scherer <git-spam-no-reply9815368754983@oli-obk.de>
Wed, 14 Dec 2022 15:36:39 +0000 (15:36 +0000)
compiler/rustc_hir_analysis/src/astconv/mod.rs
compiler/rustc_hir_analysis/src/check/compare_method.rs
compiler/rustc_hir_typeck/src/method/suggest.rs
compiler/rustc_middle/src/ty/context.rs
compiler/rustc_middle/src/ty/relate.rs
compiler/rustc_middle/src/ty/sty.rs
compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs
compiler/rustc_trait_selection/src/traits/project.rs
compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs

index c8c10385f0cd60c4d035985c1931e1277e6f8f1a..babf2ef1af418f9cb22f4870d276e8985974948d 100644 (file)
@@ -1146,7 +1146,7 @@ fn add_predicates_for_ast_type_binding(
 
             debug!(?substs_trait_ref_and_assoc_item);
 
-            ty::AliasTy { def_id: assoc_item.def_id, substs: substs_trait_ref_and_assoc_item }
+            self.tcx().mk_alias_ty(assoc_item.def_id, substs_trait_ref_and_assoc_item)
         });
 
         if !speculative {
index ba7d31cea2e2f3e778702c331738ca93ab9dffc7..4f4e9a89fc7411c1bf09ac150d874f669e9cdd71 100644 (file)
@@ -1746,10 +1746,7 @@ pub fn check_type_bounds<'tcx>(
             _ => predicates.push(
                 ty::Binder::bind_with_vars(
                     ty::ProjectionPredicate {
-                        projection_ty: ty::AliasTy {
-                            def_id: trait_ty.def_id,
-                            substs: rebased_substs,
-                        },
+                        projection_ty: tcx.mk_alias_ty(trait_ty.def_id, rebased_substs),
                         term: impl_ty_value.into(),
                     },
                     bound_vars,
index 41cd6bf314ebf557527bde7bf783f163ee9ef29a..0103f58d0614c6e6b59bf6f5a865a6de0f7a3171 100644 (file)
@@ -557,10 +557,8 @@ pub fn report_method_error(
                                         .chain(projection_ty.substs.iter().skip(1)),
                                 );
 
-                                let quiet_projection_ty = ty::AliasTy {
-                                    substs: substs_with_infer_self,
-                                    def_id: projection_ty.def_id,
-                                };
+                                let quiet_projection_ty =
+                                    tcx.mk_alias_ty(projection_ty.def_id, substs_with_infer_self);
 
                                 let term = pred.skip_binder().term;
 
index dc333b4702f39e1a2a658c88f8ddce14966db3b6..ad097b39d10844987aefbad1859c7dadf8f2d427 100644 (file)
@@ -18,7 +18,7 @@
 use crate::traits;
 use crate::ty::query::{self, TyCtxtAt};
 use crate::ty::{
-    self, AdtDef, AdtDefData, AdtKind, AliasTy, Binder, BindingMode, BoundVar, CanonicalPolyFnSig,
+    self, AdtDef, AdtDefData, AdtKind, Binder, BindingMode, BoundVar, CanonicalPolyFnSig,
     ClosureSizeProfileData, Const, ConstS, DefIdTree, FloatTy, FloatVar, FloatVid,
     GenericParamDefKind, InferTy, IntTy, IntVar, IntVid, List, ParamConst, ParamTy,
     PolyExistentialPredicate, PolyFnSig, Predicate, PredicateKind, Region, RegionKind, ReprOptions,
@@ -2591,12 +2591,7 @@ pub fn mk_dynamic(
 
     #[inline]
     pub fn mk_projection(self, item_def_id: DefId, substs: SubstsRef<'tcx>) -> Ty<'tcx> {
-        debug_assert_eq!(
-            self.generics_of(item_def_id).count(),
-            substs.len(),
-            "wrong number of generic parameters for {item_def_id:?}: {substs:?}",
-        );
-        self.mk_ty(Alias(ty::Projection, AliasTy { def_id: item_def_id, substs }))
+        self.mk_ty(Alias(ty::Projection, self.mk_alias_ty(item_def_id, substs)))
     }
 
     #[inline]
@@ -2867,6 +2862,23 @@ pub fn mk_trait_ref(
         ty::TraitRef::new(trait_def_id, substs)
     }
 
+    pub fn mk_alias_ty(
+        self,
+        def_id: DefId,
+        substs: impl IntoIterator<Item = impl Into<GenericArg<'tcx>>>,
+    ) -> ty::AliasTy<'tcx> {
+        let substs = substs.into_iter().map(Into::into);
+        let n = self.generics_of(def_id).count();
+        debug_assert_eq!(
+            (n, Some(n)),
+            substs.size_hint(),
+            "wrong number of generic parameters for {def_id:?}: {:?} \nDid you accidentally include the self-type in the params list?",
+            substs.collect::<Vec<_>>(),
+        );
+        let substs = self.mk_substs(substs);
+        ty::AliasTy { def_id, substs }
+    }
+
     pub fn mk_bound_variable_kinds<
         I: InternAs<ty::BoundVariableKind, &'tcx List<ty::BoundVariableKind>>,
     >(
index 1eac8859ca93487bed32fdf2d088805b2603431d..c463980bed50d5e78824810bf6c1bf7b14974d38 100644 (file)
@@ -280,7 +280,7 @@ fn relate<R: TypeRelation<'tcx>>(
             Err(TypeError::ProjectionMismatched(expected_found(relation, a.def_id, b.def_id)))
         } else {
             let substs = relation.relate(a.substs, b.substs)?;
-            Ok(ty::AliasTy { def_id: a.def_id, substs: &substs })
+            Ok(relation.tcx().mk_alias_ty(a.def_id, substs))
         }
     }
 }
index 27de48c1f83454e78f93baea50359551e127af32..092e68a4bf79674406b97904e036d3ea1eeff098 100644 (file)
@@ -1449,10 +1449,8 @@ pub fn with_self_ty(
         debug_assert!(!self_ty.has_escaping_bound_vars());
 
         ty::ProjectionPredicate {
-            projection_ty: ty::AliasTy {
-                def_id: self.def_id,
-                substs: tcx.mk_substs_trait(self_ty, self.substs),
-            },
+            projection_ty: tcx
+                .mk_alias_ty(self.def_id, [self_ty.into()].into_iter().chain(self.substs)),
             term: self.term,
         }
     }
index 55a05df763fe621b73b54a91c9b32af1054481bf..f2ee0387888a0692b5c82e543ffac99a66356b56 100644 (file)
@@ -3265,9 +3265,9 @@ fn point_at_chain(
                 // in. For example, this would be what `Iterator::Item` is here.
                 let ty_var = self.infcx.next_ty_var(origin);
                 // This corresponds to `<ExprTy as Iterator>::Item = _`.
-                let trait_ref = ty::Binder::dummy(ty::PredicateKind::Clause(
+                let projection = ty::Binder::dummy(ty::PredicateKind::Clause(
                     ty::Clause::Projection(ty::ProjectionPredicate {
-                        projection_ty: ty::AliasTy { substs, def_id: proj.def_id },
+                        projection_ty: tcx.mk_alias_ty(proj.def_id, substs),
                         term: ty_var.into(),
                     }),
                 ));
@@ -3277,7 +3277,7 @@ fn point_at_chain(
                     span,
                     expr.hir_id,
                     param_env,
-                    trait_ref,
+                    projection,
                 ));
                 if ocx.select_where_possible().is_empty() {
                     // `ty_var` now holds the type that `Item` is for `ExprTy`.
index ca9ee04c58c10d2b1811ab8c172615d4ba1bfabf..445d783aabcf575d6e726b28cabf5fdab2b95a91 100644 (file)
@@ -1867,10 +1867,7 @@ fn confirm_generator_candidate<'cx, 'tcx>(
         };
 
         ty::ProjectionPredicate {
-            projection_ty: ty::AliasTy {
-                substs: trait_ref.substs,
-                def_id: obligation.predicate.def_id,
-            },
+            projection_ty: tcx.mk_alias_ty(obligation.predicate.def_id, trait_ref.substs),
             term: ty.into(),
         }
     });
@@ -1909,10 +1906,7 @@ fn confirm_future_candidate<'cx, 'tcx>(
         debug_assert_eq!(tcx.associated_item(obligation.predicate.def_id).name, sym::Output);
 
         ty::ProjectionPredicate {
-            projection_ty: ty::AliasTy {
-                substs: trait_ref.substs,
-                def_id: obligation.predicate.def_id,
-            },
+            projection_ty: tcx.mk_alias_ty(obligation.predicate.def_id, trait_ref.substs),
             term: return_ty.into(),
         }
     });
@@ -1965,10 +1959,8 @@ fn confirm_builtin_candidate<'cx, 'tcx>(
         bug!("unexpected builtin trait with associated type: {:?}", obligation.predicate);
     };
 
-    let predicate = ty::ProjectionPredicate {
-        projection_ty: ty::AliasTy { substs, def_id: item_def_id },
-        term,
-    };
+    let predicate =
+        ty::ProjectionPredicate { projection_ty: tcx.mk_alias_ty(item_def_id, substs), term };
 
     confirm_param_env_candidate(selcx, obligation, ty::Binder::dummy(predicate), false)
         .with_addl_obligations(obligations)
@@ -2037,7 +2029,7 @@ fn confirm_callable_candidate<'cx, 'tcx>(
         flag,
     )
     .map_bound(|(trait_ref, ret_type)| ty::ProjectionPredicate {
-        projection_ty: ty::AliasTy { substs: trait_ref.substs, def_id: fn_once_output_def_id },
+        projection_ty: tcx.mk_alias_ty(fn_once_output_def_id, trait_ref.substs),
         term: ret_type.into(),
     });
 
index 829d4f60986054141a68d95400b1f34746e3df28..c54d901e9b10aff43f7e9106632beef59ae3e495 100644 (file)
@@ -536,7 +536,7 @@ fn need_migrate_deref_output_trait_object(
             let ty = traits::normalize_projection_type(
                 self,
                 param_env,
-                ty::AliasTy { def_id: tcx.lang_items().deref_target()?, substs: trait_ref.substs },
+                tcx.mk_alias_ty(tcx.lang_items().deref_target()?, trait_ref.substs),
                 cause.clone(),
                 0,
                 // We're *intentionally* throwing these away,