Skip to content

Commit f6968ec

Browse files
committed
Support nullary variants.
1 parent 8d6d8b7 commit f6968ec

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

compiler/ml/ast_untagged_variants.ml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,13 @@ let block_type_can_be_undefined = function
9797
false
9898
| UnknownType -> true
9999

100+
let tag_can_be_undefined tag =
101+
match tag.tag_type with
102+
| None -> false
103+
| Some (String _ | Int _ | Float _ | BigInt _ | Bool _ | Null) -> false
104+
| Some (Untagged block_type) -> block_type_can_be_undefined block_type
105+
| Some Undefined -> true
106+
100107
let has_untagged (attrs : Parsetree.attributes) =
101108
Ext_list.exists attrs (function {txt}, _ -> txt = untagged)
102109

compiler/ml/parmatch.ml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,11 @@ let all_record_args lbls =
551551
in
552552
match cdecl with
553553
| None -> x
554+
| Some cstr
555+
when Ast_untagged_variants.is_nullary_variant cstr.cd_args ->
556+
let _, tag = Ast_untagged_variants.get_cstr_loc_tag cstr in
557+
if Ast_untagged_variants.tag_can_be_undefined tag then x
558+
else (id, lbl, pat_construct)
554559
| Some cstr -> (
555560
match
556561
Ast_untagged_variants.get_block_type ~env:pat.pat_env cstr

tests/tests/src/pattern_match_json.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ function decodeGroup(group) {
3030
}
3131

3232
function decodeNull(x) {
33-
let match = x.field;
34-
if (match !== undefined && match === null) {
33+
let tmp = x.field;
34+
if (tmp === null) {
3535
return "yes it's null";
3636
} else {
3737
return "no";

0 commit comments

Comments
 (0)