@@ -393,6 +393,24 @@ impl TypeEngine {
393
393
)
394
394
}
395
395
396
+ /// Inserts a new [TypeInfo::TypeParam] into the [TypeEngine] and returns its [TypeId].
397
+ ///
398
+ /// [TypeInfo::TypeParam] is an always replaceable type and the method
399
+ /// guarantees that a new (or unused) [TypeId] will be returned on every
400
+ /// call.
401
+ pub ( crate ) fn new_type_param ( & self , type_parameter : TypeParameter ) -> TypeId {
402
+ self . new_type_param_impl ( TypeInfo :: TypeParam ( type_parameter) )
403
+ }
404
+
405
+ fn new_type_param_impl ( & self , type_param : TypeInfo ) -> TypeId {
406
+ let source_id = self . get_type_parameter_fallback_source_id ( & type_param) ;
407
+ let tsi = TypeSourceInfo {
408
+ type_info : type_param. into ( ) ,
409
+ source_id,
410
+ } ;
411
+ TypeId :: new ( self . slab . insert ( tsi) )
412
+ }
413
+
396
414
/// Inserts a new [TypeInfo::Placeholder] into the [TypeEngine] and returns its [TypeId].
397
415
///
398
416
/// [TypeInfo::Placeholder] is an always replaceable type and the method
@@ -831,6 +849,7 @@ impl TypeEngine {
831
849
TypeInfo :: Unknown => return self . new_unknown ( ) ,
832
850
TypeInfo :: Numeric => return self . new_numeric ( ) ,
833
851
TypeInfo :: Placeholder ( _) => return self . new_placeholder_impl ( ty) ,
852
+ TypeInfo :: TypeParam ( _) => return self . new_type_param_impl ( ty) ,
834
853
TypeInfo :: UnknownGeneric { .. } => return self . new_unknown_generic_impl ( ty) ,
835
854
_ => ( ) ,
836
855
}
@@ -1000,9 +1019,6 @@ impl TypeEngine {
1000
1019
| TypeInfo :: Contract
1001
1020
| TypeInfo :: Never => false ,
1002
1021
1003
- // Note that `TypeParam` is currently not used at all.
1004
- TypeInfo :: TypeParam ( _) => false ,
1005
-
1006
1022
// `StringArray`s are not changeable. We will have one shared
1007
1023
// `TypeInfo` instance for every string size. Note that in case
1008
1024
// of explicitly defined string arrays, e.g. in the storage or type ascriptions
@@ -1015,6 +1031,7 @@ impl TypeEngine {
1015
1031
TypeInfo :: Unknown
1016
1032
| TypeInfo :: Numeric
1017
1033
| TypeInfo :: Placeholder ( _)
1034
+ | TypeInfo :: TypeParam ( _)
1018
1035
| TypeInfo :: UnknownGeneric { .. } => true ,
1019
1036
1020
1037
// The `ContractCaller` can be replaceable, and thus, sometimes changeable.
@@ -1104,8 +1121,7 @@ impl TypeEngine {
1104
1121
| TypeInfo :: Never
1105
1122
| TypeInfo :: Unknown
1106
1123
| TypeInfo :: Numeric
1107
- | TypeInfo :: Contract
1108
- | TypeInfo :: TypeParam ( _) => false ,
1124
+ | TypeInfo :: Contract => false ,
1109
1125
1110
1126
// Types that are always distinguishable because they have the `name: Ident`.
1111
1127
//
@@ -1232,6 +1248,7 @@ impl TypeEngine {
1232
1248
// The above reasoning for `TypeArgument`s applies also for the `TypeParameter`s.
1233
1249
// We only need to check if the `tp` is annotated.
1234
1250
TypeInfo :: Placeholder ( tp) => tp. is_annotated ( ) ,
1251
+ TypeInfo :: TypeParam ( tp) => tp. is_annotated ( ) ,
1235
1252
1236
1253
// TODO: Improve handling of `TypeInfo::Custom` and `TypeInfo::TraitType`` within the `TypeEngine`:
1237
1254
// https://github.com/FuelLabs/sway/issues/6601
@@ -1718,6 +1735,22 @@ impl TypeEngine {
1718
1735
self . get_source_id_from_type_parameter ( tp)
1719
1736
}
1720
1737
1738
+ fn get_type_parameter_fallback_source_id ( & self , type_param : & TypeInfo ) -> Option < SourceId > {
1739
+ // `TypeInfo::TypeParam` is an always replaceable type and we know we will
1740
+ // get a new instance of it in the engine for every trait type parameter occurrence. This means
1741
+ // that it can never happen that instances from different source files point
1742
+ // to the same `TypeSourceInfo`. Therefore, we can safely remove an instance
1743
+ // of a `TypeParam` from the engine if its source file is garbage collected.
1744
+ //
1745
+ // The source file itself is always the one in which the `name` is situated.
1746
+ let TypeInfo :: TypeParam ( tp) = & type_param else {
1747
+ unreachable ! ( "The `placeholder` is checked to be of variant `TypeInfo::TypeParam`." ) ;
1748
+ } ;
1749
+
1750
+ self . get_source_id_from_type_parameter ( tp)
1751
+ }
1752
+
1753
+
1721
1754
fn get_unknown_generic_fallback_source_id ( unknown_generic : & TypeInfo ) -> Option < SourceId > {
1722
1755
// `TypeInfo::UnknownGeneric` is an always replaceable type and we know we will
1723
1756
// get a new instance of it in the engine for every, e.g., "<T1>", "<T2>", etc. occurrence.
0 commit comments