fixes #24996; Crash on marking destroy hook as .error (#25002)

fixes #24996

uses the lineinfos of `dest` is `ri` is not available (e.g. `=destroy`
doesn't have a second parameter)
This commit is contained in:
ringabout
2025-06-17 03:50:06 +08:00
committed by GitHub
parent 8e5ed5dbb7
commit c22bfe6bc0
2 changed files with 22 additions and 2 deletions

View File

@@ -198,7 +198,8 @@ proc checkForErrorPragma(c: Con; t: PType; ri: PNode; opname: string; inferredFr
if inferredFromCopy:
m.add ", which is inferred from unavailable '=copy'"
if (opname == "=" or opname == "=copy" or opname == "=dup") and ri != nil:
if (opname == "=" or opname == "=copy" or opname == "=dup") and
ri != nil:
m.add "; requires a copy because it's not the last read of '"
m.add renderTree(ri)
m.add '\''
@@ -248,7 +249,12 @@ proc genOp(c: var Con; t: PType; kind: TTypeAttachedOp; dest, ri: PNode): PNode
dbg:
if kind == attachedDestructor:
echo "destructor is ", op.id, " ", op.ast
if sfError in op.flags: checkForErrorPragma(c, t, ri, AttachedOpToStr[kind])
if sfError in op.flags:
if ri != nil:
checkForErrorPragma(c, t, ri, AttachedOpToStr[kind])
else:
# uses the lineinfos of `dest` is `ri` is not available
checkForErrorPragma(c, t, dest, AttachedOpToStr[kind])
c.genOp(op, dest)
proc genDestroy(c: var Con; dest: PNode): PNode =

14
tests/errmsgs/t24996.nim Normal file
View File

@@ -0,0 +1,14 @@
discard """
errormsg: "'=destroy' is not available for type <X>; routine: main"
joinable: false
"""
type X = object
proc `=destroy`(x: X) {.error.} =
discard
proc main() =
var x = X()
main()