[erlang-patches] [PATCH] Quote atoms if necessary in types
Henrik Nord
henrik@REDACTED
Mon Mar 21 12:02:40 CET 2011
On 03/20/2011 05:14 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 | 11 +++++++----
> lib/typer/src/typer.erl | 5 ++++-
> 2 files changed, 11 insertions(+), 5 deletions(-)
>
> diff --git a/lib/hipe/cerl/erl_types.erl b/lib/hipe/cerl/erl_types.erl
> index 080d693..a8159c7 100644
> --- a/lib/hipe/cerl/erl_types.erl
> +++ b/lib/hipe/cerl/erl_types.erl
> @@ -3350,14 +3350,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) ++ "{" ++ sequence(FieldStrings, [], ",") ++ "}".
> + "#" ++ atom_to_string(Tag) ++ "{" ++ sequence(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;
> @@ -3383,7 +3383,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);
> @@ -3894,7 +3894,7 @@ t_form_to_string({type, _L, union, Args}) ->
> sequence(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, binary, [X,Y]} = Type) ->
> case {erl_eval:partial_eval(X), erl_eval:partial_eval(Y)} of
> @@ -3919,6 +3919,9 @@ t_form_to_string_list([H|T], Acc) ->
> t_form_to_string_list([], Acc) ->
> lists:reverse(Acc).
>
> +atom_to_string(Atom) ->
> + io_lib:format("~w", [Atom]).
> +
> %%=============================================================================
> %%
> %% Utilities
> diff --git a/lib/typer/src/typer.erl b/lib/typer/src/typer.erl
> index fc8caa4..3a3bb26 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 ", atom_to_string(F)]),
> lists:concat([Prefix, TypeStr, "."])
> end;
> true ->
> @@ -547,6 +547,9 @@ get_type_string(F, A, Info, Mode) ->
> lists:concat([Prefix, TypeStr, "."])
> end.
>
> +atom_to_string(Atom) ->
> + lists:flatten(io_lib:format("~w", [Atom])).
> +
> show_type_info(File, Info) ->
> io:format("\n%% File: ~p\n%% ", [File]),
> OutputString = lists:concat(["~.", length(File)+8, "c~n"]),
>
Hi
Patch included in 'pu'
Thanks
/Henrik Nord, Erlang/OTP
More information about the erlang-patches
mailing list