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

Destructors lifting doesn't work with inheritance #11517

Closed
cooldome opened this issue Jun 17, 2019 · 0 comments

Comments

@cooldome
Copy link
Member

commented Jun 17, 2019

Test case

type
  UniquePtr*[T] = object
    val: ptr T

proc `=destroy`*[T](p: var UniquePtr[T]) =
  mixin `=destroy`
  echo "=destroy"
  if p.val != nil:
    `=destroy`(p.val[])
    dealloc(p.val)
    p.val = nil

proc `=`*[T](dest: var UniquePtr[T], src: UniquePtr[T]) {.error.}

proc `=sink`*[T](dest: var UniquePtr[T], src: UniquePtr[T]) {.inline.} =
  echo "=sink"
  if dest.val != src.val:
    if dest.val != nil:
      `=destroy`(dest)
    dest.val = src.val

proc newUniquePtr*[T](val: sink T): UniquePtr[T] =
  result.val = create(T)
  result.val[] = val

#-------------------------------------------------------------

type
  MyObject = object of RootObj
    x1: UniquePtr[int]

  MyObject2 = object of MyObject

proc newObj(x:int, y: float): MyObject =
  MyObject(x1: newUniquePtr(x))

proc newObj2(x:int, y: float): MyObject2 =
  MyObject2(x1: newUniquePtr(x))

proc test =
  let obj2 = newObj2(1, 1.0)
  echo obj2

test()

prints: (x1: nil), notice no destructor or sink calls at all. Changing one line in the example above: let obj2 = newObj2(1, 1.0) to let obj2 = newObj(1, 1.0) makes output as expected:

=sink
=sink
(x1: (val: ...))
=destroy

I had look in more details. Generated by compiler =sink and =destroy for MyObject2 are completely blank, they do exactly nothing.

@Araq Araq closed this in ab36ffb Jun 19, 2019

narimiran added a commit that referenced this issue Jun 20, 2019

[bugfix] fixes #11517
(cherry picked from commit ab36ffb)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
1 participant
You can’t perform that action at this time.