From 74b8aaf117a1968f9cd4c82555de5e50334e3f96 Mon Sep 17 00:00:00 2001 From: Christoph Knittel Date: Thu, 3 Dec 2026 12:13:25 +0000 Subject: [PATCH] Rename Ast_untagged_variants to Runtime_attributes The module reads and checks every attribute that sets a value's runtime representation (@as, @tag, @unboxed, @notUndefined, @instanceOf, @typeOf, class annotations, and the __proto__ field name check), not just untagged variants. The new name says that and matches Record_runtime and Variant_runtime. No behaviour changes. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Kbf3gL4qcnMmoKbamLEyfN Signed-off-by: Christoph Knittel --- changes/runtime-attributes-module.internal.md | 1 + compiler/backend/lam_compile_primitive.ml | 2 +- compiler/typing/UntaggedVariants.md | 8 +++---- compiler/typing/ast_attributes.ml | 2 +- compiler/typing/ast_checks.ml | 2 +- compiler/typing/includecore.ml | 6 ++--- compiler/typing/printtyp.ml | 15 ++++++------ ...gged_variants.ml => runtime_attributes.ml} | 4 ++++ compiler/typing/transl_recmodule.ml | 7 +++--- compiler/typing/typedecl.ml | 24 +++++++++---------- compiler/typing/typetexp.ml | 2 +- compiler/typing/variant_coercion.ml | 8 +++---- compiler/typing/variant_layout.ml | 2 +- compiler/typing/variant_type_spread.ml | 2 +- tests/ounit_tests/ounit_ast_helper_tests.ml | 2 +- 15 files changed, 44 insertions(+), 43 deletions(-) create mode 100644 changes/runtime-attributes-module.internal.md rename compiler/typing/{ast_untagged_variants.ml => runtime_attributes.ml} (99%) diff --git a/changes/runtime-attributes-module.internal.md b/changes/runtime-attributes-module.internal.md new file mode 100644 index 0000000000..b5c2350715 --- /dev/null +++ b/changes/runtime-attributes-module.internal.md @@ -0,0 +1 @@ +- Rename the compiler module `Ast_untagged_variants` to `Runtime_attributes`, since it handles all attributes that set runtime representation. diff --git a/compiler/backend/lam_compile_primitive.ml b/compiler/backend/lam_compile_primitive.ml index b460ee1243..a22af4af59 100644 --- a/compiler/backend/lam_compile_primitive.ml +++ b/compiler/backend/lam_compile_primitive.ml @@ -566,7 +566,7 @@ let translate output_prefix loc (cxt : Lam_compile_context.t) { name = "::"; num_nonconst = 1; - runtime = Ast_untagged_variants.generated_block_runtime ~name:"::"; + runtime = Runtime_attributes.generated_block_runtime ~name:"::"; }) args | Pmakedict -> ( diff --git a/compiler/typing/UntaggedVariants.md b/compiler/typing/UntaggedVariants.md index 74e4b72970..1359cd6f31 100644 --- a/compiler/typing/UntaggedVariants.md +++ b/compiler/typing/UntaggedVariants.md @@ -66,7 +66,7 @@ a payload and `@tag` on the type do not change the representation. | `@unboxed @tag("kind") type t = TI({x: int}) \| TS(string)` | `TI({x: 1})` | `{x: 1}` | | `@unboxed type t = @as("x") AsX(string) \| AsY(int)` | `AsX("v")` | `"v"` | -- Implementation: `ast_untagged_variants.ml:declared_tag` (a case without +- Implementation: `runtime_attributes.ml:declared_tag` (a case without `@as` is its name, resolved once in the layout), `translcore.ml:transl_exp0` (constructor application). - Tests: `spec_test` rule 2. @@ -79,7 +79,7 @@ constructor. An int literal must fit the compiler's native integer; it is emitted with the 32-bit representation of `int` values, so `@as(4294967297)` is the number `1`. -- Implementation: `ast_untagged_variants.ml:runtime_tag_of_parsetree`, +- Implementation: `runtime_attributes.ml:runtime_tag_of_parsetree`, `process_constructor_tag`. - Tests: fixtures `UntaggedInvalidVariantAsAnnotation.res` ("A variant case annotation @as(...) must be a string, integer, float, @@ -292,7 +292,7 @@ written (Known bugs). `variant_runtime.mli:value_shape`; `variant_layout.ml:declaration_shape`, `resolve_shapes`, `value_shape`, `payload_kind`; `typedecl.ml:check_manifest_annotation`; `typeopt.ml:abstractness`, - `undefinedness`; `ast_untagged_variants.ml:process_runtime_annotation`, + `undefinedness`; `runtime_attributes.ml:process_runtime_annotation`, `unsupported_class`, `check_runtime_annotation`; `includecore.ml:compare_runtime_annotations`; `variant_runtime.ml:Instance`; @@ -600,7 +600,7 @@ payload does not change its representation (rule 2) and is not compared. Type declarations in messages print the `@as` of nullary and tagged cases. - Implementation: `variant_layout.ml:layout_from_type_variant`, - `ast_untagged_variants.ml:process_tag_name`, `check_tag_field_conflicts`; + `runtime_attributes.ml:process_tag_name`, `check_tag_field_conflicts`; `includecore.ml:compare_variants`, `type_declarations`. - Tests: `spec_test` rule 13; fixtures `UntaggedTagFieldNameConflict.res`, `variant_tag_overlaps_with_field.res`, `UntaggedInvalidVariantTagAnnotation.res`, diff --git a/compiler/typing/ast_attributes.ml b/compiler/typing/ast_attributes.ml index 357cf86ede..032dae3896 100644 --- a/compiler/typing/ast_attributes.ml +++ b/compiler/typing/ast_attributes.ml @@ -143,7 +143,7 @@ let single_as ~decode (attrs : t) = let value = decode ~loc payload in Used_attributes.mark_used_attribute attr; st := Some value) - else raise (Ast_untagged_variants.Error (loc, Duplicated_bs_as))) + else raise (Runtime_attributes.Error (loc, Duplicated_bs_as))) attrs; !st diff --git a/compiler/typing/ast_checks.ml b/compiler/typing/ast_checks.ml index af86f0ca4d..2998ed3387 100644 --- a/compiler/typing/ast_checks.ml +++ b/compiler/typing/ast_checks.ml @@ -136,7 +136,7 @@ let iterator : Ast_iterator.iterator = super.label_declaration self lbl); constructor_declaration = (fun self ({pcd_name = {txt; loc}} as ctr) -> - ignore (Ast_untagged_variants.process_constructor_tag ctr); + ignore (Runtime_attributes.process_constructor_tag ctr); (match txt with | "false" | "true" | "()" -> Loc_report.raise_errorf ~loc "%s can not be redefined " txt diff --git a/compiler/typing/includecore.ml b/compiler/typing/includecore.ml index 3e7c1ee2cd..3380c726eb 100644 --- a/compiler/typing/includecore.ml +++ b/compiler/typing/includecore.ml @@ -437,7 +437,7 @@ and compare_constructors ~loc env params1 params2 ~layout1 ~layout2 in if r <> [] then r else - match Ast_untagged_variants.is_nullary_variant cd1.cd_args with + match Runtime_attributes.is_nullary_variant cd1.cd_args with | true -> let tag_type1 = Variant_runtime.constructor_tag layout1 i1 in let tag_type2 = Variant_runtime.constructor_tag layout2 i2 in @@ -555,8 +555,8 @@ let compare_runtime_annotations env id decl1 decl2 = else [ Runtime_annotation - ( Ast_untagged_variants.describe_declared_values declared1, - Ast_untagged_variants.describe_declared_values declared2 ); + ( Runtime_attributes.describe_declared_values declared1, + Runtime_attributes.describe_declared_values declared2 ); ] let type_declarations ?(equality = false) ~loc env name decl1 id decl2 = diff --git a/compiler/typing/printtyp.ml b/compiler/typing/printtyp.ml index 418724ac87..eb0d86dd57 100644 --- a/compiler/typing/printtyp.ml +++ b/compiler/typing/printtyp.ml @@ -895,22 +895,21 @@ and tree_of_type_decl id decl = let flag set attribute = if set then [attribute] else [] in (* A malformed annotation was reported when the declaration was typed *) let stated process = - try process decl.type_attributes - with Ast_untagged_variants.Error _ -> None + try process decl.type_attributes with Runtime_attributes.Error _ -> None in List.concat [ flag (Builtin_attributes.immediate decl.type_attributes) "@immediate"; flag unboxed "@unboxed"; - (match stated Ast_untagged_variants.process_tag_name with + (match stated Runtime_attributes.process_tag_name with | Some name -> [Printf.sprintf "@tag(\"%s\")" (String_literal.encode_js_string name)] | None -> []); (match - stated (Ast_untagged_variants.process_runtime_annotation ?loc:None) + stated (Runtime_attributes.process_runtime_annotation ?loc:None) with | Some annotation -> - [Ast_untagged_variants.runtime_annotation_to_string annotation] + [Runtime_attributes.runtime_annotation_to_string annotation] | None -> []); ] in @@ -932,7 +931,7 @@ and tree_of_constructor ~printing_context ~layout ~position cd = (* The [@as] of a nullary case or of a tagged case with a payload sets its runtime tag; an untagged case is its payload *) let tagged = - Ast_untagged_variants.is_nullary_variant cd.cd_args + Runtime_attributes.is_nullary_variant cd.cd_args || match Variant_runtime.constructor_at layout position with | Block (Tagged _) -> true @@ -1611,7 +1610,7 @@ let print_variant_runtime_representation_issue ppf variant_name @{@as@} payload that has a runtime representation of \ @{%s@}, which is not compatible with the expected @{%s@}." constructor_name (Path.name variant_name) - (Ast_untagged_variants.literal_tag_to_user_visible_string as_payload) + (Runtime_attributes.literal_tag_to_user_visible_string as_payload) (Path.name expected_typename) | Mismatched_unboxed_payload _ -> () | Mismatched_as_payload {constructor_name; expected_typename; as_payload} -> @@ -1626,7 +1625,7 @@ let print_variant_runtime_representation_issue ppf variant_name fprintf ppf "an @{@as@} payload that gives it the runtime type of \ @{%s@}." - (Ast_untagged_variants.literal_tag_to_user_visible_string payload)); + (Runtime_attributes.literal_tag_to_user_visible_string payload)); fprintf ppf "@ That runtime representation is not compatible with the expected \ runtime representation of @{%s@}." diff --git a/compiler/typing/ast_untagged_variants.ml b/compiler/typing/runtime_attributes.ml similarity index 99% rename from compiler/typing/ast_untagged_variants.ml rename to compiler/typing/runtime_attributes.ml index a7ddceed3d..f280843c45 100644 --- a/compiler/typing/ast_untagged_variants.ml +++ b/compiler/typing/runtime_attributes.ml @@ -1,3 +1,7 @@ +(* The attributes that set how values are represented at runtime: @as, @tag + and @unboxed on variants and fields, @notUndefined, @instanceOf, @typeOf and + the class annotations. Reads and checks them, and reports their errors. *) + open Variant_runtime type untagged_error = diff --git a/compiler/typing/transl_recmodule.ml b/compiler/typing/transl_recmodule.ml index f6047bd959..5080862a4c 100644 --- a/compiler/typing/transl_recmodule.ml +++ b/compiler/typing/transl_recmodule.ml @@ -25,7 +25,7 @@ let init_shape modl = { name = "Module"; num_nonconst = 2; - runtime = Ast_untagged_variants.generated_block_runtime ~name:"Module"; + runtime = Runtime_attributes.generated_block_runtime ~name:"Module"; } in let value_tag_info : Lambda.tag_info = @@ -33,7 +33,7 @@ let init_shape modl = { name = "value"; num_nonconst = 2; - runtime = Ast_untagged_variants.generated_block_runtime ~name:"value"; + runtime = Runtime_attributes.generated_block_runtime ~name:"value"; } in let rec init_shape_mod env mty = @@ -57,8 +57,7 @@ let init_shape modl = let init_v = match Ctype.expand_head env ty with | t when is_function t -> - const_constructor - (Ast_untagged_variants.generated_tag ~name:"Function") + const_constructor (Runtime_attributes.generated_tag ~name:"Function") | _ -> raise Not_found in add_name init_v id :: init_shape_struct env rem diff --git a/compiler/typing/typedecl.ml b/compiler/typing/typedecl.ml index 7352f7822b..0378aa2c45 100644 --- a/compiler/typing/typedecl.ml +++ b/compiler/typing/typedecl.ml @@ -187,15 +187,13 @@ let transl_labels ?record_name env closed lbls = (match Record_runtime.extra_as_attribute lbl with | Some loc -> raise - (Ast_untagged_variants.Error - (loc, Ast_untagged_variants.Duplicated_bs_as)) + (Runtime_attributes.Error (loc, Runtime_attributes.Duplicated_bs_as)) | None -> ()); (match runtime_name with | Some {txt; loc} -> - Ast_untagged_variants.check_runtime_field_name ~loc + Runtime_attributes.check_runtime_field_name ~loc (String_literal.string_semantic txt) - | None -> - Ast_untagged_variants.check_runtime_field_name ~loc:name.loc name.txt); + | None -> Runtime_attributes.check_runtime_field_name ~loc:name.loc name.txt); Builtin_attributes.warning_scope attrs (fun () -> let arg = Ast_helper.Typ.force_poly arg in let cty = transl_simple_type env closed arg in @@ -355,7 +353,7 @@ let make_constructor env type_path type_params sargs sret_type = [Some(None)] of a type that can be undefined indistinguishable from [None]. *) let check_manifest_annotation ~loc ~env (decl : Types.type_declaration) = - let open Ast_untagged_variants in + let open Runtime_attributes in let open Variant_runtime in match (process_runtime_annotation decl.type_attributes, decl.type_manifest) @@ -422,7 +420,7 @@ let check_manifest_annotation ~loc ~env (decl : Types.type_declaration) = (runtime_annotation_to_string outer, recognized_by))) let transl_declaration ~type_record_as_object env sdecl id = - Ast_untagged_variants.check_runtime_annotation ~loc:sdecl.ptype_loc + Runtime_attributes.check_runtime_annotation ~loc:sdecl.ptype_loc ~is_abstract_or_abbreviation:(sdecl.ptype_kind = Ptype_abstract) sdecl.ptype_attributes; @@ -506,12 +504,12 @@ let transl_declaration ~type_record_as_object env sdecl id = (fun {pcd_name = {txt = constructor}; pcd_args; pcd_attributes} -> match List.find_opt - (fun ({txt}, _) -> txt = Ast_untagged_variants.untagged) + (fun ({txt}, _) -> txt = Runtime_attributes.untagged) pcd_attributes with | Some ({loc}, _) -> raise - (Ast_untagged_variants.Error + (Runtime_attributes.Error ( loc, UnboxedOnConstructor { @@ -532,7 +530,7 @@ let transl_declaration ~type_record_as_object env sdecl id = let constructors_from_variant_spreads = Hashtbl.create 10 in let make_cstr scstr = let name = Ident.create scstr.pcd_name.txt in - let runtime_tag = Ast_untagged_variants.process_constructor_tag scstr in + let runtime_tag = Runtime_attributes.process_constructor_tag scstr in let targs, tret_type, args, ret_type, _cstr_params = make_constructor env (Path.Pident id) params scstr.pcd_args scstr.pcd_res @@ -1648,12 +1646,12 @@ let transl_type_decl env rec_flag sdecl_list = (fun (id, decl) -> match decl.type_kind with | Type_variant (cstrs, pending_layout) -> - Ast_untagged_variants.check_tag_field_conflicts cstrs; + Runtime_attributes.check_tag_field_conflicts cstrs; let configuration : Variant_runtime.configuration = { - unboxed = Ast_untagged_variants.has_untagged decl.type_attributes; + unboxed = Runtime_attributes.has_untagged decl.type_attributes; tag_name = - Ast_untagged_variants.process_tag_name decl.type_attributes; + Runtime_attributes.process_tag_name decl.type_attributes; } in let layout = diff --git a/compiler/typing/typetexp.ml b/compiler/typing/typetexp.ml index 4a5f68aa63..a5fc62480d 100644 --- a/compiler/typing/typetexp.ml +++ b/compiler/typing/typetexp.ml @@ -617,7 +617,7 @@ and transl_fields env policy o fields = in let add_field = function | Otag (s, a, ty1) -> - Ast_untagged_variants.check_runtime_field_name ~loc:s.loc s.txt; + Runtime_attributes.check_runtime_field_name ~loc:s.loc s.txt; let ty1 = Builtin_attributes.warning_scope a (fun () -> transl_poly_type env policy ty1) diff --git a/compiler/typing/variant_coercion.ml b/compiler/typing/variant_coercion.ml index 314fa791dc..8fa55c8c93 100644 --- a/compiler/typing/variant_coercion.ml +++ b/compiler/typing/variant_coercion.ml @@ -197,8 +197,8 @@ let variant_configuration_can_be_coerced_raises ~is_spread_context ~left_loc ~right_loc ~(left_attributes : Parsetree.attributes) ~(right_attributes : Parsetree.attributes) = (match - ( Ast_untagged_variants.has_untagged left_attributes, - Ast_untagged_variants.has_untagged right_attributes ) + ( Runtime_attributes.has_untagged left_attributes, + Runtime_attributes.has_untagged right_attributes ) with | true, true | false, false -> () | left, _right -> @@ -213,8 +213,8 @@ let variant_configuration_can_be_coerced_raises ~is_spread_context ~left_loc }))); match - ( Ast_untagged_variants.process_tag_name left_attributes, - Ast_untagged_variants.process_tag_name right_attributes ) + ( Runtime_attributes.process_tag_name left_attributes, + Runtime_attributes.process_tag_name right_attributes ) with | Some host_tag, Some spread_tag when host_tag = spread_tag -> () | None, None -> () diff --git a/compiler/typing/variant_layout.ml b/compiler/typing/variant_layout.ml index a7c385edf8..ec6fc5b198 100644 --- a/compiler/typing/variant_layout.ml +++ b/compiler/typing/variant_layout.ml @@ -6,7 +6,7 @@ classifying untagged payloads requires expanding their types. *) open Variant_runtime -open Ast_untagged_variants +open Runtime_attributes (* The shape of [t]: the one stated by the first declaration on its expansion chain that states one. Each declaration's shape already includes what the diff --git a/compiler/typing/variant_type_spread.ml b/compiler/typing/variant_type_spread.ml index 446f5c4f47..fb2433a2e7 100644 --- a/compiler/typing/variant_type_spread.ml +++ b/compiler/typing/variant_type_spread.ml @@ -95,7 +95,7 @@ let map_constructors ~(sdecl : Parsetree.type_declaration) ~all_constructors env (fun txt -> { Asttypes.txt = - Ast_untagged_variants.parsetree_tag_of_runtime txt; + Runtime_attributes.parsetree_tag_of_runtime txt; loc = cstr.cd_loc; }) cstr.cd_runtime_tag; diff --git a/tests/ounit_tests/ounit_ast_helper_tests.ml b/tests/ounit_tests/ounit_ast_helper_tests.ml index ac3a9d6d22..cfa89fa185 100644 --- a/tests/ounit_tests/ounit_ast_helper_tests.ml +++ b/tests/ounit_tests/ounit_ast_helper_tests.ml @@ -63,7 +63,7 @@ let suites = (Location.mknoloc "A") in assert_invalid_escape_error (fun () -> - Ast_untagged_variants.process_constructor_tag constructor) ); + Runtime_attributes.process_constructor_tag constructor) ); ( "the compiler reports an invalid @as escape on a field" >:: fun _ -> let field = Ast_helper.Type.field