grain icon indicating copy to clipboard operation
grain copied to clipboard

DecRefError related to closures for non heap based mutable values.

Open spotandjake opened this issue 1 year ago • 1 comments

I ran into a wierd decRef error with the below code:

let test = () => {
  let mut dependencyExists = false
  (() => {
    print(dependencyExists)
  })()
  (() => {
    print(dependencyExists)
  })()
  return dependencyExists
}
print(test())

This also happens if dependencyExists is a char or really any stack type. However if it is a Number, or heap type this error does not occur. Some cases that do work are below:

let test = () => {
  let dependencyExists = box(void)
  (() => {
    print(dependencyExists)
  })()
  (() => {
    print(dependencyExists)
  })()
  return dependencyExists
}
print("Test1")
print(test())

let test = () => {
  let mut dependencyExists = 1
  (() => {
    print(dependencyExists)
  })()
  (() => {
    print(dependencyExists)
  })()
  return dependencyExists
}
print("Test2")
print(test())

spotandjake avatar Nov 17 '24 03:11 spotandjake

I stumbled upon some more cases where this is occurring:

module Main
from "set" include Set
use Set.{ module Immutable as Set }
from "array" include Array
Array.reduce((argResult, arg) => {
  Set.empty
}, Set.empty, [> 1, 1, 1, 1])
print("me")

Which also fails when refactored as below as if the reduce was inlined (to verify the behaviour):

module Main

from "set" include Set
use Set.{ module Immutable as Set }
from "array" include Array
let test = () => {
  let mut acc = Set.empty
  Array.forEach(el => acc = ((_, _) => {
    Set.empty
  })(acc, el), [>1, 1, 1, 1])
}
test()

spotandjake avatar Nov 17 '24 20:11 spotandjake