roc-lang / roc

A fast, friendly, functional language.
https://roc-lang.org
Universal Permissive License v1.0
4.46k stars 313 forks source link

`Num.neg` not supported for `Dec`, or for any `Num` except I64/U64 when in repl #6959

Open Lubsch opened 3 months ago

Lubsch commented 3 months ago

When trying to build the following test program:

app [main] {
    cli: platform "https://github.com/roc-lang/basic-cli/releases/download/0.12.0/Lb8EgiejTUzbggO2HVVuPJFkwvvsfW6LojkLR20kTVE.tar.br",
}
import cli.Stdout

a : Dec
a = 10

main =
    Num.neg a
    |> Num.toStr
    |> Stdout.line

I get the output:

thread 'main' panicked at crates/compiler/gen_llvm/src/llvm/lowlevel.rs:2230:13:
internal error: entered unreachable code: Unrecognized dec unary operation: NumNeg
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

When investigating the source, it's clear that Num.neg is simply not implemented for Dec. This isn't mentionend in the documentation or anywhere else I've looked.

Additionally, using Num.neg on any Num that's not I64 or U64 is not supported from the repl. For example:

» a : I32
… a = 10

10 : I32
» -a
thread 'main' panicked at crates/compiler/gen_dev/src/generic64/mod.rs:1794:18:
not yet implemented: NumNeg: layout, Builtin(Int(I32))
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

You can of course work around this by subtracting from 0, but I think this should at least be documented.

Anton-4 commented 3 months ago

Thanks for reporting this @Lubsch. I've created a PR with improved documentation. For anyone interested in implementing this, I suspect the claude 3.5 sonnet API can handle the implementation if you provide it with the rest of the file (crates/compiler/gen_dev/src/generic64/mod.rs), if you get something working, bhansconnect can review it for correctness.

Lubsch commented 3 months ago

See also https://github.com/roc-lang/roc/issues/3513