Skip to content

Commit

Permalink
Ensure update_* fields work with record rewriter
Browse files Browse the repository at this point in the history
  • Loading branch information
José Valim committed Dec 3, 2012
1 parent d9029c7 commit b20fea7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
16 changes: 10 additions & 6 deletions lib/elixir/lib/kernel/record_rewriter.ex
Expand Up @@ -54,7 +54,7 @@ defmodule Kernel.RecordRewriter do

defp record_field_info(function) do
case atom_to_list(function) do
'update_' ++ field -> { :update, list_to_atom(function) }
'update_' ++ field -> { :update, list_to_atom(field) }
_ -> { :accessor, function }
end
end
Expand All @@ -65,7 +65,7 @@ defmodule Kernel.RecordRewriter do
if List.member?(optimizable, { function, length(args) + 1 }) do
{ kind, field } = record_field_info(function)
if index = Enum.find_index(fields, field == &1) do
optimize_call(line, res, kind, index, left, args)
optimize_call(line, res, kind, field, index, left, args)
end
end
nil -> nil
Expand All @@ -76,24 +76,28 @@ defmodule Kernel.RecordRewriter do
nil
end

defp optimize_call(line, _res, :accessor, index, left, []) do
defp optimize_call(line, _res, :accessor, _field, index, left, []) do
call = { :call, line,
{ :remote, line, { :atom, 0, :erlang }, { :atom, 0, :element } },
[{ :integer, 0, index + 2 }, left]
}
{ call, nil }
end

defp optimize_call(line, res, :accessor, index, left, [arg]) do
defp optimize_call(line, res, :accessor, _field, index, left, [arg]) do
call = { :call, line,
{ :remote, line, { :atom, 0, :erlang }, { :atom, 0, :setelement } },
[{ :integer, 0, index + 2 }, left, arg]
}
{ call, res }
end

defp optimize_call(_line, _res, :update, _index, _left, [_arg]) do
nil
defp optimize_call(line, res, :update, field, _index, left, args) do
call = { :call, line,
{ :remote, line, left, { :atom, 0, :"update_#{field}" } },
args
}
{ call, res }
end

## Expr
Expand Down
5 changes: 5 additions & 0 deletions lib/elixir/test/elixir/kernel/record_rewriter_test.exs
Expand Up @@ -230,6 +230,11 @@ defmodule Kernel.RecordRewriterTest do
assert optimize_clause(clause) == { rewriten, [x: Range], { Range, nil } }
end

test "updater call is no-op (for now)" do
clause = clause(fn(x = Range[]) -> x.update_first(&1 + 1) end)
assert optimize_clause(clause) == { clause, [x: Range], { Range, nil } }
end

test "noop for unknown fields" do
clause = clause(fn(x = Range[]) -> x.unknown end)
assert optimize_clause(clause) == { clause, [x: Range], nil }
Expand Down

0 comments on commit b20fea7

Please sign in to comment.