roc icon indicating copy to clipboard operation
roc copied to clipboard

Compiler crashes when type annotation is not provided

Open ageron opened this issue 1 year ago • 5 comments

The following code crashes the compiler:

elemsApproxEq = \values, index1, index2 ->
    val1 = List.get? values index1
    val2 = List.get? values index2
    Ok (Num.isApproxEq val1 val2 {})

The output is:

thread 'main' panicked at crates/compiler/gen_dev/src/generic64/mod.rs:1333:18:
not yet implemented: NumAbs: layout, Builtin(Decimal)
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

It looks like it's because Num.isApproxEq is not implemented for the Decimal type. Shouldn't it crash only if the function is called with a list of decimals?

In any case, adding the following type definition before the function definition works around the issue:

elemsApproxEq : List F64, U64, U64 -> Result Bool 

ageron avatar Aug 26 '24 06:08 ageron

Shouldn't it crash only if the function is called with a list of decimals?

We default to Dec if Dec, F64, ... are possible to help users avoid typical floating point footguns.

Anton-4 avatar Aug 26 '24 14:08 Anton-4

Yes, that makes sense. However, my code only calls this function with lists of F64s, so I feel like it's still a bug, wouldn't you agree?

ageron avatar Aug 26 '24 23:08 ageron

my code only calls this function with lists of F64s

Can you provide a repro? the error message indicates this is using a Dec.

lukewilliamboswell avatar Aug 26 '24 23:08 lukewilliamboswell

Ah, actually I just realised that some test cases included floats without the f64 qualifier, so that's the problem, sorry for the noise. However, I do get the issue in the REPL, if I just copy/paste the function definition above, before even calling it. I'm not sure whether that's expected?

ageron avatar Aug 26 '24 23:08 ageron

$ roc repl

  The rockin' roc repl
────────────────────────

Enter an expression, or :help, or :q to quit.

» elemsApproxEq = \values, index1, index2 ->
…     val1 = List.get? values index1
…     val2 = List.get? values index2
…     Ok (Num.isApproxEq val1 val2 {})
thread 'main' panicked at crates/compiler/gen_dev/src/generic64/mod.rs:1333:18:
not yet implemented: NumAbs: layout, Builtin(Decimal)
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

I assume this is because the Dec is the default number type. I'm not sure though.

Ideally we would finish implementing all these missing Num functions in the dev backends.

lukewilliamboswell avatar Aug 27 '24 02:08 lukewilliamboswell