Skip to content

Commit 0c03e7b

Browse files
cknittclaude
andcommitted
Represent variant spread patterns with a Ppat_variant_spread node
A variant spread pattern ...t was Ppat_type, the node of the polymorphic variant pattern #...t, with a res.patVariantSpread attribute telling the two apart. It now has its own node; the v0 bridge keeps the old encoding for PPXs. The type checker expands it as before, and it no longer counts as a polymorphic variant pattern. The printer no longer needs to filter the attribute. Signed-off-by: Christoph Knittel <christoph@knittel.cc> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 944c476 commit 0c03e7b

27 files changed

Lines changed: 114 additions & 53 deletions

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
#### :house: Internal
4949

5050
- 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
51+
- 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. PR_LINK
5152
- Remove the obsolete `res.ternary` and `JSX` attributes from the formatter's internal attribute filters and merge the filters into one. https://github.com/rescript-lang/rescript/pull/8740
5253
- Represent `await` on module expressions with a dedicated `Pmod_await` parsetree node instead of a `res.await` attribute, which remains only in the PPX wire representation. https://github.com/rescript-lang/rescript/pull/8738
5354
- Remove dead compiler code: warning 24, the `J.String_index` JS IR node, the `Pisint` and `Parraysets` Lambda primitives with their unused `%` primitive names (`%obj_is_int`, `%obj_dup`, `%obj_field`, `%array_safe_set`, `%null_to_opt`, `%nullable_to_opt`), the no-op `-pp` bsc flag and unread `Clflags` fields (`-unsafe` stays accepted as an explicit no-op), and the unused `unit` slot of `Pexp_coerce`. https://github.com/rescript-lang/rescript/pull/8731

‎analysis/src/completion_front_end.ml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ let completion_with_parser1 ~debug ~offset ~pos_cursor ~kind_file
537537
scope_pattern ~pattern_path
538538
?context_path:(Type_utils.context_path_from_core_type core_type)
539539
p
540-
| Ppat_type _ -> ()
540+
| Ppat_type _ | Ppat_variant_spread _ -> ()
541541
| Ppat_unpack {txt; loc} ->
542542
scope := !scope |> Scope.add_module ~name:txt ~loc
543543
| Ppat_exception p -> scope_pattern ~pattern_path ?context_path p

‎analysis/src/semantic_tokens.ml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ let command ~debug ~emitter ~source ~kind_file =
244244
| Ppat_construct (name, _) ->
245245
emitter |> emit_variant ~name ~debug;
246246
Ast_iterator.default_iterator.pat iterator p
247-
| Ppat_type {txt = lid; loc} ->
247+
| Ppat_type {txt = lid; loc} | Ppat_variant_spread {txt = lid; loc} ->
248248
emitter |> emit_type ~lid ~debug ~loc;
249249
Ast_iterator.default_iterator.pat iterator p
250250
| _ -> Ast_iterator.default_iterator.pat iterator p

‎analysis/src/utils.ml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ let identify_ppat pat =
139139
| Ppat_or _ -> "Ppat_or"
140140
| Ppat_constraint _ -> "Ppat_constraint"
141141
| Ppat_type _ -> "Ppat_type"
142+
| Ppat_variant_spread _ -> "Ppat_variant_spread"
142143
| Ppat_unpack _ -> "Ppat_unpack"
143144
| Ppat_exception _ -> "Ppat_exception"
144145
| Ppat_extension _ -> "Ppat_extension"

‎compiler/ext/config.ml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ let cmi_magic_number = "Caml1999I035"
22

33
(* Magic numbers for marshaled values of the *current* parsetree, whose layout
44
changes across compiler versions. *)
5-
and ast_impl_magic_number = "ResImpl01312"
5+
and ast_impl_magic_number = "ResImpl01313"
66

7-
and ast_intf_magic_number = "ResIntf01312"
7+
and ast_intf_magic_number = "ResIntf01313"
88

