Skip to content

Commit c22bfe6

Browse files
authored
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)
1 parent 8e5ed5d commit c22bfe6

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

compiler/injectdestructors.nim

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,8 @@ proc checkForErrorPragma(c: Con; t: PType; ri: PNode; opname: string; inferredFr
198198
if inferredFromCopy:
199199
m.add ", which is inferred from unavailable '=copy'"
200200

201-
if (opname == "=" or opname == "=copy" or opname == "=dup") and ri != nil:
201+
if (opname == "=" or opname == "=copy" or opname == "=dup") and
202+
ri != nil:
202203
m.add "; requires a copy because it's not the last read of '"
203204
m.add renderTree(ri)
204205
m.add '\''
@@ -248,7 +249,12 @@ proc genOp(c: var Con; t: PType; kind: TTypeAttachedOp; dest, ri: PNode): PNode
248249
dbg:
249250
if kind == attachedDestructor:
250251
echo "destructor is ", op.id, " ", op.ast
251-
if sfError in op.flags: checkForErrorPragma(c, t, ri, AttachedOpToStr[kind])
252+
if sfError in op.flags:
253+
if ri != nil:
254+
checkForErrorPragma(c, t, ri, AttachedOpToStr[kind])
255+
else:
256+
# uses the lineinfos of `dest` is `ri` is not available
257+
checkForErrorPragma(c, t, dest, AttachedOpToStr[kind])
252258
c.genOp(op, dest)
253259

254260
proc genDestroy(c: var Con; dest: PNode): PNode =

tests/errmsgs/t24996.nim

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
discard """
2+
errormsg: "'=destroy' is not available for type <X>; routine: main"
3+
joinable: false
4+
"""
5+
6+
type X = object
7+
8+
proc `=destroy`(x: X) {.error.} =
9+
discard
10+
11+
proc main() =
12+
var x = X()
13+
14+
main()

0 commit comments

Comments
 (0)