Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/runtime-attributes-module.internal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Rename the compiler module `Ast_untagged_variants` to `Runtime_attributes`, since it handles all attributes that set runtime representation.
2 changes: 1 addition & 1 deletion compiler/backend/lam_compile_primitive.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 -> (
Expand Down
8 changes: 4 additions & 4 deletions compiler/typing/UntaggedVariants.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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,
Expand Down Expand Up @@ -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`;
Expand Down Expand Up @@ -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`,
Expand Down
2 changes: 1 addition & 1 deletion compiler/typing/ast_attributes.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion compiler/typing/ast_checks.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions compiler/typing/includecore.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 =
Expand Down
15 changes: 7 additions & 8 deletions compiler/typing/printtyp.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -1611,7 +1610,7 @@ let print_variant_runtime_representation_issue ppf variant_name
@{<info>@as@} payload that has a runtime representation of \
@{<info>%s@}, which is not compatible with the expected @{<info>%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} ->
Expand All @@ -1626,7 +1625,7 @@ let print_variant_runtime_representation_issue ppf variant_name
fprintf ppf
"an @{<info>@as@} payload that gives it the runtime type of \
@{<info>%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 @{<info>%s@}."
Expand Down
Original file line number Diff line number Diff line change
@@ -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 =
Expand Down
7 changes: 3 additions & 4 deletions compiler/typing/transl_recmodule.ml
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ 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 =
Blk_constructor
{
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 =
Expand All @@ -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
Expand Down
24 changes: 11 additions & 13 deletions compiler/typing/typedecl.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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
{
Expand All @@ -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
Expand Down Expand Up @@ -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 =
Expand Down
2 changes: 1 addition & 1 deletion compiler/typing/typetexp.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions compiler/typing/variant_coercion.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 ->
Expand All @@ -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 -> ()
Expand Down
2 changes: 1 addition & 1 deletion compiler/typing/variant_layout.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion compiler/typing/variant_type_spread.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion tests/ounit_tests/ounit_ast_helper_tests.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading