Skip to content

Commit

Permalink
Merge branch 'hb/record_field_undefined_type/OTP-9147' into dev
Browse files Browse the repository at this point in the history
* hb/record_field_undefined_type/OTP-9147:
  Fix a bug concerning record field types
  • Loading branch information
uabboli committed Mar 23, 2011
2 parents f861b4f + b420086 commit e243040
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
10 changes: 8 additions & 2 deletions lib/stdlib/src/erl_parse.yrl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 1996-2010. All Rights Reserved.
%% Copyright Ericsson AB 1996-2011. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
Expand Down Expand Up @@ -757,7 +757,8 @@ record_fields([{typed,Expr,TypeInfo}|Fields]) ->
{atom, La, _} ->
case has_undefined(TypeInfo) of
false ->
lift_unions(abstract(undefined, La), TypeInfo);
TypeInfo2 = maybe_add_paren(TypeInfo),
lift_unions(abstract(undefined, La), TypeInfo2);
true ->
TypeInfo
end
Expand All @@ -778,6 +779,11 @@ has_undefined({type,_,union,Ts}) ->
has_undefined(_) ->
false.

maybe_add_paren({ann_type,L,T}) ->
{paren_type,L,[{ann_type,L,T}]};
maybe_add_paren(T) ->
T.

term(Expr) ->
try normalise(Expr)
catch _:_R -> ret_err(?line(Expr), "bad attribute")
Expand Down
10 changes: 2 additions & 8 deletions lib/stdlib/src/erl_pp.erl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 1996-2010. All Rights Reserved.
%% Copyright Ericsson AB 1996-2011. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
Expand Down Expand Up @@ -558,17 +558,11 @@ record_field({typed_record_field,{record_field,_,F,Val},Type}, Hook) ->
Fl = lexpr(F, L, Hook),
Vl = typed(lexpr(Val, R, Hook), Type),
{list,[{cstep,[Fl,' ='],Vl}]};
record_field({typed_record_field,Field,Type0}, Hook) ->
Type = remove_undefined(Type0),
record_field({typed_record_field,Field,Type}, Hook) ->
typed(record_field(Field, Hook), Type);
record_field({record_field,_,F}, Hook) ->
lexpr(F, 0, Hook).

remove_undefined({type,L,union,[{atom,_,undefined}|T]}) ->
{type,L,union,T};
remove_undefined(T) -> % cannot happen
T.

list({cons,_,H,T}, Es, Hook) ->
list(T, [H|Es], Hook);
list({nil,_}, Es, Hook) ->
Expand Down
24 changes: 22 additions & 2 deletions lib/stdlib/test/erl_pp_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
neg_indent/1,

otp_6321/1, otp_6911/1, otp_6914/1, otp_8150/1, otp_8238/1,
otp_8473/1, otp_8522/1, otp_8567/1, otp_8664/1]).
otp_8473/1, otp_8522/1, otp_8567/1, otp_8664/1, otp_9147/1]).

%% Internal export.
-export([ehook/6]).
Expand Down Expand Up @@ -79,7 +79,7 @@ groups() ->
{attributes, [], [misc_attrs, import_export]},
{tickets, [],
[otp_6321, otp_6911, otp_6914, otp_8150, otp_8238,
otp_8473, otp_8522, otp_8567, otp_8664]}].
otp_8473, otp_8522, otp_8567, otp_8664, otp_9147]}].

init_per_suite(Config) ->
Config.
Expand Down Expand Up @@ -1047,6 +1047,26 @@ otp_8664(Config) when is_list(Config) ->

ok.

otp_9147(doc) ->
"OTP_9147. Create well-formed types when adding 'undefined'.";
otp_9147(suite) -> [];
otp_9147(Config) when is_list(Config) ->
FileName = filename('otp_9147.erl', Config),
C1 = <<"-module(otp_9147).\n"
"-export_type([undef/0]).\n"
"-record(undef, {f1 :: F1 :: a | b}).\n"
"-type undef() :: #undef{}.\n">>,
?line ok = file:write_file(FileName, C1),
?line {ok, _, []} =
compile:file(FileName, [return,'P',{outdir,?privdir}]),
PFileName = filename('otp_9147.P', Config),
?line {ok, Bin} = file:read_file(PFileName),
%% The parentheses around "F1 :: a | b" are new (bugfix).
?line true =
lists:member("-record(undef,{f1 :: undefined | (F1 :: a | b)}).",
string:tokens(binary_to_list(Bin), "\n")),
ok.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

compile(Config, Tests) ->
Expand Down

0 comments on commit e243040

Please sign in to comment.