Skip to content

Commit 13a3f63

Browse files
committed
Untagged variants: improve error messages for cases not allowed
Fixes #6116
1 parent 24563e0 commit 13a3f63

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

jscomp/core/matching_polyfill.ml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@ let checkUntaggedVariant ~(blocks : (Location.t * Lambda.block) list) =
3131
let unknowns = ref 0 in
3232
let invariant loc =
3333
if !unknowns <> 0 && (List.length blocks <> 1)
34-
then Bs_syntaxerr.err loc InvalidUntaggedVariantDefinition;
35-
if !objects > 1 || !arrays > 1
36-
then Bs_syntaxerr.err loc InvalidUntaggedVariantDefinition;
34+
then Bs_syntaxerr.err loc (InvalidUntaggedVariantDefinition OnlyOneUnknown);
35+
if !objects > 1
36+
then Bs_syntaxerr.err loc (InvalidUntaggedVariantDefinition AtMostOneObject);
37+
if !arrays > 1
38+
then Bs_syntaxerr.err loc (InvalidUntaggedVariantDefinition AtMostOneArray);
3739
() in
3840
Ext_list.rev_iter blocks (fun (loc, block) -> match block.block_type with
3941
| Some Unknown ->

jscomp/frontend/bs_syntaxerr.ml

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
* along with this program; if not, write to the Free Software
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2424

25-
type error =
25+
type untaggedVariant = | OnlyOneUnknown | AtMostOneObject | AtMostOneArray
26+
27+
type error =
2628
| Unsupported_predicates
2729
| Conflict_bs_bs_this_bs_meth
2830
| Duplicated_bs_deriving
@@ -53,7 +55,7 @@ type error =
5355
| Bs_uncurried_arity_too_large
5456
| InvalidVariantAsAnnotation
5557
| InvalidVariantTagAnnotation
56-
| InvalidUntaggedVariantDefinition
58+
| InvalidUntaggedVariantDefinition of untaggedVariant
5759

5860
let pp_error fmt err =
5961
Format.pp_print_string fmt
@@ -104,8 +106,13 @@ let pp_error fmt err =
104106
"A variant case annotation @as(...) must be a string or integer, boolean, null, undefined"
105107
| InvalidVariantTagAnnotation ->
106108
"A variant tag annotation @tag(...) must be a string"
107-
| InvalidUntaggedVariantDefinition ->
108-
"This untagged variant definition is invalid. But since I'm still work in progress I am not able to tell you why."
109+
| InvalidUntaggedVariantDefinition untaggedVariant ->
110+
"This untagged variant definition is invalid: " ^
111+
(match untaggedVariant with
112+
| OnlyOneUnknown -> "An unknown case must be the only case with payloads."
113+
| AtMostOneObject -> "At most one case can be an object type."
114+
| AtMostOneArray -> "At most one case can be an array type."
115+
)
109116
)
110117

111118
type exn += Error of Location.t * error

jscomp/frontend/bs_syntaxerr.mli

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
* along with this program; if not, write to the Free Software
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2424

25+
type untaggedVariant = | OnlyOneUnknown | AtMostOneObject | AtMostOneArray
26+
2527
type error =
2628
| Unsupported_predicates
2729
| Conflict_bs_bs_this_bs_meth
@@ -53,7 +55,7 @@ type error =
5355
| Bs_uncurried_arity_too_large
5456
| InvalidVariantAsAnnotation
5557
| InvalidVariantTagAnnotation
56-
| InvalidUntaggedVariantDefinition
58+
| InvalidUntaggedVariantDefinition of untaggedVariant
5759

5860
val err : Location.t -> error -> 'a
5961

0 commit comments

Comments
 (0)