Skip to content

Commit 249a0e4

Browse files
committed
Improve error message for unknown case.
1 parent 2fd5256 commit 249a0e4

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

jscomp/build_tests/super_errors/expected/UntaggedUnknown.res.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
2 │ type t = Tuple((float, string)) | Float(float)
77
3 │
88

9-
This untagged variant definition is invalid: An unknown case must be the only case with payloads.
9+
This untagged variant definition is invalid: Case Tuple has a payload that is not of one of the recognized shapes (object, array, etc). Then it must be the only case with payloads.

jscomp/ml/ast_untagged_variants.ml

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
type untaggedError =
2-
| OnlyOneUnknown
2+
| OnlyOneUnknown of string
33
| AtMostOneObject
44
| AtMostOneArray
55
| AtMostOneFunction
@@ -27,7 +27,7 @@ let report_error ppf =
2727
| InvalidUntaggedVariantDefinition untaggedVariant ->
2828
fprintf ppf "This untagged variant definition is invalid: %s"
2929
(match untaggedVariant with
30-
| OnlyOneUnknown -> "An unknown case must be the only case with payloads."
30+
| OnlyOneUnknown name -> "Case " ^ name ^ " has a payload that is not of one of the recognized shapes (object, array, etc). Then it must be the only case with payloads."
3131
| AtMostOneObject -> "At most one case can be an object type."
3232
| AtMostOneArray -> "At most one case can be an array type."
3333
| AtMostOneFunction -> "At most one case can be a function type."
@@ -204,9 +204,9 @@ let checkInvariant ~isUntaggedDef ~(consts : (Location.t * tag) list)
204204
raise (Error (loc, InvalidUntaggedVariantDefinition (DuplicateLiteral s)));
205205
nonstring_literals := StringSet.add s !nonstring_literals
206206
in
207-
let invariant loc =
207+
let invariant loc name =
208208
if !unknownTypes <> 0 && List.length blocks <> 1 then
209-
raise (Error (loc, InvalidUntaggedVariantDefinition OnlyOneUnknown));
209+
raise (Error (loc, InvalidUntaggedVariantDefinition (OnlyOneUnknown name)));
210210
if !objectTypes > 1 then
211211
raise (Error (loc, InvalidUntaggedVariantDefinition AtMostOneObject));
212212
if !arrayTypes > 1 then
@@ -232,25 +232,26 @@ let checkInvariant ~isUntaggedDef ~(consts : (Location.t * tag) list)
232232
| None -> addStringLiteral ~loc literal.name);
233233
if isUntaggedDef then
234234
Ext_list.rev_iter blocks (fun (loc, block) ->
235+
let name = block.tag.name in
235236
match block.block_type with
236237
| Some UnknownType ->
237238
incr unknownTypes;
238-
invariant loc
239+
invariant loc name
239240
| Some ObjectType ->
240241
incr objectTypes;
241-
invariant loc
242+
invariant loc name
242243
| Some ArrayType ->
243244
incr arrayTypes;
244-
invariant loc
245+
invariant loc name
245246
| Some FunctionType ->
246247
incr functionTypes;
247-
invariant loc
248+
invariant loc name
248249
| Some (IntType | FloatType) ->
249250
incr numberTypes;
250-
invariant loc
251+
invariant loc name
251252
| Some StringType ->
252253
incr stringTypes;
253-
invariant loc
254+
invariant loc name
254255
| None -> ())
255256

256257
let names_from_type_variant ?(isUntaggedDef = false) ~env

0 commit comments

Comments
 (0)