【问题标题】:Type errors in dialyzer in function with specified type具有指定类型的功能中的透析器类型错误
【发布时间】:2015-08-21 18:04:11
【问题描述】:

当透析器分析以下功能时,我收到错误消息。

-spec do_request(Method, Type, Url, Expect, Headers, Body, Client) -> Response::client_response() 
when 
    Method  :: method(),
    Type    :: content_type(),
    Url     :: url(),
    Expect  :: status_codes(),
    Headers :: headers(),
    Body    :: body(),
    Client  :: #client{}.

do_request(Method, Type, Url, Expect, Headers, Body, Client) -> 
    Client2 = check_expired(Client),
    Headers2 = add_auth_header(Headers, Client2),
    %% error occurs on this line
    Response = restc:request(Method, Type, binary_to_list(Url), Expect, Headers2, Body),
    %%
    {Response, Client2}.

错误是:

The call restc:request(Method::any(),Type::any(),
[byte()],Expect::any(),Headers2::[{binary(),binary()},...],
Body::any()) breaks the contract (Method::method(), Type::content_type(),  
Url::url(), Expect::status_codes(), Headers::headers(), 
Body::body()) -> Response::response()

restc:request 具有以下类型规范:

-spec request(Method::method(), Type::content_type(), Url::url(),
Expect::status_codes(), Headers::headers(), Body::body()) -> Response::response().

调用使用的类型有:

-type method()       :: head | get | put | post | trace | options | delete.
-type url()          :: binary().
-type headers()      :: [header()].
-type header()       :: {binary(), binary()}.
-type status_codes() :: [status_code()].
-type status_code()  :: integer().
-type reason()       :: term().
-type content_type() :: json | xml | percent.
-type property()     :: atom() | tuple().
-type proplist()     :: [property()].
-type body()         :: proplist().
-type response()     :: {ok, Status::status_code(), Headers::headers(), Body::body()} |
                        {error, Status::status_code(), Headers::headers(), Body::body()} |
                        {error, Reason::reason()}.
-type client_response()       :: {response(), #client{}}.
-type token_type()     :: bearer | unsupported.

为什么当我指定了要传递的变量的类型时,dialyzer 会说我的调用正在传递具有any() 类型的变量?我查看了调用链以验证类型规范是否一致(并且与其他模块一致)。

【问题讨论】:

  • 您需要在类型规范的when 部分中为每个类型添加扩展类型;例如,[byte()] 可能与 url() 发生冲突,但我们无法从您提供的信息中得知。
  • 确实...要么,要么Headers 参数与合同不匹配。您看到的 any() 类型是由于 Dialyzer 仅在“外部”使用您的参数协定(例如,对于函数的实际调用)。在分析函数本身时,Dialyzer 假定可以给出任何参数,并在其他调用成功时需要这样的东西时缩小范围。

标签: erlang dialyzer


【解决方案1】:

问题是url() 被指定为binary(),但您在其中传入[byte()](字符串)。您需要将url() 的类型规范改为iodata(),或者先将其转换为二进制来限制您的输入。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-26
    • 2017-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-05
    相关资源
    最近更新 更多