99
(* Magic numbers of the frozen Parsetree0 (OCaml 4.06) layout used on the
1010
external-PPX wire. They must never be written in front of a

‎compiler/ml/ast_helper.ml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ module Pat = struct
142142
let or_ ?loc ?attrs a b = mk ?loc ?attrs (Ppat_or (a, b))
143143
let constraint_ ?loc ?attrs a b = mk ?loc ?attrs (Ppat_constraint (a, b))
144144
let type_ ?loc ?attrs a = mk ?loc ?attrs (Ppat_type a)
145+
let variant_spread ?loc ?attrs a = mk ?loc ?attrs (Ppat_variant_spread a)
145146
let unpack ?loc ?attrs a = mk ?loc ?attrs (Ppat_unpack a)
146147
let exception_ ?loc ?attrs a = mk ?loc ?attrs (Ppat_exception a)
147148
let extension ?loc ?attrs a = mk ?loc ?attrs (Ppat_extension a)

‎compiler/ml/ast_helper.mli‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ module Pat : sig
107107
val or_ : ?loc:loc -> ?attrs:attrs -> pattern -> pattern -> pattern
108108
val constraint_ : ?loc:loc -> ?attrs:attrs -> pattern -> core_type -> pattern
109109
val type_ : ?loc:loc -> ?attrs:attrs -> lid -> pattern
110+
val variant_spread : ?loc:loc -> ?attrs:attrs -> lid -> pattern
110111
val unpack : ?loc:loc -> ?attrs:attrs -> str -> pattern
111112
val exception_ : ?loc:loc -> ?attrs:attrs -> pattern -> pattern
112113
val extension : ?loc:loc -> ?attrs:attrs -> extension -> pattern

‎compiler/ml/ast_iterator.ml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ module P = struct
464464
| Ppat_constraint (p, t) ->
465465
sub.pat sub p;
466466
sub.typ sub t
467-
| Ppat_type s -> iter_loc sub s
467+
| Ppat_type s | Ppat_variant_spread s -> iter_loc sub s
468468
| Ppat_unpack s -> iter_loc sub s
469469
| Ppat_exception p -> sub.pat sub p
470470
| Ppat_extension x -> sub.extension sub x

‎compiler/ml/ast_mapper.ml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,7 @@ module P = struct
464464
| Ppat_constraint (p, t) ->
465465
constraint_ ~loc ~attrs (sub.pat sub p) (sub.typ sub t)
466466
| Ppat_type s -> type_ ~loc ~attrs (map_loc sub s)
467+
| Ppat_variant_spread s -> variant_spread ~loc ~attrs (map_loc sub s)
467468
| Ppat_unpack s -> unpack ~loc ~attrs (map_loc sub s)
468469
| Ppat_exception p -> exception_ ~loc ~attrs (sub.pat sub p)
469470
| Ppat_extension x -> extension ~loc ~attrs (sub.extension sub x)

‎compiler/ml/ast_mapper_from0.ml‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,17 @@ let extract_internal_loc_attr attr_name attrs =
4444
let await_marker_loc ~node_loc (marker_loc : Location.t) =
4545
if marker_loc = Location.none then node_loc else marker_loc
4646

47+
(* The [res.patVariantSpread] marker that [Ast_mapper_to0] puts on a variant
48+
spread pattern, with any payload *)
49+
let extract_variant_spread_attr (attrs : Pt.attributes) =
50+
let rec loop rev_acc = function
51+
| [] -> (false, List.rev rev_acc)
52+
| ({txt = "res.patVariantSpread"}, _) :: rest ->
53+
(true, List.rev_append rev_acc rest)
54+
| attr :: rest -> loop (attr :: rev_acc) rest
55+
in
56+
loop [] attrs
57+
4758
let extract_ternary_attr (attrs : Pt.attributes) =
4859
let rec loop rev_acc = function
4960
| [] -> (false, List.rev rev_acc)
@@ -1327,7 +1338,10 @@ module P = struct
13271338
| Ppat_or (p1, p2) -> or_ ~loc ~attrs (sub.pat sub p1) (sub.pat sub p2)
13281339
| Ppat_constraint (p, t) ->
13291340
constraint_ ~loc ~attrs (sub.pat sub p) (sub.typ sub t)
1330-
| Ppat_type s -> type_ ~loc ~attrs (map_loc sub s)
1341+
| Ppat_type s -> (
1342+
match extract_variant_spread_attr attrs with
1343+
| true, attrs -> variant_spread ~loc ~attrs (map_loc sub s)
1344+
| false, _ -> type_ ~loc ~attrs (map_loc sub s))
13311345
| Ppat_lazy _ -> failwith "Ppat_lazy is no longer present in ReScript"
13321346
| Ppat_unpack s -> unpack ~loc ~attrs (map_loc sub s)
13331347
| Ppat_open _ -> failwith "Ppat_open is no longer present in ReScript"

0 commit comments

Comments
 (0)