Skip to content

Commit

Permalink
Catch error arising in as_variant_sub when scrutinee is not variant (
Browse files Browse the repository at this point in the history
…#2935)

Fixes #2934.
  • Loading branch information
ggreif authored Nov 25, 2021
1 parent 4115ece commit 4e01321
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/lang_utils/error_codes.ml
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ let error_codes : (string * string option) list =
"M0113", None; (* Object pattern cannot consume type *)
"M0114", None; (* Object pattern cannot consume actor type *)
"M0115", None; (* Option pattern cannot consume type *)
(* "M0116" DEFUNCT Variant pattern cannot consume type *)
"M0116", None; (* Variant pattern cannot consume type *)
"M0117", None; (* Pattern cannot consume type *)
"M0118", None; (* Tuple pattern size mismatch *)
"M0119", None; (* Object field is not contained in type *)
Expand Down
10 changes: 7 additions & 3 deletions src/mo_frontend/typing.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1801,9 +1801,13 @@ and check_pat' env t pat : Scope.val_env =
in check_pat env t1 pat1
| TagP (id, pat1) ->
let t1 =
match T.lookup_val_field_opt id.it (T.as_variant_sub id.it t) with
| Some t1 -> t1
| None -> T.Non
try
match T.lookup_val_field_opt id.it (T.as_variant_sub id.it t) with
| Some t1 -> t1
| None -> T.Non
with Invalid_argument _ ->
error env pat.at "M0116" "variant pattern cannot consume expected type%a"
display_typ_expand t
in check_pat env t1 pat1
| AltP (pat1, pat2) ->
let ve1 = check_pat env t pat1 in
Expand Down
8 changes: 8 additions & 0 deletions test/fail/ok/pat-inconsistent.tc.ok
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,11 @@ pat-inconsistent.mo:49.8-49.9: type error [M0050], literal of type
Nat
does not have expected type
Bool
pat-inconsistent.mo:54.9-54.15: type error [M0116], variant pattern cannot consume expected type
Bool
pat-inconsistent.mo:59.9-59.15: warning [M0146], this pattern is never matched
pat-inconsistent.mo:58.1-60.2: warning [M0145], this switch of type
{#sparrows}
does not cover value
#sparrows
pat-inconsistent.mo:64.10-64.18: warning [M0146], this pattern is never matched
14 changes: 14 additions & 0 deletions test/fail/pat-inconsistent.mo
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,17 @@ switch (true : Bool) {
case 1 {};
case false {};
};
switch (true : Bool) {
case (#geese) {};
};
// Coverage check for disjoint variants
switch (#sparrows : { #sparrows }) {
case (#geese) {};
};
func absurd(birds : {#}) =
switch birds {
case (#geese) {};
};

0 comments on commit 4e01321

Please sign in to comment.