Skip to content

Commit 6b455b4

Browse files
cknittclaude
andcommitted
Record variant spread origins in the typed tree
The constructor patterns a variant spread ...t expands into were tagged with a res.patFromVariantSpread attribute, so that the unused-pattern check doesn't report the ones earlier cases already match. They now get a Tpat_variant_spread entry in pat_extra instead. The or-patterns grouping them inside the expansion are marked too: when earlier cases matched several of them, that group was reported as an unused sub-pattern without a location. The root of the expansion stays unmarked, so a spread that is unused as a whole is still reported. Signed-off-by: Christoph Knittel <christoph@knittel.cc> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 30d64b5 commit 6b455b4

13 files changed

Lines changed: 98 additions & 31 deletions

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
#### :bug: Bug fix
2525

26+
- Don't report constructors of a variant spread pattern (`...t`) that earlier cases already match as an unused sub-pattern without a location (warning 12). PR_LINK
2627
- Keep `@warning("-4")` on `if` expressions when formatting; the formatter dropped it. https://github.com/rescript-lang/rescript/pull/8741
2728
- Fix the formatter dropping a `@res.ternary` attribute written in source, e.g. `let b = @res.ternary x`, and print `@JSX` and `@res.ternary` with the same parens as other attributes, e.g. `(@JSX x) + 1`. https://github.com/rescript-lang/rescript/pull/8740
2829
- Respect `@warning` attributes on the module in `module type of`, e.g. `module type of @warning("-3") DeprecatedModule`, which still reported the warning. https://github.com/rescript-lang/rescript/pull/8738

‎compiler/ext/config.ml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ and ast0_impl_magic_number = "Caml1999M022"
1313

1414
and ast0_intf_magic_number = "Caml1999N022"
1515

16-
and cmt_magic_number = "Caml1999T039"
16+
and cmt_magic_number = "Caml1999T040"
1717

1818
let load_path = ref ([] : string list)

‎compiler/ml/parmatch.ml‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,8 @@ let rec pretty_val ppf v =
384384
| Tpat_constraint _ ->
385385
fprintf ppf "@[(%a : _)@]" pretty_val {v with pat_extra = rem}
386386
| Tpat_type _ ->
387-
fprintf ppf "@[(# %a)@]" pretty_val {v with pat_extra = rem})
387+
fprintf ppf "@[(# %a)@]" pretty_val {v with pat_extra = rem}
388+
| Tpat_variant_spread _ -> pretty_val ppf {v with pat_extra = rem})
388389
| [] -> (
389390
match v.pat_desc with
390391
| Tpat_any -> fprintf ppf "_"
@@ -2141,7 +2142,14 @@ let check_unused pred casel =
21412142
| Upartial ps ->
21422143
ps
21432144
|> List.filter (fun p ->
2144-
not (Variant_type_spread.is_pat_from_variant_spread_attr p))
2145+
(* Constructors of a variant spread weren't written by the
2146+
user *)
2147+
not
2148+
(List.exists
2149+
(function
2150+
| Tpat_variant_spread _, _, _ -> true
2151+
| _ -> false)
2152+
p.pat_extra))
21452153
|> List.iter (fun p ->
21462154
Location.prerr_warning p.pat_loc Warnings.Unused_pat)
21472155
| Used -> ()

