Skip to content

Commit ccea327

Browse files
authored
improve error message for duplicate labels (#6415)
1 parent f755a1c commit ccea327

File tree

4 files changed

+26
-9
lines changed

4 files changed

+26
-9
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
We've found a bug for you!
3+
/.../fixtures/duplicate_labels_error.res:3:3-6
4+
5+
1 │ type rcrd = {
6+
2 │ name: string,
7+
3 │ name: int,
8+
4 │ }
9+
5 │
10+
11+
The field name is defined several times in the record rcrd. Fields can only be added once to a record.

jscomp/build_tests/super_errors/expected/record_type_spreads.res.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
5 │ type t3 = {...t, ...t2}
88
6 │
99

10-
Two labels are named x
10+
The field x is defined several times in the record t3. Fields can only be added once to a record.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
type rcrd = {
2+
name: string,
3+
name: int,
4+
}

jscomp/ml/typedecl.ml

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ type native_repr_kind = Unboxed | Untagged
2727
type error =
2828
Repeated_parameter
2929
| Duplicate_constructor of string
30-
| Duplicate_label of string
30+
| Duplicate_label of string * string option
3131
| Recursive_abbrev of string
3232
| Cycle_in_def of string * type_expr
3333
| Definition_mismatch of type_expr * Includecore.type_mismatch list
@@ -207,17 +207,17 @@ let make_params env params =
207207
in
208208
List.map make_param params
209209

210-
let transl_labels env closed lbls =
210+
let transl_labels ?recordName env closed lbls =
211211
if !Config.bs_only then
212212
match !Builtin_attributes.check_duplicated_labels lbls with
213213
| None -> ()
214-
| Some {loc;txt=name} -> raise (Error(loc,Duplicate_label name))
214+
| Some {loc;txt=name} -> raise (Error(loc,Duplicate_label (name, recordName)))
215215
else (
216216
let all_labels = ref StringSet.empty in
217217
List.iter
218218
(fun {pld_name = {txt=name; loc}} ->
219219
if StringSet.mem name !all_labels then
220-
raise(Error(loc, Duplicate_label name));
220+
raise(Error(loc, Duplicate_label (name, recordName)));
221221
all_labels := StringSet.add name !all_labels)
222222
lbls);
223223
let mk {pld_name=name;pld_mutable=mut;pld_type=arg;pld_loc=loc;
@@ -501,7 +501,7 @@ let transl_declaration ~typeRecordAsObject env sdecl id =
501501
{typ with ptyp_desc = Ptyp_constr ({txt = Lident "option"; loc=typ.ptyp_loc}, [typ])}
502502
else typ in
503503
{lbl with pld_type = typ }) in
504-
let lbls, lbls' = transl_labels env true lbls in
504+
let lbls, lbls' = transl_labels ~recordName:(sdecl.ptype_name.txt) env true lbls in
505505
let lbls_opt = match Record_type_spread.has_type_spread lbls with
506506
| true ->
507507
let rec extract t = match t.desc with
@@ -545,7 +545,7 @@ let transl_declaration ~typeRecordAsObject env sdecl id =
545545
| [] -> ()
546546
| lbl::rest ->
547547
let name = lbl.ld_id.name in
548-
if StringSet.mem name seen then raise(Error(loc, Duplicate_label name));
548+
if StringSet.mem name seen then raise(Error(loc, Duplicate_label (name, Some sdecl.ptype_name.txt)));
549549
check_duplicates loc rest (StringSet.add name seen) in
550550
(match lbls_opt with
551551
| Some (lbls, lbls') ->
@@ -1998,8 +1998,10 @@ let report_error ppf = function
19981998
fprintf ppf "A type parameter occurs several times"
19991999
| Duplicate_constructor s ->
20002000
fprintf ppf "Two constructors are named %s" s
2001-
| Duplicate_label s ->
2002-
fprintf ppf "Two labels are named %s" s
2001+
| Duplicate_label (s, None) ->
2002+
fprintf ppf "The field @{<info>%s@} is defined several times in this record. Fields can only be added once to a record." s
2003+
| Duplicate_label (s, Some recordName) ->
2004+
fprintf ppf "The field @{<info>%s@} is defined several times in the record @{<info>%s@}. Fields can only be added once to a record." s recordName
20032005
| Recursive_abbrev s ->
20042006
fprintf ppf "The type abbreviation %s is cyclic" s
20052007
| Cycle_in_def (s, ty) ->

0 commit comments

Comments
 (0)