Skip to content

Commit 55a4c59

Browse files
author
José Valim
committed
Support generated: true in quote
Signed-off-by: José Valim <jose.valim@plataformatec.com.br>
1 parent 30cbdb8 commit 55a4c59

File tree

5 files changed

+24
-6
lines changed

5 files changed

+24
-6
lines changed

lib/elixir/lib/kernel/special_forms.ex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,10 @@ defmodule Kernel.SpecialForms do
765765
quote. Read the Stacktrace information section below for more
766766
information.
767767
768+
* `:generated` - marks the given chunk as generated so it does not emit warnings.
769+
Currently it only works on special forms (for example, you cannot annotate
770+
a `case` but not an `if`).
771+
768772
* `:context` - sets the resolution context.
769773
770774
* `:bind_quoted` - passes a binding to the macro. Whenever a binding is

lib/elixir/src/elixir.hrl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
imports_hygiene=true,
3030
unquote=true,
3131
unquoted=false,
32-
escape=false
32+
escape=false,
33+
generated=false
3334
}).
3435

3536
-record(elixir_tokenizer, {

lib/elixir/src/elixir_exp.erl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ expand({quote, Meta, [KV, Do]}, E) when is_list(Do) ->
157157
false -> compile_error(Meta, E#elixir_scope.file, "missing do keyword in quote")
158158
end,
159159

160-
ValidOpts = [context, location, line, file, unquote, bind_quoted],
160+
ValidOpts = [context, location, line, file, unquote, bind_quoted, generated],
161161
{EKV, ET} = expand_opts(Meta, quote, ValidOpts, KV, E),
162162

163163
Context = case lists:keyfind(context, 1, EKV) of
@@ -198,7 +198,13 @@ expand({quote, Meta, [KV, Do]}, E) when is_list(Do) ->
198198
false -> DefaultUnquote
199199
end,
200200

201-
Q = #elixir_quote{line=Line, file=File, unquote=Unquote, context=Context},
201+
Generated = lists:keyfind(generated, 1, EKV) == {generated, true},
202+
203+
%% TODO: Do not allow negative line numbers once Erlang 18
204+
%% support is dropped as it only allows negative line
205+
%% annotations alongside the generated check.
206+
Q = #elixir_quote{line=Line, file=File, unquote=Unquote,
207+
context=Context, generated=Generated},
202208

203209
{Quoted, _Q} = elixir_quote:quote(Exprs, Binding, Q, ET),
204210
expand(Quoted, ET);

lib/elixir/src/elixir_quote.erl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,10 @@ do_quote_tuple({Left, Meta, Right}, Q, E) ->
321321
{{'{}', [], [TLeft, meta(Meta, Q), TRight]}, RQ}.
322322

323323
meta(Meta, Q) ->
324-
file(line(Meta, Q), Q).
324+
generated(file(line(Meta, Q), Q), Q).
325+
326+
generated(Meta, #elixir_quote{generated=true}) -> [{generated, true}|Meta];
327+
generated(Meta, #elixir_quote{generated=false}) -> Meta.
325328

326329
file(Meta, #elixir_quote{file=nil}) -> Meta;
327330
file(Meta, #elixir_quote{file=File}) -> [{file, File}|Meta].

lib/elixir/test/elixir/kernel/quote_test.exs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ defmodule Kernel.QuoteTest do
2727
assert quote(line: line, do: bar(1, 2, 3)) == {:bar, [line: 26], [1, 2, 3]}
2828
end
2929

30+
test "generated" do
31+
assert quote(generated: true, do: bar(1)) == {:bar, [generated: true], [1]}
32+
end
33+
3034
test "unquote call" do
3135
assert quote(do: foo(bar)[unquote(:baz)]) == quote(do: foo(bar)[:baz])
3236
assert quote(do: unquote(:bar)()) == quote(do: bar())
@@ -216,7 +220,7 @@ defmodule Kernel.QuoteTest.ErrorsTest do
216220

217221
mod = Kernel.QuoteTest.ErrorsTest
218222
file = __ENV__.file |> Path.relative_to_cwd |> String.to_char_list
219-
assert [{^mod, :add, 2, [file: ^file, line: 196]}|_] = System.stacktrace
223+
assert [{^mod, :add, 2, [file: ^file, line: 200]}|_] = System.stacktrace
220224
end
221225

222226
test "outside function error" do
@@ -226,7 +230,7 @@ defmodule Kernel.QuoteTest.ErrorsTest do
226230

227231
mod = Kernel.QuoteTest.ErrorsTest
228232
file = __ENV__.file |> Path.relative_to_cwd |> String.to_char_list
229-
assert [{^mod, _, _, [file: ^file, line: 224]}|_] = System.stacktrace
233+
assert [{^mod, _, _, [file: ^file, line: 228]}|_] = System.stacktrace
230234
end
231235
end
232236

0 commit comments

Comments
 (0)