From 57296a51397d65ce4f342d6cbc92a9de2629875c Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Mon, 3 Jul 2023 03:04:48 +0800 Subject: fixes #22197; Distinct ref objects + destructor cause C++ codegen error (#22207) --- compiler/semmagic.nim | 8 ++++++++ tests/destructor/tnonvardestructor.nim | 27 ++++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/compiler/semmagic.nim b/compiler/semmagic.nim index 22c2fb57e..e7057053b 100644 --- a/compiler/semmagic.nim +++ b/compiler/semmagic.nim @@ -609,6 +609,14 @@ proc magicsAfterOverloadResolution(c: PContext, n: PNode, let op = getAttachedOp(c.graph, t, attachedDestructor) if op != nil: result[0] = newSymNode(op) + + if op.typ != nil and op.typ.len == 2 and op.typ[1].kind != tyVar and + skipAddr(n[1]).typ.kind == tyDistinct: + if n[1].kind == nkSym and n[1].sym.kind == skParam and + n[1].typ.kind == tyVar: + result[1] = genDeref(n[1]) + else: + result[1] = skipAddr(n[1]) of mTrace: result = n let t = n[1].typ.skipTypes(abstractVar) diff --git a/tests/destructor/tnonvardestructor.nim b/tests/destructor/tnonvardestructor.nim index e482ba137..1f59a3978 100644 --- a/tests/destructor/tnonvardestructor.nim +++ b/tests/destructor/tnonvardestructor.nim @@ -192,4 +192,29 @@ block: doAssert $(x[]) == "()" # bug #11705 - foo() \ No newline at end of file + foo() + +block: # bug #22197 + type + H5IdObj = object + H5Id = ref H5IdObj + + FileID = distinct H5Id + + H5GroupObj = object + file_id: FileID + H5Group = ref H5GroupObj + + ## This would make it work! + #proc `=destroy`*(x: FileID) = `=destroy`(cast[H5Id](x)) + ## If this does not exist, it also works! + proc newFileID(): FileID = FileID(H5Id()) + + proc `=destroy`(grp: var H5GroupObj) = + ## Closes the group and resets all references to nil. + if cast[pointer](grp.fileId) != nil: + `=destroy`(grp.file_id) + + var grp = H5Group() + reset(grp.file_id) + reset(grp) -- cgit 1.4.1-2-gfad0