From a1e51d125944ad840a306d0cddec681848808f4c Mon Sep 17 00:00:00 2001 From: Hans Bolinder Date: Mon, 22 Apr 2019 13:54:12 +0200 Subject: dialyzer: Add parentheses around annotated union elements In contracts, annotated elements of unions are printed with parentheses around them. They are not parseable otherwise. --- .../test/small_SUITE_data/results/union_paren | 7 +++++++ .../test/small_SUITE_data/src/union_paren.erl | 24 ++++++++++++++++++++++ lib/hipe/cerl/erl_types.erl | 10 ++++++++- 3 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 lib/dialyzer/test/small_SUITE_data/results/union_paren create mode 100644 lib/dialyzer/test/small_SUITE_data/src/union_paren.erl diff --git a/lib/dialyzer/test/small_SUITE_data/results/union_paren b/lib/dialyzer/test/small_SUITE_data/results/union_paren new file mode 100644 index 0000000000..3a3526df89 --- /dev/null +++ b/lib/dialyzer/test/small_SUITE_data/results/union_paren @@ -0,0 +1,7 @@ + +union_paren.erl:12: Function t2/0 has no local return +union_paren.erl:13: The call union_paren:t2(3.14) breaks the contract (integer() | atom()) -> integer() +union_paren.erl:19: Function t3/0 has no local return +union_paren.erl:20: The pattern 3.14 can never match the type atom() | integer() +union_paren.erl:5: Function t1/0 has no local return +union_paren.erl:6: The call union_paren:t1(3.14) breaks the contract ((A::integer()) | (B::atom())) -> integer() diff --git a/lib/dialyzer/test/small_SUITE_data/src/union_paren.erl b/lib/dialyzer/test/small_SUITE_data/src/union_paren.erl new file mode 100644 index 0000000000..4691a57d98 --- /dev/null +++ b/lib/dialyzer/test/small_SUITE_data/src/union_paren.erl @@ -0,0 +1,24 @@ +-module(union_paren). + +-compile(export_all). + +t1() -> + t1(3.14). + +-spec t1((A :: integer()) | (B :: atom())) -> integer(). +t1(A) -> + fy:bar(A). + +t2() -> + t2(3.14). + +-spec t2(integer() | atom()) -> integer(). +t2(A) -> + fy:bar(A). + +t3() -> + 3.14 = t3(foo). + +-spec t3(_) -> (I :: integer()) | (A :: atom()). +t3(A) when is_atom(A) -> A; +t3(I) when is_integer(I) -> I. diff --git a/lib/hipe/cerl/erl_types.erl b/lib/hipe/cerl/erl_types.erl index d61cd8664c..badf58936f 100644 --- a/lib/hipe/cerl/erl_types.erl +++ b/lib/hipe/cerl/erl_types.erl @@ -5338,7 +5338,15 @@ t_form_to_string({type, _L, tuple, any}) -> "tuple()"; t_form_to_string({type, _L, tuple, Args}) -> "{" ++ flat_join(t_form_to_string_list(Args), ",") ++ "}"; t_form_to_string({type, _L, union, Args}) -> - flat_join(t_form_to_string_list(Args), " | "); + flat_join(lists:map(fun(Arg) -> + case Arg of + {ann_type, _AL, _} -> + "(" ++ t_form_to_string(Arg) ++ ")"; + _ -> + t_form_to_string(Arg) + end + end, Args), + " | "); t_form_to_string({type, _L, Name, []} = T) -> try M = mod, -- cgit v1.2.3