fixes refc with non-var destructor; cancel warnings (#23156)

fixes https://forum.nim-lang.org/t/10807
This commit is contained in:
ringabout
2024-02-13 15:11:49 +08:00
committed by GitHub
parent 1e9a3c438b
commit 35ec9c31bd
4 changed files with 23 additions and 7 deletions

View File

@@ -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
@@ -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)
@@ -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)")

View File

@@ -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:

View File

@@ -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"):

View File

@@ -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