Skip to content

Commit

Permalink
cerl_inline: Fix a name capture bug
Browse files Browse the repository at this point in the history
The way variables created by make_template() are used, it is necessary
that the names are unique in the entire function.  This has not
happened to cause any problems in the past because all other compiler
passes created atom variable names, not integer variable names. If
other passes start to create integer variable names, this bug is
exposed.
  • Loading branch information
bjorng committed Mar 23, 2018
1 parent e48f59a commit 43a91c5
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions lib/compiler/src/cerl_inline.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1822,6 +1822,14 @@ new_var(Env) ->
Name = env__new_vname(Env),
c_var(Name).

%% The way a template variable is used makes it necessary
%% to make sure that it is unique in the entire function.
%% Therefore, template variables are atoms with the prefix "@i".

new_template_var(Env) ->
Name = env__new_tname(Env),
c_var(Name).

residualize_var(R, S) ->
S1 = count_size(weight(var), S),
{ref_to_var(R), st__set_var_referenced(R#ref.loc, S1)}.
Expand Down Expand Up @@ -2183,7 +2191,7 @@ make_template(E, Vs0, Env0) ->
T = make_data_skel(data_type(E), Ts),
E1 = update_data(E, data_type(E),
[hd(get_ann(T)) || T <- Ts]),
V = new_var(Env1),
V = new_template_var(Env1),
Env2 = env__bind(var_name(V), E1, Env1),
{set_ann(T, [V]), [V | Vs1], Env2};
false ->
Expand All @@ -2198,7 +2206,7 @@ make_template(E, Vs0, Env0) ->
Env2 = env__bind(V, E1, Env1),
{T, Vs1, Env2};
_ ->
V = new_var(Env0),
V = new_template_var(Env0),
Env1 = env__bind(var_name(V), E, Env0),
{set_ann(V, [V]), [V | Vs0], Env1}
end
Expand Down Expand Up @@ -2564,6 +2572,11 @@ env__is_defined(Key, Env) ->
env__new_vname(Env) ->
rec_env:new_key(Env).

env__new_tname(Env) ->
rec_env:new_key(fun(I) ->
list_to_atom("@i"++integer_to_list(I))
end, Env).

env__new_fname(A, N, Env) ->
rec_env:new_key(fun (X) ->
S = integer_to_list(X),
Expand Down

0 comments on commit 43a91c5

Please sign in to comment.