[erlang-bugs] bogus 'unsafe variable' warning with try-else
Mikael Pettersson
mikpelinux@REDACTED
Thu Nov 20 12:49:52 CET 2014
The following, adapted from an example in the online Erlang
Reference Manual, looks like a bug to me:
> cat bug.erl
-module(bug).
-compile(export_all).
termize_file(Name) ->
{ok,F} = file:open(Name, [read,binary]),
try
{ok,Bin} = file:read(F, 1024*1024)
after
file:close(F)
end,
binary_to_term(Bin).
> erlc bug.erl
bug.erl:11: variable 'Bin' unsafe in 'try' (line 6)
Unless I'm missing something, there is no way to reach the
binary_to_term/1 call without successfully evaluating the
{ok,Bin} = ... match expression; therefore the use is not
unsafe. If the file:read/2 call or the {ok,Bin} match fails,
the after clause runs and the entire try expression fails,
and the binary_to_term/1 is not reached.
Reproduced with 17.3 and r15b03-1.
Workaround: match on the value of the try itself:
"{ok,Bin} = try file:read(...) after ... end".
/Mikael
More information about the erlang-bugs
mailing list