‎compiler/ml/printtyped.ml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,10 @@ and pattern i ppf x =
213213
line i ppf "Tpat_type %a\n" fmt_path id;
214214
attributes i ppf attrs;
215215
pattern i ppf {x with pat_extra = rem}
216+
| (Tpat_variant_spread (id, _), _, attrs) :: rem ->
217+
line i ppf "Tpat_variant_spread %a\n" fmt_path id;
218+
attributes i ppf attrs;
219+
pattern i ppf {x with pat_extra = rem}
216220
| [] -> (
217221
match x.pat_desc with
218222
| Tpat_any -> line i ppf "Tpat_any\n"

‎compiler/ml/tast_iterator.ml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ let extension_constructor sub {ext_kind; _} =
115115

116116
let pat sub {pat_extra; pat_desc; pat_env; _} =
117117
let extra = function
118-
| Tpat_type _ -> ()
118+
| Tpat_type _ | Tpat_variant_spread _ -> ()
119119
| Tpat_unpack -> ()
120120
| Tpat_constraint ct -> sub.typ sub ct
121121
in

‎compiler/ml/tast_mapper.ml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ let extension_constructor sub x =
158158

159159
let pat sub x =
160160
let extra = function
161-
| (Tpat_type _ | Tpat_unpack) as d -> d
161+
| (Tpat_type _ | Tpat_variant_spread _ | Tpat_unpack) as d -> d
162162
| Tpat_constraint ct -> Tpat_constraint (sub.typ sub ct)
163163
in
164164
let pat_env = sub.env sub x.pat_env in

‎compiler/ml/typecore.ml‎

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -655,18 +655,18 @@ let extract_type_from_pat_variant_spread env lid expected_ty =
655655
(path, decl, constructors, ty)
656656
| _ -> raise (Error (lid.loc, env, Not_a_variant_type lid.txt))
657657

658+
(* The or-pattern of the constructors of the variant [...t] matches, with the
659+
path and type of [t] *)
658660
let build_ppat_or_for_variant_spread pat env expected_ty =
659661
match pat with
660662
| {ppat_desc = Ppat_variant_spread lident} ->
661-
let _, _, constructors, ty =
663+
let path, _, constructors, ty =
662664
extract_type_from_pat_variant_spread !env lident expected_ty
663665
in
664666
let synthetic_or_patterns =
665667
constructors
666668
|> List.map (fun (c : Types.constructor_declaration) ->
667-
Ast_helper.Pat.mk
668-
~attrs:[Variant_type_spread.mk_pat_from_variant_spread_attr ()]
669-
~loc:lident.loc
669+
Ast_helper.Pat.mk ~loc:lident.loc
670670
(Ppat_construct
671671
( Location.mkloc
672672
(Longident.Lident (Ident.name c.cd_id))
@@ -686,16 +686,43 @@ let build_ppat_or_for_variant_spread pat env expected_ty =
686686
| pat :: pats ->
687687
List.fold_left (fun p1 p2 -> Ast_helper.Pat.or_ p1 p2) pat pats
688688
in
689-
Some (pat, ty)
689+
Some (pat, path, ty)
690690
| _ -> None
691691

692-
let maybe_expand_variant_spread_in_pattern pattern env expected_ty =
692+
(* Record on the patterns of the expansion of [...t] that they come from the
693+
spread, so that the ones made unused by earlier cases aren't reported: the
694+
constructors, and the or-patterns grouping them. The root stands for the
695+
spread the user wrote, and is reported when all of it is unused. *)
696+
let mark_variant_spread_constructors path lid (p : Typedtree.pattern) =
697+
let rec mark (p : Typedtree.pattern) =
698+
let p =
699+
match p.pat_desc with
700+
| Tpat_or (p1, p2, row) ->
701+
{p with pat_desc = Tpat_or (mark p1, mark p2, row)}
702+
| _ -> p
703+
in
704+
{
705+
p with
706+
pat_extra = (Tpat_variant_spread (path, lid), lid.loc, []) :: p.pat_extra;
707+
}
708+
in
709+
match p.pat_desc with
710+
| Tpat_or (p1, p2, row) -> {p with pat_desc = Tpat_or (mark p1, mark p2, row)}
711+
| _ -> mark p
712+
713+
(* Expand a variant spread into the or-pattern of its constructors, and mark
714+
them once typed *)
715+
let expand_variant_spread_in_pattern pattern env expected_ty k =
693716
match pattern.Parsetree.ppat_desc with
694-
| Ppat_variant_spread _ -> (
717+
| Ppat_variant_spread lid -> (
695718
match build_ppat_or_for_variant_spread pattern env expected_ty with
696-
| None -> assert false (* TODO: Fix. *)
697-
| Some (pattern, _) -> pattern)
698-
| _ -> pattern
719+
| Some (({ppat_desc = Ppat_variant_spread _} as pattern), _, _) ->
720+
(* No constructors *)
721+
(pattern, k)
722+
| Some (pattern, path, _) ->
723+
(pattern, fun p -> k (mark_variant_spread_constructors path lid p))
724+
| None -> assert false)
725+
| _ -> (pattern, k)
699726

700727
(* Type paths *)
701728

@@ -1237,7 +1264,7 @@ let rec type_pat ~constrs ~labels ~no_existentials ~mode ~explode ~env sp
12371264

12381265
and type_pat_aux ~constrs ~labels ~no_existentials ~mode ~explode ~env sp
12391266
expected_ty k =
1240-
let sp = maybe_expand_variant_spread_in_pattern sp env expected_ty in
1267+
let sp, k = expand_variant_spread_in_pattern sp env expected_ty k in
12411268
let mode' = if mode = Splitting_or then Normal else mode in
12421269
let type_pat ?(constrs = constrs) ?(labels = labels) ?(mode = mode')
12431270
?(explode = explode) ?(env = env) =
@@ -1327,10 +1354,12 @@ and type_pat_aux ~constrs ~labels ~no_existentials ~mode ~explode ~env sp
13271354
| Ppat_alias (sq, name) ->
13281355
let override_type_from_variant_spread, sq =
13291356
match sq with
1330-
| {ppat_desc = Ppat_variant_spread _} -> (
1331-
match build_ppat_or_for_variant_spread sq env expected_ty with
1332-
| Some (p, ty) -> (Some ty, p)
1333-
| None -> (None, sq))
1357+
| {ppat_desc = Ppat_variant_spread lid} ->
1358+
(* [sq] is expanded when typed; [v] in [...t as v] has type [t] *)
1359+
let _, _, _, ty =
1360+
extract_type_from_pat_variant_spread !env lid expected_ty
1361+
in
1362+
(Some ty, sq)
13341363
| _ -> (None, sq)
13351364
in
13361365
assert (constrs = None);

‎compiler/ml/typedtree.ml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ and record_pat_rest = {
4545
and pat_extra =
4646
| Tpat_constraint of core_type
4747
| Tpat_type of Path.t * Longident.t loc
48+
| Tpat_variant_spread of Path.t * Longident.t loc
4849
| Tpat_unpack
4950

5051
and pattern_desc =

‎compiler/ml/typedtree.mli‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ and pat_extra =
6262
where [disjunction] is a [Tpat_or _] representing the
6363
branches of [tconst].
6464
*)
65+
| Tpat_variant_spread of Path.t * Longident.t loc
66+
(** ...tconst on the patterns below the root of the disjunction
67+
that replaces the variant spread [...tconst]: its
68+
constructors and the disjunctions grouping them *)
6569
| Tpat_unpack
6670
(** (module P) { pat_desc = Tpat_var "P"
6771
; pat_extra = (Tpat_unpack, _, _) :: ... }

‎compiler/ml/typedtree_iter.ml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ end = struct
180180
List.iter
181181
(fun (cstr, _, _attrs) ->
182182
match cstr with
183-
| Tpat_type _ -> ()
183+
| Tpat_type _ | Tpat_variant_spread _ -> ()
184184
| Tpat_unpack -> ()
185185
| Tpat_constraint ct -> iter_core_type ct)
186186
pat.pat_extra;

0 commit comments

Comments
 (0)