Skip to content

Disallow use of unquote when quote is used in pattern#15469

Merged
sabiwara merged 3 commits into
elixir-lang:mainfrom
sabiwara:unquote-pattern
Jun 13, 2026
Merged

Disallow use of unquote when quote is used in pattern#15469
sabiwara merged 3 commits into
elixir-lang:mainfrom
sabiwara:unquote-pattern

Conversation

@sabiwara

Copy link
Copy Markdown
Contributor

Better error message for: #15461

The only thing I don't love is that unquote comes from the public option, and this is an internal-only mechanism, but I think it's probably fine?

Comment thread lib/elixir/src/elixir_expand.erl Outdated
Unquote = case E of
#{context := nil} -> proplists:get_value(unquote, EOpts, DefaultUnquote);
% unquote=raise when quote/1 is called in a guard or pattern
_ -> raise

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may want to preserve unquote: false though.

One alternative is to call has_unquote_calls separately, if you don’t to couple those. Given it is extremely unlikely to have quote in patterns and guards anyway, maybe that’s the best call?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may also need to change this too: https://github.com/sabiwara/elixir/blob/d7f22b71fd9d8d02535d68e39b461222c0e2fdaf/lib/elixir/src/elixir_quote.erl#L386

So perhaps doing a separate check would indeed be cleaner with less duplication?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh nice, yeah I agree performance isn't a concern in that case and it will be much more robust 👍

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pushed - much simpler indeed 😌

Comment thread lib/elixir/src/elixir_expand.erl Outdated
Comment on lines +272 to +277
maybe
true ?= map_get(context, E) /= nil,
true ?= Unquote,
true ?= elixir_quote:has_unquotes(Exprs),
file_error(Meta, E, ?MODULE, quote_in_pattern_with_unquote)
end,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a style thing but I would write it rather as:

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed it's better!
(Sorry for that, I haven't done any erlang recently and I'm a bit rusty, couldn't remember if it had to be guard syntax only or sth like that 😄)

@sabiwara

Copy link
Copy Markdown
Contributor Author

@josevalim will backport after merge

@sabiwara sabiwara merged commit df9d1d6 into elixir-lang:main Jun 13, 2026
15 checks passed
@sabiwara sabiwara deleted the unquote-pattern branch June 13, 2026 12:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants