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

[duplicate] Generic casting doesn't work in VM #9423

Closed
PMunch opened this issue Oct 18, 2018 · 1 comment · Fixed by #21816
Closed

[duplicate] Generic casting doesn't work in VM #9423

PMunch opened this issue Oct 18, 2018 · 1 comment · Fixed by #21816
Labels
Distinct Types VM see also `const` label

Comments

@PMunch
Copy link
Contributor

PMunch commented Oct 18, 2018

Following the deficiencies I found with the deques module I tried to implement my own stack data structure (which is what I was using a deque for). It however also ran into trouble when run under a VM. It appears like the conversion back from a Stack[T] type to a seq[T] makes the VM copy the object while compiled Nim allows this like you would expect. I tried the same with a distinct string instead of a sequence but that didn't seem to trigger the issue so I'm guessing it has to do with the generics.

type Stack*[T] = distinct seq[T]

proc newStack*[T](): Stack[T] =
  Stack[T](newSeq[T]())

proc push*[T](stack: var Stack[T], elem: T) =
  seq[T](stack).add(elem)

proc pop*[T](stack: var Stack[T]): T =
  result = seq[T](stack)[seq[T](stack).high]
  seq[T](stack).setLen(seq[T](stack).high)

proc len*[T](stack: Stack[T]): int =
  seq[T](stack).len

when isMainModule:
  var stack = newStack[int]()

  stack.push(5)
  echo stack.len # Echos 1
  echo stack.pop() # Echos 5

  import macros

  macro something(): untyped =
    result = newStmtList()
    var stack = newStack[int]()

    stack.push(5)
    echo stack.len # Echos 0
    echo stack.pop() # Error: No elements

  something()
@andreaferretti andreaferretti added the VM see also `const` label label Oct 18, 2018
@timotheecour
Copy link
Member

when defined case3a:
  type Foo[T] = object
    x: int
  type Foo1 = Foo[int]
  type Foo2 = distinct Foo1
  template fn() =
    var a = Foo2(Foo1(x: 1))
    a.Foo1.x = 2
    echo a.Foo1
  fn()
  static: fn()
when defined case3:
  type Foo = seq[int]
  type Foo2 = distinct Foo
  template fn() =
    var a = Foo2(@[1])
    a.Foo.add 2
    echo a.Foo
  fn()
  static: fn()

case3a works
case3 bugs in VM, showing:

@[1]
@[1, 2]

=> it fails with seq and string and works with object including generic object
=> closing as duplicate of #12282

@timotheecour timotheecour changed the title Generic casting doesn't work in VM [duplicate] Generic casting doesn't work in VM Dec 8, 2020
timotheecour added a commit to timotheecour/Nim that referenced this issue Mar 31, 2021
timotheecour added a commit to timotheecour/Nim that referenced this issue Mar 31, 2021
Araq pushed a commit that referenced this issue May 10, 2023
* fix #9423 distinct generics now work in vm

* fixes cpp tests

---------

Co-authored-by: Timothee Cour <[email protected]>
capocasa pushed a commit to capocasa/Nim that referenced this issue May 15, 2023
… in VM (nim-lang#21816)

* fix nim-lang#9423 distinct generics now work in vm

* fixes cpp tests

---------

Co-authored-by: Timothee Cour <[email protected]>
capocasa pushed a commit to capocasa/Nim that referenced this issue May 16, 2023
… in VM (nim-lang#21816)

* fix nim-lang#9423 distinct generics now work in vm

* fixes cpp tests

---------

Co-authored-by: Timothee Cour <[email protected]>
narimiran pushed a commit that referenced this issue Jun 19, 2023
* fix #9423 distinct generics now work in vm

* fixes cpp tests

---------

Co-authored-by: Timothee Cour <[email protected]>
(cherry picked from commit deaf684)
bung87 pushed a commit to bung87/Nim that referenced this issue Jul 29, 2023
… in VM (nim-lang#21816)

* fix nim-lang#9423 distinct generics now work in vm

* fixes cpp tests

---------

Co-authored-by: Timothee Cour <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Distinct Types VM see also `const` label
Projects
None yet
3 participants