@@ -540,6 +540,57 @@ enum edge_cases {
540540 ] in
541541 check (list (pair string (option int ))) " edge case variants" expected enum_def
542542
543+ (* * Test enum as array index *)
544+ let test_enum_array_index () =
545+ let source = {|
546+ enum Protocol {
547+ TCP = 6 ,
548+ UDP = 17 ,
549+ ICMP = 1
550+ }
551+
552+ map< Protocol , u32> protocol_stats : PercpuArray (32 )
553+
554+ @ helper
555+ fn test_enum_index () -> u32 {
556+ var proto = TCP
557+ var count = protocol_stats[proto]
558+ if (count != none) {
559+ return count
560+ } else {
561+ return 0
562+ }
563+ }
564+
565+ @ xdp fn packet_handler (ctx : * xdp_md) -> xdp_action {
566+ return XDP_PASS
567+ }
568+ |} in
569+
570+ try
571+ (* Parse the source *)
572+ let ast = parse_string source in
573+
574+ (* Build symbol table with XDP builtin types *)
575+ let symbol_table = Test_utils.Helpers. create_test_symbol_table ast in
576+
577+ (* Type check the AST *)
578+ let _typed_ast = type_check_and_annotate_ast ~symbol_table: (Some symbol_table) ast in
579+
580+ (* If we reach here, type checking succeeded *)
581+ check bool " enum array index type checking passes" true true
582+ with
583+ | Type_error (msg , _ ) when String. contains msg 'A' && String. contains msg 'r' ->
584+ (* If we get "Array index must be integer type" error, the test fails *)
585+ check bool (" enum array index should be allowed: " ^ msg) false true
586+ | Type_error (_ , _ ) ->
587+ (* Other type errors are acceptable for this test *)
588+ check bool " enum array index type checking passes" true true
589+ | Parse_error (msg , _ ) ->
590+ check bool (" parse error: " ^ msg) false true
591+ | e ->
592+ check bool (" unexpected error: " ^ Printexc. to_string e) false true
593+
543594(* * Main test suite *)
544595let () =
545596 run " Enum Tests" [
@@ -553,6 +604,7 @@ let () =
553604 " type_checking" , [
554605 test_case " enum type unification" `Quick test_enum_type_checking;
555606 test_case " enum expressions" `Quick test_enum_expressions;
607+ test_case " enum as array index" `Quick test_enum_array_index;
556608 ];
557609 " code_generation" , [
558610 test_case " enum C code generation" `Quick test_enum_code_generation;
0 commit comments