Skip to content

Commit 4e01321

Browse files
authored
Catch error arising in as_variant_sub when scrutinee is not variant (#2935)
Fixes #2934.
1 parent 4115ece commit 4e01321

File tree

4 files changed

+30
-4
lines changed

4 files changed

+30
-4
lines changed

src/lang_utils/error_codes.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ let error_codes : (string * string option) list =
119119
"M0113", None; (* Object pattern cannot consume type *)
120120
"M0114", None; (* Object pattern cannot consume actor type *)
121121
"M0115", None; (* Option pattern cannot consume type *)
122-
(* "M0116" DEFUNCT Variant pattern cannot consume type *)
122+
"M0116", None; (* Variant pattern cannot consume type *)
123123
"M0117", None; (* Pattern cannot consume type *)
124124
"M0118", None; (* Tuple pattern size mismatch *)
125125
"M0119", None; (* Object field is not contained in type *)

src/mo_frontend/typing.ml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1801,9 +1801,13 @@ and check_pat' env t pat : Scope.val_env =
18011801
in check_pat env t1 pat1
18021802
| TagP (id, pat1) ->
18031803
let t1 =
1804-
match T.lookup_val_field_opt id.it (T.as_variant_sub id.it t) with
1805-
| Some t1 -> t1
1806-
| None -> T.Non
1804+
try
1805+
match T.lookup_val_field_opt id.it (T.as_variant_sub id.it t) with
1806+
| Some t1 -> t1
1807+
| None -> T.Non
1808+
with Invalid_argument _ ->
1809+
error env pat.at "M0116" "variant pattern cannot consume expected type%a"
1810+
display_typ_expand t
18071811
in check_pat env t1 pat1
18081812
| AltP (pat1, pat2) ->
18091813
let ve1 = check_pat env t pat1 in

test/fail/ok/pat-inconsistent.tc.ok

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,11 @@ pat-inconsistent.mo:49.8-49.9: type error [M0050], literal of type
132132
Nat
133133
does not have expected type
134134
Bool
135+
pat-inconsistent.mo:54.9-54.15: type error [M0116], variant pattern cannot consume expected type
136+
Bool
137+
pat-inconsistent.mo:59.9-59.15: warning [M0146], this pattern is never matched
138+
pat-inconsistent.mo:58.1-60.2: warning [M0145], this switch of type
139+
{#sparrows}
140+
does not cover value
141+
#sparrows
142+
pat-inconsistent.mo:64.10-64.18: warning [M0146], this pattern is never matched

test/fail/pat-inconsistent.mo

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,17 @@ switch (true : Bool) {
4949
case 1 {};
5050
case false {};
5151
};
52+
53+
switch (true : Bool) {
54+
case (#geese) {};
55+
};
56+
57+
// Coverage check for disjoint variants
58+
switch (#sparrows : { #sparrows }) {
59+
case (#geese) {};
60+
};
61+
62+
func absurd(birds : {#}) =
63+
switch birds {
64+
case (#geese) {};
65+
};

0 commit comments

Comments
 (0)