-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Fix crash in Macro.to_string #13905
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix crash in Macro.to_string #13905
Conversation
lib/elixir/lib/code/formatter.ex
Outdated
end | ||
|
||
defp interpolation_to_algebra(quoted, %{skip_eol: skip_eol} = state) do | ||
defp interpolated_to_algebra(quoted, %{skip_eol: skip_eol} = state) do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I renamed this because I found it confusing, but if anybody has another suggestion I'm happy to take it.
I think the bug is actually in the normalizer. :( We only convert the sigil to proper syntax if it has a delimiter: elixir/lib/elixir/lib/code/formatter.ex Line 1421 in 3ce07dc
I think the issue is that the normalizer is adding a delimiter, when it is not a proper AST. WDYT? |
OK, so the idea is to treat Currently there are two differences:
iex> Code.Normalizer.normalize(quote do: foo(<<"abc", 123>>, []))
{:foo, [closing: [line: nil]],
[
{:<<>>, [], [{:__block__, [], ["abc"]}, {:__block__, [token: "123"], ~c"{"}]},
{:__block__, [], [[]]} # <=== [] is wrapped
]}
iex> Code.Normalizer.normalize(quote do: sigil_r(<<"abc", 123>>, []))
{:sigil_r, [delimiter: "\"", context: Elixir, imports: [{2, Kernel}]],
[
{:<<>>, [], [{:__block__, [], ["abc"]}, {:__block__, [token: "123"], ~c"{"}]},
[] # <=== [] is NOT wrapped
]} |
Exactly. We should not add a delimiter and do other sigil things if it is not a sigil. |
3ce07dc
to
adfe040
Compare
@josevalim makes sense to me, re-pushed with the new approach adfe040. I wonder though if it could be considered a regression, this felt intentional based on this commit 2d1c64c. Maybe when creating sigils through meta-programming, if that's a thing? Oh, and we also need to re-evaluate the use of |
@sabiwara we should definitely support sigils through meta-programming. I think we should not check for delimiters in Code.Normalizer. Instead, we should normalize that both arguments of the sigil macro are "valid" (from the point of view of the formatter) and only add the delimiter if we can normalize them. |
OK I see what you mean now, sorry for the misunderstanding 🙇 BTW, unrelated but this feels like a good change, there is no need to lose the original delimiter in ExUnit no? I won't need it anymore for this PR though. |
Agreed it is a good change! |
94c5620
to
e02ef3e
Compare
e02ef3e
to
020025a
Compare
@josevalim forced-pushed take 3: 020025a. I also caught a couple of crashes when the modifiers are invalid, covered these cases too. |
This is the first issue described in #13895.