Am I missing something here, or does this expose a compiler problem?
type
W = ref object of RootObj
B = ref object of RootObj
# var newW {.threadvar.}: proc(): W {.gcsafe.} # Compiles okay with this declaration
var newW: proc(): W {.gcsafe.} # This declaration triggers a compile error
newW = proc(): W {.gcsafe.} = discard
proc xyz(b: B, handler: proc() {.gcsafe.}) = discard
proc p() =
let b = B.new()
b.xyz do() {.gcsafe.}:
let w {.used.} = newW() # Compile error occurs here: "':anonymous' is not GC-safe as it calls 'newW'"
The above example was extracted from an existing code base that I'm working on. I encountered it while rationalizing the use of threadvar versus global variables. In that case the proc variable is set at start-up, and should be global instead of threadvar.
From @ElegantBeef:
proc in type positions are default closure so it likely should be var newW: proc(): W {.gcsafe, nimcall.}
From @Araq:
The proc variable is itself a closure and contains GC'ed memory.
So that's what I missed. Thanks!