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

Roc does not detect type mismatch error when performing math on values defined in separate file #6682

Open
imclerran opened this issue Apr 26, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@imclerran
Copy link

imclerran commented Apr 26, 2024

Crash message:

This expectation crashed while running:

10│  expect foo 1u8 == 1 + externalDerivedValue
     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

The crash reported this message:

Hit an erroneous type when creating a layout for `4.IdentId(21)`
1 failed and 0 passed in 333 ms.

OR:

thread 'main' panicked at crates/compiler/alias_analysis/src/lib.rs:612:38:
no entry found for key

(Min repro for both below)

Expected behavior

Preferred behavior:

  • Fail to compile with message about type mismatch.

Alternative acceptable behavior:

  • Crash with message about Integer overflow

Description of error case

To reproduce, the following criteria must be met:

  1. The constant must be defined in another file
  2. The constant must be derived from another constant The failure mode will vary depending on whether the constant is defined by a literal, or based on another another constant.
  3. The constant must be operated on with another value typed as Int *
  4. A value of set type must be passed into the Int *
  5. The constant must exceed on its own, the maximum value of the type held by the Int *

Note that point 5 means that if you do instStar + constant, where intStar holds 1u8, and constant == 255, Roc will crash with an integer overflow error. However, if constant == 256, it will trigger the Hit an erroneous type when creating a layout for '4.IdentId(21)' error described here.

Minimum reproduction

# MinRepro.roc
interface MinRepro
    exposes []
    imports [
        External.{ 
            # externalBaseValue,
            externalDerivedValue,
        }
    ]
    
# internalBaseValue = 256 # use in foo to to compile but produce mathematically incorrect result (expect fails)
# internalDerivedValue = 1 * internalBaseConst # use in foo to get *correct/expected* type mismatch error 

foo : Int * -> U16
foo = \n -> Num.toU16 (n + externalDerivedValue) # replacing with externalBaseValue will produce rust panic

expect 
    res = foo 1u8 # if using internalBaseValue, res == 1  (should == 257)
    res == 1 + externalDerivedValue
# External.roc
interface External
    exposes [
        externalBaseValue,
        externalDerivedValue,
    ]
    imports []

externalBaseValue = 256
externalDerivedValue = 1 * externalBaseValue
@imclerran
Copy link
Author

imclerran commented Apr 26, 2024

4 ways to define a value == 3 failure modes + 1 success

Note that behavior varies wildly depending on how and where the value is defined. I have identified 4 different distinct failure modes:

  1. Failure mode 1 ❌
    • Value : is defined in the same file, and is not derived from any other value.
    • Result: compiles, treats the constant like a zero, operation is mathematically incorrect.
  2. Success ✅
    • Value: is defined in the same file, and is derived from another value.
    • Result: Type mismatch error.
  3. Failure mode 2 ❌
    • Value: is defined in separate file, and is not derived from any other value.
    • Result: Rust panics and crashes with "no entry found for key"
  4. Failure mode 3 ❌
    • Value: is defined in separate file, and is derived from another value.
    • Result: Roc crashes with "Hit an erroneous type when creating a layout for 4.IdentId(21)"

@imclerran imclerran changed the title Roc does not correctly detect integer overflow error when performing math on constants defined in separate file Roc does not detect integer overflow error when performing math on constants defined in separate file Apr 26, 2024
@lukewilliamboswell lukewilliamboswell added the bug Something isn't working label Apr 29, 2024
@imclerran imclerran changed the title Roc does not detect integer overflow error when performing math on constants defined in separate file Roc does not detect type mismatch error when performing math on constants defined in separate file Apr 29, 2024
@imclerran imclerran changed the title Roc does not detect type mismatch error when performing math on constants defined in separate file Roc does not detect type mismatch error when performing math on values defined in separate file Apr 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants