[erlang-patches] [PATCH] Quote atoms if necessary in types

Henrik Nord henrik@REDACTED
Fri Sep 16 11:10:24 CEST 2011


On 09/15/2011 07:38 PM, Tomas Abrahamsson wrote:
> Atoms in some occurrences were not correctly quoted when formatted to
> strings, for instance by the typer program. Example:
>
>      -module(tb).
>      -export(['UPPERCASE-FUNCTION-NAME'/0, f1/0, f2/0, f3/0]).
>      -record('UPPERCASE-RECORD-NAME', {x}).
>      -record(r2, {'UPPERCASE-FIELD-NAME'}).
>      -type 'UPPERCASE-TYPE-NAME'() :: integer().
>
>      'UPPERCASE-FUNCTION-NAME'() ->  ok.
>
>      f1() ->  #'UPPERCASE-RECORD-NAME'{x=1}.
>
>      f2() ->  #r2{'UPPERCASE-FIELD-NAME'=1}.
>
>      -spec f3() ->  'UPPERCASE-TYPE-NAME'().
>      f3() ->  1.
>
> Given the program above, the output from typer --plt some.plt tb.erl
> resulted in the following specs being printed:
>
>      -spec UPPERCASE-FUNCTION-NAME() ->  'ok'.
>      -spec f1() ->  #UPPERCASE-RECORD-NAME{x::1}.
>      -spec f2() ->  #r2{UPPERCASE-FIELD-NAME::1}.
>      -spec f3() ->  UPPERCASE-TYPE-NAME().
>
> This commit changes the output to become the following:
>
>      -spec 'UPPERCASE-FUNCTION-NAME'() ->  'ok'.
>      -spec f1() ->  #'UPPERCASE-RECORD-NAME'{x::1}.
>      -spec f2() ->  #r2{'UPPERCASE-FIELD-NAME'::1}.
>      -spec f3() ->  'UPPERCASE-TYPE-NAME'().
> ---
>   lib/hipe/cerl/erl_types.erl |   16 +++++++++++-----
>   lib/typer/src/typer.erl     |    2 +-
>   2 files changed, 12 insertions(+), 6 deletions(-)
>
> diff --git a/lib/hipe/cerl/erl_types.erl b/lib/hipe/cerl/erl_types.erl
> index 1748c1c..7ff1707 100644
> --- a/lib/hipe/cerl/erl_types.erl
> +++ b/lib/hipe/cerl/erl_types.erl
> @@ -211,7 +211,8 @@
>   	 record_field_diffs_to_string/2,
>   	 subst_all_vars_to_any/1,
>   	 lift_list_to_pos_empty/1,
> -	 is_erl_type/1
> +	 is_erl_type/1,
> +	 atom_to_string/1
>   	]).
>
>   %%-define(DO_ERL_TYPES_TEST, true).
> @@ -3360,14 +3361,14 @@ t_to_string(?var(Id), _RecDict) when is_integer(Id) ->
>
>   record_to_string(Tag, [_|Fields], FieldNames, RecDict) ->
>     FieldStrings = record_fields_to_string(Fields, FieldNames, RecDict, []),
> -  "#" ++ atom_to_list(Tag) ++ "{" ++ string:join(FieldStrings, ",") ++ "}".
> +  "#" ++ atom_to_string(Tag) ++ "{" ++ string:join(FieldStrings, ",") ++ "}".
>
>   record_fields_to_string([F|Fs], [{FName, _DefType}|FDefs], RecDict, Acc) ->
>     NewAcc =
>       case t_is_any(F) orelse t_is_atom('undefined', F) of
>         true ->  Acc;
>         false ->
> -	StrFV = atom_to_list(FName) ++ "::" ++ t_to_string(F, RecDict),
> +	StrFV = atom_to_string(FName) ++ "::" ++ t_to_string(F, RecDict),
>   	%% ActualDefType = t_subtract(DefType, t_atom('undefined')),
>   	%% Str = case t_is_any(ActualDefType) of
>   	%% 	  true ->  StrFV;
> @@ -3393,7 +3394,7 @@ field_diffs([F|Fs], [{FName, DefType}|FDefs], RecDict, Acc) ->
>       case t_is_subtype(F, DefType) of
>         true ->  Acc;
>         false ->
> -	Str = atom_to_list(FName) ++ "::" ++ t_to_string(DefType, RecDict),
> +	Str = atom_to_string(FName) ++ "::" ++ t_to_string(DefType, RecDict),
>   	[Str|Acc]
>       end,
>     field_diffs(Fs, FDefs, RecDict, NewAcc);
> @@ -3906,7 +3907,7 @@ t_form_to_string({type, _L, union, Args}) ->
>     string:join(t_form_to_string_list(Args), " | ");
>   t_form_to_string({type, _L, Name, []} = T) ->
>     try t_to_string(t_from_form(T))
> -  catch throw:{error, _} ->  atom_to_list(Name) ++ "()"
> +  catch throw:{error, _} ->  atom_to_string(Name) ++ "()"
>     end;
>   t_form_to_string({type, _L, Name, List}) ->
>     io_lib:format("~w(~s)",
> @@ -3920,6 +3921,11 @@ t_form_to_string_list([H|T], Acc) ->
>   t_form_to_string_list([], Acc) ->
>     lists:reverse(Acc).
>
> +-spec atom_to_string(atom()) ->  string().
> +
> +atom_to_string(Atom) ->
> +  lists:flatten(io_lib:format("~w", [Atom])).
> +
>   %%=============================================================================
>   %%
>   %% Utilities
> diff --git a/lib/typer/src/typer.erl b/lib/typer/src/typer.erl
> index e40c4f3..fd906c8 100644
> --- a/lib/typer/src/typer.erl
> +++ b/lib/typer/src/typer.erl
> @@ -539,7 +539,7 @@ get_type_string(F, A, Info, Mode) ->
>         case {Mode, Type} of
>   	{file, {contract, _}} ->  "";
>   	_ ->
> -	  Prefix = lists:concat(["-spec ", F]),
> +	  Prefix = lists:concat(["-spec ", erl_types:atom_to_string(F)]),
>   	  lists:concat([Prefix, TypeStr, "."])
>         end;
>       true ->
Thank you, refetched this

-- 
/Henrik Nord Erlang/OTP




More information about the erlang-patches mailing list