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/array-record-rest-coercion.breaking.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- A record rest pattern whose rest type is a record with the runtime names `"0"`, `"1"`, …, a coercion between such a record and a record represented as an object (in either direction), and an untagged variant with such a record and an array or tuple case are now errors, as they produced an object where the type promises a JavaScript array, or matched the wrong case.
1 change: 1 addition & 0 deletions changes/array-record-spread.fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Keep records whose fields have the runtime names `"0"`, `"1"`, … JavaScript arrays when they are spread or written as `{}`, and match them as arrays when they are untagged variant payloads.
5 changes: 2 additions & 3 deletions compiler/core/js_dump.ml
Original file line number Diff line number Diff line change
Expand Up @@ -860,9 +860,8 @@ and expression_desc cxt ~(level : int) f x : cxt =
(Ext_list.map_combine fields el (fun x ->
Js_op.Lit (Ext_ident.convert x)))))
(*name convention of Record is slight different from modules*)
| Caml_block (el, _, Blk_record {fields}) ->
if Lambda.record_fields_are_array fields then
expression_desc cxt ~level f (Array el)
| Caml_block (el, _, Blk_record {fields; array}) ->
if array then expression_desc cxt ~level f (Array el)
Comment thread
cknitt marked this conversation as resolved.
Resolved
else
let fields =
Ext_list.array_list_filter_map fields el (fun (f, opt) x ->
Expand Down
3 changes: 2 additions & 1 deletion compiler/gentype/translate_type_declarations.ml
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,8 @@ let traslate_declaration_kind ~config ~loc ~output_file_relative ~resolver
let unboxed =
match representation with
| Record_unboxed _ -> true
| Record_regular | Record_inlined _ | Record_extension -> false
| Record_regular | Record_array | Record_inlined _ | Record_extension ->
false
in
let {Translate_type_expr_from_types.dependencies; type_} =
label_declarations |> translate_label_declarations ~unboxed
Expand Down
2 changes: 1 addition & 1 deletion compiler/ml/UntaggedVariants.md
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ The type checker rejects an untagged variant, with
| Condition | Reason | Fixture |
| --- | --- | --- |
| two object cases | At most one case can be an object type. | `UntaggedAtMostOneObject.res` |
| two cases of one instance class, compared by path (arrays and tuples share `Array`) | At most one case can be an instance of C; for `Array`, an array or tuple type. | `UntaggedAtMostOneInstance.res`, `UntaggedTupleAndArray.res`, `untagged_same_class_twice.res` |
| two cases of one instance class, compared by path (arrays, tuples and records represented as arrays share `Array`) | At most one case can be an instance of C; for `Array`, an array, a tuple or a record represented as an array. | `UntaggedAtMostOneInstance.res`, `UntaggedTupleAndArray.res`, `untagged_array_record_and_array.res`, `untagged_same_class_twice.res` |
| two function cases | At most one case can be a function type. | `UntaggedAtMostOneFunction.res` |
| two string cases | At most one case can be a string type. | `UntaggedAtMostOneString.res` |
| two number cases (`int` and `float` count together) | At most one case can be a number type (int or float). | `UntaggedAtMostOneNumber.res` |
Expand Down
3 changes: 2 additions & 1 deletion compiler/ml/ast_untagged_variants.ml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ let report_error ppf =
"At most one case can be "
^ (match kind with
| ObjectType -> "an object type"
| InstanceType Array -> "an array or tuple type"
| InstanceType Array ->
"an array, a tuple or a record represented as an array"
| InstanceType i -> "an instance of " ^ Instance.to_string i
| FunctionType -> "a function type"
| StringType -> "a string type"
Expand Down
20 changes: 18 additions & 2 deletions compiler/ml/ctype.ml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ type subtype_context =
issues: Record_coercion.record_field_subtype_violation list;
}
| Coercion_into_private_type of {typename: Path.t}
| Record_array_mismatch of {
array_record_name: Path.t;
object_record_name: Path.t;
}

exception Subtype of type_pairs * type_pairs * subtype_context option

Expand Down Expand Up @@ -3863,7 +3867,10 @@ let rec subtype_rec env trace t1 t2 cstrs =
(p2, _, {type_kind = Type_record (fields2, repr2)}) ) ->
let same_repr =
match (repr1, repr2) with
| Record_regular, Record_regular ->
(* Not between [Record_array] and [Record_regular]: an object is
no array, and an array coerced to an object record would
still pass for the array case of an untagged variant *)
| Record_regular, Record_regular | Record_array, Record_array ->
true (* handled in the fields checks *)
| Record_unboxed b1, Record_unboxed b2 -> b1 = b2
| Record_inlined _, Record_inlined _ -> repr1 = repr2
Expand Down Expand Up @@ -3893,7 +3900,16 @@ let rec subtype_rec env trace t1 t2 cstrs =
t1,
t2,
!(current_univar_pairs ()),
None (* TODO(subtype-errors) Record representation *) )
match (repr1, repr2) with
| Record_regular, Record_array ->
Some
(Record_array_mismatch
{array_record_name = p2; object_record_name = p1})
| Record_array, Record_regular ->
Some
(Record_array_mismatch
{array_record_name = p1; object_record_name = p2})
| _ -> None (* TODO(subtype-errors) Record representation *) )
:: cstrs
| (p1, _, {type_kind = tk1}), (p2, _, {type_kind = tk2}) ->
( trace,
Expand Down
6 changes: 6 additions & 0 deletions compiler/ml/ctype.mli
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ type subtype_context =
| Coercion_into_private_type of {typename: Path.t}
(** The target is a private record or variant, whose values only its own
module can build. *)
| Record_array_mismatch of {
array_record_name: Path.t;
object_record_name: Path.t;
}
(** One record is represented as a JavaScript array, the other as an
object. *)

type type_pairs = (type_expr * type_expr) list
exception Unify of type_pairs
Expand Down
23 changes: 9 additions & 14 deletions compiler/ml/lambda.ml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ type tag_info =
| Blk_record of {
fields: (string * bool (* optional *)) array;
mutable_flag: Asttypes.mutable_flag;
array: bool;
(* [Record_array]: the fields are the elements of a JavaScript
array *)
}
| Blk_module of string list
| Blk_module_export of Ident.t list
Expand Down Expand Up @@ -59,14 +62,16 @@ let mutable_flag_of_tag_info (tag : tag_info) =
| Blk_module_export _ | Blk_extension ->
Immutable

let blk_record fields mutable_flag = Blk_record {fields; mutable_flag}
let blk_record fields mutable_flag ~array =
Blk_record {fields; mutable_flag; array}
let blk_record_ext fields mutable_flag = Blk_record_ext {fields; mutable_flag}

let blk_record_inlined fields name num_nonconst ~runtime mutable_flag =
Blk_record_inlined {fields; name; num_nonconst; mutable_flag; runtime}

let ref_tag_info : tag_info =
Blk_record {fields = [|("contents", false)|]; mutable_flag = Mutable}
Blk_record
{fields = [|("contents", false)|]; mutable_flag = Mutable; array = false}

type field_dbg_info =
| Fld_record of {name: string}
Expand Down Expand Up @@ -801,16 +806,6 @@ and eq_option l1 l2 =

and eq_approx_list ls ls1 = Ext_list.for_all2_no_exn ls ls1 eq_approx

let runtime_names_are_array runtime_name fields =
let len = Array.length fields in
let rec loop i =
if i = len then true
else string_of_int i = runtime_name fields.(i) && loop (i + 1)
in
len <> 0 && loop 0

let record_fields_are_array fields = runtime_names_are_array fst fields

(* The JavaScript value a constant is emitted as by [Lam_compile_const] and
[Js_dump], not its source constructor. A constant of another
representation keeps the dispatch at run time. *)
Expand All @@ -828,8 +823,8 @@ let untagged_value (c : structured_constant) : Untagged_dispatch.value option =
| Const_bigint (sign, digits) ->
Some (Literal (BigInt (Bigint_utils.to_string sign digits)))
| Const_block (Blk_tuple, _) -> Some Array
| Const_block (Blk_record {fields}, _) ->
if record_fields_are_array fields then Some Array else Some Object
| Const_block (Blk_record {array}, _) ->
if array then Some Array else Some Object
| Const_block
( ( Blk_constructor _ | Blk_record_inlined _ | Blk_poly_var
| Blk_record_ext _ | Blk_module _ | Blk_module_export _ | Blk_extension
Expand Down
15 changes: 4 additions & 11 deletions compiler/ml/lambda.mli
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ type tag_info =
| Blk_record of {
fields: (string * bool (* optional *)) array;
mutable_flag: mutable_flag;
array: bool;
(* [Record_array]: the fields are the elements of a JavaScript
array *)
}
| Blk_module of string list
| Blk_module_export of Ident.t list
Expand All @@ -54,7 +57,7 @@ type tag_info =

val tag_label_of_tag_info : tag_info -> string
val mutable_flag_of_tag_info : tag_info -> mutable_flag
val blk_record : (string * bool) array -> mutable_flag -> tag_info
val blk_record : (string * bool) array -> mutable_flag -> array:bool -> tag_info

val blk_record_ext : string array -> mutable_flag -> tag_info

Expand Down Expand Up @@ -547,13 +550,3 @@ val name_lambda : let_kind -> t -> (Ident.t -> t) -> t
val bind : let_kind -> Ident.t -> t -> t -> t

val default_function_attribute : function_attribute

val record_fields_are_array : (string * bool) array -> bool
(** Whether a nonempty record has consecutive field names starting at ["0"],
so it is represented as a JavaScript array. Optionality does not affect
the layout. *)

val runtime_names_are_array : ('a -> string) -> 'a array -> bool
(** [record_fields_are_array] over fields of any shape, given their runtime
name, so that a caller holding the labels of a large record (JSX props)
need not build a copy of them. *)
2 changes: 1 addition & 1 deletion compiler/ml/matching.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1656,7 +1656,7 @@ let make_record_matching loc all_labels def = function
let lbl = all_labels.(pos) in
let access =
match lbl.lbl_repres with
| Record_regular ->
| Record_regular | Record_array ->
prim
~primitive:
(Pfield (lbl.lbl_pos, Lambda.fld_record lbl.lbl_runtime_name))
Expand Down
6 changes: 4 additions & 2 deletions compiler/ml/printlambda.ml
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,10 @@ let print_taginfo ppf = function
| Blk_constructor {name; num_nonconst} ->
fprintf ppf "%s/%i" name num_nonconst
| Blk_poly_var -> fprintf ppf "polyvar"
| Blk_record {fields = ss} ->
fprintf ppf "[%s]" (String.concat ";" (List.map fst (Array.to_list ss)))
| Blk_record {fields = ss; array} ->
fprintf ppf "%s[%s]"
(if array then "array" else "")
(String.concat ";" (List.map fst (Array.to_list ss)))
| Blk_module ss -> fprintf ppf "[%s]" (String.concat ";" ss)
| Blk_module_export _ -> fprintf ppf "module/exports"
| Blk_record_inlined {fields = ss} ->
Expand Down
10 changes: 9 additions & 1 deletion compiler/ml/printtyp.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1831,7 +1831,15 @@ let report_subtyping_error ppf env tr1 txt1 tr2 ctx =
fprintf ppf
"@ @{<info>%s@} is private: its values can only be built in the \
module that defines it, so another type cannot be coerced to it."
(Path.name typename))
(Path.name typename)
| Record_array_mismatch {array_record_name; object_record_name} ->
fprintf ppf
"@ The record @{<info>%s@} is represented as a JavaScript array \
(its fields have the runtime names \"0\", \"1\", ...) and \
@{<info>%s@} as an object, so neither can be coerced to the \
other."
(Path.name array_record_name)
(Path.name object_record_name))
| None -> ())

let report_ambiguous_type_error ppf env (tp0, tp0') tpl txt1 txt2 txt3 =
Expand Down
1 change: 1 addition & 0 deletions compiler/ml/printtyped.ml
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ let record_representation i ppf =
let open Types in
function
| Record_regular -> line i ppf "Record_regular\n"
| Record_array -> line i ppf "Record_array\n"
| Record_unboxed b -> line i ppf "Record_unboxed %b\n" b
| Record_inlined {name} -> line i ppf "Record_inlined %s\n" name
| Record_extension -> line i ppf "Record_extension\n"
Expand Down
3 changes: 2 additions & 1 deletion compiler/ml/rec_check.ml
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,8 @@ let rec expression : Env.env -> Typedtree.expression -> Use.t =
let use =
match rep with
| Record_unboxed _ -> fun x -> x
| Record_regular | Record_inlined _ | Record_extension -> Use.guard
| Record_regular | Record_array | Record_inlined _ | Record_extension ->
Use.guard
in
let field env (_, _, e) = expression env e in
Use.join (use (array field env es)) (option expression env eo)
Expand Down
9 changes: 9 additions & 0 deletions compiler/ml/record_runtime.ml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ let declaration_name (lbl : Types.label_declaration) =
| Some name -> name
| None -> Ident.name lbl.ld_id

(* A record whose fields have the runtime names "0", "1", ... in declaration
order is represented as a JavaScript array ([Record_array]). *)
let is_array (lbls : Types.label_declaration list) =
let rec loop i = function
| [] -> true
| lbl :: rest -> declaration_name lbl = string_of_int i && loop (i + 1) rest
in
lbls <> [] && loop 0 lbls

(* Two fields collide when they end up with the same runtime name, which a
rename can cause between fields whose declared names differ. *)
let rec check_duplicated_labels_aux (lbls : Parsetree.label_declaration list)
Expand Down
4 changes: 4 additions & 0 deletions compiler/ml/record_runtime.mli
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@

val declaration_name : Types.label_declaration -> string

val is_array : Types.label_declaration list -> bool
(** Whether the fields have the runtime names ["0"], ["1"], ... in declaration
order, so the record is represented as a JavaScript array. *)

val extra_as_attribute : Parsetree.label_declaration -> Location.t option
(** The location of a second [@as] naming the field, which is one too many. *)

Expand Down
44 changes: 26 additions & 18 deletions compiler/ml/translcore.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1039,18 +1039,22 @@ let fields_by_position fields =

(* How [transl_record] builds a record: by copying the fields of [init] one
by one (or with no [init] at all), or by taking a shallow copy of [init]
and setting the other fields. JSX props follow the same rule.
and setting the other fields. JSX props follow the same rule. A shallow
copy is an object spread, so an array record always copies its fields.
TODO: More strategies
3 + 2 * List.length lbl_expr_list >= size (density) *)
let record_copies_fields
(fields : (label_description * Longident.t loc * expression) array)
~(init : expression option) =
match init with
| None -> true
| Some _ ->
| Some _ -> (
let lbl_all = all_labels fields in
Array.length lbl_all < 20
&& not (Array.exists (fun lbl -> lbl.lbl_optional) lbl_all)
match lbl_all.(0).lbl_repres with
| Record_array -> true
| Record_regular | Record_unboxed _ | Record_inlined _ | Record_extension ->
Array.length lbl_all < 20
&& not (Array.exists (fun lbl -> lbl.lbl_optional) lbl_all))

let jsx_props_of ~multi (props : expression) =
match props.exp_desc with
Expand All @@ -1074,21 +1078,16 @@ let jsx_props_of ~multi (props : expression) =
let record children =
Some (Jsx_props_record {fields; init; copy_fields; children})
in
let is_array =
Lambda.runtime_names_are_array
(fun (lbl : label_description) -> lbl.lbl_runtime_name)
(all_labels fields)
in
match children with
(* a record with the runtime names "0", "1", ... is an array *)
| _ when is_array -> None
| None -> record None
(* children printed as JSX children must be the [children] prop *)
| Some (lbl, _) when lbl.lbl_runtime_name <> "children" -> None
| Some (lbl, child) -> (
match jsx_children ~multi lbl child with
| Some children -> record (Some children)
| None -> None))
(* props that are a JavaScript array: an ordinary call *)
| Texp_record {representation = Record_array} -> None
| _ -> Some (Jsx_props_spread props)

(* [None] when the call doesn't have the shape the PPX produces; it then
Expand Down Expand Up @@ -1419,7 +1418,7 @@ and transl_exp0 (e : Typedtree.expression) : Lambda.t =
| Texp_field (arg, _, lbl) -> (
let targ = transl_exp arg in
match lbl.lbl_repres with
| Record_regular ->
| Record_regular | Record_array ->
prim
~primitive:
(Pfield (lbl.lbl_pos, Lambda.fld_record lbl.lbl_runtime_name))
Expand All @@ -1439,7 +1438,7 @@ and transl_exp0 (e : Typedtree.expression) : Lambda.t =
| Texp_setfield (arg, _, lbl, newval) ->
let access =
match lbl.lbl_repres with
| Record_regular ->
| Record_regular | Record_array ->
Psetfield (lbl.lbl_pos, Lambda.fld_record_set lbl.lbl_runtime_name)
| Record_inlined _ ->
Psetfield
Expand Down Expand Up @@ -1802,7 +1801,7 @@ and transl_record loc env fields repres opt_init_expr =
| None, Some _ ->
let access =
match repres with
| Record_regular ->
| Record_regular | Record_array ->
Pfield (i, Lambda.fld_record lbl.lbl_runtime_name)
| Record_inlined _ ->
Pfield (i, Lambda.fld_record_inline lbl.lbl_runtime_name)
Expand All @@ -1823,8 +1822,12 @@ and transl_record loc env fields repres opt_init_expr =
if mut = Mutable then raise Not_constant;
let cl = List.map extract_constant ll in
match repres with
| Record_regular ->
const (Const_block (Lambda.blk_record (field_shape ()) mut, cl))
| Record_regular | Record_array ->
const
(Const_block
( Lambda.blk_record (field_shape ()) mut
~array:(repres = Record_array),
cl ))
| Record_inlined {name; representation} ->
let runtime =
match Variant_runtime.representation representation with
Expand All @@ -1848,9 +1851,12 @@ and transl_record loc env fields repres opt_init_expr =
| Record_extension -> raise Not_constant
with Not_constant -> (
match repres with
| Record_regular ->
| Record_regular | Record_array ->
prim
~primitive:(Pmakeblock (Lambda.blk_record (field_shape ()) mut))
~primitive:
(Pmakeblock
(Lambda.blk_record (field_shape ()) mut
~array:(repres = Record_array)))
~args:ll loc
| Record_inlined {name; representation} ->
let runtime =
Expand Down Expand Up @@ -1901,6 +1907,8 @@ and transl_record loc env fields repres opt_init_expr =
Psetfield
(lbl.lbl_pos, Lambda.fld_record_inline_set lbl.lbl_runtime_name)
| Record_unboxed _ -> assert false
(* [record_copies_fields] holds for an array record *)
| Record_array -> assert false
| Record_extension ->
Psetfield
( lbl.lbl_pos + 1,
Expand Down
Loading
Loading