Skip to content

Commit c66322e

Browse files
committed
Refactor array access type checking to ensure index is either an integer type or an enum, improving clarity and consistency in type validation.
1 parent c216721 commit c66322e

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

src/type_checker.ml

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -715,13 +715,6 @@ and type_check_function_pointer_call ctx typed_callee typed_args arg_types pos =
715715
and type_check_array_access ctx arr idx pos =
716716
let typed_idx = type_check_expression ctx idx in
717717

718-
(* Index must be integer type, enum *)
719-
let resolved_idx_type = resolve_user_type ctx typed_idx.texpr_type in
720-
(match resolved_idx_type with
721-
| U8 | U16 | U32 | U64 | I8 | I16 | I32 | I64 -> ()
722-
| Enum _ -> () (* Enums are compatible with integers for array indexing *)
723-
| _ -> type_error "Array index must be integer type" pos);
724-
725718
(* Check if this is map access first *)
726719
(match arr.expr_desc with
727720
| Identifier map_name when Hashtbl.mem ctx.maps map_name ->
@@ -738,7 +731,13 @@ and type_check_array_access ctx arr idx pos =
738731
{ texpr_desc = TArrayAccess (typed_arr, typed_idx); texpr_type = map_decl.value_type; texpr_pos = pos }
739732
| None -> type_error ("Map key type mismatch") pos)
740733
| _ ->
741-
(* Regular array access *)
734+
(* Regular array access - index must be integer type or enum *)
735+
let resolved_idx_type = resolve_user_type ctx typed_idx.texpr_type in
736+
(match resolved_idx_type with
737+
| U8 | U16 | U32 | U64 | I8 | I16 | I32 | I64 -> ()
738+
| Enum _ -> () (* Enums are compatible with integers for array indexing *)
739+
| _ -> type_error "Array index must be integer type" pos);
740+
742741
let typed_arr = type_check_expression ctx arr in
743742
(match typed_arr.texpr_type with
744743
| Array (element_type, _) ->

0 commit comments

Comments
 (0)