Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/elixir/src/elixir_expand.erl
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,9 @@ expand({quote, Meta, [Opts, Do]}, S, E) when is_list(Do) ->
Unquote = proplists:get_value(unquote, EOpts, DefaultUnquote),
Generated = proplists:get_value(generated, EOpts, false),

(map_get(context, E) /= nil) andalso Unquote andalso elixir_quote:has_unquotes(Exprs) andalso
file_error(Meta, E, ?MODULE, quote_in_pattern_with_unquote),

{Q, QContext, QPrelude} = elixir_quote:build(Meta, Line, File, Context, Unquote, Generated, ET),
{EPrelude, SP, EP} = expand(QPrelude, ST, ET),
{EContext, SC, EC} = expand(QContext, SP, EP),
Expand Down Expand Up @@ -1216,6 +1219,8 @@ format_error({expected_compile_time_module, Kind, GivenTerm}) ->
format_error({unquote_outside_quote, Unquote}) ->
%% Unquote can be "unquote" or "unquote_splicing".
io_lib:format("~p called outside quote", [Unquote]);
format_error(quote_in_pattern_with_unquote) ->
"unquote is not allowed when quote is used inside a pattern or guard";
format_error({invalid_bind_quoted_for_quote, BQ}) ->
io_lib:format("invalid :bind_quoted for quote, expected a keyword list of variable names, got: ~ts",
['Elixir.Macro':to_string(BQ)]);
Expand Down
15 changes: 15 additions & 0 deletions lib/elixir/test/elixir/kernel/errors_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,21 @@ defmodule Kernel.ErrorsTest do
"""
end

test "invalid unquote when quote/1 is in a pattern" do
assert_compile_error(
["unquote is not allowed when quote is used inside a pattern or guard"],
~c"""
defmodule Kernel.ErrorsTest.InvalidUnquoteInQuotePattern do
def my_fun(ast) do
case ast do
quote(do: foo(unquote(x))) -> x
end
end
end
"""
)
end

test "invalid attribute" do
msg = ~r"cannot inject attribute @foo into function/macro because cannot escape "

Expand Down
Loading