Skip to content

Commit cb0ab83

Browse files
committed
add a warning when rest record would be empty
1 parent 60267ff commit cb0ab83

3 files changed

Lines changed: 16 additions & 1 deletion

File tree

compiler/ext/warnings.ml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ type t =
8888
| Bs_toplevel_expression_unit of
8989
(string * top_level_unit_help) option (* 109 *)
9090
| Bs_todo of string option (* 110 *)
91+
| Bs_record_rest_empty (* 111 *)
9192

9293
(* If you remove a warning, leave a hole in the numbering. NEVER change
9394
the numbers of existing warnings.
@@ -154,8 +155,9 @@ let number = function
154155
| Bs_uninterpreted_delimiters _ -> 108
155156
| Bs_toplevel_expression_unit _ -> 109
156157
| Bs_todo _ -> 110
158+
| Bs_record_rest_empty -> 111
157159

158-
let last_warning_number = 110
160+
let last_warning_number = 111
159161

160162
let letter_all =
161163
let rec loop i = if i = 0 then [] else i :: loop (i - 1) in
@@ -532,6 +534,9 @@ let message = function
532534
`%s->ignore`"
533535
help_text help_text
534536
| _ -> "")
537+
| Bs_record_rest_empty ->
538+
"All fields of the rest type are already present in the explicit pattern. \
539+
The rest record will always be empty."
535540
| Bs_todo maybe_text ->
536541
(match maybe_text with
537542
| None -> "Todo found."

compiler/ext/warnings.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ type t =
8181
| Bs_toplevel_expression_unit of
8282
(string * top_level_unit_help) option (* 109 *)
8383
| Bs_todo of string option (* 110 *)
84+
| Bs_record_rest_empty (* 111 *)
8485

8586
val parse_options : bool -> string -> unit
8687

compiler/ml/typecore.ml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1608,6 +1608,15 @@ and type_pat_aux ~constrs ~labels ~no_existentials ~mode ~explode ~env sp
16081608
(fun (l : Types.label_declaration) -> Ident.name l.ld_id)
16091609
rest_labels
16101610
in
1611+
(* Warn if all rest fields are already explicit — the rest record will be empty *)
1612+
if
1613+
rest_field_names <> []
1614+
&& List.for_all
1615+
(fun f -> List.mem f explicit_fields)
1616+
rest_field_names
1617+
then
1618+
Location.prerr_warning rest_pat.ppat_loc
1619+
Warnings.Bs_record_rest_empty;
16111620
(* Validate: fields in both explicit and rest must be optional in the explicit pattern *)
16121621
let not_optional =
16131622
List.filter

0 commit comments

Comments
 (0)