Skip to content

Commit 2108ccb

Browse files
author
Francois Brodeur
authored
Any type fix within complex patterns (#315)
Any type fix within complex patterns
1 parent 864f085 commit 2108ccb

File tree

4 files changed

+16
-4
lines changed

4 files changed

+16
-4
lines changed

src/typechecker.erl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
-define(top(), {remote_type, _, [{atom,_,gradualizer}
4646
,{atom,_,top},[]]}).
4747
-define(record_field(Name), {record_field, _, {atom, _, Name}, _}).
48+
-define(record_field_expr(Expr), {record_field, _, _, Expr}).
4849
-define(typed_record_field(Name), {typed_record_field, ?record_field(Name), _}).
4950
-define(typed_record_field(Name, Type), {typed_record_field, ?record_field(Name), Type}).
5051
-define(type_field_type(Name, Type), {type, _, field_type, [{atom, _, Name}, Type]}).
@@ -1175,6 +1176,8 @@ expect_record_type({var, _, Var}, Record, #tenv{records = REnv}) ->
11751176
_NotFound ->
11761177
{type_error, Record}
11771178
end;
1179+
expect_record_type({type, _, any, []}, _Record, _TEnv) ->
1180+
any;
11781181
expect_record_type(_, Ty, _) ->
11791182
{type_error, Ty}.
11801183

@@ -3711,8 +3714,6 @@ add_type_pat({var, _, A} = Var, Ty, TEnv, VEnv) ->
37113714
%% Match all
37123715
{Ty, Ty, VEnv#{A => Ty}, constraints:empty()}
37133716
end;
3714-
add_type_pat(Expr, {type, _, any, []} = Ty, _TEnv, VEnv) ->
3715-
{type(none), Ty, add_any_types_pat(Expr, VEnv), constraints:empty()};
37163717
add_type_pat(Pat, ?type(union, UnionTys)=UnionTy, TEnv, VEnv) ->
37173718
{PatTys, UBounds, VEnvs, Css} =
37183719
lists:foldr(fun (Ty, {PatTysAcc, UBoundsAcc, VEnvAcc, CsAcc}=Acc) ->
@@ -3843,7 +3844,7 @@ add_type_pat({record, P, Record, Fields}, Ty, TEnv, VEnv) ->
38433844
any ->
38443845
{type(none)
38453846
,Ty
3846-
,union_var_binds([add_any_types_pat(Field, VEnv) || Field <- Fields], TEnv)
3847+
,union_var_binds([add_any_types_pat(Field, VEnv) || ?record_field_expr(Field) <- Fields], TEnv)
38473848
,constraints:empty()};
38483849
{fields_ty, Tys, Cs} ->
38493850
{PatTys, UBounds, VEnv1, Cs1} = add_type_pat_fields(Fields, Tys, TEnv, VEnv),

test/should_pass/records.erl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,6 @@ rec_index_subtype() ->
6161
-spec record_as_tuple(#r{}) -> tuple().
6262
record_as_tuple(R) ->
6363
R.
64+
65+
-record(rec_any, {f}).
66+
f(#rec_any{f = F} = R) -> F.

test/should_pass/tuple_union_pat.erl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,11 @@
55
-spec f(tuple() | integer()) -> ok.
66
f({1, 2}) ->
77
ok.
8+
9+
-spec g({ok, binary()} | {error, term()}) -> integer().
10+
g({error, key_not_found} = _Response) ->
11+
1;
12+
g({error, _} = _Response) ->
13+
2;
14+
g({ok, _} = _Response) ->
15+
3.

test/typechecker_tests.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ add_type_pat_test_() ->
562562
[{"Pattern matching list against any()",
563563
?_assert(type_check_forms(["f([E|_]) -> E."]))},
564564
{"Pattern matching record against any()",
565-
?_assert(type_check_forms(["-record(f, {r}).",
565+
?_assert(type_check_forms(["-record(r, {f}).",
566566
"f(#r{f = F}) -> F."]))}
567567
].
568568

0 commit comments

Comments
 (0)