[英]Why Erlang Dialyzer cannot find type error in the following code?
[英]Erlang: Why Dialyzer does not notice this error?
现在,我尝试使用Dialyzer并使用-spec , -type 。
我将以下代码提供给Dialyzer,我希望Dialyzer注意到“hoge(a)+ 1无效”,但Dialyzer没有注意到。
-spec hoge (X) -> bad when X :: a;
(X) -> number() when X :: number().
hoge(X) when is_number(X) -> 1;
hoge(a) -> bad.
foo() ->
_ = hoge(a) + 1.
但是,在另一个环境中,
-spec hoge (X) -> bad when X :: a;
(X) -> string() when X :: number().
hoge(X) when is_number(X) -> "1";
hoge(a) -> bad.
foo() ->
_ = hoge(a) + 1.
透析器告诉我这个错误,
test.erl:12: The call erlang:'+'('bad' | [49,...],1) will never return since it differs in the 1st argument from the success typing arguments: (number(),number())
为什么Dialyzer在第一次设置时无法注意到类型错误。
-spec hoge (X) -> bad when X :: a;
(X) -> number() when X :: number().
这个合同(规范)意味着不是“hoge是'a' - >'坏'|数字() - >数字()”但是“'a'|数字() - >'坏'|数字()”?
这是第一个示例的完整模块。
-module(example).
-export([hoge/1, foo/0]).
-spec hoge (X) -> bad when X :: a;
(X) -> number() when X :: number().
hoge(X) when is_number(X) -> 1;
hoge(a) -> bad.
foo() ->
_ = hoge(a) + 1.
“为什么Dialyzer没有发现这个错误”问题的标准答案总是“因为它永远不会错”。 透析器永远不会承诺找到所有错误。
在您的问题示例中,如果没有规范,Dialyzer的类型推断算法确实会为所有参数和所有返回值生成一个联合类型。 根据规范,Dialyzer仍然会推断出工会,但应该使用规范来缩小调用的返回值,然后产生错误。 这看起来像是“灵敏度降低”的情况(但本身并不是错误)。 无论如何,您可以提交错误报告。
在您的工作示例中,任何可能的值都会导致错误的结果,而Dialyzer自己的类型推断就足够了,即使没有规范也是如此。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.