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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#### :bug: Bug fix

- Fix the formatter dropping attributes on dict literals and flattening nested dict spreads. https://github.com/rescript-lang/rescript/pull/8746
- Fix attributes on patterns being duplicated before `as` and dropped on constants when formatting. https://github.com/rescript-lang/rescript/pull/8750
- Fix compiler crashes when wrapping values in `Some` or passing optional arguments whose types come from a transitive dependency. https://github.com/rescript-lang/rescript/pull/8719
- Fix unused-pattern warnings for variant spreads reported without a location. https://github.com/rescript-lang/rescript/pull/8744
Expand Down Expand Up @@ -51,6 +52,7 @@

#### :house: Internal

- Represent dict literals with dedicated `Pexp_dict` and `Texp_dict` nodes. https://github.com/rescript-lang/rescript/pull/8746
- Forward-port integer range pattern regression coverage from v12 to v13. https://github.com/rescript-lang/rescript/pull/8718
- Remove the unused `res.iflet` encoding of `if let`, which the parser always rejects with a syntax error suggesting a `switch`. The error is unchanged. https://github.com/rescript-lang/rescript/pull/8741
- Represent variant spread patterns (`...t`) with a dedicated `Ppat_variant_spread` parsetree node instead of `Ppat_type` with a `res.patVariantSpread` attribute, which remains only in the PPX wire representation. https://github.com/rescript-lang/rescript/pull/8742
Expand Down
5 changes: 5 additions & 0 deletions analysis/reanalyze/src/arnold.ml
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,11 @@ module Compile = struct
[e1; e2] |> List.map (expression ~ctx) |> Command.unordered_sequence
| Texp_tuple expressions | Texp_array expressions ->
expressions |> List.map (expression ~ctx) |> Command.unordered_sequence
| Texp_dict entries ->
entries
|> List.map (function Typedtree.Tdict_entry (_, e) | Tdict_spread e ->
expression ~ctx e)
|> Command.unordered_sequence
| Texp_assert _ -> Command.nothing
| Texp_try (e, cases) ->
let c_e = e |> expression ~ctx in
Expand Down
6 changes: 6 additions & 0 deletions analysis/reanalyze/src/side_effects.ml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ let rec expr_no_side_effects (expr : Typedtree.expression) =
| Texp_field (e, _lid, _ld) -> e |> expr_no_side_effects
| Texp_setfield _ -> false
| Texp_array el -> el |> List.for_all expr_no_side_effects
| Texp_dict entries ->
entries
|> List.for_all (function
| Typedtree.Tdict_entry (_, e) -> e |> expr_no_side_effects
(* Object.assign reads the spread dicts *)
| Tdict_spread _ -> false)
| Texp_ifthenelse (e1, e2, eo) ->
e1 |> expr_no_side_effects && e2 |> expr_no_side_effects
&& eo |> expr_opt_no_side_effects
Expand Down
6 changes: 6 additions & 0 deletions analysis/src/completion_back_end.ml
Original file line number Diff line number Diff line change
Expand Up @@ -983,6 +983,12 @@ and get_completions_for_context_path ~state ~debug ~full ~opens ~raw_opens ~pos
Completion.create "array" ~env
~kind:(Completion.Value (Ctype.newconstr Predef.path_array []));
]
| CPDict ->
if Debug.verbose () then print_endline "[ctx_path]--> CPDict";
[
Completion.create "dict" ~env
~kind:(Completion.Value (Ctype.newconstr Predef.path_dict []));
]
| CPArray (Some cp) -> (
if Debug.verbose () then
print_endline "[ctx_path]--> CPArray (with payload)";
Expand Down
1 change: 1 addition & 0 deletions analysis/src/completion_front_end.ml
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ let rec expr_to_context_path_inner ~(in_jsx_context : bool)
(match exprs with
| [] -> None
| exp :: _ -> expr_to_context_path ~in_jsx_context exp))
| Pexp_dict _ -> Some CPDict
| Pexp_ident {txt = Lident "->"} -> None
| Pexp_ident {txt; loc} ->
Some
Expand Down
17 changes: 17 additions & 0 deletions analysis/src/dump_ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,23 @@ and print_expr_item expr ~pos ~indentation =
^ "\n"
^ add_indentation indentation
^ ")"
| Pexp_dict entries ->
"Pexp_dict(\n"
^ (entries
|> List.map (fun entry ->
add_indentation (indentation + 1)
^
match entry with
| Parsetree.Pdict_entry (key, expr) ->
(key |> print_loc_denominator_loc ~pos)
^ "\"" ^ key.txt ^ "\": "
^ print_expr_item expr ~pos ~indentation:(indentation + 1)
| Pdict_spread expr ->
"..." ^ print_expr_item expr ~pos ~indentation:(indentation + 1))
|> String.concat "\n")
^ "\n"
^ add_indentation indentation
^ ")"
| Pexp_match (match_expr, cases) ->
"Pexp_match("
^ print_expr_item match_expr ~pos ~indentation:0
Expand Down
2 changes: 1 addition & 1 deletion analysis/src/hint.ml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ let inlay ~source ~kind_file ~pos ~max_length ~full ~state ~debug =
match expr.pexp_desc with
| Pexp_constant _ | Pexp_tuple _ | Pexp_record _ | Pexp_variant _
| Pexp_apply _ | Pexp_match _ | Pexp_construct _ | Pexp_ifthenelse _
| Pexp_ternary _ | Pexp_array _ | Pexp_ident _ | Pexp_try _
| Pexp_ternary _ | Pexp_array _ | Pexp_dict _ | Pexp_ident _ | Pexp_try _
| Pexp_object_get _ | Pexp_object_set _ | Pexp_field _ | Pexp_open _
| Pexp_fun _ | Pexp_template _ | Pexp_tagged_template _ ->
push vb.pvb_pat.ppat_loc Type
Expand Down
3 changes: 0 additions & 3 deletions analysis/src/semantic_tokens.ml
Original file line number Diff line number Diff line change
Expand Up @@ -265,9 +265,6 @@ let command ~debug ~emitter ~source ~kind_file =
|> List.exists (fun ({Location.txt}, _) -> txt = "res.spread")
in
not has_spread_attr
(* Dict syntax (`dict{...}`) is converted to `Primitive_dict.make` *)
| Ldot (Lident "Primitive_dict", "make") -> false
| Lident "Primitive_dict" -> false
(* Array access (`arr[index]`) is converted to `Array.get` *)
| Ldot (Lident "Array", "get") -> false
(* Array mutation (`arr[index]`) is converted to `Array.set` *)
Expand Down
2 changes: 2 additions & 0 deletions analysis/src/shared_types.ml
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,7 @@ module Completable = struct
type context_path =
| CPString
| CPArray of context_path option
| CPDict
| CPInt
| CPFloat
| CPBool
Expand Down Expand Up @@ -734,6 +735,7 @@ module Completable = struct
| CPArray (Some ctx_path) ->
"array<" ^ context_path_to_string ctx_path ^ ">"
| CPArray None -> "array"
| CPDict -> "dict"
| CPId {path; completion_context} ->
completion_context_to_string completion_context ^ list path
| CPField {context_path = cp; field_name = s} ->
Expand Down
1 change: 1 addition & 0 deletions analysis/src/utils.ml
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ let identify_pexp pexp =
| Pexp_await _ -> "Pexp_await"
| Pexp_jsx_element _ -> "Pexp_jsx_element"
| Pexp_regexp _ -> "Pexp_regexp"
| Pexp_dict _ -> "Pexp_dict"
Comment thread
cknitt marked this conversation as resolved.
Resolved
Comment thread
cknitt marked this conversation as resolved.
Resolved
| Pexp_template _ -> "Pexp_template"
| Pexp_tagged_template _ -> "Pexp_tagged_template"

