nim-works / nimskull

An in development statically typed systems programming language; with sustainability at its core. We, the community of users, maintain it.
https://nim-works.github.io/nimskull/index.html
Other
279 stars 39 forks source link

double destroy with `sink` and generic tuple type #1415

Closed zerbina closed 3 months ago

zerbina commented 3 months ago

Assigning to a sink parameter that uses an instantiated generic tuple type creates an unsafe shallow copy, which can lead to double frees, depending on the type.

Example

type
  Object = object
    val: int
  Tuple[T] = (T,)

proc `=copy`(x: var Object, y: Object) =
  echo "copied"

proc `=destroy`(x: var Object) =
  if x.val != 0:
    echo "destroyed: ", x.val

proc test(x: sink Tuple[Object]) =
  var y: Tuple[Object]
  y = x # last use of `x`, so this should move, or at least create a proper copy

test((Object(val: 1),))

Actual Output

destroyed: 1
destroyed: 1

Expected Output

destroyed: 1

Additional Information