@@ -5,10 +5,7 @@ use crate::ty::fold::{FallibleTypeFolder, TypeFolder};
5
5
use crate :: ty:: layout:: IntegerExt ;
6
6
use crate :: ty:: query:: TyCtxtAt ;
7
7
use 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 } ;
12
9
use rustc_apfloat:: Float as _;
13
10
use rustc_ast as ast;
14
11
use rustc_attr:: { self as attr, SignedInt , UnsignedInt } ;
@@ -18,6 +15,7 @@ use rustc_errors::ErrorGuaranteed;
18
15
use rustc_hir as hir;
19
16
use rustc_hir:: def:: { CtorOf , DefKind , Res } ;
20
17
use rustc_hir:: def_id:: DefId ;
18
+ use rustc_index:: bit_set:: GrowableBitSet ;
21
19
use rustc_macros:: HashStable ;
22
20
use rustc_span:: { sym, DUMMY_SP } ;
23
21
use rustc_target:: abi:: { Integer , Size , TargetDataLayout } ;
@@ -32,6 +30,12 @@ pub struct Discr<'tcx> {
32
30
pub ty : Ty < ' tcx > ,
33
31
}
34
32
33
+ #[ derive( Copy , Clone , Debug ) ]
34
+ pub enum NotUniqueParam < ' tcx > {
35
+ DuplicateParam ( ty:: GenericArg < ' tcx > ) ,
36
+ NotParam ( ty:: GenericArg < ' tcx > ) ,
37
+ }
38
+
35
39
impl < ' tcx > fmt:: Display for Discr < ' tcx > {
36
40
fn fmt ( & self , fmt : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
37
41
match * self . ty . kind ( ) {
@@ -49,8 +53,8 @@ impl<'tcx> fmt::Display for Discr<'tcx> {
49
53
50
54
fn int_size_and_signed < ' tcx > ( tcx : TyCtxt < ' tcx > , ty : Ty < ' tcx > ) -> ( Size , bool ) {
51
55
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 ) ,
56
+ ty :: Int ( ity) => ( Integer :: from_int_ty ( & tcx, ity) , true ) ,
57
+ ty :: Uint ( uty) => ( Integer :: from_uint_ty ( & tcx, uty) , false ) ,
54
58
_ => bug ! ( "non integer discriminant" ) ,
55
59
} ;
56
60
( int. size ( ) , signed)
@@ -176,7 +180,7 @@ impl<'tcx> TyCtxt<'tcx> {
176
180
if let ty:: Adt ( def, substs) = * ty. kind ( ) {
177
181
for field in def. all_fields ( ) {
178
182
let field_ty = field. ty ( self , substs) ;
179
- if let Error ( _) = field_ty. kind ( ) {
183
+ if let ty :: Error ( _) = field_ty. kind ( ) {
180
184
return true ;
181
185
}
182
186
}
@@ -311,7 +315,7 @@ impl<'tcx> TyCtxt<'tcx> {
311
315
let ( mut a, mut b) = ( source, target) ;
312
316
loop {
313
317
match ( & a. kind ( ) , & b. kind ( ) ) {
314
- ( & Adt ( a_def, a_substs) , & Adt ( b_def, b_substs) )
318
+ ( & ty :: Adt ( a_def, a_substs) , & ty :: Adt ( b_def, b_substs) )
315
319
if a_def == b_def && a_def. is_struct ( ) =>
316
320
{
317
321
if let Some ( f) = a_def. non_enum_variant ( ) . fields . last ( ) {
@@ -321,7 +325,7 @@ impl<'tcx> TyCtxt<'tcx> {
321
325
break ;
322
326
}
323
327
}
324
- ( & Tuple ( a_tys) , & Tuple ( b_tys) ) if a_tys. len ( ) == b_tys. len ( ) => {
328
+ ( & ty :: Tuple ( a_tys) , & ty :: Tuple ( b_tys) ) if a_tys. len ( ) == b_tys. len ( ) => {
325
329
if let Some ( & a_last) = a_tys. last ( ) {
326
330
a = a_last;
327
331
b = * b_tys. last ( ) . unwrap ( ) ;
@@ -427,7 +431,7 @@ impl<'tcx> TyCtxt<'tcx> {
427
431
. filter ( |& ( _, k) | {
428
432
match k. unpack ( ) {
429
433
GenericArgKind :: Lifetime ( region) => match region. kind ( ) {
430
- ReEarlyBound ( ref ebr) => {
434
+ ty :: ReEarlyBound ( ref ebr) => {
431
435
!impl_generics. region_param ( ebr, self ) . pure_wrt_drop
432
436
}
433
437
// Error: not a region param
@@ -453,6 +457,49 @@ impl<'tcx> TyCtxt<'tcx> {
453
457
result
454
458
}
455
459
460
+ /// Checks whether each generic argument is simply a unique generic parameter.
461
+ pub fn uses_unique_generic_params (
462
+ self ,
463
+ substs : SubstsRef < ' tcx > ,
464
+ ignore_regions : bool ,
465
+ ) -> Result < ( ) , NotUniqueParam < ' tcx > > {
466
+ let mut seen = GrowableBitSet :: default ( ) ;
467
+ for arg in substs {
468
+ match arg. unpack ( ) {
469
+ GenericArgKind :: Lifetime ( lt) => {
470
+ if !ignore_regions {
471
+ match lt. kind ( ) {
472
+ ty:: ReEarlyBound ( p) => {
473
+ if !seen. insert ( p. index ) {
474
+ return Err ( NotUniqueParam :: DuplicateParam ( lt. into ( ) ) ) ;
475
+ }
476
+ }
477
+ _ => return Err ( NotUniqueParam :: NotParam ( lt. into ( ) ) ) ,
478
+ }
479
+ }
480
+ }
481
+ GenericArgKind :: Type ( t) => match t. kind ( ) {
482
+ ty:: Param ( p) => {
483
+ if !seen. insert ( p. index ) {
484
+ return Err ( NotUniqueParam :: DuplicateParam ( t. into ( ) ) ) ;
485
+ }
486
+ }
487
+ _ => return Err ( NotUniqueParam :: NotParam ( t. into ( ) ) ) ,
488
+ } ,
489
+ GenericArgKind :: Const ( c) => match c. val ( ) {
490
+ ty:: ConstKind :: Param ( p) => {
491
+ if !seen. insert ( p. index ) {
492
+ return Err ( NotUniqueParam :: DuplicateParam ( c. into ( ) ) ) ;
493
+ }
494
+ }
495
+ _ => return Err ( NotUniqueParam :: NotParam ( c. into ( ) ) ) ,
496
+ } ,
497
+ }
498
+ }
499
+
500
+ Ok ( ( ) )
501
+ }
502
+
456
503
/// Returns `true` if `def_id` refers to a closure (e.g., `|x| x * 2`). Note
457
504
/// that closures have a `DefId`, but the closure *expression* also
458
505
/// has a `HirId` that is located within the context where the
@@ -594,30 +641,33 @@ impl<'tcx> TyCtxt<'tcx> {
594
641
if visitor. found_recursion { Err ( expanded_type) } else { Ok ( expanded_type) }
595
642
}
596
643
597
- pub fn bound_type_of ( self , def_id : DefId ) -> EarlyBinder < Ty < ' tcx > > {
598
- EarlyBinder ( self . type_of ( def_id) )
644
+ pub fn bound_type_of ( self , def_id : DefId ) -> ty :: EarlyBinder < Ty < ' tcx > > {
645
+ ty :: EarlyBinder ( self . type_of ( def_id) )
599
646
}
600
647
601
- pub fn bound_fn_sig ( self , def_id : DefId ) -> EarlyBinder < ty:: PolyFnSig < ' tcx > > {
602
- EarlyBinder ( self . fn_sig ( def_id) )
648
+ pub fn bound_fn_sig ( self , def_id : DefId ) -> ty :: EarlyBinder < ty:: PolyFnSig < ' tcx > > {
649
+ ty :: EarlyBinder ( self . fn_sig ( def_id) )
603
650
}
604
651
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) )
652
+ pub fn bound_impl_trait_ref (
653
+ self ,
654
+ def_id : DefId ,
655
+ ) -> Option < ty:: EarlyBinder < ty:: TraitRef < ' tcx > > > {
656
+ self . impl_trait_ref ( def_id) . map ( |i| ty:: EarlyBinder ( i) )
607
657
}
608
658
609
659
pub fn bound_explicit_item_bounds (
610
660
self ,
611
661
def_id : DefId ,
612
- ) -> EarlyBinder < & ' tcx [ ( ty:: Predicate < ' tcx > , rustc_span:: Span ) ] > {
613
- EarlyBinder ( self . explicit_item_bounds ( def_id) )
662
+ ) -> ty :: EarlyBinder < & ' tcx [ ( ty:: Predicate < ' tcx > , rustc_span:: Span ) ] > {
663
+ ty :: EarlyBinder ( self . explicit_item_bounds ( def_id) )
614
664
}
615
665
616
666
pub fn bound_item_bounds (
617
667
self ,
618
668
def_id : DefId ,
619
- ) -> EarlyBinder < & ' tcx ty:: List < ty:: Predicate < ' tcx > > > {
620
- EarlyBinder ( self . item_bounds ( def_id) )
669
+ ) -> ty :: EarlyBinder < & ' tcx ty:: List < ty:: Predicate < ' tcx > > > {
670
+ ty :: EarlyBinder ( self . item_bounds ( def_id) )
621
671
}
622
672
}
623
673
@@ -930,35 +980,40 @@ impl<'tcx> Ty<'tcx> {
930
980
pub fn is_structural_eq_shallow ( self , tcx : TyCtxt < ' tcx > ) -> bool {
931
981
match self . kind ( ) {
932
982
// Look for an impl of both `PartialStructuralEq` and `StructuralEq`.
933
- Adt ( ..) => tcx. has_structural_eq_impls ( self ) ,
983
+ ty :: Adt ( ..) => tcx. has_structural_eq_impls ( self ) ,
934
984
935
985
// Primitive types that satisfy `Eq`.
936
- Bool | Char | Int ( _) | Uint ( _) | Str | Never => true ,
986
+ ty :: Bool | ty :: Char | ty :: Int ( _) | ty :: Uint ( _) | ty :: Str | ty :: Never => true ,
937
987
938
988
// Composite types that satisfy `Eq` when all of their fields do.
939
989
//
940
990
// Because this function is "shallow", we return `true` for these composites regardless
941
991
// of the type(s) contained within.
942
- Ref ( ..) | Array ( ..) | Slice ( _) | Tuple ( ..) => true ,
992
+ ty :: Ref ( ..) | ty :: Array ( ..) | ty :: Slice ( _) | ty :: Tuple ( ..) => true ,
943
993
944
994
// Raw pointers use bitwise comparison.
945
- RawPtr ( _) | FnPtr ( _) => true ,
995
+ ty :: RawPtr ( _) | ty :: FnPtr ( _) => true ,
946
996
947
997
// Floating point numbers are not `Eq`.
948
- Float ( _) => false ,
998
+ ty :: Float ( _) => false ,
949
999
950
1000
// Conservatively return `false` for all others...
951
1001
952
1002
// Anonymous function types
953
- FnDef ( ..) | Closure ( ..) | Dynamic ( ..) | Generator ( ..) => false ,
1003
+ ty :: FnDef ( ..) | ty :: Closure ( ..) | ty :: Dynamic ( ..) | ty :: Generator ( ..) => false ,
954
1004
955
1005
// Generic or inferred types
956
1006
//
957
1007
// FIXME(ecstaticmorse): Maybe we should `bug` here? This should probably only be
958
1008
// called for known, fully-monomorphized types.
959
- Projection ( _) | Opaque ( ..) | Param ( _) | Bound ( ..) | Placeholder ( _) | Infer ( _) => false ,
1009
+ ty:: Projection ( _)
1010
+ | ty:: Opaque ( ..)
1011
+ | ty:: Param ( _)
1012
+ | ty:: Bound ( ..)
1013
+ | ty:: Placeholder ( _)
1014
+ | ty:: Infer ( _) => false ,
960
1015
961
- Foreign ( _) | GeneratorWitness ( ..) | Error ( _) => false ,
1016
+ ty :: Foreign ( _) | ty :: GeneratorWitness ( ..) | ty :: Error ( _) => false ,
962
1017
}
963
1018
}
964
1019
@@ -974,13 +1029,13 @@ impl<'tcx> Ty<'tcx> {
974
1029
/// - `&'a *const &'b u8 -> *const &'b u8`
975
1030
pub fn peel_refs ( self ) -> Ty < ' tcx > {
976
1031
let mut ty = self ;
977
- while let Ref ( _, inner_ty, _) = ty. kind ( ) {
1032
+ while let ty :: Ref ( _, inner_ty, _) = ty. kind ( ) {
978
1033
ty = * inner_ty;
979
1034
}
980
1035
ty
981
1036
}
982
1037
983
- pub fn outer_exclusive_binder ( self ) -> DebruijnIndex {
1038
+ pub fn outer_exclusive_binder ( self ) -> ty :: DebruijnIndex {
984
1039
self . 0 . outer_exclusive_binder
985
1040
}
986
1041
}
@@ -1177,8 +1232,8 @@ pub struct AlwaysRequiresDrop;
1177
1232
/// with their underlying types.
1178
1233
pub fn normalize_opaque_types < ' tcx > (
1179
1234
tcx : TyCtxt < ' tcx > ,
1180
- val : & ' tcx List < ty:: Predicate < ' tcx > > ,
1181
- ) -> & ' tcx List < ty:: Predicate < ' tcx > > {
1235
+ val : & ' tcx ty :: List < ty:: Predicate < ' tcx > > ,
1236
+ ) -> & ' tcx ty :: List < ty:: Predicate < ' tcx > > {
1182
1237
let mut visitor = OpaqueTypeExpander {
1183
1238
seen_opaque_tys : FxHashSet :: default ( ) ,
1184
1239
expanded_cache : FxHashMap :: default ( ) ,
0 commit comments