Skip to content

Elixir 1.19 inconsistent typing warning #14347

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

Closed
nwjlyons opened this issue Mar 19, 2025 · 4 comments
Closed

Elixir 1.19 inconsistent typing warning #14347

nwjlyons opened this issue Mar 19, 2025 · 4 comments

Comments

@nwjlyons
Copy link
Contributor

Elixir and Erlang/OTP versions

Erlang/OTP 27 [erts-15.1.2] [source] [64-bit] [smp:10:10] [ds:10:10:10] [async-threads:1] [jit]

Elixir 1.19.0-dev (b601b1c) (compiled with Erlang/OTP 27)

Operating system

mac

Current behavior

I know 1.19 is not released yet so feel free to close this ticket.

I only got this warning when running mix compile, not with elixirc. Both with 1.19-dev using asdf install elixir main-otp-27

git repo which shows the problem https://github.com/nwjlyons/foo/blob/main/lib/foo.ex

comparison with structs warning

["foo-#{i}": i > 0]
full compiler warning
  warning: comparison with structs found:

      i > 0

  given types:

      dynamic(
        %Date{} or %DateTime{} or %NaiveDateTime{} or %Time{} or %URI{} or %Version{} or
          %Version.Requirement{} or atom() or binary() or float() or integer() or list(term())
      ) > integer()

  where "i" was given the type:

      # type: dynamic(
        %Date{} or %DateTime{} or %NaiveDateTime{} or %Time{} or %URI{} or %Version{} or
          %Version.Requirement{} or atom() or binary() or float() or integer() or list(term())
      )
      # from: lib/foo.ex:3
      to_string(i)

  Comparison operators (>, <, >=, <=, min, and max) perform structural and not semantic comparison. Comparing with a struct won't give meaningful results. Structs that can be compared typically define a compare/2 function within their modules that can be used for semantic comparison.

  typing violation found at:
  │
3 │     ["foo-#{i}": i > 0]
  │                    ~
  │
  └─ lib/foo.ex:3:20: Foo.error/1

no warning for

if(i > 0, do: "foo-#{i}")

# or

if(i > 0, do: :"foo-#{i}")

Expected behavior

To be consistent with the if expressions. So consistently no warnings or all warnings.

@nwjlyons nwjlyons changed the title Elixir 1.19 incorrect typing violation Elixir 1.19 inconsistent typing warning Mar 19, 2025
@josevalim
Copy link
Member

Thank you. We could address this warning but at the cost of preventing fewer bugs. For example, someone can pass a string or a decimal to this code, and it would return something, but that may not be valid. My suggestion then would be to explicit add a is_integer(i) guard and, since we will add type checking of guards in the next release, it should all just work. So I would say this will be closed as part of #13227 but you will need to change your code to make it more explicit.

@josevalim
Copy link
Member

Closing as duplicate of #13227 for now. We may revisit to not emit this warning in the future.

@josevalim josevalim reopened this Mar 25, 2025
@josevalim
Copy link
Member

We got more reports, so we will tackle this.

@josevalim
Copy link
Member

Closed by b27b911.

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

2 participants