Skip to content

Commit 1f14265

Browse files
committed
No ok in guards
1 parent 299cb91 commit 1f14265

File tree

2 files changed

+34
-49
lines changed

2 files changed

+34
-49
lines changed

lib/elixir/lib/module/types/of.ex

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -261,10 +261,7 @@ defmodule Module.Types.Of do
261261
Module.Types.Pattern.of_match_var(left, type, expr, stack, context)
262262

263263
:guard ->
264-
case Module.Types.Pattern.of_guard(left, type, expr, stack, context) do
265-
{:ok, type, context} -> {type, context}
266-
{:error, context} -> {dynamic(), context}
267-
end
264+
Module.Types.Pattern.of_guard(left, type, expr, stack, context)
268265

269266
:expr ->
270267
case Module.Types.Expr.of_expr(left, stack, context) do
@@ -311,10 +308,8 @@ defmodule Module.Types.Of do
311308

312309
defp specifier_size(_pattern_or_guard, {:size, _, [arg]}, expr, stack, context)
313310
when not is_integer(arg) do
314-
case Module.Types.Pattern.of_guard(arg, integer(), expr, stack, context) do
315-
{:ok, _, context} -> context
316-
{:error, context} -> context
317-
end
311+
{_type, context} = Module.Types.Pattern.of_guard(arg, integer(), expr, stack, context)
312+
context
318313
end
319314

320315
defp specifier_size(_kind, _specifier, _expr, _stack, context) do

lib/elixir/lib/module/types/pattern.ex

Lines changed: 31 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,8 @@ defmodule Module.Types.Pattern do
3333
expected_types = Enum.map(patterns, fn _ -> dynamic end)
3434

3535
with {:ok, _trees, types, context} <-
36-
of_pattern_args(patterns, expected_types, stack, context),
37-
{:ok, _, context} <-
38-
map_reduce_ok(guards, context, &of_guard(&1, @guard, &1, stack, &2)) do
36+
of_pattern_args(patterns, expected_types, stack, context) do
37+
{_, context} = Enum.map_reduce(guards, context, &of_guard(&1, @guard, &1, stack, &2))
3938
{:ok, types, context}
4039
end
4140
end
@@ -543,57 +542,57 @@ defmodule Module.Types.Pattern do
543542
# :atom
544543
def of_guard(atom, expected, expr, stack, context) when is_atom(atom) do
545544
if atom_type?(expected, atom) do
546-
{:ok, atom([atom]), context}
545+
{atom([atom]), context}
547546
else
548-
{:error, Of.incompatible_error(expr, expected, atom([atom]), stack, context)}
547+
{dynamic(), Of.incompatible_error(expr, expected, atom([atom]), stack, context)}
549548
end
550549
end
551550

552551
# 12
553552
def of_guard(literal, expected, expr, stack, context) when is_integer(literal) do
554553
if integer_type?(expected) do
555-
{:ok, integer(), context}
554+
{integer(), context}
556555
else
557-
{:error, Of.incompatible_error(expr, expected, integer(), stack, context)}
556+
{dynamic(), Of.incompatible_error(expr, expected, integer(), stack, context)}
558557
end
559558
end
560559

561560
# 1.2
562561
def of_guard(literal, expected, expr, stack, context) when is_float(literal) do
563562
if float_type?(expected) do
564-
{:ok, float(), context}
563+
{float(), context}
565564
else
566-
{:error, Of.incompatible_error(expr, expected, float(), stack, context)}
565+
{dynamic(), Of.incompatible_error(expr, expected, float(), stack, context)}
567566
end
568567
end
569568

570569
# "..."
571570
def of_guard(literal, expected, expr, stack, context) when is_binary(literal) do
572571
if binary_type?(expected) do
573-
{:ok, binary(), context}
572+
{binary(), context}
574573
else
575-
{:error, Of.incompatible_error(expr, expected, binary(), stack, context)}
574+
{dynamic(), Of.incompatible_error(expr, expected, binary(), stack, context)}
576575
end
577576
end
578577

579578
# []
580579
def of_guard([], expected, expr, stack, context) do
581580
if empty_list_type?(expected) do
582-
{:ok, empty_list(), context}
581+
{empty_list(), context}
583582
else
584-
{:error, Of.incompatible_error(expr, expected, empty_list(), stack, context)}
583+
{dynamic(), Of.incompatible_error(expr, expected, empty_list(), stack, context)}
585584
end
586585
end
587586

588587
# [expr, ...]
589588
def of_guard(list, _expected, expr, stack, context) when is_list(list) do
590589
{prefix, suffix} = unpack_list(list, [])
591590