Expand Down
2 changes: 1 addition & 1 deletion compiler/core/lam_analysis.ml
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ let rec no_side_effects (lam : Lambda.t) : bool =
true
(* A tagged template invokes its tag at runtime, so it always has side
effects. *)
| Ptagged_template _ | Pjs_call _ | Pinit_mod | Pupdate_mod
| Ptagged_template _ | Pjs_call _ | Pdict_spread | Pinit_mod | Pupdate_mod
| Pjs_object_get _ | Pjs_object_set _ | Pdebugger | Pjs_fn_method
(* Await promise *)
| Pawait
Expand Down
5 changes: 5 additions & 0 deletions compiler/core/lam_compile_primitive.ml
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,11 @@ let translate output_prefix loc (cxt : Lam_compile_context.t)
Some (Js_op.Lit txt, expr)
| _ -> None))
| _ -> assert false)
| Pdict_spread ->
E.call
~info:(Js_call_info.na_full_call false)
(E.js_global "Object.assign")
args
| Pdict_has -> (
match args with
| [obj; prop] -> E.in_ prop obj
Expand Down
6 changes: 3 additions & 3 deletions compiler/ext/config.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ let cmi_magic_number = "Caml1999I035"

(* Magic numbers for marshaled values of the *current* parsetree, whose layout
changes across compiler versions. *)
and ast_impl_magic_number = "ResImpl01313"
and ast_impl_magic_number = "ResImpl01314"

and ast_intf_magic_number = "ResIntf01313"
and ast_intf_magic_number = "ResIntf01314"

(* Magic numbers of the frozen Parsetree0 (OCaml 4.06) layout used on the
external-PPX wire. They must never be written in front of a
Expand All @@ -13,6 +13,6 @@ and ast0_impl_magic_number = "Caml1999M022"

and ast0_intf_magic_number = "Caml1999N022"

and cmt_magic_number = "Caml1999T039"
and cmt_magic_number = "Caml1999T040"

let load_path = ref ([] : string list)
105 changes: 105 additions & 0 deletions compiler/ml/ast_dict.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
(* 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)])];
- 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]. *)

open Parsetree

type ('row, 'spread) part = Rows of 'row list | Spread of 'spread

