1
1
use std:: cell:: RefCell ;
2
2
use std:: default:: Default ;
3
3
use std:: hash:: Hash ;
4
+ use std:: iter;
4
5
use std:: lazy:: SyncOnceCell as OnceCell ;
5
6
use std:: path:: PathBuf ;
6
7
use std:: rc:: Rc ;
@@ -22,6 +23,7 @@ use rustc_hir::def_id::{CrateNum, DefId, DefIndex, CRATE_DEF_INDEX, LOCAL_CRATE}
22
23
use rustc_hir:: lang_items:: LangItem ;
23
24
use rustc_hir:: { BodyId , Mutability } ;
24
25
use rustc_index:: vec:: IndexVec ;
26
+ use rustc_middle:: ty:: fast_reject:: SimplifiedType ;
25
27
use rustc_middle:: ty:: { self , TyCtxt } ;
26
28
use rustc_session:: Session ;
27
29
use rustc_span:: hygiene:: MacroKind ;
@@ -1625,6 +1627,7 @@ crate enum PrimitiveType {
1625
1627
Never ,
1626
1628
}
1627
1629
1630
+ type SimplifiedTypes = FxHashMap < PrimitiveType , ArrayVec < SimplifiedType , 2 > > ;
1628
1631
impl PrimitiveType {
1629
1632
crate fn from_hir ( prim : hir:: PrimTy ) -> PrimitiveType {
1630
1633
use ast:: { FloatTy , IntTy , UintTy } ;
@@ -1680,68 +1683,68 @@ impl PrimitiveType {
1680
1683
}
1681
1684
}
1682
1685
1683
- crate fn impls ( & self , tcx : TyCtxt < ' _ > ) -> & ' static ArrayVec < DefId , 4 > {
1684
- Self :: all_impls ( tcx) . get ( self ) . expect ( "missing impl for primitive type" )
1685
- }
1686
-
1687
- crate fn all_impls ( tcx : TyCtxt < ' _ > ) -> & ' static FxHashMap < PrimitiveType , ArrayVec < DefId , 4 > > {
1688
- static CELL : OnceCell < FxHashMap < PrimitiveType , ArrayVec < DefId , 4 > > > = OnceCell :: new ( ) ;
1686
+ crate fn simplified_types ( ) -> & ' static SimplifiedTypes {
1687
+ use ty:: fast_reject:: SimplifiedTypeGen :: * ;
1688
+ use ty:: { FloatTy , IntTy , UintTy } ;
1689
+ use PrimitiveType :: * ;
1690
+ static CELL : OnceCell < SimplifiedTypes > = OnceCell :: new ( ) ;
1689
1691
1692
+ let single = |x| iter:: once ( x) . collect ( ) ;
1690
1693
CELL . get_or_init ( move || {
1691
- use self :: PrimitiveType :: * ;
1692
-
1693
- let single = |a : Option < DefId > | a. into_iter ( ) . collect ( ) ;
1694
- let both = |a : Option < DefId > , b : Option < DefId > | -> ArrayVec < _ , 4 > {
1695
- a. into_iter ( ) . chain ( b) . collect ( )
1696
- } ;
1697
-
1698
- let lang_items = tcx. lang_items ( ) ;
1699
1694
map ! {
1700
- Isize => single( lang_items. isize_impl( ) ) ,
1701
- I8 => single( lang_items. i8_impl( ) ) ,
1702
- I16 => single( lang_items. i16_impl( ) ) ,
1703
- I32 => single( lang_items. i32_impl( ) ) ,
1704
- I64 => single( lang_items. i64_impl( ) ) ,
1705
- I128 => single( lang_items. i128_impl( ) ) ,
1706
- Usize => single( lang_items. usize_impl( ) ) ,
1707
- U8 => single( lang_items. u8_impl( ) ) ,
1708
- U16 => single( lang_items. u16_impl( ) ) ,
1709
- U32 => single( lang_items. u32_impl( ) ) ,
1710
- U64 => single( lang_items. u64_impl( ) ) ,
1711
- U128 => single( lang_items. u128_impl( ) ) ,
1712
- F32 => both( lang_items. f32_impl( ) , lang_items. f32_runtime_impl( ) ) ,
1713
- F64 => both( lang_items. f64_impl( ) , lang_items. f64_runtime_impl( ) ) ,
1714
- Char => single( lang_items. char_impl( ) ) ,
1715
- Bool => single( lang_items. bool_impl( ) ) ,
1716
- Str => both( lang_items. str_impl( ) , lang_items. str_alloc_impl( ) ) ,
1717
- Slice => {
1718
- lang_items
1719
- . slice_impl( )
1720
- . into_iter( )
1721
- . chain( lang_items. slice_u8_impl( ) )
1722
- . chain( lang_items. slice_alloc_impl( ) )
1723
- . chain( lang_items. slice_u8_alloc_impl( ) )
1724
- . collect( )
1725
- } ,
1726
- Array => single( lang_items. array_impl( ) ) ,
1727
- Tuple => ArrayVec :: new( ) ,
1728
- Unit => ArrayVec :: new( ) ,
1729
- RawPointer => {
1730
- lang_items
1731
- . const_ptr_impl( )
1732
- . into_iter( )
1733
- . chain( lang_items. mut_ptr_impl( ) )
1734
- . chain( lang_items. const_slice_ptr_impl( ) )
1735
- . chain( lang_items. mut_slice_ptr_impl( ) )
1736
- . collect( )
1737
- } ,
1738
- Reference => ArrayVec :: new( ) ,
1695
+ Isize => single( IntSimplifiedType ( IntTy :: Isize ) ) ,
1696
+ I8 => single( IntSimplifiedType ( IntTy :: I8 ) ) ,
1697
+ I16 => single( IntSimplifiedType ( IntTy :: I16 ) ) ,
1698
+ I32 => single( IntSimplifiedType ( IntTy :: I32 ) ) ,
1699
+ I64 => single( IntSimplifiedType ( IntTy :: I64 ) ) ,
1700
+ I128 => single( IntSimplifiedType ( IntTy :: I128 ) ) ,
1701
+ Usize => single( UintSimplifiedType ( UintTy :: Usize ) ) ,
1702
+ U8 => single( UintSimplifiedType ( UintTy :: U8 ) ) ,
1703
+ U16 => single( UintSimplifiedType ( UintTy :: U16 ) ) ,
1704
+ U32 => single( UintSimplifiedType ( UintTy :: U32 ) ) ,
1705
+ U64 => single( UintSimplifiedType ( UintTy :: U64 ) ) ,
1706
+ U128 => single( UintSimplifiedType ( UintTy :: U128 ) ) ,
1707
+ F32 => single( FloatSimplifiedType ( FloatTy :: F32 ) ) ,
1708
+ F64 => single( FloatSimplifiedType ( FloatTy :: F64 ) ) ,
1709
+ Str => single( StrSimplifiedType ) ,
1710
+ Bool => single( BoolSimplifiedType ) ,
1711
+ Char => single( CharSimplifiedType ) ,
1712
+ Array => single( ArraySimplifiedType ) ,
1713
+ Slice => single( SliceSimplifiedType ) ,
1714
+ // FIXME: If we ever add an inherent impl for tuples
1715
+ // with different lengths, they won't show in rustdoc.
1716
+ //
1717
+ // Either manually update this arrayvec at this point
1718
+ // or start with a more complex refactoring.
1719
+ Tuple => [ TupleSimplifiedType ( 2 ) , TupleSimplifiedType ( 3 ) ] . into( ) ,
1720
+ Unit => single( TupleSimplifiedType ( 0 ) ) ,
1721
+ RawPointer => [ PtrSimplifiedType ( Mutability :: Not ) , PtrSimplifiedType ( Mutability :: Mut ) ] . into( ) ,
1722
+ Reference => [ RefSimplifiedType ( Mutability :: Not ) , RefSimplifiedType ( Mutability :: Mut ) ] . into( ) ,
1723
+ // FIXME: This will be wrong if we ever add inherent impls
1724
+ // for function pointers.
1739
1725
Fn => ArrayVec :: new( ) ,
1740
- Never => ArrayVec :: new ( ) ,
1726
+ Never => single ( NeverSimplifiedType ) ,
1741
1727
}
1742
1728
} )
1743
1729
}
1744
1730
1731
+ crate fn impls < ' tcx > ( & self , tcx : TyCtxt < ' tcx > ) -> impl Iterator < Item = DefId > + ' tcx {
1732
+ Self :: simplified_types ( )
1733
+ . get ( self )
1734
+ . into_iter ( )
1735
+ . flatten ( )
1736
+ . flat_map ( move |& simp| tcx. incoherent_impls ( simp) )
1737
+ . copied ( )
1738
+ }
1739
+
1740
+ crate fn all_impls ( tcx : TyCtxt < ' _ > ) -> impl Iterator < Item = DefId > + ' _ {
1741
+ Self :: simplified_types ( )
1742
+ . values ( )
1743
+ . flatten ( )
1744
+ . flat_map ( move |& simp| tcx. incoherent_impls ( simp) )
1745
+ . copied ( )
1746
+ }
1747
+
1745
1748
crate fn as_sym ( & self ) -> Symbol {
1746
1749
use PrimitiveType :: * ;
1747
1750
match self {
0 commit comments