@@ -190,6 +190,37 @@ enum TypeRootFilter {
190
190
TraitType ( String ) ,
191
191
}
192
192
193
+ #[ derive( Clone , Hash , Eq , PartialEq , Debug ) ]
194
+ enum TypeFilter {
195
+ Unknown ,
196
+ Never ,
197
+ Placeholder ,
198
+ TypeParam ( usize ) ,
199
+ StringSlice ,
200
+ StringArray ( usize ) ,
201
+ U8 ,
202
+ U16 ,
203
+ U32 ,
204
+ U64 ,
205
+ U256 ,
206
+ Bool ,
207
+ Custom ( String ) ,
208
+ B256 ,
209
+ Contract ,
210
+ ErrorRecovery ,
211
+ Tuple ( usize , Vec < Box < TypeFilter > > ) ,
212
+ Enum ( ParsedDeclId < EnumDeclaration > , Vec < Box < TypeFilter > > ) ,
213
+ Struct ( ParsedDeclId < StructDeclaration > , Vec < Box < TypeFilter > > ) ,
214
+ ContractCaller ( String ) ,
215
+ Array ( usize , Box < TypeFilter > ) ,
216
+ Storage ,
217
+ RawUntypedPtr ,
218
+ RawUntypedSlice ,
219
+ Ptr ( Box < TypeFilter > ) ,
220
+ Slice ( Box < TypeFilter > ) ,
221
+ TraitType ( String ) ,
222
+ }
223
+
193
224
/// Map holding trait implementations for types.
194
225
///
195
226
/// Note: "impl self" blocks are considered traits and are stored in the
@@ -198,7 +229,7 @@ enum TypeRootFilter {
198
229
pub ( crate ) struct TraitMap {
199
230
trait_impls : TraitImpls ,
200
231
satisfied_cache : im:: HashSet < u64 > ,
201
- insert_for_type_cache : im:: HashMap < TypeRootFilter , im:: Vector < TypeId > > ,
232
+ insert_for_type_cache : im:: HashMap < TypeFilter , im:: Vector < TypeId > > ,
202
233
}
203
234
204
235
pub ( crate ) enum IsImplSelf {
@@ -542,7 +573,7 @@ impl TraitMap {
542
573
let trait_map = TraitMap {
543
574
trait_impls,
544
575
satisfied_cache : im:: HashSet :: default ( ) ,
545
- insert_for_type_cache : im:: HashMap :: < TypeRootFilter , im:: Vector < TypeId > > :: new ( ) ,
576
+ insert_for_type_cache : im:: HashMap :: < TypeFilter , im:: Vector < TypeId > > :: new ( ) ,
546
577
} ;
547
578
548
579
self . extend ( trait_map, engines) ;
@@ -640,7 +671,7 @@ impl TraitMap {
640
671
type_id : TypeId ,
641
672
code_block_first_pass : CodeBlockFirstPass ,
642
673
) {
643
- let root_filter = TraitMap :: get_type_root_filter ( engines, type_id) ;
674
+ let root_filter = TraitMap :: get_type_filter ( engines, type_id) ;
644
675
if let Some ( values) = self . insert_for_type_cache . get_mut ( & root_filter) {
645
676
let unify_checker = UnifyCheck :: non_dynamic_equality ( engines) . with_unify_ref_mut ( false ) ;
646
677
if values. iter ( ) . any ( |v| unify_checker. check ( type_id, * v) ) {
@@ -1732,4 +1763,89 @@ impl TraitMap {
1732
1763
} => Self :: get_type_root_filter ( engines, referenced_type. type_id ) ,
1733
1764
}
1734
1765
}
1766
+
1767
+ // This is used by the trait map to filter the entries into a HashMap with the return type string as key.
1768
+ fn get_type_filter ( engines : & Engines , type_id : TypeId ) -> TypeFilter {
1769
+ use TypeInfo :: * ;
1770
+ match & * engines. te ( ) . get ( type_id) {
1771
+ Unknown => TypeFilter :: Unknown ,
1772
+ Never => TypeFilter :: Never ,
1773
+ UnknownGeneric { .. } | Placeholder ( _) => TypeFilter :: Placeholder ,
1774
+ TypeParam ( n) => TypeFilter :: TypeParam ( * n) ,
1775
+ StringSlice => TypeFilter :: StringSlice ,
1776
+ StringArray ( x) => TypeFilter :: StringArray ( x. val ( ) ) ,
1777
+ UnsignedInteger ( x) => match x {
1778
+ IntegerBits :: Eight => TypeFilter :: U8 ,
1779
+ IntegerBits :: Sixteen => TypeFilter :: U16 ,
1780
+ IntegerBits :: ThirtyTwo => TypeFilter :: U32 ,
1781
+ IntegerBits :: SixtyFour => TypeFilter :: U64 ,
1782
+ IntegerBits :: V256 => TypeFilter :: U256 ,
1783
+ } ,
1784
+ Boolean => TypeFilter :: Bool ,
1785
+ Custom {
1786
+ qualified_call_path : call_path,
1787
+ ..
1788
+ } => TypeFilter :: Custom ( call_path. call_path . suffix . to_string ( ) ) ,
1789
+ B256 => TypeFilter :: B256 ,
1790
+ Numeric => TypeFilter :: U64 , // u64 is the default
1791
+ Contract => TypeFilter :: Contract ,
1792
+ ErrorRecovery ( _) => TypeFilter :: ErrorRecovery ,
1793
+ Tuple ( fields) => TypeFilter :: Tuple (
1794
+ fields. len ( ) ,
1795
+ fields
1796
+ . iter ( )
1797
+ . map ( |f| Box :: new ( Self :: get_type_filter ( engines, f. type_id ) ) )
1798
+ . collect :: < Vec < _ > > ( ) ,
1799
+ ) ,
1800
+ UntypedEnum ( _) => unreachable ! ( ) ,
1801
+ UntypedStruct ( _) => unreachable ! ( ) ,
1802
+ Enum ( decl_id) => {
1803
+ // TODO Remove unwrap once #6475 is fixed
1804
+ TypeFilter :: Enum (
1805
+ engines. de ( ) . get_parsed_decl_id ( decl_id) . unwrap ( ) ,
1806
+ engines
1807
+ . de ( )
1808
+ . get_enum ( decl_id)
1809
+ . type_parameters
1810
+ . iter ( )
1811
+ . map ( |f| Box :: new ( Self :: get_type_filter ( engines, f. type_id ) ) )
1812
+ . collect :: < Vec < _ > > ( ) ,
1813
+ )
1814
+ }
1815
+ Struct ( decl_id) => {
1816
+ // TODO Remove unwrap once #6475 is fixed
1817
+ TypeFilter :: Struct (
1818
+ engines. de ( ) . get_parsed_decl_id ( decl_id) . unwrap ( ) ,
1819
+ engines
1820
+ . de ( )
1821
+ . get_struct ( decl_id)
1822
+ . type_parameters
1823
+ . iter ( )
1824
+ . map ( |f| Box :: new ( Self :: get_type_filter ( engines, f. type_id ) ) )
1825
+ . collect :: < Vec < _ > > ( ) ,
1826
+ )
1827
+ }
1828
+ ContractCaller { abi_name, .. } => TypeFilter :: ContractCaller ( abi_name. to_string ( ) ) ,
1829
+ Array ( type_argument, length) => TypeFilter :: Array (
1830
+ length. val ( ) ,
1831
+ Box :: new ( Self :: get_type_filter ( engines, type_argument. type_id ) ) ,
1832
+ ) ,
1833
+ Storage { .. } => TypeFilter :: Storage ,
1834
+ RawUntypedPtr => TypeFilter :: RawUntypedPtr ,
1835
+ RawUntypedSlice => TypeFilter :: RawUntypedSlice ,
1836
+ Ptr ( type_argument) => TypeFilter :: Ptr ( Box :: new ( Self :: get_type_filter (
1837
+ engines,
1838
+ type_argument. type_id ,
1839
+ ) ) ) ,
1840
+ Slice ( type_argument) => TypeFilter :: Slice ( Box :: new ( Self :: get_type_filter (
1841
+ engines,
1842
+ type_argument. type_id ,
1843
+ ) ) ) ,
1844
+ Alias { ty, .. } => Self :: get_type_filter ( engines, ty. type_id ) ,
1845
+ TraitType { name, .. } => TypeFilter :: TraitType ( name. to_string ( ) ) ,
1846
+ Ref {
1847
+ referenced_type, ..
1848
+ } => Self :: get_type_filter ( engines, referenced_type. type_id ) ,
1849
+ }
1850
+ }
1735
1851
}
0 commit comments