Skip to content

Commit

Permalink
fixes refc with non-var destructor; cancel warnings (nim-lang#23156)
Browse files Browse the repository at this point in the history
  • Loading branch information
ringabout authored Feb 13, 2024
1 parent 1e9a3c4 commit 35ec9c3
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
16 changes: 13 additions & 3 deletions compiler/semstmts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1966,6 +1966,11 @@ proc bindTypeHook(c: PContext; s: PSym; n: PNode; op: TTypeAttachedOp; suppressV
t.len == 2 and t.returnType == nil and t.firstParamType.kind == tyVar
of attachedTrace:
t.len == 3 and t.returnType == nil and t.firstParamType.kind == tyVar and t[2].kind == tyPointer
of attachedDestructor:
if c.config.selectedGC in {gcArc, gcAtomicArc, gcOrc}:
t.len == 2 and t.returnType == nil
else:
t.len == 2 and t.returnType == nil and t.firstParamType.kind == tyVar
else:
t.len >= 2 and t.returnType == nil

Expand All @@ -1977,7 +1982,8 @@ proc bindTypeHook(c: PContext; s: PSym; n: PNode; op: TTypeAttachedOp; suppressV
elif obj.kind == tyGenericInvocation: obj = obj.genericHead
else: break
if obj.kind in {tyObject, tyDistinct, tySequence, tyString}:
if (not suppressVarDestructorWarning) and op == attachedDestructor and t.firstParamType.kind == tyVar:
if (not suppressVarDestructorWarning) and op == attachedDestructor and t.firstParamType.kind == tyVar and
c.config.selectedGC in {gcArc, gcAtomicArc, gcOrc}:
message(c.config, n.info, warnDeprecated, "A custom '=destroy' hook which takes a 'var T' parameter is deprecated; it should take a 'T' parameter")
obj = canonType(c, obj)
let ao = getAttachedOp(c.graph, obj, op)
Expand All @@ -1997,8 +2003,12 @@ proc bindTypeHook(c: PContext; s: PSym; n: PNode; op: TTypeAttachedOp; suppressV
localError(c.config, n.info, errGenerated,
"signature for '=trace' must be proc[T: object](x: var T; env: pointer)")
of attachedDestructor:
localError(c.config, n.info, errGenerated,
"signature for '=destroy' must be proc[T: object](x: var T) or proc[T: object](x: T)")
if c.config.selectedGC in {gcArc, gcAtomicArc, gcOrc}:
localError(c.config, n.info, errGenerated,
"signature for '=destroy' must be proc[T: object](x: var T) or proc[T: object](x: T)")
else:
localError(c.config, n.info, errGenerated,
"signature for '=destroy' must be proc[T: object](x: var T)")
else:
localError(c.config, n.info, errGenerated,
"signature for '" & s.name.s & "' must be proc[T: object](x: var T)")
Expand Down
3 changes: 2 additions & 1 deletion lib/std/tasks.nim
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ type

proc `=copy`*(x: var Task, y: Task) {.error.}

when defined(nimAllowNonVarDestructor):
const arcLike = defined(gcArc) or defined(gcAtomicArc) or defined(gcOrc)
when defined(nimAllowNonVarDestructor) and arcLike:
proc `=destroy`*(t: Task) {.inline, gcsafe.} =
## Frees the resources allocated for a `Task`.
if t.args != nil:
Expand Down
3 changes: 2 additions & 1 deletion lib/std/widestrs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ when not (defined(cpu16) or defined(cpu8)):
bytes: int
data: WideCString

when defined(nimAllowNonVarDestructor):
const arcLike = defined(gcArc) or defined(gcAtomicArc) or defined(gcOrc)
when defined(nimAllowNonVarDestructor) and arcLike:
proc `=destroy`(a: WideCStringObj) =
if a.data != nil:
when compileOption("threads"):
Expand Down
8 changes: 6 additions & 2 deletions tests/iter/t22619.nim
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,12 @@ block:

var numDestroy = 0

proc `=destroy`(x: Value) =
inc numDestroy
when defined(gcRefc):
proc `=destroy`(x: var Value) =
inc numDestroy
else:
proc `=destroy`(x: Value) =
inc numDestroy

iterator iter(s: seq[Value]): int {.closure.} =
# because it is used across yields, `s2` is lifted into the iterator's
Expand Down

0 comments on commit 35ec9c3

Please sign in to comment.