Skip to content

Warn on missing parentheses in bitstring modifiers#11862

Merged
josevalim merged 4 commits intoelixir-lang:mainfrom
sabiwara:11811-warn-expand-bitstring-modifier
Jul 27, 2022
Merged

Warn on missing parentheses in bitstring modifiers#11862
josevalim merged 4 commits intoelixir-lang:mainfrom
sabiwara:11811-warn-expand-bitstring-modifier

Conversation

@sabiwara
Copy link
Copy Markdown
Contributor

Closes #11811

@sabiwara sabiwara changed the title 11811 Warn on missing parentheses in bitstring modifiers Warn on missing parentheses in bitstring modifiers May 22, 2022
purge(Sample)
end

test "custom bitstring modifier is being expanded to macro call" do
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.

Can you please add the tests to kernel/binary_test.exs?

Copy link
Copy Markdown
Member

@josevalim josevalim left a comment

Choose a reason for hiding this comment

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

Thank you! Can you also please warn if someone writes <<foo::binary()>> instead of <<foo::binary>>?

Note though that we can only merge this after v1.14 is out, because it is too early to introduce the warning. :)

@sabiwara
Copy link
Copy Markdown
Contributor Author

Thank you! Can you also please warn if someone writes <<foo::binary()>> instead of <<foo::binary>>?

Sure, will do!

Note though that we can only merge this after v1.14 is out, because it is too early to introduce the warning. :)

Noted :)

@sabiwara sabiwara force-pushed the 11811-warn-expand-bitstring-modifier branch from b74fb35 to 9746f67 Compare May 22, 2022 23:49
Comment on lines +390 to +391
io_lib:format("bitstring specifier \"~ts\" does not exist and is being expanded to \"~ts()\","
" please use parentheses to remove the ambiguity", [Name, Name]);
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.

I based this message on the following for consistency:

extra parentheses on a remote function capture &~ts.~ts()/0 have been deprecated. Please remove the parentheses: &~ts.~ts/0

Comment thread lib/elixir/src/elixir_quote.erl Outdated
Size ->
<<Bits:Size, Bytes/binary>> = BitString,
{'<<>>', [], [{'::', [], [Bits, {size, [], [Size]}]}, {'::', [], [Bytes, {binary, [], []}]}]}
{'<<>>', [], [{'::', [], [Bits, {size, [], [Size]}]}, {'::', [], [Bytes, {binary, [], nil}]}]}
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.

Without this change, @bitstring <<"foo", 16::4>> was generating a warning because expanded to binary().
Technically, this is a breaking change, but I think it is needed for consistency?

end

defmacrop signed_16 do
defmacro signed_16 do
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.

Note: With defmacrop, I wasn't able to access the custom modifiers in Code.eval_string even by passing __ENV__.
Please let me know if there is a better way.

Copy link
Copy Markdown
Member

@josevalim josevalim left a comment

Choose a reason for hiding this comment

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

Looks great!

Waiting for the main branch to move to Elixir v1.15 before merging.

@josevalim josevalim merged commit 0960c92 into elixir-lang:main Jul 27, 2022
@josevalim
Copy link
Copy Markdown
Member

💚 💙 💜 💛 ❤️

@sabiwara sabiwara deleted the 11811-warn-expand-bitstring-modifier branch July 27, 2022 12:45
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.

Warn on variables becoming function calls in more occasions

2 participants