(* Group consecutive rows *)
let group (entries : [`Row of 'row | `Spread of 'spread] list) :
('row, 'spread) part list =
let flush rows acc =
match rows with
| [] -> acc
| _ -> Rows (List.rev rows) :: acc
in
let rec loop rows acc = function
| [] -> List.rev (flush rows acc)
| `Row row :: rest -> loop (row :: rows) acc rest
| `Spread spread :: rest -> loop [] (Spread spread :: flush rows acc) rest
in
loop [] [] entries

(* The rows of a dict without spreads, or the target and the sources it's
built from *)
let target_and_sources (parts : ('row, 'spread) part list) =
match parts with
| [] -> `Rows []
| [Rows rows] -> `Rows rows
| Rows target :: sources -> `Spread (target, sources)
| sources -> `Spread ([], sources)

let entries (entries : dict_entry list) =
List.map
(function
| Pdict_entry (key, value) -> `Row (key, value)
| Pdict_spread spread -> `Spread spread)
entries

let parts dict_entries = group (entries dict_entries)

let mk_loc loc_start loc_end = {Location.loc_start; loc_end; loc_ghost = false}

(* From the first key to the last value *)
let rows_loc ~loc (rows : (string Location.loc * expression) list) =
match (rows, List.rev rows) with
| (first_key, _) :: _, (_, last_value) :: _ ->
mk_loc first_key.loc.loc_start last_value.pexp_loc.loc_end
| _ -> loc

(* [[("a", 1), ...]], the argument of [Primitive_dict.make] *)
let rows_array ~loc (rows : (string Location.loc * expression) list) =
Ast_helper.Exp.array ~loc
(List.map
(fun ((key : string Location.loc), value) ->
Ast_helper.Exp.tuple
~loc:(mk_loc key.loc.loc_start value.pexp_loc.loc_end)
[
Ast_helper.Exp.constant ~loc:key.loc
(Ast_helper.Const.string key.txt);
value;
])
rows)

let primitive_dict ~loc ?attrs name =
Ast_helper.Exp.ident ~loc ?attrs
(Location.mkloc
(Longident.Ldot (Longident.Lident Primitive_modules.dict, name))
loc)

let spread_marker = "res.dictSpread"

let make ~loc rows =
Ast_helper.Exp.apply ~loc
(primitive_dict ~loc "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
| `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,
Ast_helper.Exp.array ~loc
(List.map
(function
| Rows rows -> make ~loc:(rows_loc ~loc rows) rows
| Spread spread -> spread)
sources) );
]
in
{e with pexp_attributes = attrs}
1 change: 1 addition & 0 deletions compiler/ml/ast_helper.ml
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ module Exp = struct
let field ?loc ?attrs a b = mk ?loc ?attrs (Pexp_field (a, b))
let setfield ?loc ?attrs a b c = mk ?loc ?attrs (Pexp_setfield (a, b, c))
let array ?loc ?attrs a = mk ?loc ?attrs (Pexp_array a)
let dict ?loc ?attrs a = mk ?loc ?attrs (Pexp_dict a)
let ifthenelse ?loc ?attrs a b c = mk ?loc ?attrs (Pexp_ifthenelse (a, b, c))
let ternary ?loc ?attrs condition consequent alternate =
mk ?loc ?attrs (Pexp_ternary (condition, consequent, alternate))
Expand Down
1 change: 1 addition & 0 deletions compiler/ml/ast_helper.mli
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ module Exp : sig
val setfield :
?loc:loc -> ?attrs:attrs -> expression -> lid -> expression -> expression
val array : ?loc:loc -> ?attrs:attrs -> expression list -> expression
val dict : ?loc:loc -> ?attrs:attrs -> dict_entry list -> expression
val ifthenelse :
?loc:loc ->
?attrs:attrs ->
Expand Down
8 changes: 8 additions & 0 deletions compiler/ml/ast_iterator.ml
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,14 @@ module E = struct
iter_loc sub lid;
sub.expr sub e2
| Pexp_array el -> List.iter (sub.expr sub) el
| Pexp_dict entries ->
List.iter
(function
| Pdict_entry (key, e) ->
iter_loc sub key;
sub.expr sub e
| Pdict_spread e -> sub.expr sub e)
entries
| Pexp_ifthenelse (e1, e2, e3) ->
sub.expr sub e1;
sub.expr sub e2;
Expand Down
8 changes: 8 additions & 0 deletions compiler/ml/ast_mapper.ml
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,14 @@ module E = struct
| Pexp_setfield (e1, lid, e2) ->
setfield ~loc ~attrs (sub.expr sub e1) (map_loc sub lid) (sub.expr sub e2)
| Pexp_array el -> array ~loc ~attrs (List.map (sub.expr sub) el)
| Pexp_dict entries ->
dict ~loc ~attrs
(List.map
(function
| Pdict_entry (key, e) ->
Pdict_entry (map_loc sub key, sub.expr sub e)
| Pdict_spread e -> Pdict_spread (sub.expr sub e))
entries)
| Pexp_ifthenelse (e1, e2, e3) ->
ifthenelse ~loc ~attrs (sub.expr sub e1) (sub.expr sub e2)
(map_opt (sub.expr sub) e3)
Expand Down
Loading
Loading