Skip to content

Commit

Permalink
compiler: Use strict generators in comprehensions
Browse files Browse the repository at this point in the history
If I have counted correctly, in the modules I have updated,
there 105 strict list generators with failable patterns and 73 relaxed
list generators with failable patterns.
  • Loading branch information
bjorng committed Oct 21, 2024
1 parent 81055d6 commit 61bb556
Show file tree
Hide file tree
Showing 29 changed files with 107 additions and 106 deletions.
4 changes: 2 additions & 2 deletions lib/compiler/src/beam_asm.erl
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ build_file(Code, Attr, Dict, NumLabels, NumFuncs, ExtraChunks0, CompileInfo, Com

%% Compile all extra chunks.

CheckedChunks = [chunk(Key, Value) || {Key, Value} <- ExtraChunks],
CheckedChunks = [chunk(Key, Value) || {Key, Value} <:- ExtraChunks],

%% Create IFF chunk.

Expand Down Expand Up @@ -291,7 +291,7 @@ build_atom_table(Options, Dict) ->
case member(no_long_atoms, Options) of
false ->
%% Build an atom table for Erlang/OTP 28 and later.
AtomTab = [[encode(?tag_u, Len),Text] || [Len,Text] <- AtomTab0],
AtomTab = [[encode(?tag_u, Len),Text] || [Len,Text] <:- AtomTab0],
chunk(<<"AtU8">>, <<-NumAtoms:32>>, AtomTab);
true ->
%% Build an atom table compatible with Erlang/OTP 27
Expand Down
2 changes: 1 addition & 1 deletion lib/compiler/src/beam_call_types.erl
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ will_succeed(Mod, Func, Args) ->

max_tuple_size(#t_union{tuple_set=[_|_]=Set}=Union) ->
Union = meet(Union, #t_tuple{}), %Assertion.
Arities = [Arity || {{Arity, _Tag}, _Record} <- Set],
Arities = [Arity || {{Arity, _Tag}, _Record} <:- Set],
lists:max(Arities);
max_tuple_size(#t_tuple{exact=true,size=Size}) ->
Size;
Expand Down
6 changes: 3 additions & 3 deletions lib/compiler/src/beam_clean.erl
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@

module({Mod,Exp,Attr,Fs0,_}, Opts) ->
Fs1 = move_out_funs(Fs0),
Order = [Lbl || {function,_,_,Lbl,_} <- Fs1],
All = #{Lbl => Func || {function,_,_,Lbl,_}=Func <- Fs1},
Order = [Lbl || {function,_,_,Lbl,_} <:- Fs1],
All = #{Lbl => Func || {function,_,_,Lbl,_}=Func <:- Fs1},
WorkList = rootset(Fs1, Exp, Attr),
Used = find_all_used(WorkList, All, sets:from_list(WorkList)),
Fs2 = remove_unused(Order, Used, All),
Expand All @@ -49,7 +49,7 @@ rootset(Fs, Root0, Attr) ->
[OnLoad] -> [OnLoad|Root0]
end,
Root = sofs:set(Root1, [function]),
Map0 = [{{Name,Arity},Lbl} || {function,Name,Arity,Lbl,_} <- Fs],
Map0 = [{{Name,Arity},Lbl} || {function,Name,Arity,Lbl,_} <:- Fs],
Map = sofs:relation(Map0, [{function,label}]),
sofs:to_external(sofs:image(Map, Root)).

Expand Down
10 changes: 5 additions & 5 deletions lib/compiler/src/beam_core_to_ssa.erl
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ expr(#c_let{vars=Cvs,arg=Ca,body=Cb}, Sub0, St0) ->
%% Break known multiple values into separate sets.
Sets = case Ka of
#ivalues{args=Kas} ->
[#iset{vars=[V],arg=Val} || {V,Val} <- zip(Kps, Kas)];
[#iset{vars=[V],arg=Val} || {V,Val} <:- zip(Kps, Kas)];
_Other ->
[#iset{vars=Kps,arg=Ka}]
end,
Expand Down Expand Up @@ -1451,7 +1451,7 @@ reorder_bin_ints(Cs0) ->
%% * The patterns that follow are also safe to re-order.
try
Cs = sort([{reorder_bin_int_sort_key(C),C} || C <- Cs0]),
[C || {_,C} <- Cs]
[C || {_,C} <:- Cs]
catch
throw:not_possible ->
Cs0
Expand Down Expand Up @@ -1692,7 +1692,7 @@ new_clauses(Cs, #b_var{name=U}) ->
#cg_bin_int{next=N} ->
[N|As];
#cg_map{op=exact,es=Es} ->
Vals = [V || #cg_map_pair{val=V} <- Es],
Vals = [V || #cg_map_pair{val=V} <:- Es],
Vals ++ As;
_Other ->
As
Expand Down Expand Up @@ -1802,7 +1802,7 @@ do_squeeze_clauses(Cs, Size, Count) when Count >= 16; Size =< 1 ->
Cs;
do_squeeze_clauses(Cs, Size, _Count) ->
[C#iclause{pats=[squeeze_segments(P, Size)|Pats]} ||
#iclause{pats=[P|Pats]}=C <- Cs].
#iclause{pats=[P|Pats]}=C <:- Cs].

squeeze_segments(BinSeg, Size) ->
squeeze_segments(BinSeg, 0, 0, Size).
Expand Down Expand Up @@ -1919,7 +1919,7 @@ arg_val(Arg, C) ->
%% Literals will sort before variables
%% as intended.
erts_internal:cmp_term(A, B) < 0
end, [Key || #cg_map_pair{key=Key} <- Es])
end, [Key || #cg_map_pair{key=Key} <:- Es])
end.

%%%
Expand Down
16 changes: 8 additions & 8 deletions lib/compiler/src/beam_dict.erl
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ atom_table(#asm{atoms=Atoms}) ->
{NumAtoms,[begin
L = atom_to_binary(A, utf8),
[byte_size(L),L]
end || {A,_} <- Sorted]}.
end || {A,_} <:- Sorted]}.

%% Returns the table of local functions.
%% local_table(Dict) -> {NumLocals, [{Function, Arity, Label}...]}
Expand All @@ -283,7 +283,7 @@ export_table(#asm{exports = Exports}) ->

import_table(#asm{imports=Imp,next_import=NumImports}) ->
Sorted = lists:keysort(2, gb_trees:to_list(Imp)),
ImpTab = [MFA || {MFA,_} <- Sorted],
ImpTab = [MFA || {MFA,_} <:- Sorted],
{NumImports,ImpTab}.

-spec string_table(bdict()) -> {non_neg_integer(), binary()}.
Expand All @@ -295,15 +295,15 @@ string_table(#asm{strings=Strings,string_offset=Size}) ->

lambda_table(#asm{locals=Loc0,exports=Ext0,lambdas={NumLambdas,Lambdas0}}) ->
Lambdas1 = sofs:relation(Lambdas0),
Loc = sofs:relation([{Lbl,{F,A}} || {F,A,Lbl} <- Loc0]),
Ext = sofs:relation([{Lbl,{F,A}} || {F,A,Lbl} <- Ext0]),
Loc = sofs:relation([{Lbl,{F,A}} || {F,A,Lbl} <:- Loc0]),
Ext = sofs:relation([{Lbl,{F,A}} || {F,A,Lbl} <:- Ext0]),
All = sofs:union(Loc, Ext),
Lambdas2 = sofs:relative_product1(Lambdas1, All),
%% Initialize OldUniq to 0. It will be set to an unique value
%% based on the MD5 checksum of the BEAM code for the module.
OldUniq = 0,
Lambdas = [<<F:32,A:32,Lbl:32,Index:32,NumFree:32,OldUniq:32>> ||
{{Index,Lbl,NumFree},{F,A}} <- sofs:to_external(Lambdas2)],
{{Index,Lbl,NumFree},{F,A}} <:- sofs:to_external(Lambdas2)],
{NumLambdas,Lambdas}.

%% Returns the literal table.
Expand All @@ -318,7 +318,7 @@ literal_table(#asm{literals=Tab,next_literal=NumLiterals}) ->
[{Num,Lit}|Acc]
end, [], Tab),
L1 = lists:sort(L0),
L = [[<<(byte_size(Term)):32>>,Term] || {_,Term} <- L1],
L = [[<<(byte_size(Term)):32>>,Term] || {_,Term} <:- L1],
{NumLiterals,L}.

my_term_to_binary(Term) ->
Expand Down Expand Up @@ -352,10 +352,10 @@ line_table(#asm{fnames=Fnames0,lines=Lines0,
num_lines=NumLineInstrs,exec_line=ExecLine}) ->
NumFnames = maps:size(Fnames0),
Fnames1 = lists:keysort(2, maps:to_list(Fnames0)),
Fnames = [Name || {Name,_} <- Fnames1],
Fnames = [Name || {Name,_} <:- Fnames1],
NumLines = maps:size(Lines0),
Lines1 = lists:keysort(2, maps:to_list(Lines0)),
Lines = [L || {L,_} <- Lines1],
Lines = [L || {L,_} <:- Lines1],
{NumLineInstrs,NumFnames,Fnames,NumLines,Lines,ExecLine}.

%% Search for binary string Str in the binary string pool Pool.
Expand Down
4 changes: 2 additions & 2 deletions lib/compiler/src/beam_digraph.erl
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ in_edges(#dg{in_es=InEsMap}, V) ->

-spec in_neighbours(graph(), vertex()) -> [vertex()].
in_neighbours(#dg{in_es=InEsMap}, V) ->
[From || {From,_,_} <- maps:get(V, InEsMap, [])].
[From || {From,_,_} <:- maps:get(V, InEsMap, [])].

-spec is_path(graph(), vertex(), vertex()) -> boolean().
is_path(G, From, To) ->
Expand Down Expand Up @@ -180,7 +180,7 @@ out_edges(#dg{out_es=OutEsMap}, V) ->

-spec out_neighbours(graph(), vertex()) -> [vertex()].
out_neighbours(#dg{out_es=OutEsMap}, V) ->
[To || {_,To,_} <- maps:get(V, OutEsMap, [])].
[To || {_,To,_} <:- maps:get(V, OutEsMap, [])].

-spec no_vertices(graph()) -> non_neg_integer().
no_vertices(#dg{vs=Vs}) ->
Expand Down
5 changes: 3 additions & 2 deletions lib/compiler/src/beam_disasm.erl
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ beam_disasm_code(<<_SS:32, % Sub-Size (length of information before code)
[function__code_update(Function,
resolve_names(Is, Imports, Str,
Labels, Lambdas, Literals, M))
|| Function = #function{code=Is} <- Functions]
|| Function = #function{code=Is} <:- Functions]
catch
error:Rsn ->
?NO_DEBUG('code disassembling failed: ~p~n', [Rsn]),
Expand Down Expand Up @@ -1410,7 +1410,8 @@ decode_field_flags(FieldFlags) when is_integer(FieldFlags) ->
%%-----------------------------------------------------------------------

mk_imports(ImportList) ->
gb_trees:from_orddict([{I,{extfunc,M,F,A}} || {I,M,F,A} <- ImportList]).
gb_trees:from_orddict([{I,{extfunc,M,F,A}} ||
{I,M,F,A} <:- ImportList]).

mk_atoms(AtomList) ->
gb_trees:from_orddict(AtomList).
Expand Down
10 changes: 5 additions & 5 deletions lib/compiler/src/beam_ssa.erl
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ insert_on_edges_1([], Blocks, Count) ->

insert_on_edges_reroute(#b_switch{fail=Fail0,list=List0}=Sw, Old, New) ->
Fail = rename_label(Fail0, Old, New),
List = [{Value, rename_label(Dst, Old, New)} || {Value, Dst} <- List0],
List = [{Value, rename_label(Dst, Old, New)} || {Value, Dst} <:- List0],
Sw#b_switch{fail=Fail,list=List};
insert_on_edges_reroute(#b_br{succ=Succ0,fail=Fail0}=Br, Old, New) ->
Succ = rename_label(Succ0, Old, New),
Expand Down Expand Up @@ -358,7 +358,7 @@ successors(#b_blk{last=Terminator}) ->
#b_br{succ=Succ,fail=Fail} ->
[Fail,Succ];
#b_switch{fail=Fail,list=List} ->
[Fail|[L || {_,L} <- List]];
[Fail|[L || {_,L} <:- List]];
#b_ret{} ->
[]
end.
Expand Down Expand Up @@ -941,7 +941,7 @@ fix_phis([{L,Blk0}|Bs], S) ->
fix_phis([], _) -> [].

fix_phis_1([#b_set{op=phi,args=Args0}=I|Is], L, S) ->
Args = [{Val,Pred} || {Val,Pred} <- Args0,
Args = [{Val,Pred} || {Val,Pred} <:- Args0,
is_successor(L, Pred, S)],
[I#b_set{args=Args}|fix_phis_1(Is, L, S)];
fix_phis_1(Is, _, _) -> Is.
Expand Down Expand Up @@ -980,7 +980,7 @@ trim_phis(#b_blk{is=[#b_set{op=phi}|_]=Is0}=Blk, Seen) ->
trim_phis(Blk, _Seen) -> Blk.

trim_phis_1([#b_set{op=phi,args=Args0}=I|Is], Seen) ->
Args = [P || {_,L}=P <- Args0, sets:is_element(L, Seen)],
Args = [P || {_,L}=P <:- Args0, sets:is_element(L, Seen)],
[I#b_set{args=Args}|trim_phis_1(Is, Seen)];
trim_phis_1(Is, _Seen) -> Is.

Expand Down Expand Up @@ -1093,7 +1093,7 @@ split_blocks_is([I|Is], P, Acc) ->
split_blocks_is([], _, _) -> no.

update_phi_labels_is([#b_set{op=phi,args=Args0}=I0|Is], Old, New) ->
Args = [{Arg,rename_label(Lbl, Old, New)} || {Arg,Lbl} <- Args0],
Args = [{Arg,rename_label(Lbl, Old, New)} || {Arg,Lbl} <:- Args0],
I = I0#b_set{args=Args},
[I|update_phi_labels_is(Is, Old, New)];
update_phi_labels_is(Is, _, _) -> Is.
Expand Down
8 changes: 4 additions & 4 deletions lib/compiler/src/beam_ssa_alias.erl
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ opt(StMap0, FuncDb0) ->

killsets(Funs, StMap) ->
OptStates = [{F,map_get(F, StMap)} || F <- Funs],
#{ F=>killsets_fun(reverse(SSA)) || {F,#opt_st{ssa=SSA}} <- OptStates }.
#{ F=>killsets_fun(reverse(SSA)) || {F,#opt_st{ssa=SSA}} <:- OptStates }.

killsets_fun(Blocks) ->
%% Pre-calculate the live-ins due to Phi-instructions.
Expand Down Expand Up @@ -655,7 +655,7 @@ aa_terminator(#b_ret{arg=Arg,anno=Anno0}, SS, Lbl2SS0) ->
Lbl2SS = Lbl2SS0#{ returns => Type2Status },
{Lbl2SS, []};
aa_terminator(#b_switch{fail=F,list=Ls}, _SS, Lbl2SS) ->
{Lbl2SS,[F|[L || {_,L} <- Ls]]}.
{Lbl2SS,[F|[L || {_,L} <:- Ls]]}.

%% Store the updated SS for the point where execution leaves the
%% block.
Expand Down Expand Up @@ -1069,7 +1069,7 @@ aa_construct_tuple(Dst, IdxValues, Types, SS, AAS) ->
[Dst, [#{idx=>Idx,v=>V,status=>aa_get_status(V, SS, Types),
killed=>aa_dies(V, Types, KillSet),
plain=>aa_is_plain_value(V, Types)}
|| {Idx,V} <- IdxValues]]),
|| {Idx,V} <:- IdxValues]]),
?DP("~s~n", [beam_ssa_ss:dump(SS)]),
aa_build_tuple_or_pair(Dst, IdxValues, Types, KillSet, SS, []).

Expand Down Expand Up @@ -1168,7 +1168,7 @@ aa_bif(Dst, Bif, Args, SS, _AAS) ->

aa_phi(Dst, Args0, SS0, #aas{cnt=Cnt0}=AAS) ->
%% TODO: Use type info?
Args = [V || {V,_} <- Args0],
Args = [V || {V,_} <:- Args0],
?DP("Phi~n"),
SS1 = aa_alias_surviving_args(Args, {phi,Dst}, SS0, AAS),
?DP(" after aa_alias_surviving_args:~n~s.~n", [beam_ssa_ss:dump(SS1)]),
Expand Down
10 changes: 5 additions & 5 deletions lib/compiler/src/beam_ssa_bool.erl
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ pre_opt([], _, _, Count, Blocks) ->
{Blocks,Count}.

pre_opt_is([#b_set{op=phi,dst=Dst,args=Args0}=I0|Is], Reached, Sub0, Acc) ->
Args1 = [{Val,From} || {Val,From} <- Args0,
Args1 = [{Val,From} || {Val,From} <:- Args0,
sets:is_element(From, Reached)],
Args = sub_args(Args1, Sub0),
case all_same(Args) of
Expand Down Expand Up @@ -674,7 +674,7 @@ bool_opt_rewrite(Bool, From, Br, Blocks0, St0) ->
%% because the map of definitions in St#st.defs would not be updated
%% to include the newly optimized blocks.
DomBlk0 = map_get(Dom, Blocks1),
Blocks2 = maps:without([L || {L,#b_blk{}} <- Bs], Blocks1),
Blocks2 = maps:without([L || {L,#b_blk{}} <:- Bs], Blocks1),

%% Convert the optimized digraph back to SSA code.
Blocks3 = digraph_to_ssa([Root], G, Blocks2),
Expand Down Expand Up @@ -739,7 +739,7 @@ collect_phi_args(Args, Anno) ->
[] ->
%% This phi node only contains literal values.
%% Force the inclusion of referenced blocks.
Ls = [{block,L} || {_,L} <- Args],
Ls = [{block,L} || {_,L} <:- Args],
{[],Ls}
end;
false ->
Expand Down Expand Up @@ -874,7 +874,7 @@ build_digraph_is([#b_set{op=phi,args=Args0}=I0|Is], Last, Vtx, Map, G, St) ->
Args = [{V,case Map of
#{L:=Other} -> Other;
#{} -> not_possible()
end} || {V,L} <- Args0],
end} || {V,L} <:- Args0],
I = I0#b_set{args=Args},
build_digraph_is_1(I, Is, Last, Vtx, Map, G, St);
build_digraph_is([#b_set{}=I|Is], Last, Vtx, Map, G, St) ->
Expand Down Expand Up @@ -1632,7 +1632,7 @@ digraph_to_ssa_blk(From, G, Blocks, Acc0) ->
{external,Sub} ->
#b_blk{is=Is0} = Blk = map_get(From, Blocks),
Is = [I#b_set{args=sub_args(Args0, Sub)} ||
#b_set{args=Args0}=I <- Is0],
#b_set{args=Args0}=I <:- Is0],
{Blk#b_blk{is=Is},[]}
end.

Expand Down
6 changes: 3 additions & 3 deletions lib/compiler/src/beam_ssa_bsm.erl
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ cm_handle_priors(Src, DstCtx, Bool, Acc, MatchSeq, Lbl, State0) ->
%% we can only consider the ones whose success path
%% dominate us.
Dominators = maps:get(Lbl, State0#cm.dominators, []),
[Ctx || {ValidAfter, Ctx} <- Priors,
[Ctx || {ValidAfter, Ctx} <:- Priors,
member(ValidAfter, Dominators)];
error ->
[]
Expand Down Expand Up @@ -776,7 +776,7 @@ aca_cs_is([], Counter, VRs, _BRs, Acc) ->
{VRs, reverse(Acc), Counter}.

aca_cs_last(#b_switch{arg=Arg0,list=Switch0,fail=Fail0}=Sw, VRs, BRs) ->
Switch = [{Literal, maps:get(Lbl, BRs)} || {Literal, Lbl} <- Switch0],
Switch = [{Literal, maps:get(Lbl, BRs)} || {Literal, Lbl} <:- Switch0],
Sw#b_switch{arg=aca_cs_arg(Arg0, VRs),
fail=maps:get(Fail0, BRs),
list=Switch};
Expand Down Expand Up @@ -821,7 +821,7 @@ aca_cs_arg(Arg, VRs) ->
%% contexts to us.

allow_context_passthrough({Fs, ModInfo0}) ->
FsUses = [{F, beam_ssa:uses(beam_ssa:rpo(Bs), Bs)} || #b_function{bs=Bs}=F <- Fs],
FsUses = [{F, beam_ssa:uses(beam_ssa:rpo(Bs), Bs)} || #b_function{bs=Bs}=F <:- Fs],
ModInfo = acp_forward_params(FsUses, ModInfo0),
{Fs, ModInfo}.

Expand Down
2 changes: 1 addition & 1 deletion lib/compiler/src/beam_ssa_check.erl
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ build_map_key({list,_,Elems}, Env) ->
build_map_key({tuple,_,Elems}, Env) ->
list_to_tuple([build_map_key(E, Env) || E <- Elems]);
build_map_key({map,_,Elems}, Env) ->
#{build_map_key(K, Env) => build_map_key(V, Env) || {K,V} <- Elems};
#{build_map_key(K, Env) => build_map_key(V, Env) || {K,V} <:- Elems};
build_map_key({var,_,V}, Env) ->
map_get(V, Env);
build_map_key(_Key, _Env) ->
Expand Down
4 changes: 2 additions & 2 deletions lib/compiler/src/beam_ssa_codegen.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2028,7 +2028,7 @@ force_reg({Kind,_}=R, _) when Kind =:= x; Kind =:= y ->
successors(#cg_br{succ=Succ,fail=Fail}) ->
ordsets:from_list([Succ,Fail]);
successors(#cg_switch{fail=Fail,list=List}) ->
ordsets:from_list([Fail|[Lbl || {_,Lbl} <- List]]);
ordsets:from_list([Fail|[Lbl || {_,Lbl} <:- List]]);
successors(#cg_ret{}) -> [].

%% linearize(Blocks) -> [{BlockLabel,#cg_blk{}}].
Expand Down Expand Up @@ -2134,7 +2134,7 @@ translate_phis(L, #cg_br{succ=Target,fail=Target}, Blocks) ->
translate_phis(_, _, _) -> [].

phi_copies([#b_set{dst=Dst,args=PhiArgs}|Sets], L) ->
CopyArgs = [V || {V,Target} <- PhiArgs, Target =:= L],
CopyArgs = [V || {V,Target} <:- PhiArgs, Target =:= L],
[#cg_set{op=copy,dst=Dst,args=CopyArgs}|phi_copies(Sets, L)];
phi_copies([], _) -> [].

Expand Down
Loading

0 comments on commit 61bb556

Please sign in to comment.