Skip to content
Browse files

Ensure RecordRewriter is no-op for non record fields

  • Loading branch information...
1 parent af95958 commit ba43183f1b447e1e41c5229dae1bcf501abd8c19 @josevalim josevalim committed Dec 3, 2012
Showing with 15 additions and 8 deletions.
  1. +10 −8 lib/elixir/lib/kernel/record_rewriter.ex
  2. +5 −0 lib/elixir/test/elixir/kernel/record_rewriter_test.exs
View
18 lib/elixir/lib/kernel/record_rewriter.ex
@@ -47,7 +47,7 @@ defmodule Kernel.RecordRewriter do
optimizable = record.__record__(:optimizable)
{ fields, optimizable }
rescue
- [UndefinedFunctionError, FunctionClauseError] -> { [], [] }
+ [UndefinedFunctionError, FunctionClauseError] -> nil
end
end
end
@@ -60,13 +60,15 @@ defmodule Kernel.RecordRewriter do
end
defp optimize_call(line, { record, _ } = res, left, { :atom, _, function }, args) do
- { fields, optimizable } = record_fields(record)
-
- 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)
- end
+ case record_fields(record) do
+ { fields, optimizable } ->
+ 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)
+ end
+ end
+ nil -> nil
end
end
View
5 lib/elixir/test/elixir/kernel/record_rewriter_test.exs
@@ -235,6 +235,11 @@ defmodule Kernel.RecordRewriterTest do
assert optimize_clause(clause) == { clause, [x: Range], nil }
end
+ test "noop for not records fields" do
+ clause = clause(fn(x = { :not_a_record, _ }) -> x.unknown end)
+ assert optimize_clause(clause) == { clause, [x: :not_a_record], nil }
+ end
+
test "noop for rewriten fields" do
clause = clause(fn(x = BadRange[]) -> x.first end)
assert optimize_clause(clause) == { clause, [x: BadRange], nil }

0 comments on commit ba43183

Please sign in to comment.
Something went wrong with that request. Please try again.