1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
//! Type checking pass for ReluxScript
use crate::parser::*;
use crate::semantic::{SemanticError, TypeEnv, TypeInfo, types::ast_type_to_type_info};
/// Type checker - validates types throughout the AST
pub struct TypeChecker {
env: TypeEnv,
errors: Vec<SemanticError>,
/// Current function's return type (for return statement checking)
current_return_type: Option<TypeInfo>,
/// Current impl block's target struct name (for resolving `Self`)
current_impl_target: Option<String>,
/// Current plugin's self type (for implicit `self` in visitor methods)
current_plugin_self: Option<TypeInfo>,
}
impl TypeChecker {
pub fn new(env: &TypeEnv) -> Self {
Self {
env: env.clone(),
errors: Vec::new(),
current_return_type: None,
current_impl_target: None,
current_plugin_self: None,
}
}
/// Run type checking
pub fn check(&mut self, program: &Program) -> Result<(), Vec<SemanticError>> {
match &program.decl {
TopLevelDecl::Plugin(plugin) => self.check_plugin(plugin),
TopLevelDecl::Writer(writer) => self.check_writer(writer),
TopLevelDecl::Interface(_) => {} // Interfaces are type declarations, not code
TopLevelDecl::Module(module) => self.check_module(module),
}
// Debug: dump all XPath→Type mappings
self.env.dump_paths();
if self.errors.is_empty() {
Ok(())
} else {
Err(std::mem::take(&mut self.errors))
}
}
/// Get the type environment
pub fn into_env(self) -> TypeEnv {
self.env
}
fn check_plugin(&mut self, plugin: &PluginDecl) {
// Build the synthetic plugin struct type
// The generated code will have `pub struct PluginName { pub state: State }`
let mut plugin_fields = std::collections::HashMap::new();
// Find the State struct in the plugin body
for item in &plugin.body {
if let PluginItem::Struct(s) = item {
if s.name == "State" {
// Found State struct - add it as a field to the plugin struct
let state_fields: std::collections::HashMap<String, TypeInfo> = s.fields.iter()
.map(|field| (field.name.clone(), ast_type_to_type_info(&field.ty)))
.collect();
plugin_fields.insert("state".to_string(), TypeInfo::Struct {
name: "State".to_string(),
fields: state_fields,
});
break;
}
}
}
// Create the plugin struct type and register `self`
let plugin_struct = TypeInfo::Struct {
name: plugin.name.clone(),
fields: plugin_fields,
};
// Register the plugin struct so it can be looked up
self.env.define_struct(plugin.name.clone(), match &plugin_struct {
TypeInfo::Struct { fields, .. } => fields.clone(),
_ => std::collections::HashMap::new(),
});
// Set impl target so `Self` resolves to plugin struct
self.current_impl_target = Some(plugin.name.clone());
// Define `self` with the plugin struct type (wrapped in Ref since visitor methods take &mut self)
let self_type = TypeInfo::Ref {
mutable: true,
inner: Box::new(plugin_struct),
};
self.env.define("self".to_string(), self_type.clone());
// Don't push/pop scope - we want to retain all types for codegen
for item in &plugin.body {
match item {
PluginItem::Function(f) => self.check_function(f),
PluginItem::Impl(impl_block) => {
self.check_impl_block(impl_block);
// Restore `self` to plugin type after impl block
// (impl methods may have redefined `self` as their target type)
self.env.define("self".to_string(), self_type.clone());
}
_ => {}
}
}
self.current_impl_target = None;
}
fn check_writer(&mut self, writer: &WriterDecl) {
// Don't push/pop scope - we want to retain all types for codegen
for item in &writer.body {
match item {
PluginItem::Function(f) => self.check_function(f),
PluginItem::Impl(impl_block) => self.check_impl_block(impl_block),
_ => {}
}
}
}
fn check_module(&mut self, module: &ModuleDecl) {
// Don't push/pop scope - we want to retain all types for codegen
for item in &module.items {
match item {
PluginItem::Function(f) => self.check_function(f),
PluginItem::Impl(impl_block) => self.check_impl_block(impl_block),
_ => {}
}
}
}
fn check_impl_block(&mut self, impl_block: &crate::parser::ImplBlock) {
eprintln!("[TYPE CHECKER] Processing impl block for: {}", impl_block.target);
// Set the current impl target so `Self` can be resolved
self.current_impl_target = Some(impl_block.target.clone());
// Check each method in the impl block
for method in &impl_block.items {
self.check_function(method);
}
// Clear the impl target
self.current_impl_target = None;
}
fn check_function(&mut self, f: &FnDecl) {
let return_type = f
.return_type
.as_ref()
.map(ast_type_to_type_info)
.unwrap_or(TypeInfo::Unit);
self.current_return_type = Some(return_type);
// NOTE: We don't push/pop scope for functions because we want the TypeEnv
// to retain all variable types (including parameters and narrowed types)
// so they're available to the decorator during codegen.
// Define parameters using their XPath
for param in &f.params {
let ty = ast_type_to_type_info(¶m.ty);
// Resolve `Self` to the actual impl struct type
let resolved_ty = self.resolve_self_type(ty);
eprintln!("[TYPE CHECKER] Defining param '{}' at path '{}' with type: {:?}", param.name, param.path, resolved_ty);
// Define at XPath (new canonical way)
self.env.define_at_path(¶m.path, resolved_ty.clone());
// Also define by name (legacy - for backwards compatibility)
self.env.define(param.name.clone(), resolved_ty);
}
// Check body
self.check_block(&f.body);
// Don't pop scope - keep all types for decorator
self.current_return_type = None;
}
/// Resolve `Self` type to the actual impl struct type
fn resolve_self_type(&self, ty: TypeInfo) -> TypeInfo {
match ty {
TypeInfo::AstNode(ref name) if name == "Self" => {
// Replace `Self` with the current impl target struct
if let Some(ref target) = self.current_impl_target {
if let Some(fields) = self.env.get_struct_fields(target) {
TypeInfo::Struct {
name: target.clone(),
fields: fields.clone(),
}
} else {
// Struct not defined - use empty fields
TypeInfo::Struct {
name: target.clone(),
fields: std::collections::HashMap::new(),
}
}
} else {
ty // No impl target, return as-is
}
}
TypeInfo::Ref { mutable, inner } => {
// Recursively resolve Self in reference types
TypeInfo::Ref {
mutable,
inner: Box::new(self.resolve_self_type(*inner)),
}
}
_ => ty,
}
}
fn check_block(&mut self, block: &Block) {
for stmt in &block.stmts {
self.check_stmt(stmt);
}
}
/// Infer the type of a block
/// A block's type is the type of its final expression (if any), or Unit
fn infer_block(&mut self, block: &Block) -> TypeInfo {
println!("DEBUG infer_block: block has {} statements", block.stmts.len());
// Check all statements except potentially the last
for stmt in &block.stmts {
match stmt {
// If any statement is a return/break/continue, the block has type Never
Stmt::Return(_) => {
eprintln!("DEBUG infer_block: found Return, returning Never");
return TypeInfo::Never;
}
Stmt::Break(_) => return TypeInfo::Never,
Stmt::Continue(_) => return TypeInfo::Never,
_ => self.check_stmt(stmt),
}
}
// If the last statement is an expression statement (no semicolon),
// the block evaluates to that expression's type
// If/match statements can also be expressions
if let Some(last_stmt) = block.stmts.last() {
match last_stmt {
Stmt::Expr(expr_stmt) => {
return self.infer_expr(&expr_stmt.expr);
}
Stmt::If(if_stmt) => {
// Treat if statement as expression
let if_expr = Box::new(IfExpr {
condition: if_stmt.condition.clone(),
pattern: if_stmt.pattern.clone(),
then_branch: if_stmt.then_branch.clone(),
else_branch: if_stmt.else_branch.clone(),
span: if_stmt.span,
path: if_stmt.path.clone(),
});
return self.infer_expr(&Expr::If(if_expr));
}
_ => {}
}
}
TypeInfo::Unit
}
fn check_stmt(&mut self, stmt: &Stmt) {
eprintln!("DEBUG check_stmt: checking statement variant {:?}", std::mem::discriminant(stmt));
match stmt {
Stmt::Let(let_stmt) => {
// If there's a type annotation, use it as the expected type for bidirectional inference
let expected_type = let_stmt.ty.as_ref().map(ast_type_to_type_info);
// Infer type from initializer if present
let init_type = if let Some(ref init) = let_stmt.init {
Some(self.infer_expr_with_expected(init, expected_type.as_ref()))
} else {
None
};
if let Some(declared_type) = expected_type {
// Has type annotation
if let Some(ref inferred) = init_type {
if !inferred.is_assignable_to(&declared_type) {
self.errors.push(SemanticError::new(
"RS003",
format!(
"Type mismatch: expected {}, found {}",
declared_type.display_name(),
inferred.display_name()
),
let_stmt.span,
));
}
}
self.define_pattern_in_env(&let_stmt.pattern, declared_type);
} else if let Some(inferred) = init_type {
// No type annotation, use inferred type
self.define_pattern_in_env(&let_stmt.pattern, inferred);
} else {
// No type and no init - use Unknown
self.define_pattern_in_env(&let_stmt.pattern, TypeInfo::Unknown);
}
}
Stmt::Const(const_stmt) => {
let init_type = self.infer_expr(&const_stmt.init);
if let Some(ref type_ann) = const_stmt.ty {
let declared_type = ast_type_to_type_info(type_ann);
if !init_type.is_assignable_to(&declared_type) {
self.errors.push(SemanticError::new(
"RS003",
format!(
"Type mismatch: expected {}, found {}",
declared_type.display_name(),
init_type.display_name()
),
const_stmt.span,
));
}
self.env.define(const_stmt.name.clone(), declared_type);
} else {
self.env.define(const_stmt.name.clone(), init_type);
}
}
Stmt::Expr(expr_stmt) => {
self.infer_expr(&expr_stmt.expr);
}
Stmt::If(if_stmt) => {
let cond_type = self.infer_expr(&if_stmt.condition);
// For if-let, the condition is a pattern match expression, not a boolean
// Only check for bool if there's no pattern
if if_stmt.pattern.is_none() && !matches!(cond_type, TypeInfo::Bool | TypeInfo::Unknown) {
self.errors.push(SemanticError::new(
"RS003",
format!(
"Condition must be bool, found {}",
cond_type.display_name()
),
if_stmt.span,
));
}
// Type narrowing for if-let patterns (positive matches)
// NOTE: Don't push/pop scope here - we want the narrowing to persist
// in semantic_type_env so the decorator can see it
// If there's a pattern, try to narrow the scrutinee type
if let Some(ref pattern) = if_stmt.pattern {
eprintln!("DEBUG narrowing: found if-let pattern, attempting type narrowing");
// Extract the scrutinee variable name (only works for simple identifiers)
if let Some(var_name) = self.extract_simple_scrutinee(&if_stmt.condition) {
// Get the original type before narrowing
let original_type = self.env.lookup(&var_name).cloned();
// Get the narrowed type from the pattern
if let Some(narrowed_type) = self.pattern_to_concrete_type(pattern) {
// Unwrap Ref if the original type is a reference
let unwrapped_original = original_type.as_ref().and_then(|t| {
if let TypeInfo::Ref { inner, .. } = t {
Some(inner.as_ref().clone())
} else {
Some(t.clone())
}
});
// If narrowing from a parent enum, create NarrowedAstNode
let final_type = if let (Some(TypeInfo::AstNode(parent_enum)), TypeInfo::AstNode(variant_type)) = (&unwrapped_original, &narrowed_type) {
// Extract variant name from pattern
let variant_name = match pattern {
Pattern::Variant { name, .. } => name.clone(),
_ => variant_type.clone(),
};
eprintln!("DEBUG narrowing: creating NarrowedAstNode {{ current: {}, parent: {}, variant: {} }}", variant_type, parent_enum, variant_name);
TypeInfo::NarrowedAstNode {
current_type: variant_type.clone(),
parent_enum: parent_enum.clone(),
variant: variant_name,
}
} else {
narrowed_type
};
eprintln!("DEBUG narrowing: shadowing '{}' with type {:?} in then-branch", var_name, final_type);
// Shadow the variable with the narrowed type - this will persist!
self.env.define(var_name, final_type);
}
}
// Also register bindings INSIDE the pattern (e.g., `arg` in `Some(arg)`)
// Get the type of the scrutinee to know what type the bindings should have
let scrutinee_type = self.infer_expr(&if_stmt.condition);
self.register_pattern_bindings(pattern, &scrutinee_type);
}
self.check_block(&if_stmt.then_branch);
// Type narrowing for negated matches with early return
// Pattern: if !matches!(x, Type) { return; }
// After this pattern, we know x IS Type in the continuation
if let Expr::Unary(unary) = &if_stmt.condition {
// Check if it's a NOT operation
if unary.op == UnaryOp::Not {
// Check if the operand is a Matches expression
if let Expr::Matches(matches_expr) = unary.operand.as_ref() {
// Check if the then-branch has an early return
if self.has_early_return(&if_stmt.then_branch) {
eprintln!("DEBUG narrowing: detected !matches!() with early return pattern");
// Extract scrutinee variable name
if let Some(var_name) = self.extract_simple_scrutinee(&matches_expr.scrutinee) {
// Get the narrowed type from the pattern
let narrowed_type = if let Pattern::Variant { name, .. } | Pattern::Ident(name) = &matches_expr.pattern {
if name == "Some" {
// Special case: Some pattern narrows Option<T> to T
eprintln!("DEBUG narrowing: pattern 'Some' detected, extracting inner type from Option");
let scrutinee_type = self.infer_expr(&matches_expr.scrutinee);
// Unwrap Ref if present
let scrutinee_type = match scrutinee_type {
TypeInfo::Ref { inner, .. } => *inner,
other => other,
};
if let TypeInfo::Option(inner) = scrutinee_type {
eprintln!("DEBUG narrowing: extracted inner type from Option: {:?}", inner);
Some(*inner)
} else {
eprintln!("DEBUG narrowing: scrutinee is not Option type, got: {:?}", scrutinee_type);
None
}
} else {
self.pattern_to_concrete_type(&matches_expr.pattern)
}
} else {
self.pattern_to_concrete_type(&matches_expr.pattern)
};
if let Some(narrowed_type) = narrowed_type {
eprintln!("DEBUG narrowing: negated matches with early return - narrowing '{}' to {:?} after if-statement", var_name, narrowed_type);
// Narrow in the CURRENT scope (not a new scope)
// After the early return, we know the variable has the narrowed type
self.env.define(var_name, narrowed_type);
}
}
}
}
}
}
for (cond, block) in &if_stmt.else_if_branches {
let cond_type = self.infer_expr(cond);
if !matches!(cond_type, TypeInfo::Bool | TypeInfo::Unknown) {
self.errors.push(SemanticError::new(
"RS003",
format!(
"Condition must be bool, found {}",
cond_type.display_name()
),
if_stmt.span,
));
}
self.env.push_scope();
self.check_block(block);
self.env.pop_scope();
}
if let Some(ref else_block) = if_stmt.else_branch {
self.env.push_scope();
self.check_block(else_block);
self.env.pop_scope();
}
}
Stmt::Match(match_stmt) => {
let _scrutinee_type = self.infer_expr(&match_stmt.scrutinee);
for arm in &match_stmt.arms {
self.env.push_scope();
self.infer_expr(&arm.body);
self.env.pop_scope();
}
}
Stmt::For(for_stmt) => {
let iter_type = self.infer_expr(&for_stmt.iter);
// Determine element type
let elem_type = match &iter_type {
TypeInfo::Vec(inner) => (**inner).clone(),
TypeInfo::Ref { inner, .. } => {
if let TypeInfo::Vec(elem) = inner.as_ref() {
TypeInfo::Ref {
mutable: false,
inner: elem.clone(),
}
} else {
TypeInfo::Unknown
}
}
_ => TypeInfo::Unknown,
};
self.env.push_scope();
// Define variables from pattern
self.define_pattern_in_env(&for_stmt.pattern, elem_type);
self.check_block(&for_stmt.body);
self.env.pop_scope();
}
Stmt::While(while_stmt) => {
let cond_type = self.infer_expr(&while_stmt.condition);
if !matches!(cond_type, TypeInfo::Bool | TypeInfo::Unknown) {
self.errors.push(SemanticError::new(
"RS003",
format!(
"Condition must be bool, found {}",
cond_type.display_name()
),
while_stmt.span,
));
}
self.env.push_scope();
self.check_block(&while_stmt.body);
self.env.pop_scope();
}
Stmt::Loop(loop_stmt) => {
self.env.push_scope();
self.check_block(&loop_stmt.body);
self.env.pop_scope();
}
Stmt::Return(return_stmt) => {
if let Some(ref value) = return_stmt.value {
// Clone expected type to avoid borrow issues
let expected_return = self.current_return_type.clone();
// Pass expected return type for bidirectional inference
let value_type = self.infer_expr_with_expected(value, expected_return.as_ref());
if let Some(ref expected) = expected_return {
if !value_type.is_assignable_to(expected) {
self.errors.push(SemanticError::new(
"RS003",
format!(
"Return type mismatch: expected {}, found {}",
expected.display_name(),
value_type.display_name()
),
return_stmt.span,
));
}
}
} else if let Some(ref expected) = self.current_return_type {
if !matches!(expected, TypeInfo::Unit | TypeInfo::Unknown) {
self.errors.push(SemanticError::new(
"RS003",
format!(
"Return type mismatch: expected {}, found ()",
expected.display_name()
),
return_stmt.span,
));
}
}
}
Stmt::Break(_) | Stmt::Continue(_) => {}
Stmt::Traverse(traverse_stmt) => {
// Check the target expression
self.infer_expr(&traverse_stmt.target);
// Check the traverse kind
match &traverse_stmt.kind {
crate::parser::TraverseKind::Inline(inline) => {
self.env.push_scope();
// Check state variables
for let_stmt in &inline.state {
let init_type = if let Some(ref init) = let_stmt.init {
Some(self.infer_expr(init))
} else {
None
};
if let Some(ref type_ann) = let_stmt.ty {
let declared_type = ast_type_to_type_info(type_ann);
if let Some(ref inferred) = init_type {
if !inferred.is_assignable_to(&declared_type) {
self.errors.push(SemanticError::new(
"RS003",
format!(
"Type mismatch: expected {}, found {}",
declared_type.display_name(),
inferred.display_name()
),
let_stmt.span,
));
}
}
self.define_pattern_in_env(&let_stmt.pattern, declared_type);
} else if let Some(inferred) = init_type {
self.define_pattern_in_env(&let_stmt.pattern, inferred);
} else {
self.define_pattern_in_env(&let_stmt.pattern, TypeInfo::Unknown);
}
}
// Check methods
for method in &inline.methods {
self.check_function(method);
}
self.env.pop_scope();
}
crate::parser::TraverseKind::Delegated(_visitor_name) => {
// Visitor name validation would happen here
}
}
}
Stmt::Function(fn_decl) => {
// Nested function declaration
// Define the function in the current scope
let param_types: Vec<TypeInfo> = fn_decl.params.iter()
.map(|p| ast_type_to_type_info(&p.ty))
.collect();
let ret_type = fn_decl.return_type.as_ref()
.map(ast_type_to_type_info)
.unwrap_or(TypeInfo::Unit);
let fn_type = TypeInfo::Function {
params: param_types,
ret: Box::new(ret_type),
};
self.env.define(fn_decl.name.clone(), fn_type);
// Type check the function body in a new scope
self.env.push_scope();
// Define parameters
for param in &fn_decl.params {
let param_type = ast_type_to_type_info(¶m.ty);
self.env.define(param.name.clone(), param_type);
}
// Check the body
self.check_block(&fn_decl.body);
self.env.pop_scope();
}
Stmt::Verbatim(_) => {
// Verbatim blocks are opaque to type checking
// No analysis performed on raw code
}
Stmt::CustomPropAssignment(assign) => {
// Check the node and value expressions
let _node_type = self.infer_expr(&assign.node);
let _value_type = self.infer_expr(&assign.value);
// TODO: Implement custom property type tracking
// For now, we just validate the expressions compile
}
Stmt::Unsafe(unsafe_block) => {
// Type check statements inside the unsafe block
for stmt in &unsafe_block.body.stmts {
self.check_stmt(stmt);
}
}
}
}
/// Define variables from a pattern in the current environment
fn define_pattern_in_env(&mut self, pattern: &Pattern, type_info: TypeInfo) {
match pattern {
Pattern::Ident(name) => {
eprintln!("[TYPE ENV DEFINE] Defining '{}' with type {:?}", name, type_info);
self.env.define(name.clone(), type_info);
}
Pattern::Tuple(patterns) => {
// Extract tuple element types if available
match &type_info {
TypeInfo::Tuple(elem_types) => {
// Match each pattern with its corresponding type
for (i, pat) in patterns.iter().enumerate() {
let elem_type = elem_types.get(i)
.cloned()
.unwrap_or(TypeInfo::Unknown);
self.define_pattern_in_env(pat, elem_type);
}
}
_ => {
// If not a tuple type, give all elements Unknown type
for pat in patterns {
self.define_pattern_in_env(pat, TypeInfo::Unknown);
}
}
}
}
Pattern::Array(_) => {
// Array destructuring not yet implemented
}
Pattern::Object(_) => {
// Object destructuring not yet implemented
}
Pattern::Rest(_) => {
// Rest pattern not yet implemented
}
Pattern::Or(patterns) => {
// For OR patterns, all branches must bind the same variables with same types
// For now, just define variables from the first pattern
if let Some(first) = patterns.first() {
self.define_pattern_in_env(first, type_info);
}
}
Pattern::Literal(_) | Pattern::Wildcard => {
// No variables to define
}
Pattern::Struct { .. } => {
// Struct patterns not yet implemented
}
Pattern::Variant { .. } => {
// Variant patterns not yet implemented
}
Pattern::Ref { pattern: inner, .. } => {
// ref pattern - define variables from the inner pattern
// The type remains the same (ref doesn't change the type in our IR)
self.define_pattern_in_env(inner, type_info);
}
}
}
/// Infer the type of an expression
/// `expected` is an optional hint from the context (e.g., struct field type, variable annotation)
fn infer_expr(&mut self, expr: &Expr) -> TypeInfo {
self.infer_expr_with_expected(expr, None)
}
/// Infer expression type with an expected type hint for bidirectional inference
fn infer_expr_with_expected(&mut self, expr: &Expr, expected: Option<&TypeInfo>) -> TypeInfo {
match expr {
Expr::Literal(lit) => match lit {
Literal::String(_) => TypeInfo::Str,
Literal::Int(_) => TypeInfo::I32,
Literal::Float(_) => TypeInfo::F64,
Literal::Bool(_) => TypeInfo::Bool,
Literal::Null => TypeInfo::Null,
Literal::Unit => TypeInfo::Unit,
},
Expr::Ident(ident) => {
let ty = self.env
.lookup(&ident.name)
.cloned()
.unwrap_or(TypeInfo::Unknown);
eprintln!("[TYPE INFER] Ident '{}' has type: {:?}", ident.name, ty);
// Store the resolved type at the XPath for later lookup by decorator
if !ident.path.is_empty() {
self.env.define_at_path(&ident.path, ty.clone());
}
ty
}
Expr::Binary(binary) => {
let left_type = self.infer_expr(&binary.left);
let right_type = self.infer_expr(&binary.right);
match binary.op {
// Comparison operators return bool
BinaryOp::Eq
| BinaryOp::NotEq
| BinaryOp::Lt
| BinaryOp::Gt
| BinaryOp::LtEq
| BinaryOp::GtEq => TypeInfo::Bool,
// Logical operators return bool
BinaryOp::And | BinaryOp::Or => TypeInfo::Bool,
// Arithmetic operators
BinaryOp::Add | BinaryOp::Sub | BinaryOp::Mul | BinaryOp::Div | BinaryOp::Mod => {
// If either is f64, result is f64
if matches!(left_type, TypeInfo::F64) || matches!(right_type, TypeInfo::F64)
{
TypeInfo::F64
} else if matches!(left_type, TypeInfo::Str) {
// String concatenation
TypeInfo::Str
} else {
TypeInfo::I32
}
}
// Null-coalescing: returns the inner type of the Option or the right type
BinaryOp::NullCoalesce => {
// If left is Option<T>, result is T (or right_type if right is also the inner type)
if let TypeInfo::Option(inner) = left_type {
*inner
} else {
// Just use right type as fallback
right_type
}
}
}
}
Expr::Unary(unary) => {
let operand_type = self.infer_expr(&unary.operand);
match unary.op {
UnaryOp::Not => TypeInfo::Bool,
UnaryOp::Neg => operand_type,
UnaryOp::Deref => operand_type.deref().cloned().unwrap_or(TypeInfo::Unknown),
UnaryOp::Ref => TypeInfo::Ref {
mutable: false,
inner: Box::new(operand_type),
},
UnaryOp::RefMut => TypeInfo::Ref {
mutable: true,
inner: Box::new(operand_type),
},
}
}
Expr::Call(call) => {
let callee_type = self.infer_expr(&call.callee);
// Check arguments with expected parameter types for bidirectional inference
match &callee_type {
TypeInfo::Function { params, ret } => {
for (i, arg) in call.args.iter().enumerate() {
let expected_param = params.get(i);
self.infer_expr_with_expected(arg, expected_param);
}
*ret.clone()
}
_ => {
// Method calls on known types
if let Expr::Member(member) = call.callee.as_ref() {
let obj_type = self.infer_expr(&member.object);
return self.infer_method_call(&obj_type, &member.property, &call.args);
}
// Unknown callee - just check arguments without expected types
for arg in &call.args {
self.infer_expr(arg);
}
TypeInfo::Unknown
}
}
}
Expr::Member(member) => {
// Check if this member expression has a narrowed type
// e.g., after `if matches!(node.expr, CallExpression)`, `node.expr` is narrowed
if let Expr::Ident(ident) = member.object.as_ref() {
let member_path = format!("{}.{}", ident.name, member.property);
if let Some(narrowed) = self.env.lookup(&member_path) {
eprintln!("[TYPE INFER] Member '{}' has narrowed type: {}", member_path, narrowed.display_name());
return narrowed.clone();
}
}
let obj_type = self.infer_expr(&member.object);
let field_type = self.get_field_type(&obj_type, &member.property);
// Store the resolved type at the XPath for later lookup by decorator
if !member.path.is_empty() {
eprintln!("[TYPE INFER] Storing member type at path '{}': {}", member.path, field_type.display_name());
self.env.define_at_path(&member.path, field_type.clone());
}
field_type
}
Expr::Index(index) => {
let obj_type = self.infer_expr(&index.object);
self.infer_expr(&index.index);
match obj_type {
TypeInfo::Vec(inner) => *inner,
TypeInfo::HashMap(_, v) => TypeInfo::Option(v),
_ => TypeInfo::Unknown,
}
}
Expr::StructInit(init) => {
// Check field types
if let Some(fields) = self.env.get_struct_fields(&init.name) {
let fields = fields.clone();
for (field_name, value) in &init.fields {
// Pass expected field type for bidirectional inference
let field_expected = fields.get(field_name);
let value_type = self.infer_expr_with_expected(value, field_expected);
if let Some(expected_type) = field_expected {
if !value_type.is_assignable_to(expected_type) {
self.errors.push(SemanticError::new(
"RS003",
format!(
"Field '{}' type mismatch: expected {}, found {}",
field_name,
expected_type.display_name(),
value_type.display_name()
),
init.span,
));
}
}
}
TypeInfo::Struct {
name: init.name.clone(),
fields,
}
} else {
// AST node type
for (_, value) in &init.fields {
self.infer_expr(value);
}
TypeInfo::AstNode(init.name.clone())
}
}
Expr::VecInit(vec_init) => {
if vec_init.elements.is_empty() {
// Use expected type if available (e.g., from struct field or variable annotation)
if let Some(TypeInfo::Vec(inner)) = expected {
TypeInfo::Vec(inner.clone())
} else {
TypeInfo::Vec(Box::new(TypeInfo::Unknown))
}
} else {
// Infer from first element, but could also check against expected
let elem_type = self.infer_expr(&vec_init.elements[0]);
TypeInfo::Vec(Box::new(elem_type))
}
}
Expr::If(if_expr) => {
eprintln!("DEBUG Expr::If: inferring if expression");
self.infer_expr(&if_expr.condition);
// Infer type from the then branch
self.env.push_scope();
let then_type = self.infer_block(&if_expr.then_branch);
eprintln!("DEBUG Expr::If: then_type = {:?}", then_type);
self.env.pop_scope();
// If there's an else branch, infer its type too
if let Some(ref else_block) = if_expr.else_branch {
self.env.push_scope();
let else_type = self.infer_block(else_block);
eprintln!("DEBUG Expr::If: else_type = {:?}", else_type);
self.env.pop_scope();
// If both branches have the same type, use that
// If one is Never (!), use the other branch's type
// Otherwise, use Unit
let result_type = if then_type == else_type {
then_type
} else if matches!(then_type, TypeInfo::Never) {
else_type
} else if matches!(else_type, TypeInfo::Never) {
then_type
} else {
TypeInfo::Unit
};
eprintln!("DEBUG Expr::If: result_type = {:?}", result_type);
result_type
} else {
// No else branch means the if-expression can be skipped entirely
// So it always evaluates to Unit
eprintln!("DEBUG Expr::If: no else branch, returning Unit");
TypeInfo::Unit
}
}
Expr::Match(match_expr) => {
self.infer_expr(&match_expr.scrutinee);
// Infer first arm to establish expected type for other arms
let first_arm_type = if !match_expr.arms.is_empty() {
self.env.push_scope();
let t = self.infer_expr_with_expected(&match_expr.arms[0].body, expected);
self.env.pop_scope();
t
} else {
TypeInfo::Unknown
};
// Clone the type to avoid borrow issues
let arm_expected_owned = if matches!(first_arm_type, TypeInfo::Unknown) {
expected.cloned()
} else {
Some(first_arm_type.clone())
};
for arm in match_expr.arms.iter().skip(1) {
self.env.push_scope();
self.infer_expr_with_expected(&arm.body, arm_expected_owned.as_ref());
self.env.pop_scope();
}
first_arm_type
}
Expr::Closure(closure) => {
self.env.push_scope();
let mut param_count = 0;
for param in &closure.params {
match param {
ClosureParam::Ident(name) => {
let var_type = self.env.fresh_var();
self.env.define(name.clone(), var_type);
param_count += 1;
}
ClosureParam::Tuple(names) => {
for name in names {
let var_type = self.env.fresh_var();
self.env.define(name.clone(), var_type);
}
param_count += 1; // Tuple counts as one parameter
}
ClosureParam::Typed { name, ty } => {
self.env.define(name.clone(), ast_type_to_type_info(ty));
param_count += 1;
}
}
}
let body_type = self.infer_expr(&closure.body);
self.env.pop_scope();
TypeInfo::Function {
params: vec![TypeInfo::Unknown; param_count],
ret: Box::new(body_type),
}
}
Expr::Ref(ref_expr) => {
let inner = self.infer_expr(&ref_expr.expr);
TypeInfo::Ref {
mutable: ref_expr.mutable,
inner: Box::new(inner),
}
}
Expr::Deref(deref_expr) => {
let inner = self.infer_expr(&deref_expr.expr);
inner.deref().cloned().unwrap_or(TypeInfo::Unknown)
}
Expr::Assign(assign) => {
self.infer_expr(&assign.target);
self.infer_expr(&assign.value);
TypeInfo::Unit
}
Expr::CompoundAssign(compound) => {
self.infer_expr(&compound.target);
self.infer_expr(&compound.value);
TypeInfo::Unit
}
Expr::Range(range) => {
if let Some(ref start) = range.start {
self.infer_expr(start);
}
if let Some(ref end) = range.end {
self.infer_expr(end);
}
TypeInfo::Unknown // Range type
}
Expr::Paren(inner) => self.infer_expr(inner),
Expr::Tuple(elements) => {
let element_types: Vec<_> = elements.iter().map(|e| self.infer_expr(e)).collect();
TypeInfo::Tuple(element_types)
}
Expr::Block(block) => {
// Type of a block is the type of its last expression (if any)
self.env.push_scope();
let result_type = self.infer_block(block);
self.env.pop_scope();
result_type
}
Expr::Try(inner) => {
// Type of expr? is the Ok variant of Result<T, E>
let inner_type = self.infer_expr(inner);
// If inner is Result<T, E>, type is T
// For now, just return the inner type (simplified)
inner_type
}
Expr::Matches(_) => {
// matches! macro always returns bool
TypeInfo::Bool
}
Expr::Return(value) => {
// Return expression never produces a value (it diverges)
if let Some(ref expr) = value {
self.infer_expr(expr);
}
TypeInfo::Unit // Never type, but using Unit for now
}
Expr::Break => TypeInfo::Unit, // Diverges
Expr::Continue => TypeInfo::Unit, // Diverges
Expr::RegexCall(regex_call) => {
use crate::parser::RegexMethod;
// Return type depends on method
match regex_call.method {
RegexMethod::Matches => TypeInfo::Bool,
RegexMethod::Find => TypeInfo::Option(Box::new(TypeInfo::Str)),
RegexMethod::FindAll => TypeInfo::Vec(Box::new(TypeInfo::Str)),
RegexMethod::Captures => {
// Return Option<Captures> where Captures has a get(i32) -> Str method
let mut fields = std::collections::HashMap::new();
fields.insert("get".to_string(), TypeInfo::Function {
params: vec![TypeInfo::I32],
ret: Box::new(TypeInfo::Str),
});
TypeInfo::Option(Box::new(TypeInfo::Struct {
name: "Captures".to_string(),
fields,
}))
}
RegexMethod::Replace | RegexMethod::ReplaceAll => TypeInfo::Str,
}
}
Expr::CustomPropAccess(_access) => {
// Custom properties always return Option<T>
// TODO: Track property types and return Option<ActualType>
// For now, return Option<Unknown>
TypeInfo::Option(Box::new(TypeInfo::Unknown))
}
Expr::Path(_path) => {
// Path expressions like std::ptr::null() are Rust stdlib paths
// Type depends on the path - for now treat as Unknown
TypeInfo::Unknown
}
}
}
/// Get the type of a field access
fn get_field_type(&self, obj_type: &TypeInfo, field: &str) -> TypeInfo {
match obj_type {
TypeInfo::Struct { fields, .. } => {
fields.get(field).cloned().unwrap_or(TypeInfo::Unknown)
}
TypeInfo::Ref { inner, .. } => self.get_field_type(inner, field),
TypeInfo::NarrowedAstNode { current_type, .. } => {
// Use the current narrowed type for field lookup
self.get_field_type(&TypeInfo::AstNode(current_type.clone()), field)
}
TypeInfo::AstNode(node_type) => {
// Try to get field type from AST schema
use crate::codegen::type_context::{get_typed_field_mapping, map_reluxscript_to_swc};
eprintln!("[GET FIELD TYPE] node_type='{}', field='{}'", node_type, field);
// Map Babel/ReluxScript type name to SWC type name
let (swc_type, _) = map_reluxscript_to_swc(node_type);
eprintln!("[GET FIELD TYPE] Mapped '{}' -> '{}'", node_type, swc_type);
if let Some(mapping) = get_typed_field_mapping(&swc_type, field) {
eprintln!("[GET FIELD TYPE] Found mapping: result_type_swc='{}'", mapping.result_type_swc);
// Parse the result_type_swc to create proper TypeInfo
let type_str = mapping.result_type_swc;
if type_str.starts_with("Option<") && type_str.ends_with(">") {
// Extract inner type from Option<T>
let inner_str = &type_str[7..type_str.len()-1]; // Remove "Option<" and ">"
let inner_type = if inner_str.starts_with("Box<") && inner_str.ends_with(">") {
// Option<Box<T>>
let inner_inner = &inner_str[4..inner_str.len()-1]; // Remove "Box<" and ">"
Box::new(TypeInfo::AstNode(inner_inner.to_string()))
} else {
Box::new(TypeInfo::AstNode(inner_str.to_string()))
};
return TypeInfo::Option(inner_type);
} else if type_str.starts_with("Vec<") && type_str.ends_with(">") {
let inner_str = &type_str[4..type_str.len()-1];
return TypeInfo::Vec(Box::new(TypeInfo::AstNode(inner_str.to_string())));
} else {
return TypeInfo::AstNode(type_str.to_string());
}
}
// Fallback for unmapped fields
match field {
"name" | "value" | "operator" | "kind" => TypeInfo::Str,
"body" | "params" | "arguments" | "elements" | "properties" => {
TypeInfo::Vec(Box::new(TypeInfo::Unknown))
}
"id" | "init" | "left" | "right" | "object" | "property" | "callee"
| "argument" | "test" | "consequent" | "alternate" => TypeInfo::Unknown,
_ => TypeInfo::Unknown,
}
}
_ => TypeInfo::Unknown,
}
}
/// Infer return type of a method call
fn infer_method_call(&self, obj_type: &TypeInfo, method: &str, _args: &[Expr]) -> TypeInfo {
let result = match (obj_type, method) {
// String methods
(TypeInfo::Str, "clone") => TypeInfo::Str,
(TypeInfo::Str, "len") => TypeInfo::I32,
(TypeInfo::Str, "is_empty") => TypeInfo::Bool,
(TypeInfo::Str, "starts_with" | "ends_with" | "contains") => TypeInfo::Bool,
(TypeInfo::Str, "to_uppercase" | "to_lowercase") => TypeInfo::Str,
(TypeInfo::Str, "chars") => TypeInfo::Vec(Box::new(TypeInfo::Str)),
// Vec methods
(TypeInfo::Vec(inner), "clone") => TypeInfo::Vec(inner.clone()),
(TypeInfo::Vec(_), "len") => TypeInfo::I32,
(TypeInfo::Vec(_), "is_empty") => TypeInfo::Bool,
(TypeInfo::Vec(_), "push") => TypeInfo::Unit,
(TypeInfo::Vec(inner), "pop") => TypeInfo::Option(inner.clone()),
(TypeInfo::Vec(inner), "get") => TypeInfo::Option(Box::new(TypeInfo::Ref {
mutable: false,
inner: inner.clone(),
})),
(TypeInfo::Vec(inner), "iter") => TypeInfo::Vec(inner.clone()),
(TypeInfo::Vec(_), "collect") => TypeInfo::Vec(Box::new(TypeInfo::Unknown)),
// Option methods
(TypeInfo::Option(inner), "unwrap") => (**inner).clone(),
(TypeInfo::Option(inner), "unwrap_or") => (**inner).clone(),
(TypeInfo::Option(inner), "unwrap_or_else") => (**inner).clone(),
(TypeInfo::Option(_), "is_some" | "is_none") => TypeInfo::Bool,
(TypeInfo::Option(inner), "map") => TypeInfo::Option(inner.clone()),
(TypeInfo::Option(inner), "and_then") => TypeInfo::Option(inner.clone()),
// HashMap methods
(TypeInfo::HashMap(_, v), "get") => TypeInfo::Option(Box::new(TypeInfo::Ref {
mutable: false,
inner: v.clone(),
})),
(TypeInfo::HashMap(_, _), "insert") => TypeInfo::Unit,
(TypeInfo::HashMap(_, _), "contains_key") => TypeInfo::Bool,
(TypeInfo::HashMap(_, _), "len") => TypeInfo::I32,
// Reference dereferencing for method calls
(TypeInfo::Ref { inner, .. }, method) => self.infer_method_call(inner, method, _args),
// AST node methods
(TypeInfo::AstNode(_), "clone") => obj_type.clone(),
(TypeInfo::AstNode(_), "visit_children") => TypeInfo::Unit,
_ => TypeInfo::Unknown,
};
eprintln!("[METHOD CALL] {}.{}() -> {:?}", obj_type.display_name(), method, result);
result
}
// ========================================================================
// Type Narrowing Support
// ========================================================================
/// Check if a block ends with an early return statement
fn has_early_return(&self, block: &Block) -> bool {
if let Some(last_stmt) = block.stmts.last() {
matches!(last_stmt, Stmt::Return(_))
} else {
false
}
}
/// Register bindings from a pattern with their appropriate types
/// For example, in `if let Some(x) = opt`, register `x` with the inner type of Option
fn register_pattern_bindings(&mut self, pattern: &Pattern, scrutinee_type: &TypeInfo) {
eprintln!("[PATTERN BINDING DEBUG] pattern={:?}, scrutinee_type={:?}", pattern, scrutinee_type);
match pattern {
Pattern::Ident(name) => {
// Simple binding - use the scrutinee type
eprintln!("[PATTERN BINDING] Registering '{}' with type {:?}", name, scrutinee_type);
self.env.define(name.clone(), scrutinee_type.clone());
}
Pattern::Variant { name, inner } => {
// Variant pattern like Some(x) or JSXElement
if name == "Some" {
// Option unwrapping - extract inner type
if let TypeInfo::Option(inner_type) = scrutinee_type {
if let Some(inner_pattern) = inner {
eprintln!("[PATTERN BINDING] Option::Some pattern, inner type: {:?}", inner_type);
self.register_pattern_bindings(inner_pattern, inner_type);
}
} else if let TypeInfo::Ref { inner: ref_inner, .. } = scrutinee_type {
// Handle &Option<T> case
if let TypeInfo::Option(inner_type) = ref_inner.as_ref() {
if let Some(inner_pattern) = inner {
eprintln!("[PATTERN BINDING] &Option::Some pattern, inner type: {:?}", inner_type);
// Create a reference to the inner type
let inner_ref_type = TypeInfo::Ref {
mutable: false,
inner: inner_type.clone(),
};
self.register_pattern_bindings(inner_pattern, &inner_ref_type);
}
}
}
} else if let Some(inner_pattern) = inner {
// Other variant patterns - the binding gets the narrowed type
if let Some(narrowed_type) = self.pattern_to_concrete_type(pattern) {
self.register_pattern_bindings(inner_pattern, &narrowed_type);
}
}
}
_ => {
// Other patterns - skip for now
}
}
}
/// Extract a simple scrutinee variable name from an expression
/// Returns Some(name) for identifiers, references, or member accesses
fn extract_simple_scrutinee(&self, expr: &Expr) -> Option<String> {
match expr {
Expr::Ident(ident) => {
eprintln!("DEBUG narrowing: extracted scrutinee '{}'", ident.name);
Some(ident.name.clone())
}
Expr::Ref(ref_expr) => {
// Handle &x and &mut x
if let Expr::Ident(ident) = ref_expr.expr.as_ref() {
eprintln!("DEBUG narrowing: extracted scrutinee '{}' from reference", ident.name);
Some(ident.name.clone())
} else {
eprintln!("DEBUG narrowing: reference to non-identifier ({:?}), cannot narrow", ref_expr.expr);
None
}
}
Expr::Member(mem) => {
// Handle member access like node.expr
// Build the full path: object.property
if let Expr::Ident(ident) = mem.object.as_ref() {
let path = format!("{}.{}", ident.name, mem.property);
eprintln!("DEBUG narrowing: extracted scrutinee '{}' from member access", path);
Some(path)
} else {
eprintln!("DEBUG narrowing: member access with non-ident object, cannot narrow");
None
}
}
_ => {
eprintln!("DEBUG narrowing: complex expression (variant {:?}), cannot narrow", std::mem::discriminant(expr));
None
}
}
}
/// Map a pattern variant name to its concrete AST type
/// E.g., "ArrayPattern" -> TypeInfo::AstNode("ArrayPat")
fn pattern_to_concrete_type(&self, pattern: &Pattern) -> Option<TypeInfo> {
// Helper function to map pattern names to SWC types
let map_pattern_name = |name: &str| -> Option<String> {
let concrete_type = match name {
// Expression patterns
"ArrayExpression" => "ArrayLit",
"ArrowFunctionExpression" => "ArrowExpr",
"AssignmentExpression" => "AssignExpr",
"BinaryExpression" => "BinExpr",
"CallExpression" => "CallExpr",
"ConditionalExpression" => "CondExpr",
"FunctionExpression" => "FnExpr",
"Identifier" => "Ident",
"MemberExpression" => "MemberExpr",
"NewExpression" => "NewExpr",
"ObjectExpression" => "ObjectLit",
"SequenceExpression" => "SeqExpr",
"StringLiteral" => "Str",
"NumericLiteral" => "Number",
"BooleanLiteral" => "Bool",
"NullLiteral" => "Null",
"RegExpLiteral" => "Regex",
"TemplateLiteral" => "Tpl",
"UnaryExpression" => "UnaryExpr",
"UpdateExpression" => "UpdateExpr",
// Pattern patterns
"ArrayPattern" => "ArrayPat",
"ObjectPattern" => "ObjectPat",
"ObjectPatternProperty" => "KeyValuePatProp",
"RestElement" => "RestPat",
"AssignmentPattern" => "AssignPat",
// Statement patterns
"BlockStatement" => "BlockStmt",
"ExpressionStatement" => "ExprStmt",
"ReturnStatement" => "ReturnStmt",
"IfStatement" => "IfStmt",
"ForStatement" => "ForStmt",
"WhileStatement" => "WhileStmt",
"BreakStatement" => "BreakStmt",
"ContinueStatement" => "ContinueStmt",
// Other common types
"VariableDeclaration" => "VarDecl",
"FunctionDeclaration" => "FnDecl",
"ClassDeclaration" => "ClassDecl",
// Not a known pattern - check if it's a user-defined type
_ => return None,
};
Some(concrete_type.to_string())
};
match pattern {
Pattern::Variant { name, .. } | Pattern::Ident(name) => {
// Both Variant and Ident patterns can be AST pattern names
if let Some(concrete_type) = map_pattern_name(name) {
eprintln!("DEBUG narrowing: pattern '{}' narrows to AstNode('{}')", name, concrete_type);
Some(TypeInfo::AstNode(concrete_type))
} else {
// Check if it's a user-defined type in the environment
if let Some(ty) = self.env.lookup(name) {
eprintln!("DEBUG narrowing: pattern '{}' found as user type {:?}", name, ty);
Some(ty.clone())
} else {
eprintln!("DEBUG narrowing: pattern '{}' not recognized", name);
None
}
}
}
_ => {
eprintln!("DEBUG narrowing: pattern type not supported for narrowing");
None
}
}
}
}