@@ -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,19 @@ pub struct Discr<'tcx> {
32
30
pub ty : Ty < ' tcx > ,
33
31
}
34
32
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
+
35
46
impl < ' tcx > fmt:: Display for Discr < ' tcx > {
36
47
fn fmt ( & self , fmt : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
37
48
match * self . ty . kind ( ) {
@@ -49,8 +60,8 @@ impl<'tcx> fmt::Display for Discr<'tcx> {
49
60
50
61
fn int_size_and_signed < ' tcx > ( tcx : TyCtxt < ' tcx > , ty : Ty < ' tcx > ) -> ( Size , bool ) {
51
62
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 ) ,
54
65
_ => bug ! ( "non integer discriminant" ) ,
55
66
} ;
56
67
( int. size ( ) , signed)
@@ -176,7 +187,7 @@ impl<'tcx> TyCtxt<'tcx> {
176
187
if let ty:: Adt ( def, substs) = * ty. kind ( ) {
177
188
for field in def. all_fields ( ) {
178
189
let field_ty = field. ty ( self , substs) ;
179
- if let Error ( _) = field_ty. kind ( ) {
190
+ if let ty :: Error ( _) = field_ty. kind ( ) {
180
191
return true ;
181
192
}
182
193
}
@@ -311,7 +322,7 @@ impl<'tcx> TyCtxt<'tcx> {
311
322
let ( mut a, mut b) = ( source, target) ;
312
323
loop {
313
324
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) )
315
326
if a_def == b_def && a_def. is_struct ( ) =>
316
327
{
317
328
if let Some ( f) = a_def. non_enum_variant ( ) . fields . last ( ) {
@@ -321,7 +332,7 @@ impl<'tcx> TyCtxt<'tcx> {
321
332
break ;
322
333
}
323
334
}
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 ( ) => {
325
336
if let Some ( & a_last) = a_tys. last ( ) {
326
337
a = a_last;
327
338
b = * b_tys. last ( ) . unwrap ( ) ;
@@ -427,7 +438,7 @@ impl<'tcx> TyCtxt<'tcx> {
427
438
. filter ( |& ( _, k) | {
428
439
match k. unpack ( ) {
429
440
GenericArgKind :: Lifetime ( region) => match region. kind ( ) {
430
- ReEarlyBound ( ref ebr) => {
441
+ ty :: ReEarlyBound ( ref ebr) => {
431
442
!impl_generics. region_param ( ebr, self ) . pure_wrt_drop
432
443
}
433
444
// Error: not a region param
@@ -453,6 +464,47 @@ impl<'tcx> TyCtxt<'tcx> {
453
464
result
454
465
}
455
466
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
+
456
508
/// Returns `true` if `def_id` refers to a closure (e.g., `|x| x * 2`). Note
457
509
/// that closures have a `DefId`, but the closure *expression* also
458
510
/// has a `HirId` that is located within the context where the
@@ -594,30 +646,33 @@ impl<'tcx> TyCtxt<'tcx> {
594
646
if visitor. found_recursion { Err ( expanded_type) } else { Ok ( expanded_type) }
595
647
}
596
648
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) )
599
651
}
600
652
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) )
603
655
}
604
656
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) )
607
662
}
608
663
609
664
pub fn bound_explicit_item_bounds (
610
665
self ,
611
666
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) )
614
669
}
615
670
616
671
pub fn bound_item_bounds (
617
672
self ,
618
673
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) )
621
676
}
622
677
}
623
678
@@ -930,35 +985,40 @@ impl<'tcx> Ty<'tcx> {
930
985
pub fn is_structural_eq_shallow ( self , tcx : TyCtxt < ' tcx > ) -> bool {
931
986
match self . kind ( ) {
932
987
// 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 ) ,
934
989
935
990
// 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 ,
937
992
938
993
// Composite types that satisfy `Eq` when all of their fields do.
939
994
//
940
995
// Because this function is "shallow", we return `true` for these composites regardless
941
996
// of the type(s) contained within.
942
- Ref ( ..) | Array ( ..) | Slice ( _) | Tuple ( ..) => true ,
997
+ ty :: Ref ( ..) | ty :: Array ( ..) | ty :: Slice ( _) | ty :: Tuple ( ..) => true ,
943
998
944
999
// Raw pointers use bitwise comparison.
945
- RawPtr ( _) | FnPtr ( _) => true ,
1000
+ ty :: RawPtr ( _) | ty :: FnPtr ( _) => true ,
946
1001
947
1002
// Floating point numbers are not `Eq`.
948
- Float ( _) => false ,
1003
+ ty :: Float ( _) => false ,
949
1004
950
1005
// Conservatively return `false` for all others...
951
1006
952
1007
// Anonymous function types
953
- FnDef ( ..) | Closure ( ..) | Dynamic ( ..) | Generator ( ..) => false ,
1008
+ ty :: FnDef ( ..) | ty :: Closure ( ..) | ty :: Dynamic ( ..) | ty :: Generator ( ..) => false ,
954
1009
955
1010
// Generic or inferred types
956
1011
//
957
1012
// FIXME(ecstaticmorse): Maybe we should `bug` here? This should probably only be
958
1013
// 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 ,
960
1020
961
- Foreign ( _) | GeneratorWitness ( ..) | Error ( _) => false ,
1021
+ ty :: Foreign ( _) | ty :: GeneratorWitness ( ..) | ty :: Error ( _) => false ,
962
1022
}
963
1023
}
964
1024
@@ -974,13 +1034,13 @@ impl<'tcx> Ty<'tcx> {
974
1034
/// - `&'a *const &'b u8 -> *const &'b u8`
975
1035
pub fn peel_refs ( self ) -> Ty < ' tcx > {
976
1036
let mut ty = self ;
977
- while let Ref ( _, inner_ty, _) = ty. kind ( ) {
1037
+ while let ty :: Ref ( _, inner_ty, _) = ty. kind ( ) {
978
1038
ty = * inner_ty;
979
1039
}
980
1040
ty
981
1041
}
982
1042
983
- pub fn outer_exclusive_binder ( self ) -> DebruijnIndex {
1043
+ pub fn outer_exclusive_binder ( self ) -> ty :: DebruijnIndex {
984
1044
self . 0 . outer_exclusive_binder
985
1045
}
986
1046
}
@@ -1177,8 +1237,8 @@ pub struct AlwaysRequiresDrop;
1177
1237
/// with their underlying types.
1178
1238
pub fn normalize_opaque_types < ' tcx > (
1179
1239
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 > > {
1182
1242
let mut visitor = OpaqueTypeExpander {
1183
1243
seen_opaque_tys : FxHashSet :: default ( ) ,
1184
1244
expanded_cache : FxHashMap :: default ( ) ,
0 commit comments