sharkdp / numbat

A statically typed programming language for scientific computations with first class support for physical dimensions and units
https://numbat.dev
Apache License 2.0
1.26k stars 53 forks source link

Error on unit definition using square roots #561

Closed ngollon closed 1 month ago

ngollon commented 2 months ago

Using the unit definition unit test = sqrt(kg)

the web interface breaks and always shows the error

[USER]: unreachable
RuntimeError: unreachable
    at https://numbat.dev/pkg/numbat_wasm_bg.wasm:wasm-function[655]:0xccfad
    at https://numbat.dev/pkg/numbat_wasm_bg.wasm:wasm-function[1471]:0x10f350
    at https://numbat.dev/pkg/numbat_wasm_bg.wasm:wasm-function[191]:0x84d30
    at https://numbat.dev/pkg/numbat_wasm_bg.wasm:wasm-function[108]:0x654cd
    at https://numbat.dev/pkg/numbat_wasm_bg.wasm:wasm-function[66]:0x4bc26
    at https://numbat.dev/pkg/numbat_wasm_bg.wasm:wasm-function[54]:0x3f8fc
    at https://numbat.dev/pkg/numbat_wasm_bg.wasm:wasm-function[843]:0xdd040
    at Numbat.interpret (https://numbat.dev/pkg/numbat_wasm.js:257:26)
    at ce.fn.init.interpret (https://numbat.dev/index.js:68:29)
    at s (https://numbat.dev/jquery.terminal.min.js:46:123939)

Link: https://numbat.dev/?q=unit+test+%3D+sqrt%28kg%29⏎test

Goju-Ryu commented 2 months ago

I just tried the same thing with unit example = sqrt(m) and got an error about recursion in rust. See example here

To make it happen you have to write to lines yourself, the first just being one of the units and the other on the next. The first line gives the reported error and the second gives the recursion error.

[USER]: recursive use of an object detected which would
lead to unsafe aliasing in rust

@https://numbat.dev/pkg/numbat_wasm.js:535:24
<?>.wasm-function[1715]@[wasm code]
<?>.wasm-function[1714]@[wasm code]
<?>.wasm-function[843]@[wasm code]
interpret@https://numbat.dev/pkg/numbat_wasm.js:257:42
interpret@https://numbat.dev/index.js:68:38
s@https://numbat.dev/jquery.terminal.min.js:46:123943
me@https://numbat.dev/jquery.terminal.min.js:46:124921
ENTER@https://numbat.dev/jquery.terminal.min.js:46:45312
it@https://numbat.dev/jquery.terminal.min.js:46:64967
dispatch@https://numbat.dev/jquery.min.js:2:40002

Update: same error happens if you use just the example definition and try to print it twice the same way as in the original example.

sharkdp commented 2 months ago

Thank you very much for reporting this. Running this in debug mode on the command line shows a failing debug-assertion:

>>> unit test = sqrt(kg)

  unit test: Mass^(1/2) = sqrt(kilogram)

>>> test
thread 'main' panicked at numbat/src/unit.rs:140:21:
assertion failed: key[0].1.is_integer()
sharkdp commented 1 month ago

This is now fixed.