packages and forgotten imports

Ulf Wiger etxuwig@REDACTED
Thu Apr 17 17:19:06 CEST 2003


I've been playing around with packages in Erlang for a
while, and must say that it happens annoyingly often that I
forget to insert 'import' clauses for OTP modules that I
want to use.

The standard Erlang problem of calling undefined external
functions is usually not a very big one, IMHO, but it
becomes decidedly more common with packages due to the
legacy treatment of "the empty package".

A suggestion: if a module with a package name is compiled,
the linter could issue a warning if an implicitly named
module is not found in the 'outdir'.

It's not difficult to order modules in the Makefile so that
compilation is done in dependency order; this has to be done
anyway if one defines custom behaviours or parse transforms
(I know, that doesn't happen every day.)

I've attached a suggested patch to erl_lint.erl.

Given the following test module (test.erl):

-module(mypackage.test).

-export([foo/0]).
%% forgotten -import(lists).

foo() ->
   lists:foldl(fun(X,A) -> [X|A] end, [], [a,b,c]).




1> compile:file(test,[{outdir,"../ebin"},return_warnings]).
{ok,'mypackage.test',

[{"./test.erl",[{7,erl_lint,{maybe_missing_import,"lists"}}]}]}
42> compile:file(test,[{outdir,"../ebin"},report_warnings]).
./test.erl:7: Warning: Missing import for module lists
{ok,'mypackage.test'}


/Uffe
-- 
Ulf Wiger, Senior Specialist,
   / / /   Architecture & Design of Carrier-Class Software
  / / /    Strategic Product & System Management
 / / /     Ericsson AB, Connectivity and Control Nodes

-------------- next part --------------
121a122,123
> format_error({maybe_missing_import, Name}) ->
>     io_lib:format("Missing import for module ~s", [Name]);
2045c2047,2050
<                             M1 = packages:concat(St1#lint.package,
---
> 			    St2 = add_warning(
> 				    not(exists_in_package(Name, St1)),
> 				    L, {maybe_missing_import,Name}, St1),
>                             M1 = packages:concat(St2#lint.package,
2047c2052
<                             {{atom,L,list_to_atom(M1)}, St1}
---
>                             {{atom,L,list_to_atom(M1)}, St2}
2067a2073,2082
>     end.
> 
> exists_in_package(Name, St) ->
>     OutDir = proplists:get_value(outdir,St#lint.compile),
>     case file:read_file_info(
> 	   filename:join(OutDir, Name ++ code:objfile_extension())) of
> 	{ok, _} ->
> 	    true;
> 	_ ->
> 	    false


More information about the erlang-patches mailing list