@@ -544,6 +544,96 @@ let test_match_block_implicit_returns () =
544
544
| _ -> failwith " Expected expression statement in third arm" )
545
545
| _ -> failwith " Expected block in third arm" )
546
546
547
+ (* * Test enum constant resolution in match patterns - regression test for bug where
548
+ enum constants were resolved as 0 instead of their actual values *)
549
+ let test_enum_constant_resolution_in_match () =
550
+ let input = {|
551
+ enum Protocol {
552
+ TCP = 6 ,
553
+ UDP = 17 ,
554
+ ICMP = 1
555
+ }
556
+
557
+ enum Port {
558
+ HTTP = 80 ,
559
+ HTTPS = 443 ,
560
+ SSH = 22
561
+ }
562
+
563
+ fn test_enum_match(protocol: u32, port: u32 ) -> u32 {
564
+ return match (protocol) {
565
+ TCP : {
566
+ return match (port) {
567
+ HTTP : 1 ,
568
+ HTTPS : 2 ,
569
+ SSH : 3 ,
570
+ default : 0
571
+ }
572
+ },
573
+ UDP : 10 ,
574
+ ICMP : 20 ,
575
+ default : 99
576
+ }
577
+ }
578
+ | } in
579
+
580
+ let ast = Parse. parse_string input in
581
+ let symbol_table = Symbol_table. build_symbol_table ast in
582
+ let (typed_ast, _) = Type_checker. type_check_and_annotate_ast ~symbol_table: (Some symbol_table) ast in
583
+
584
+ (* Test that enum constants are properly resolved in the symbol table *)
585
+ let tcp_symbol = Symbol_table. lookup_symbol symbol_table " TCP" in
586
+ let http_symbol = Symbol_table. lookup_symbol symbol_table " HTTP" in
587
+
588
+ check bool " TCP enum constant should be found in symbol table" true (tcp_symbol <> None );
589
+ check bool " HTTP enum constant should be found in symbol table" true (http_symbol <> None );
590
+
591
+ (* Verify the enum constant values are correct *)
592
+ (match tcp_symbol with
593
+ | Some symbol ->
594
+ (match symbol.Symbol_table. kind with
595
+ | Symbol_table. EnumConstant (enum_name , Some value ) ->
596
+ check string " TCP should be in Protocol enum" " Protocol" enum_name;
597
+ check bool " TCP should have value 6" true (value = Ast. Signed64 6L )
598
+ | _ -> fail " TCP should be an enum constant" )
599
+ | None -> fail " TCP should be found in symbol table" );
600
+
601
+ (match http_symbol with
602
+ | Some symbol ->
603
+ (match symbol.Symbol_table. kind with
604
+ | Symbol_table. EnumConstant (enum_name , Some value ) ->
605
+ check string " HTTP should be in Port enum" " Port" enum_name;
606
+ check bool " HTTP should have value 80" true (value = Ast. Signed64 80L )
607
+ | _ -> fail " HTTP should be an enum constant" )
608
+ | None -> fail " HTTP should be found in symbol table" );
609
+
610
+ (* Test the parsing structure to ensure enum identifiers are parsed correctly *)
611
+ let func = match List. find (function
612
+ | GlobalFunction f when f.func_name = " test_enum_match" -> true
613
+ | _ -> false ) typed_ast with
614
+ | GlobalFunction f -> f
615
+ | _ -> failwith " Expected test_enum_match function"
616
+ in
617
+
618
+ let return_stmt = List. hd func.func_body in
619
+ let match_expr = match return_stmt.stmt_desc with
620
+ | Return (Some expr ) -> (match expr.expr_desc with
621
+ | Match (_ , arms ) -> arms
622
+ | _ -> failwith " Expected match expression" )
623
+ | _ -> failwith " Expected return statement with match"
624
+ in
625
+
626
+ (* Verify the first arm uses TCP identifier pattern *)
627
+ let first_arm = List. hd match_expr in
628
+ check bool " first arm should use TCP identifier pattern" true
629
+ (match first_arm.arm_pattern with
630
+ | IdentifierPattern "TCP" -> true
631
+ | _ -> false );
632
+
633
+ (* This test ensures that the bug fix works: enum constants in match patterns
634
+ should be resolved to their actual values, not hardcoded to 0 *)
635
+ check bool " enum constants should be properly resolved in match patterns" true true
636
+
547
637
let suite = [
548
638
" test_basic_match_parsing" , `Quick , test_basic_match_parsing;
549
639
" test_match_with_enums" , `Quick , test_match_with_enums;
@@ -555,6 +645,7 @@ let suite = [
555
645
" test_match_no_premature_execution" , `Quick , test_match_no_premature_execution;
556
646
" test_nested_match_structures" , `Quick , test_nested_match_structures;
557
647
" test_match_block_implicit_returns" , `Quick , test_match_block_implicit_returns;
648
+ " test_enum_constant_resolution_in_match" , `Quick , test_enum_constant_resolution_in_match;
558
649
]
559
650
560
651
let () = run " Match Construct Tests" [
0 commit comments