分析表示式警告的原因

2021-08-30 01:19:02 字數 3111 閱讀 9950

erlang的表示式如果不用的話,會警告的,但是有些又不警告,比較奇怪,做了下試驗,再看了compiler的原始碼有了以下的結果:

root@yufeng-desktop:~# nl expr.erl

1 -module(expr).

2 -export([test/0]).

3 test()->

4 1,

5 1.0,

6 ,7 [1,2,3],

8 <<>>,

9 <<1,2,3>>,

10 c:pid(0,1,2),

11 make_ref(),

12 atom,

13 fun()-> ok end,

14 open_port(, [in, out]),

15 "hello",

16 "",

17 ,

18 {},

19 true,

20 false,

21 1.

22 root@yufeng-desktop:~# erlc[color=red] +return[/color] expr.erl

./expr.erl:4: warning: a term is constructed, but never used

./expr.erl:5: warning: a term is constructed, but never used

./expr.erl:7: warning: a term is constructed, but never used

./expr.erl:8: warning: a term is constructed, but never used

./expr.erl:9: warning: a term is constructed, but never used

./expr.erl:11: warning: the call to make_ref/0 has no effect

./expr.erl:13: warning: a term is constructed, but never used

./expr.erl:15: warning: a term is constructed, but never used

./expr.erl:17: warning: a term is constructed, but never used

./expr.erl:18: warning: a term is constructed, but never used

compiler function compile:compile/3 returned:,,

,,,}},,,

,]}]}

看下lib/compiler/src/sys_core_fold.erl的源**:

expr(#c_literal=l, ctxt, _sub) ->

case ctxt of

effect ->

case val of

->

%% keep as - might give slightly better code.

l;_ when is_atom(val) ->

%% for cleanliness replace with void().

void();

_ ->

%% warn and replace with void().

[color=red]add_warning(l, useless_building),[/color]

void()

end;

value -> l

end;

expr(#c_cons=cons, ctxt, sub) ->

h1 = expr(h0, ctxt, sub),

t1 = expr(t0, ctxt, sub),

case ctxt of

effect ->

[color=red]add_warning(cons, useless_building),[/color]

expr(make_effect_seq([h1,t1], sub), ctxt, sub);

value ->

ann_c_cons(anno, h1, t1)

end;

expr(#c_tuple=tuple, ctxt, sub) ->

es = expr_list(es0, ctxt, sub),

case ctxt of

effect ->

[color=red] add_warning(tuple, useless_building),[/color]

expr(make_effect_seq(es, sub), ctxt, sub);

value ->

ann_c_tuple(anno, es)

end;

expr(#c_binary=bin0, ctxt, sub) ->

%% warn for useless building, but always build the binary

%% anyway to preserve a possible exception.

case ctxt of

effect -> [color=red]add_warning(bin0, useless_building);[/color]

value -> ok

end,

bin1 = bin0#c_binary,

bin = bin_un_utf(bin1),

eval_binary(bin);

expr(#c_fun{}=fun, effect, _) ->

%% a fun is created, but not used. warn, and replace with the void value.

[color=red] add_warning(fun, useless_building),[/color]

void();

得出的結論是:

如果編譯器能夠明確的知道你表示式的值的話,除非這幾個東西 atom, , boolean 不警告以外,其他無用的一律警告, 而且盡可能的不產生**。 這就解釋為什麼大部分的函式返回值是 原子或者 .

表示式 表示式樹 表示式求值

總時間限制 1000ms 記憶體限制 65535kb 描述 眾所周知,任何乙個表示式,都可以用一棵表示式樹來表示。例如,表示式a b c,可以表示為如下的表示式樹 a b c 現在,給你乙個中綴表示式,這個中綴表示式用變數來表示 不含數字 請你將這個中綴表示式用表示式二叉樹的形式輸出出來。輸入輸入分...

正規表示式分析

1.請分析這下面這兩個輸出吧,最好乙個乙個斜槓地分析。system.out.println 8abc8 replaceall 8 8abc system.out.println 8abc8 replaceall 8 8abc 2.為什麼a和b可以有相同的輸出,c和d卻不可以呢?system.out....

表示式語法分析

表示式語法分析 遞迴子程式法 time limit 1000 ms memory limit 65536 kib problem description 遞迴子程式法是一種確定的自頂向下語法分析方法,要求文法是ll 1 文法。它的實現思想是對應文法中每個非終結符編寫乙個遞迴過程,每個過程的功能是識別...