Skip to content

Commit

Permalink
[matcher] don't allow null-only switches if we need a value
Browse files Browse the repository at this point in the history
closes #11366
  • Loading branch information
Simn committed Nov 9, 2023
1 parent 1e5b2a2 commit e6442b2
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/optimization/analyzerTexprTransformer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ let rec func ctx bb tf t p =
block_element bb e,e1
| TBlock [e1] ->
value bb e1
| TBlock _ | TIf _ | TSwitch _ | TTry _ ->
| TBlock _ | TIf(_,_,Some _) | TSwitch _ | TTry _ ->
bind_to_temp bb e
| TCall({eexpr = TIdent s},el) when is_really_unbound s ->
check_unbound_call s el;
Expand Down Expand Up @@ -178,7 +178,7 @@ let rec func ctx bb tf t p =
| TThrow _ | TReturn _ | TBreak | TContinue ->
let bb = block_element bb e in
bb,mk (TConst TNull) t_dynamic e.epos
| TVar _ | TFor _ | TWhile _ ->
| TVar _ | TFor _ | TWhile _ | TIf _ ->
Error.raise_typing_error "Cannot use this expression as value" e.epos
and value bb e =
let bb,e = value' bb e in
Expand Down
2 changes: 1 addition & 1 deletion src/typing/matcher/texprConverter.ml
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ let to_texpr ctx t_switch with_type dt =
let e_else = loop dt_rec params dt2 in
begin match e_else with
| None ->
if toplevel then
if toplevel && with_type = NoValue then
Some (mk (TIf(e_cond,e_then,None)) t_switch e_then.epos)
else
report_not_exhaustive !v_lookup e []
Expand Down
6 changes: 6 additions & 0 deletions tests/misc/projects/Issue11366/Main.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
function main() {
var x = switch [][0] {
case null:
'x';
}
}
1 change: 1 addition & 0 deletions tests/misc/projects/Issue11366/compile-fail.hxml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--main Main
1 change: 1 addition & 0 deletions tests/misc/projects/Issue11366/compile-fail.hxml.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Main.hx:2: characters 17-22 : Unmatched patterns: _
2 changes: 1 addition & 1 deletion tests/misc/projects/Issue2538/compile-fail.hxml.stderr
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Main.hx:7: characters 13-25 : Cannot use Void as value
Main.hx:7: characters 13-25 : Cannot use this expression as value

0 comments on commit e6442b2

Please sign in to comment.