assemblyscript
assemblyscript copied to clipboard
isReference(expression) returning false positive
let a = [1, 2, 3];
if (isReference(unchecked(a[0]))) ERROR("This should not be possible")
This is almost a minimal reproduction. Please feel free to ask me to help reduce it more if this is not minimal enough.
The issue is not that the branch would execute, but that it is compiled (ERROR
yields a compile time diagnostic), which is a result of static type checks with expressions instead of type arguments always preserving side effects, like in this case if the array had length 0, so a[0]
would error.
Please validate that this is true. I put the error into the playground and got the following error.
;; INFO asc module.ts --textFile module.wat --outFile module.wasm --bindings raw -O3 --runtime stub
;; ERROR AS102: User-defined: "This should not be possible"
;; :
;; 2 │ if (isReference(unchecked(a[0]))) ERROR("This should not be possible")
;; │ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;; └─ in module.ts(2,35)
;;
;; FAILURE 1 compile error(s)
(module
;; FAILURE 1 compile error(s)
)
To anyone experiencing this problem, the following workaround seems to work just fine:
let a = [1, 2, 3];
let e = unchecked(a[0]);
if (isReference(e)) ERROR("This should not be possible")
The condition is compiled as this
(block (result i32)
(drop
(call $~lib/array/Array<i32>#__uget
(call $~lib/rt/__tostack
(global.get $assembly/index/a)
)
(i32.const 0)
)
)
(i32.const 0)
)
It should be inferred but unfortunately not.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed in one week if no further activity occurs. Thank you for your contributions!