Skip to content

Commit 4e11ff2

Browse files
committed
Emit leaner code on with var <-
1 parent ce17aa9 commit 4e11ff2

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

lib/elixir/src/elixir_clauses.erl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,16 @@ with(Meta, Args, S, E) ->
169169

170170
{{with, Meta, EExprs ++ [[{do, EDo} | EOpts]]}, S3, E}.
171171

172-
expand_with({'<-', Meta, [Left, Right]}, {S, E, _HasMatch}) ->
172+
expand_with({'<-', Meta, [Left, Right]}, {S, E, HasMatch}) ->
173173
{ERight, SR, ER} = elixir_expand:expand(Right, S, E),
174174
SM = elixir_env:reset_read(SR, S),
175175
{[ELeft], SL, EL} = head([Left], SM, ER),
176-
{{'<-', Meta, [ELeft, ERight]}, {SL, EL, true}};
176+
NewHasMatch =
177+
case ELeft of
178+
{Var, _, Ctx} when is_atom(Var), is_atom(Ctx) -> HasMatch;
179+
_ -> true
180+
end,
181+
{{'<-', Meta, [ELeft, ERight]}, {SL, EL, NewHasMatch}};
177182
expand_with(Expr, {S, E, HasMatch}) ->
178183
{EExpr, SE, EE} = elixir_expand:expand(Expr, S, E),
179184
{EExpr, {SE, EE, HasMatch}}.

lib/elixir/src/elixir_erl_pass.erl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,8 @@ with_else_closure(Meta, TranslatedClauses, S) ->
435435
FunCall = {call, Ann, FunErlVar, [ArgErlVar]},
436436
{{clause, Ann, [ArgErlVar], [], [FunCall]}, FunAssign, SA}.
437437

438+
translate_with_do([{'<-', Meta, [{Var, _, Ctx} = Left, Expr]} | Rest], Ann, Do, Else, S) when is_atom(Var), is_atom(Ctx) ->
439+
translate_with_do([{'=', Meta, [Left, Expr]} | Rest], Ann, Do, Else, S);
438440
translate_with_do([{'<-', Meta, [Left, Expr]} | Rest], _Ann, Do, Else, S) ->
439441
Ann = ?ann(Meta),
440442
{Args, Guards} = elixir_utils:extract_guards(Left),

0 commit comments

Comments
 (0)