From 6126143eda4fc9cea7a2928de1b11cfddec0e2bb Mon Sep 17 00:00:00 2001 From: Stavros Aronis Date: Tue, 22 Jan 2013 17:57:54 +0100 Subject: Properly support functions with arbitrary arity in type specs. --- .../test/small_SUITE_data/src/unknown_arity_function_spec.erl | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 lib/dialyzer/test/small_SUITE_data/src/unknown_arity_function_spec.erl (limited to 'lib/dialyzer') diff --git a/lib/dialyzer/test/small_SUITE_data/src/unknown_arity_function_spec.erl b/lib/dialyzer/test/small_SUITE_data/src/unknown_arity_function_spec.erl new file mode 100644 index 0000000000..c7d7459614 --- /dev/null +++ b/lib/dialyzer/test/small_SUITE_data/src/unknown_arity_function_spec.erl @@ -0,0 +1,10 @@ +-module(unknown_arity_function_spec). + +-export([test/2]). + +%-type t() :: 42 | fun((...) -> t()). +%-type f() :: fun((...) -> 42). + +-spec test(fun((...) -> 42), list()) -> 42. +test(F, L) -> + 42 = apply(F, L). -- cgit v1.2.3 From 250c2fef8242317c741c83c2b4ec2283fe4bceb5 Mon Sep 17 00:00:00 2001 From: Stavros Aronis Date: Fri, 7 Dec 2012 18:53:18 +0100 Subject: Report spec discrepancy on mismatching lists --- lib/dialyzer/src/dialyzer_contracts.erl | 27 ++++++++-- .../small_SUITE_data/results/empty_list_infimum | 2 + .../small_SUITE_data/src/empty_list_infimum.erl | 57 ++++++++++++++++++++++ 3 files changed, 83 insertions(+), 3 deletions(-) create mode 100644 lib/dialyzer/test/small_SUITE_data/results/empty_list_infimum create mode 100644 lib/dialyzer/test/small_SUITE_data/src/empty_list_infimum.erl (limited to 'lib/dialyzer') diff --git a/lib/dialyzer/src/dialyzer_contracts.erl b/lib/dialyzer/src/dialyzer_contracts.erl index 0b932d5a1f..157c951f77 100644 --- a/lib/dialyzer/src/dialyzer_contracts.erl +++ b/lib/dialyzer/src/dialyzer_contracts.erl @@ -254,14 +254,35 @@ check_extraneous([C|Cs], SuccType) -> end. check_extraneous_1(Contract, SuccType) -> - CRngs = erl_types:t_elements(erl_types:t_fun_range(Contract)), + CRng = erl_types:t_fun_range(Contract), + CRngs = erl_types:t_elements(CRng), STRng = erl_types:t_fun_range(SuccType), ?debug("CR = ~p\nSR = ~p\n", [CRngs, STRng]), - case [CR || CR <- CRngs, erl_types:t_is_none(erl_types:t_inf(CR, STRng, opaque))] of - [] -> ok; + case [CR || CR <- CRngs, + erl_types:t_is_none(erl_types:t_inf(CR, STRng, opaque))] of + [] -> + CRngList = list_part(CRng), + STRngList = list_part(STRng), + case is_not_nil_list(CRngList) andalso is_not_nil_list(STRngList) of + false -> ok; + true -> + CRngElements = erl_types:t_list_elements(CRngList), + STRngElements = erl_types:t_list_elements(STRngList), + Inf = erl_types:t_inf(CRngElements, STRngElements, opaque), + case erl_types:t_is_none(Inf) of + true -> {error, invalid_contract}; + false -> ok + end + end; CRs -> {error, {extra_range, erl_types:t_sup(CRs), STRng}} end. +list_part(Type) -> + erl_types:t_inf(erl_types:t_list(), Type, opaque). + +is_not_nil_list(Type) -> + erl_types:t_is_list(Type) andalso not erl_types:t_is_nil(Type). + %% This is the heart of the "range function" -spec process_contracts([contract_pair()], [erl_types:erl_type()]) -> erl_types:erl_type(). diff --git a/lib/dialyzer/test/small_SUITE_data/results/empty_list_infimum b/lib/dialyzer/test/small_SUITE_data/results/empty_list_infimum new file mode 100644 index 0000000000..6f189099ed --- /dev/null +++ b/lib/dialyzer/test/small_SUITE_data/results/empty_list_infimum @@ -0,0 +1,2 @@ + +empty_list_infimum.erl:38: Invalid type specification for function empty_list_infimum:list_vhost_permissions/1. The success typing is (_) -> [[{_,_}]] diff --git a/lib/dialyzer/test/small_SUITE_data/src/empty_list_infimum.erl b/lib/dialyzer/test/small_SUITE_data/src/empty_list_infimum.erl new file mode 100644 index 0000000000..b58fa732cb --- /dev/null +++ b/lib/dialyzer/test/small_SUITE_data/src/empty_list_infimum.erl @@ -0,0 +1,57 @@ +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is VMware, Inc. +%% + +-module(empty_list_infimum). + +-record(permission, {configure, write, read}). +-record(user_vhost, {username, virtual_host}). +-record(user_permission, {user_vhost, permission}). + +%%---------------------------------------------------------------------------- + +-export([i_delete/1]). + +-type(vhost() :: binary()). + +-type(info_key() :: atom()). +-type(info_keys() :: [info_key()]). + +-type(info() :: {info_key(), any()}). +-type(infos() :: [info()]). + +%%---------------------------------------------------------------------------- + +-spec i_delete(vhost()) -> 'ok'. + +i_delete(VHostPath) -> + [ok || _ <- list_vhost_permissions(VHostPath)], + ok. + +%%---------------------------------------------------------------------------- + +vhost_perms_info_keys() -> + [user, configure, write, read]. + +-spec list_vhost_permissions(vhost()) -> infos(). + +list_vhost_permissions(VHostPath) -> + list_permissions(vhost_perms_info_keys(), rabbit_foo:some_list()). + +filter_props(Keys, Props) -> + [T || T = {K, _} <- Props, lists:member(K, Keys)]. + +list_permissions(Keys, SomeList) -> + [filter_props(Keys, [{user, Username}, + {vhost, VHostPath}, + {configure, ConfigurePerm}, + {write, WritePerm}, + {read, ReadPerm}]) || + #user_permission{user_vhost = #user_vhost{username = Username, + virtual_host = VHostPath}, + permission = #permission{configure = ConfigurePerm, + write = WritePerm, + read = ReadPerm}} <- + SomeList]. -- cgit v1.2.3