From 5500d6a7cf1061ca7167f10945a41e74ed7e6a47 Mon Sep 17 00:00:00 2001 From: Christoph Knittel Date: Mon, 12 Oct 2026 19:37:06 +0000 Subject: [PATCH] Mark the v0 encoding of dict literals External PPXs no longer turn source calls of Primitive_dict.make into dict literals, or flatten dict literals spread into dict literals. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01BC5F5QPeQh5cHBjGBnvJ2T Signed-off-by: Christoph Knittel --- changes/ppx-dict-literal-marker.fix.md | 1 + compiler/ml/ast_dict.ml | 21 ++++++++++++------- compiler/ml/ast_mapper_from0.ml | 14 +++++++------ tests/ounit_tests/ounit_ast_mapper0_tests.ml | 18 ++++++++++++---- .../data/ast-mapping/DictLiterals.res | 4 ++++ .../ast-mapping/expected/DictLiterals.res.txt | 4 ++++ 6 files changed, 45 insertions(+), 17 deletions(-) create mode 100644 changes/ppx-dict-literal-marker.fix.md diff --git a/changes/ppx-dict-literal-marker.fix.md b/changes/ppx-dict-literal-marker.fix.md new file mode 100644 index 00000000000..7369704fb43 --- /dev/null +++ b/changes/ppx-dict-literal-marker.fix.md @@ -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 diff --git a/compiler/ml/ast_dict.ml b/compiler/ml/ast_dict.ml index 98bb83ca5cd..dd375b1683c 100644 --- a/compiler/ml/ast_dict.ml +++ b/compiler/ml/ast_dict.ml @@ -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 @@ -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) ); ] diff --git a/compiler/ml/ast_mapper_from0.ml b/compiler/ml/ast_mapper_from0.ml index 810a42dcdb8..fa4d1e2e58c 100644 --- a/compiler/ml/ast_mapper_from0.ml +++ b/compiler/ml/ast_mapper_from0.ml @@ -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 = @@ -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 @@ -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 diff --git a/tests/ounit_tests/ounit_ast_mapper0_tests.ml b/tests/ounit_tests/ounit_ast_mapper0_tests.ml index 30038cf3d1e..b898ab40043 100644 --- a/tests/ounit_tests/ounit_ast_mapper0_tests.ml +++ b/tests/ounit_tests/ounit_ast_mapper0_tests.ml @@ -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 @@ -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 @@ -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 diff --git a/tests/syntax_tests/data/ast-mapping/DictLiterals.res b/tests/syntax_tests/data/ast-mapping/DictLiterals.res index 466a37f3051..458a36ccaf3 100644 --- a/tests/syntax_tests/data/ast-mapping/DictLiterals.res +++ b/tests/syntax_tests/data/ast-mapping/DictLiterals.res @@ -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]) diff --git a/tests/syntax_tests/data/ast-mapping/expected/DictLiterals.res.txt b/tests/syntax_tests/data/ast-mapping/expected/DictLiterals.res.txt index 73fac34ce89..d51cd533915 100644 --- a/tests/syntax_tests/data/ast-mapping/expected/DictLiterals.res.txt +++ b/tests/syntax_tests/data/ast-mapping/expected/DictLiterals.res.txt @@ -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])