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/ppx-dict-literal-marker.fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Fix external PPXs turning `Primitive_dict.make(...)` calls into dict literals and flattening dict literals spread into dict literals. https://github.com/rescript-lang/rescript/pull/8846
Comment thread
cknitt marked this conversation as resolved.
Resolved
Comment thread
cknitt marked this conversation as resolved.
Resolved
21 changes: 14 additions & 7 deletions compiler/ml/ast_dict.ml
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
(* Dict literals, [Pexp_dict]: [dict{"a": 1, ...d}]. They used to be parsed
into calls of the runtime's [Primitive_dict], which remain their encoding on
the v0 PPX wire:
- without spreads, [Primitive_dict.make([("a", 1)])];
- without spreads, [Primitive_dict.make([("a", 1)])], whose [make] carries
the [res.dict] marker;
- with spreads, [Primitive_dict.spread(target, [part, ...])], whose
[spread] carries the [res.dictSpread] marker. The target is
[Primitive_dict.make(rows)] with the rows before the first spread, and
the parts are the spreads and [Primitive_dict.make(rows)] for the rows
between them, like [Object.assign]. *)
between them, like [Object.assign]. These [make] calls carry the
[res.dictRows] marker.
The markers tell the encoding apart from calls written in the source, and
the rows of a dict literal from a dict literal spread into it. *)

open Parsetree

Expand Down Expand Up @@ -74,30 +78,33 @@ let primitive_dict ~loc ?attrs name =
(Longident.Ldot (Longident.Lident Primitive_modules.dict, name))
loc)

let literal_marker = "res.dict"
let spread_marker = "res.dictSpread"
let rows_marker = "res.dictRows"

let make ~loc rows =
let make ~loc ~marker rows =
Ast_helper.Exp.apply ~loc
(primitive_dict ~loc "make")
(primitive_dict ~loc ~attrs:[(Location.mknoloc marker, PStr [])] "make")
[(Asttypes.Nolabel, rows_array ~loc rows)]