592-
with {:ok, prefix, context} <-
593-
map_reduce_ok(prefix, context, &of_guard(&1, dynamic(), expr, stack, &2)),
594-
{:ok, suffix, context} <- of_guard(suffix, dynamic(), expr, stack, context) do
595-
{:ok, non_empty_list(Enum.reduce(prefix, &union/2), suffix), context}
596-
end
591+
{prefix, context} =
592+
Enum.map_reduce(prefix, context, &of_guard(&1, dynamic(), expr, stack, &2))
593+
594+
{suffix, context} = of_guard(suffix, dynamic(), expr, stack, context)
595+
{non_empty_list(Enum.reduce(prefix, &union/2), suffix), context}
597596
end
598597

599598
# {left, right}
@@ -605,64 +604,55 @@ defmodule Module.Types.Pattern do
605604
def of_guard({:%, _, [module, {:%{}, _, args}]} = struct, _expected, _expr, stack, context)
606605
when is_atom(module) do
607606
fun = &of_guard(&1, dynamic(), struct, &2, &3)
608-
{type, context} = Of.struct(struct, module, args, :skip_defaults, stack, context, fun)
609-
{:ok, type, context}
607+
Of.struct(struct, module, args, :skip_defaults, stack, context, fun)
610608
end
611609

612610
# %{...}
613611
def of_guard({:%{}, _meta, args}, _expected, expr, stack, context) do
614-
{type, context} = Of.closed_map(args, stack, context, &of_guard(&1, dynamic(), expr, &2, &3))
615-
{:ok, type, context}
612+
Of.closed_map(args, stack, context, &of_guard(&1, dynamic(), expr, &2, &3))
616613
end
617614

618615
# <<>>
619616
def of_guard({:<<>>, _meta, args}, expected, expr, stack, context) do
620617
if binary_type?(expected) do
621618
context = Of.binary(args, :guard, stack, context)
622-
{:ok, binary(), context}
619+
{binary(), context}
623620
else
624-
{:error, Of.incompatible_error(expr, expected, binary(), stack, context)}
621+
{dynamic(), Of.incompatible_error(expr, expected, binary(), stack, context)}
625622
end
626623
end
627624

628625
# ^var
629626
def of_guard({:^, _meta, [var]}, expected, expr, stack, context) do
630627
# This is by definition a variable defined outside of this pattern, so we don't track it.
631-
{type, context} = Of.intersect(Of.var(var, context), expected, expr, stack, context)
632-
{:ok, type, context}
628+
Of.intersect(Of.var(var, context), expected, expr, stack, context)
633629
end
634630

635631
# {...}
636632
def of_guard({:{}, _meta, args}, _expected, expr, stack, context) do
637-
with {:ok, types, context} <-
638-
map_reduce_ok(args, context, &of_guard(&1, dynamic(), expr, stack, &2)) do
639-
{:ok, tuple(types), context}
640-
end
633+
{types, context} = Enum.map_reduce(args, context, &of_guard(&1, dynamic(), expr, stack, &2))
634+
{tuple(types), context}
641635
end
642636

643637
# var.field
644638
def of_guard({{:., _, [callee, key]}, _, []} = map_fetch, _expected, expr, stack, context)
645639
when not is_atom(callee) do
646-
with {:ok, type, context} <- of_guard(callee, dynamic(), expr, stack, context) do
647-
{type, context} = Of.map_fetch(map_fetch, type, key, stack, context)
648-
{:ok, type, context}
649-
end
640+
{type, context} = of_guard(callee, dynamic(), expr, stack, context)
641+
Of.map_fetch(map_fetch, type, key, stack, context)
650642
end
651643

652644
# Remote
653645
def of_guard({{:., _, [:erlang, function]}, _, args}, _expected, expr, stack, context)
654646
when is_atom(function) do
655-
with {:ok, args_type, context} <-
656-
map_reduce_ok(args, context, &of_guard(&1, dynamic(), expr, stack, &2)) do
657-
{type, context} = Of.apply(:erlang, function, args_type, expr, stack, context)
658-
{:ok, type, context}
659-
end
647+
{args_type, context} =
648+
Enum.map_reduce(args, context, &of_guard(&1, dynamic(), expr, stack, &2))
649+
650+
Of.apply(:erlang, function, args_type, expr, stack, context)
660651
end
661652

662653
# var
663654
def of_guard(var, expected, expr, stack, context) when is_var(var) do
664-
{type, context} = Of.intersect(Of.var(var, context), expected, expr, stack, context)
665-
{:ok, type, context}
655+
Of.intersect(Of.var(var, context), expected, expr, stack, context)
666656
end
667657

668658
## Helpers

0 commit comments

Comments
 (0)