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

Destructor lifting regression #11149

Closed
cooldome opened this issue Apr 30, 2019 · 1 comment

Comments

Projects
None yet
1 participant
@cooldome
Copy link
Member

commented Apr 30, 2019

Here is a test case, this issue caused me so much pain. It took 2 weeks to make a reproducible test case.

type
  opt*[T] = object
    case exists: bool
      of true: val: T
      of false: discard

proc some*[T](val: sink T): opt[T] {.inline.} =
  ## Returns an ``opt`` that has the value.
  ## nil is considered as none for reference types
  result.exists = true
  result.val = val

proc none*(T: typedesc): opt[T] {.inline.} =
  ## Returns an ``opt`` for this type that has no value.
  # the default is the none type
  discard

proc none*[T]: opt[T] {.inline.} =
  ## Alias for ``none(T)``.
  none(T)

proc unsafeGet*[T](self: opt[T]): lent T {.inline.} =
  ## Returns the value of a ``some``. Behavior is undefined for ``none``.
  self.val

###########################

type
  VSeq*[T] = object
    len: int 
    data: ptr UncheckedArray[T]

proc `=destroy`*[T](m: var VSeq[T]) {.inline.} =
  if m.data != nil:
    echo "vec destroy"
    dealloc(m.data)
    m.data = nil

proc `=`*[T](m: var VSeq[T], m2: VSeq[T]) {.error.}

proc `=sink`*[T](m: var VSeq[T], m2: VSeq[T]) {.inline.} =
  if m.data != m2.data:
    `=destroy`(m)
  m.len = m2.len
  m.data = m2.data

proc newVSeq*[T](len: int): VSeq[T] =
  result.len = len
  if len > 0:
    result.data = cast[ptr UncheckedArray[T]](alloc(sizeof(T) * len))

let x = some newVSeq[float](5)
echo x.unsafeGet.len
let y = none(VSeq[float])

Compilation fails with:
Error: internal error: '=destroy' operator not found for type opt

The most confusing part that the error happens on SECOND assignment. On the first assignment destructors are present and if you remove last line let y = none(VSeq[float]) in example above everything compiles with destructors correctly generated and injected.

I have spent a lot of time on this one and I am clueless.

@cooldome

This comment has been minimized.

Copy link
Member Author

commented May 1, 2019

I think I have a fix now. PR is expected shortly

cooldome added a commit to cooldome/Nim that referenced this issue May 2, 2019

@Araq Araq closed this in d3db189 May 4, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.