@@ -5,10 +5,7 @@ use crate::ty::fold::{FallibleTypeFolder, TypeFolder};
55use crate :: ty:: layout:: IntegerExt ;
66use crate :: ty:: query:: TyCtxtAt ;
77use crate :: ty:: subst:: { GenericArgKind , Subst , SubstsRef } ;
8- use crate :: ty:: {
9- self , DebruijnIndex , DefIdTree , EarlyBinder , List , ReEarlyBound , Ty , TyCtxt , TyKind :: * ,
10- TypeFoldable ,
11- } ;
8+ use crate :: ty:: { self , DefIdTree , Ty , TyCtxt , TypeFoldable } ;
129use rustc_apfloat:: Float as _;
1310use rustc_ast as ast;
1411use rustc_attr:: { self as attr, SignedInt , UnsignedInt } ;
@@ -18,6 +15,7 @@ use rustc_errors::ErrorGuaranteed;
1815use rustc_hir as hir;
1916use rustc_hir:: def:: { CtorOf , DefKind , Res } ;
2017use rustc_hir:: def_id:: DefId ;
18+ use rustc_index:: bit_set:: GrowableBitSet ;
2119use rustc_macros:: HashStable ;
2220use rustc_span:: { sym, DUMMY_SP } ;
2321use rustc_target:: abi:: { Integer , Size , TargetDataLayout } ;
@@ -32,6 +30,19 @@ pub struct Discr<'tcx> {
3230 pub ty : Ty < ' tcx > ,
3331}
3432
33+ /// Used as an input to [`TyCtxt::uses_unique_generic_params`].
34+ #[ derive( Copy , Clone , Debug , PartialEq , Eq ) ]
35+ pub enum IgnoreRegions {
36+ Yes ,
37+ No ,
38+ }
39+
40+ #[ derive( Copy , Clone , Debug ) ]
41+ pub enum NotUniqueParam < ' tcx > {
42+ DuplicateParam ( ty:: GenericArg < ' tcx > ) ,
43+ NotParam ( ty:: GenericArg < ' tcx > ) ,
44+ }
45+
3546impl < ' tcx > fmt:: Display for Discr < ' tcx > {
3647 fn fmt ( & self , fmt : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
3748 match * self . ty . kind ( ) {
@@ -49,8 +60,8 @@ impl<'tcx> fmt::Display for Discr<'tcx> {
4960
5061fn int_size_and_signed < ' tcx > ( tcx : TyCtxt < ' tcx > , ty : Ty < ' tcx > ) -> ( Size , bool ) {
5162 let ( int, signed) = match * ty. kind ( ) {
52- Int ( ity) => ( Integer :: from_int_ty ( & tcx, ity) , true ) ,
53- Uint ( uty) => ( Integer :: from_uint_ty ( & tcx, uty) , false ) ,
63+ ty :: Int ( ity) => ( Integer :: from_int_ty ( & tcx, ity) , true ) ,
64+ ty :: Uint ( uty) => ( Integer :: from_uint_ty ( & tcx, uty) , false ) ,
5465 _ => bug ! ( "non integer discriminant" ) ,
5566 } ;
5667 ( int. size ( ) , signed)
@@ -176,7 +187,7 @@ impl<'tcx> TyCtxt<'tcx> {
176187 if let ty:: Adt ( def, substs) = * ty. kind ( ) {
177188 for field in def. all_fields ( ) {
178189 let field_ty = field. ty ( self , substs) ;
179- if let Error ( _) = field_ty. kind ( ) {
190+ if let ty :: Error ( _) = field_ty. kind ( ) {
180191 return true ;
181192 }
182193 }
@@ -311,7 +322,7 @@ impl<'tcx> TyCtxt<'tcx> {
311322 let ( mut a, mut b) = ( source, target) ;
312323 loop {
313324 match ( & a. kind ( ) , & b. kind ( ) ) {
314- ( & Adt ( a_def, a_substs) , & Adt ( b_def, b_substs) )
325+ ( & ty :: Adt ( a_def, a_substs) , & ty :: Adt ( b_def, b_substs) )
315326 if a_def == b_def && a_def. is_struct ( ) =>
316327 {
317328 if let Some ( f) = a_def. non_enum_variant ( ) . fields . last ( ) {
@@ -321,7 +332,7 @@ impl<'tcx> TyCtxt<'tcx> {
321332 break ;
322333 }
323334 }
324- ( & Tuple ( a_tys) , & Tuple ( b_tys) ) if a_tys. len ( ) == b_tys. len ( ) => {
335+ ( & ty :: Tuple ( a_tys) , & ty :: Tuple ( b_tys) ) if a_tys. len ( ) == b_tys. len ( ) => {
325336 if let Some ( & a_last) = a_tys. last ( ) {
326337 a = a_last;
327338 b = * b_tys. last ( ) . unwrap ( ) ;
@@ -427,7 +438,7 @@ impl<'tcx> TyCtxt<'tcx> {
427438 . filter ( |& ( _, k) | {
428439 match k. unpack ( ) {
429440 GenericArgKind :: Lifetime ( region) => match region. kind ( ) {
430- ReEarlyBound ( ref ebr) => {
441+ ty :: ReEarlyBound ( ref ebr) => {
431442 !impl_generics. region_param ( ebr, self ) . pure_wrt_drop
432443 }
433444 // Error: not a region param
@@ -453,6 +464,47 @@ impl<'tcx> TyCtxt<'tcx> {
453464 result
454465 }
455466
467+ /// Checks whether each generic argument is simply a unique generic parameter.
468+ pub fn uses_unique_generic_params (
469+ self ,
470+ substs : SubstsRef < ' tcx > ,
471+ ignore_regions : IgnoreRegions ,
472+ ) -> Result < ( ) , NotUniqueParam < ' tcx > > {
473+ let mut seen = GrowableBitSet :: default ( ) ;
474+ for arg in substs {
475+ match arg. unpack ( ) {
476+ GenericArgKind :: Lifetime ( lt) => {
477+ if ignore_regions == IgnoreRegions :: No {
478+ let ty:: ReEarlyBound ( p) = lt. kind ( ) else {
479+ return Err ( NotUniqueParam :: NotParam ( lt. into ( ) ) )
480+ } ;
481+ if !seen. insert ( p. index ) {
482+ return Err ( NotUniqueParam :: DuplicateParam ( lt. into ( ) ) ) ;
483+ }
484+ }
485+ }
486+ GenericArgKind :: Type ( t) => match t. kind ( ) {
487+ ty:: Param ( p) => {
488+ if !seen. insert ( p. index ) {
489+ return Err ( NotUniqueParam :: DuplicateParam ( t. into ( ) ) ) ;
490+ }
491+ }
492+ _ => return Err ( NotUniqueParam :: NotParam ( t. into ( ) ) ) ,
493+ } ,
494+ GenericArgKind :: Const ( c) => match c. val ( ) {
495+ ty:: ConstKind :: Param ( p) => {
496+ if !seen. insert ( p. index ) {
497+ return Err ( NotUniqueParam :: DuplicateParam ( c. into ( ) ) ) ;
498+ }
499+ }
500+ _ => return Err ( NotUniqueParam :: NotParam ( c. into ( ) ) ) ,
501+ } ,
502+ }
503+ }
504+
505+ Ok ( ( ) )
506+ }
507+
456508 /// Returns `true` if `def_id` refers to a closure (e.g., `|x| x * 2`). Note
457509 /// that closures have a `DefId`, but the closure *expression* also
458510 /// has a `HirId` that is located within the context where the
@@ -594,30 +646,33 @@ impl<'tcx> TyCtxt<'tcx> {
594646 if visitor. found_recursion { Err ( expanded_type) } else { Ok ( expanded_type) }
595647 }
596648
597- pub fn bound_type_of ( self , def_id : DefId ) -> EarlyBinder < Ty < ' tcx > > {
598- EarlyBinder ( self . type_of ( def_id) )
649+ pub fn bound_type_of ( self , def_id : DefId ) -> ty :: EarlyBinder < Ty < ' tcx > > {
650+ ty :: EarlyBinder ( self . type_of ( def_id) )
599651 }
600652
601- pub fn bound_fn_sig ( self , def_id : DefId ) -> EarlyBinder < ty:: PolyFnSig < ' tcx > > {
602- EarlyBinder ( self . fn_sig ( def_id) )
653+ pub fn bound_fn_sig ( self , def_id : DefId ) -> ty :: EarlyBinder < ty:: PolyFnSig < ' tcx > > {
654+ ty :: EarlyBinder ( self . fn_sig ( def_id) )
603655 }
604656
605- pub fn bound_impl_trait_ref ( self , def_id : DefId ) -> Option < EarlyBinder < ty:: TraitRef < ' tcx > > > {
606- self . impl_trait_ref ( def_id) . map ( |i| EarlyBinder ( i) )
657+ pub fn bound_impl_trait_ref (
658+ self ,
659+ def_id : DefId ,
660+ ) -> Option < ty:: EarlyBinder < ty:: TraitRef < ' tcx > > > {
661+ self . impl_trait_ref ( def_id) . map ( |i| ty:: EarlyBinder ( i) )
607662 }
608663
609664 pub fn bound_explicit_item_bounds (
610665 self ,
611666 def_id : DefId ,
612- ) -> EarlyBinder < & ' tcx [ ( ty:: Predicate < ' tcx > , rustc_span:: Span ) ] > {
613- EarlyBinder ( self . explicit_item_bounds ( def_id) )
667+ ) -> ty :: EarlyBinder < & ' tcx [ ( ty:: Predicate < ' tcx > , rustc_span:: Span ) ] > {
668+ ty :: EarlyBinder ( self . explicit_item_bounds ( def_id) )
614669 }
615670
616671 pub fn bound_item_bounds (
617672 self ,
618673 def_id : DefId ,
619- ) -> EarlyBinder < & ' tcx ty:: List < ty:: Predicate < ' tcx > > > {
620- EarlyBinder ( self . item_bounds ( def_id) )
674+ ) -> ty :: EarlyBinder < & ' tcx ty:: List < ty:: Predicate < ' tcx > > > {
675+ ty :: EarlyBinder ( self . item_bounds ( def_id) )
621676 }
622677}
623678
@@ -930,35 +985,40 @@ impl<'tcx> Ty<'tcx> {
930985 pub fn is_structural_eq_shallow ( self , tcx : TyCtxt < ' tcx > ) -> bool {
931986 match self . kind ( ) {
932987 // Look for an impl of both `PartialStructuralEq` and `StructuralEq`.
933- Adt ( ..) => tcx. has_structural_eq_impls ( self ) ,
988+ ty :: Adt ( ..) => tcx. has_structural_eq_impls ( self ) ,
934989
935990 // Primitive types that satisfy `Eq`.
936- Bool | Char | Int ( _) | Uint ( _) | Str | Never => true ,
991+ ty :: Bool | ty :: Char | ty :: Int ( _) | ty :: Uint ( _) | ty :: Str | ty :: Never => true ,
937992
938993 // Composite types that satisfy `Eq` when all of their fields do.
939994 //
940995 // Because this function is "shallow", we return `true` for these composites regardless
941996 // of the type(s) contained within.
942- Ref ( ..) | Array ( ..) | Slice ( _) | Tuple ( ..) => true ,
997+ ty :: Ref ( ..) | ty :: Array ( ..) | ty :: Slice ( _) | ty :: Tuple ( ..) => true ,
943998
944999 // Raw pointers use bitwise comparison.
945- RawPtr ( _) | FnPtr ( _) => true ,
1000+ ty :: RawPtr ( _) | ty :: FnPtr ( _) => true ,
9461001
9471002 // Floating point numbers are not `Eq`.
948- Float ( _) => false ,
1003+ ty :: Float ( _) => false ,
9491004
9501005 // Conservatively return `false` for all others...
9511006
9521007 // Anonymous function types
953- FnDef ( ..) | Closure ( ..) | Dynamic ( ..) | Generator ( ..) => false ,
1008+ ty :: FnDef ( ..) | ty :: Closure ( ..) | ty :: Dynamic ( ..) | ty :: Generator ( ..) => false ,
9541009
9551010 // Generic or inferred types
9561011 //
9571012 // FIXME(ecstaticmorse): Maybe we should `bug` here? This should probably only be
9581013 // called for known, fully-monomorphized types.
959- Projection ( _) | Opaque ( ..) | Param ( _) | Bound ( ..) | Placeholder ( _) | Infer ( _) => false ,
1014+ ty:: Projection ( _)
1015+ | ty:: Opaque ( ..)
1016+ | ty:: Param ( _)
1017+ | ty:: Bound ( ..)
1018+ | ty:: Placeholder ( _)
1019+ | ty:: Infer ( _) => false ,
9601020
961- Foreign ( _) | GeneratorWitness ( ..) | Error ( _) => false ,
1021+ ty :: Foreign ( _) | ty :: GeneratorWitness ( ..) | ty :: Error ( _) => false ,
9621022 }
9631023 }
9641024
@@ -974,13 +1034,13 @@ impl<'tcx> Ty<'tcx> {
9741034 /// - `&'a *const &'b u8 -> *const &'b u8`
9751035 pub fn peel_refs ( self ) -> Ty < ' tcx > {
9761036 let mut ty = self ;
977- while let Ref ( _, inner_ty, _) = ty. kind ( ) {
1037+ while let ty :: Ref ( _, inner_ty, _) = ty. kind ( ) {
9781038 ty = * inner_ty;
9791039 }
9801040 ty
9811041 }
9821042
983- pub fn outer_exclusive_binder ( self ) -> DebruijnIndex {
1043+ pub fn outer_exclusive_binder ( self ) -> ty :: DebruijnIndex {
9841044 self . 0 . outer_exclusive_binder
9851045 }
9861046}
@@ -1177,8 +1237,8 @@ pub struct AlwaysRequiresDrop;
11771237/// with their underlying types.
11781238pub fn normalize_opaque_types < ' tcx > (
11791239 tcx : TyCtxt < ' tcx > ,
1180- val : & ' tcx List < ty:: Predicate < ' tcx > > ,
1181- ) -> & ' tcx List < ty:: Predicate < ' tcx > > {
1240+ val : & ' tcx ty :: List < ty:: Predicate < ' tcx > > ,
1241+ ) -> & ' tcx ty :: List < ty:: Predicate < ' tcx > > {
11821242 let mut visitor = OpaqueTypeExpander {
11831243 seen_opaque_tys : FxHashSet :: default ( ) ,
11841244 expanded_cache : FxHashMap :: default ( ) ,
0 commit comments