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

Compiler allows comparing a comparable to a literal number #1610

Closed
fbonetti opened this issue Jul 6, 2017 · 5 comments
Closed

Compiler allows comparing a comparable to a literal number #1610

fbonetti opened this issue Jul 6, 2017 · 5 comments

Comments

@fbonetti
Copy link

fbonetti commented Jul 6, 2017

SSCCE

This should fail to compile, but it doesn't:

module Bug exposing (bug)


bug : comparable -> Bool
bug n =
    n == 1

Running the code doesn't produce a runtime exception, but it does return some strange results.

import Bug exposing (bug)

bug 1
> True

bug 'a'
> False

bug ""
> False

bug "asdf"
> False

bug []
> False

bug (0)
> True

bug (1)
> False

This bug only occurs when comparing a comparable to a literal number (as in, the number is hardcoded). For example, this correctly fails to compile:

bug : comparable -> number -> Bool
bug n a =
    n == a

Environment

$ elm --version
0.18.0

$ elm-repl --version
0.18.0

$ node --version
v8.1.0

$ sw_vers
ProductName:	Mac OS X
ProductVersion:	10.11.6
BuildVersion:	15G1108

Edit: Added environment info

@process-bot
Copy link

Thanks for the issue! Make sure it satisfies this checklist. My human colleagues will appreciate it!

Here is what to expect next, and if anyone wants to comment, keep these things in mind.

@jvoigtlaender
Copy link
Contributor

@fbonetti, on the mailing list you asked whether this is a known problem. It does look like one of this conglomerate: #1581, #1268, #1270, #1281, #1316, which according to #1373 (comment) are fixed (but the fix not yet in a released version of the compiler).

@fbonetti
Copy link
Author

@jvoigtlaender Thanks for looking into this. It does look like the same issue.

@evancz
Copy link
Member

evancz commented Jul 14, 2017

Given the code:

bug : comparable -> Bool
bug n =
    n == 1

The new compiler gives this error:

-- TYPE MISMATCH -------------------------------------------------

The right side of (==) is causing a type mismatch.

133|     n == 1
              ^
(==) is expecting the right side to be a:

    comparable

But the right side is:

    number

Hint: Your type annotation uses type variable `comparable` which means any type
of value can flow through. Your code is saying it CANNOT be anything though!
Maybe change your type annotation to be more specific? Maybe the code has a
problem? More at:
<https://github.com/elm-lang/elm-compiler/blob/0.19.0/hints/type-annotations.md>

Hint: With operators like (==) I always check the left side first. If it seems
fine, I assume it is correct and check the right side. So the problem may be in
how the left and right arguments interact.

The first hint could be improved now that it is catching this sort of thing, but it is an error now!

@evancz evancz closed this as completed Jul 14, 2017
@fbonetti
Copy link
Author

Thanks!!

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

No branches or pull requests

4 participants