@@ -1194,13 +1194,13 @@ impl<'ast> ast::visit::Visit<'ast> for AddAnnotations<'_> {
1194
1194
}
1195
1195
1196
1196
fn visit_typed_function ( & mut self , fun : & ' ast ast:: TypedFunction ) {
1197
- self . printer . clear_type_variables ( ) ;
1198
-
1199
1197
// Since type variable names are local to definitions, any type variables
1200
1198
// in other parts of the module shouldn't affect what we print for the
1201
1199
// annotations of this functions. The only variables which cannot clash
1202
1200
// are ones defined in the signature of this function, which we register
1203
- // when we visit the parameters of this function below.
1201
+ // when we visit the parameters of this function inside `collect_type_variables`.
1202
+ self . printer . clear_type_variables ( ) ;
1203
+ collect_type_variables ( & mut self . printer , fun) ;
1204
1204
1205
1205
ast:: visit:: visit_typed_function ( self , fun) ;
1206
1206
@@ -1240,12 +1240,6 @@ impl<'ast> ast::visit::Visit<'ast> for AddAnnotations<'_> {
1240
1240
}
1241
1241
}
1242
1242
1243
- fn visit_type_ast_var ( & mut self , _location : & ' ast SrcSpan , name : & ' ast EcoString ) {
1244
- // Register this type variable so that we don't duplicate names when
1245
- // adding annotations.
1246
- self . printer . register_type_variable ( name. clone ( ) ) ;
1247
- }
1248
-
1249
1243
fn visit_typed_expr_fn (
1250
1244
& mut self ,
1251
1245
location : & ' ast SrcSpan ,
@@ -1340,6 +1334,24 @@ impl<'a> AddAnnotations<'a> {
1340
1334
}
1341
1335
}
1342
1336
1337
+ struct TypeVariableCollector < ' a , ' b > {
1338
+ printer : & ' a mut Printer < ' b > ,
1339
+ }
1340
+
1341
+ /// Collect type variables defined within a function and register them for a
1342
+ /// `Printer`
1343
+ fn collect_type_variables ( printer : & mut Printer < ' _ > , function : & ast:: TypedFunction ) {
1344
+ TypeVariableCollector { printer } . visit_typed_function ( function) ;
1345
+ }
1346
+
1347
+ impl < ' ast , ' a , ' b > ast:: visit:: Visit < ' ast > for TypeVariableCollector < ' a , ' b > {
1348
+ fn visit_type_ast_var ( & mut self , _location : & ' ast SrcSpan , name : & ' ast EcoString ) {
1349
+ // Register this type variable so that we don't duplicate names when
1350
+ // adding annotations.
1351
+ self . printer . register_type_variable ( name. clone ( ) ) ;
1352
+ }
1353
+ }
1354
+
1343
1355
pub struct QualifiedConstructor < ' a > {
1344
1356
import : & ' a Import < EcoString > ,
1345
1357
used_name : EcoString ,
@@ -1411,34 +1423,6 @@ impl<'a> QualifiedToUnqualifiedImportFirstPass<'a> {
1411
1423
}
1412
1424
1413
1425
impl < ' ast > ast:: visit:: Visit < ' ast > for QualifiedToUnqualifiedImportFirstPass < ' ast > {
1414
- fn visit_typed_expr_fn (
1415
- & mut self ,
1416
- location : & ' ast SrcSpan ,
1417
- type_ : & ' ast Arc < Type > ,
1418
- kind : & ' ast FunctionLiteralKind ,
1419
- arguments : & ' ast [ TypedArg ] ,
1420
- body : & ' ast Vec1 < TypedStatement > ,
1421
- return_annotation : & ' ast Option < ast:: TypeAst > ,
1422
- ) {
1423
- for argument in arguments {
1424
- if let Some ( annotation) = & argument. annotation {
1425
- self . visit_type_ast ( annotation) ;
1426
- }
1427
- }
1428
- if let Some ( return_) = return_annotation {
1429
- self . visit_type_ast ( return_) ;
1430
- }
1431
- ast:: visit:: visit_typed_expr_fn (
1432
- self ,
1433
- location,
1434
- type_,
1435
- kind,
1436
- arguments,
1437
- body,
1438
- return_annotation,
1439
- ) ;
1440
- }
1441
-
1442
1426
fn visit_type_ast_constructor (
1443
1427
& mut self ,
1444
1428
location : & ' ast SrcSpan ,
@@ -1637,34 +1621,6 @@ impl<'a> QualifiedToUnqualifiedImportSecondPass<'a> {
1637
1621
}
1638
1622
1639
1623
impl < ' ast > ast:: visit:: Visit < ' ast > for QualifiedToUnqualifiedImportSecondPass < ' ast > {
1640
- fn visit_typed_expr_fn (
1641
- & mut self ,
1642
- location : & ' ast SrcSpan ,
1643
- type_ : & ' ast Arc < Type > ,
1644
- kind : & ' ast FunctionLiteralKind ,
1645
- arguments : & ' ast [ TypedArg ] ,
1646
- body : & ' ast Vec1 < TypedStatement > ,
1647
- return_annotation : & ' ast Option < ast:: TypeAst > ,
1648
- ) {
1649
- for argument in arguments {
1650
- if let Some ( annotation) = & argument. annotation {
1651
- self . visit_type_ast ( annotation) ;
1652
- }
1653
- }
1654
- if let Some ( return_) = return_annotation {
1655
- self . visit_type_ast ( return_) ;
1656
- }
1657
- ast:: visit:: visit_typed_expr_fn (
1658
- self ,
1659
- location,
1660
- type_,
1661
- kind,
1662
- arguments,
1663
- body,
1664
- return_annotation,
1665
- ) ;
1666
- }
1667
-
1668
1624
fn visit_type_ast_constructor (
1669
1625
& mut self ,
1670
1626
location : & ' ast SrcSpan ,
@@ -1869,34 +1825,6 @@ impl<'a> UnqualifiedToQualifiedImportFirstPass<'a> {
1869
1825
}
1870
1826
1871
1827
impl < ' ast > ast:: visit:: Visit < ' ast > for UnqualifiedToQualifiedImportFirstPass < ' ast > {
1872
- fn visit_typed_expr_fn (
1873
- & mut self ,
1874
- location : & ' ast SrcSpan ,
1875
- type_ : & ' ast Arc < Type > ,
1876
- kind : & ' ast FunctionLiteralKind ,
1877
- arguments : & ' ast [ TypedArg ] ,
1878
- body : & ' ast Vec1 < TypedStatement > ,
1879
- return_annotation : & ' ast Option < ast:: TypeAst > ,
1880
- ) {
1881
- for argument in arguments {
1882
- if let Some ( annotation) = & argument. annotation {
1883
- self . visit_type_ast ( annotation) ;
1884
- }
1885
- }
1886
- if let Some ( return_) = return_annotation {
1887
- self . visit_type_ast ( return_) ;
1888
- }
1889
- ast:: visit:: visit_typed_expr_fn (
1890
- self ,
1891
- location,
1892
- type_,
1893
- kind,
1894
- arguments,
1895
- body,
1896
- return_annotation,
1897
- ) ;
1898
- }
1899
-
1900
1828
fn visit_type_ast_constructor (
1901
1829
& mut self ,
1902
1830
location : & ' ast SrcSpan ,
@@ -2073,47 +2001,6 @@ impl<'a> UnqualifiedToQualifiedImportSecondPass<'a> {
2073
2001
}
2074
2002
2075
2003
impl < ' ast > ast:: visit:: Visit < ' ast > for UnqualifiedToQualifiedImportSecondPass < ' ast > {
2076
- fn visit_typed_expr_fn (
2077
- & mut self ,
2078
- location : & ' ast SrcSpan ,
2079
- type_ : & ' ast Arc < Type > ,
2080
- kind : & ' ast FunctionLiteralKind ,
2081
- arguments : & ' ast [ TypedArg ] ,
2082
- body : & ' ast Vec1 < TypedStatement > ,
2083
- return_annotation : & ' ast Option < ast:: TypeAst > ,
2084
- ) {
2085
- for argument in arguments {
2086
- if let Some ( annotation) = & argument. annotation {
2087
- self . visit_type_ast ( annotation) ;
2088
- }
2089
- }
2090
- if let Some ( return_) = return_annotation {
2091
- self . visit_type_ast ( return_) ;
2092
- }
2093
- ast:: visit:: visit_typed_expr_fn (
2094
- self ,
2095
- location,
2096
- type_,
2097
- kind,
2098
- arguments,
2099
- body,
2100
- return_annotation,
2101
- ) ;
2102
- }
2103
-
2104
- fn visit_typed_function ( & mut self , fun : & ' ast ast:: TypedFunction ) {
2105
- for arg in & fun. arguments {
2106
- if let Some ( annotation) = & arg. annotation {
2107
- self . visit_type_ast ( annotation) ;
2108
- }
2109
- }
2110
-
2111
- if let Some ( return_annotation) = & fun. return_annotation {
2112
- self . visit_type_ast ( return_annotation) ;
2113
- }
2114
- ast:: visit:: visit_typed_function ( self , fun) ;
2115
- }
2116
-
2117
2004
fn visit_type_ast_constructor (
2118
2005
& mut self ,
2119
2006
location : & ' ast SrcSpan ,
0 commit comments