(* The calls encoding the dict literal [dict{entries}] at [loc] *)
let to_calls ~loc ~attrs dict_entries =
let e =
match target_and_sources (parts dict_entries) with
| `Rows rows -> make ~loc rows
| `Rows rows -> make ~loc ~marker:literal_marker rows
| `Spread (target, sources) ->
Ast_helper.Exp.apply ~loc
(primitive_dict ~loc
~attrs:[(Location.mknoloc spread_marker, PStr [])]
"spread")
[
(Nolabel, make ~loc:(rows_loc ~loc target) target);
(Nolabel, make ~loc:(rows_loc ~loc target) ~marker:rows_marker target);
( Nolabel,
Ast_helper.Exp.array ~loc
(List.map
(function
| Rows rows -> make ~loc:(rows_loc ~loc rows) rows
| Rows rows ->
make ~loc:(rows_loc ~loc rows) ~marker:rows_marker rows
| Spread spread -> spread)
sources) );
]
Expand Down
14 changes: 8 additions & 6 deletions compiler/ml/ast_mapper_from0.ml
Original file line number Diff line number Diff line change
Expand Up @@ -549,18 +549,20 @@ end
module E = struct
(* Value expressions for the core language *)

(* The rows of [Primitive_dict.make([("a", 1), ...])] *)
let dict_rows (e : expression) =
(* The rows of [Primitive_dict.make([("a", 1), ...])], whose [make] carries
[marker] *)
let dict_rows ~marker (e : expression) =
match e.pexp_desc with
| Pexp_apply
( {
pexp_desc = Pexp_ident {txt = Ldot (Lident "Primitive_dict", "make")};
pexp_attributes = [];
pexp_attributes = [({txt}, PStr [])];
},
[
( Asttypes.Noloc.Nolabel,
{pexp_desc = Pexp_array items; pexp_attributes = []} );
] ) ->
] )
when txt = marker ->
let row = function
| {
pexp_desc =
Expand Down Expand Up @@ -592,7 +594,7 @@ module E = struct
let dict_entries (e : expression) =
let chunk (e : expression) =
match e.pexp_attributes with
| [] -> dict_rows e
| [] -> dict_rows ~marker:Ast_dict.rows_marker e
| _ -> None
in
let rows = List.map (fun row -> `Row row) in
Expand Down Expand Up @@ -620,7 +622,7 @@ module E = struct
| None -> [`Spread source])
sources)
| None -> None)
| _ -> Option.map rows (dict_rows e)
| _ -> Option.map rows (dict_rows ~marker:Ast_dict.literal_marker e)

let has_await_attribute attrs =
List.exists
Expand Down
18 changes: 14 additions & 4 deletions tests/ounit_tests/ounit_ast_mapper0_tests.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1543,7 +1543,7 @@ let test_dict_literal_through_ast0 _ =
_ ) ->
OUnit.assert_equal ~msg:source wire_function name;
OUnit.assert_equal ~msg:source ~printer:(String.concat ", ")
(if name = "spread" then ["res.dictSpread"] else [])
(if name = "spread" then ["res.dictSpread"] else ["res.dict"])
(attr_names pexp_attributes)
| _ -> assert_failure ("Expected a Primitive_dict call: " ^ source));
match map_expr0 wire with
Expand All @@ -1559,8 +1559,11 @@ let test_dict_literal_through_ast0 _ =
({|dict{"a\"b": 1}|}, "make");
("dict{...a}", "spread");
({|dict{"a": 1, ...b, "c": 3, ...d}|}, "spread");
({|dict{"a": 1, ...dict{"b": 2}}|}, "spread");
({|dict{...dict{"b": 2, ...dict{}}, "c": 3}|}, "spread");
];
(* A call a PPX writes, with a semantic string key *)
(* A call without the marker, as written in the source or by a PPX, stays a
call *)
let make =
Ast_helper0.Exp.apply ~loc
(Ast_helper0.Exp.ident ~loc
Expand All @@ -1581,8 +1584,15 @@ let test_dict_literal_through_ast0 _ =
]
in
match (map_expr0 make).pexp_desc with
| Pexp_dict [Pdict_entry ({txt = "k"}, _)] -> ()
| _ -> assert_failure "Expected a dict literal from Primitive_dict.make"
| Pexp_apply
{
funct =
{
pexp_desc = Pexp_ident {txt = Ldot (Lident "Primitive_dict", "make")};
};
} ->
()
| _ -> assert_failure "Expected a call of Primitive_dict.make"

let test_removed_regexp_extension _ =
List.iter
Expand Down
4 changes: 4 additions & 0 deletions tests/syntax_tests/data/ast-mapping/DictLiterals.res
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ let spreads = dict{...a, ...b}
let attributed = @attr dict{"a": 1}
let attributedSpread = @attr dict{"a": 1, ...b}
let nested = dict{"a": dict{"b": 1}}
let spreadDictLiteral = dict{"a": 1, ...dict{"b": 2}}
let spreadDictLiterals = dict{...dict{"b": 2}, "c": 3, ...dict{"d": 4, ...dict{}}}
let userMake = Primitive_dict.make([("a", 1)])
let userSpread = Primitive_dict.spread(Primitive_dict.make([]), [d])
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ let spreads = dict{...a, ...b}
let attributed = @attr dict{"a": 1}
let attributedSpread = @attr dict{"a": 1, ...b}
let nested = dict{"a": dict{"b": 1}}
let spreadDictLiteral = dict{"a": 1, ...dict{"b": 2}}
let spreadDictLiterals = dict{...dict{"b": 2}, "c": 3, ...dict{"d": 4, ...dict{}}}
let userMake = Primitive_dict.make([("a", 1)])
let userSpread = Primitive_dict.spread(Primitive_dict.make([]), [d])
Loading