Skip to content
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

Elixir doesn't warn with trying to concat string literals with invalid operator and string interpolation #13496

Closed
davydog187 opened this issue Apr 13, 2024 · 6 comments

Comments

@davydog187
Copy link
Contributor

Elixir and Erlang/OTP versions

Erlang/OTP 26 [erts-14.2.1] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [jit]

Elixir 1.16.0 (compiled with Erlang/OTP 26)

Operating system

MacOS

Current behavior

# concat.ex
defmodule Concat do
  def warns do
    "foo" + "bar"
  end

  def doesnt(reason) when is_binary(reason) do
    "#{reason}" + "bar"
  end
end

When compiling with elixirc concat.ex, I get the following warning

    warning: the call to +/2 will fail with ArithmeticError3"foo" + "bar"~~~~~~~~~~~~~
    │
    └─ concat.ex:3

notice that it doesn't warn for doesnt/1. It seems that adding string interpolation breaks the check.

Expected behavior

I would expect the same ArithmeticError warning for doesnt/1.

@davydog187
Copy link
Contributor Author

Also confirmed this behavior exists in elixir 1.16.2

@v0idpwn
Copy link
Contributor

v0idpwn commented Apr 13, 2024

I may be wrong here, but my interpretation is: this warning comes from Erlang and only works for literals. Since the interpolated string isn't a literal in Erlang, the warning isn't raised there and then isn't translated by the Elixir compiler.

@davydog187
Copy link
Contributor Author

I'm not sure how the compiler represents string interpolation internally (function call maybe?), but since it knows that the resulting type is a string, it should be smart enough to handle this case.

If it's an OTP bug so be it, but I reckon the Elixir compiler can handle this

@sabiwara
Copy link
Contributor

this warning comes from Erlang and only works for literals. Since the interpolated string isn't a literal in Erlang, the warning isn't raised there and then isn't translated by the Elixir compiler.

@v0idpwn is correct.

If it's an OTP bug so be it, but I reckon the Elixir compiler can handle this

I don't think this would qualify as a bug, as it is more in the realm of what a type-system like dialyzer is providing.
But it should hopefully be addressed in Elixir by the upcoming gradual type system.

@josevalim
Copy link
Member

Yes, and those warnings are not guaranteed. Nor Erlang nor Elixir compilers are attempting to find typing violations, those are rather found when trying to optimize code, and reported as a convenience. As mentioned above, those are rather addressed by type systems, Dialyzer, etc. :)

@josevalim josevalim closed this as not planned Won't fix, can't repro, duplicate, stale Apr 13, 2024
@davydog187
Copy link
Contributor Author

We'll wait for the type system then 😀

FWIW, I was not aware than these warnings should be treated as a convenience, and even so the consistency is unexpected.

If there's an opportunity to clarify this somewhere I think it would be helpful as this issue was inspired by code written by a newcomer to Elixir and only caught by an obscure dialyzer error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

4 participants