Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

js: lent doesn't work for seq, ptr #16073

Closed
timotheecour opened this issue Nov 20, 2020 · 0 comments · Fixed by #19393
Closed

js: lent doesn't work for seq, ptr #16073

timotheecour opened this issue Nov 20, 2020 · 0 comments · Fixed by #19393

Comments

@timotheecour
Copy link
Member

timotheecour commented Nov 20, 2020

js: lent doesn't work for seq, ptr

Example 1

when true:
  proc byLent[T](a: T): lent T = a
  let b = @[21,23]
  doAssert byLent(b).unsafeAddr == b.unsafeAddr # BUG: fails at RT: Error: Error: unhandled exception:

  let r = new(float)
  r[] = 10.0
  doAssert byLent(r)[] == 10.0 # BUG: fails at RT: Error: Error: unhandled exception:

Current Output

assert fails

Expected Output

works, as in c backend

Example 2

more complex, shows what works, what doesn't at once thanks to check:
nim r -b:js -d:nodejs main

when true:
  import std/unittest
  template main2 =
    proc byLent[T](a: T): lent T = a
    # proc byLent[T](a: T): lent T = result=a
    let a = [11,12]
    let ss = {1, 2, 3, 5}
    doAssert byLent(a) == [11,12]
    doAssert byLent(a).unsafeAddr == a.unsafeAddr
    doAssert byLent(ss) == {1, 2, 3, 5}
    doAssert byLent(ss).unsafeAddr == ss.unsafeAddr

    let b = @[21,23]
    doAssert byLent(b) == @[21,23]
    doAssert b.unsafeAddr == b.unsafeAddr
    check byLent(b).unsafeAddr == b.unsafeAddr # BUG: fails at RT: Error: Error: unhandled exception:

    let r = new(float)
    r[] = 10.0
    check byLent(r)[] == 10.0 # BUG: fails at RT: Error: Error: unhandled exception:

    when not defined(js): # bug https://github.com/timotheecour/Nim/issues/372
      let p = create(float)
      p[] = 20.0
      doAssert byLent(p)[] == 20.0

    proc byLent2[T](a: openarray[T]): lent T = a[0]
    doAssert byLent2(a) == 11
    doAssert byLent2(a).unsafeAddr == a[0].unsafeAddr
    doAssert byLent2(b) == 21
    doAssert byLent2(b).unsafeAddr == b[0].unsafeAddr

    proc byLent3[T](a: varargs[T]): lent T = a[1]
    let
      x = 10
      y = 20
      z = 30
    doAssert byLent3(x, y, z) == 20
  main2()
/Users/timothee/git_clone/nim/timn/tests/nim/all/t11336.nim(19, 31): Check failed: byLent(b`gensym0).unsafeAddr == b`gensym0.unsafeAddr
/Users/timothee/git_clone/nim/timn/tests/nim/all/t11336.nim(23, 22): Check failed: byLent(r`gensym0)[] == 10.0
byLent(r`gensym0)[] was undefined

Additional Information

metagn added a commit to metagn/Nim that referenced this issue Jan 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant