assemblyscript icon indicating copy to clipboard operation
assemblyscript copied to clipboard

isReference(expression) returning false positive

Open jtenner opened this issue 2 years ago • 4 comments

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.

jtenner avatar Sep 02 '22 19:09 jtenner

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.

dcodeIO avatar Sep 02 '22 20:09 dcodeIO

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)
)

jtenner avatar Sep 02 '22 20:09 jtenner

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")

jtenner avatar Sep 02 '22 20:09 jtenner

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.

HerrCai0907 avatar Sep 03 '22 11:09 HerrCai0907

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!

github-actions[bot] avatar Oct 03 '22 23:10 github-actions[bot]