Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes #23627; Simple destructor code gives invalid C #23631

Merged
merged 2 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions compiler/ccgexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1484,9 +1484,13 @@ proc rawConstExpr(p: BProc, n: PNode; d: var TLoc) =
if id == p.module.labels:
# expression not found in the cache:
inc(p.module.labels)
p.module.s[cfsData].addf("static NIM_CONST $1 $2 = ", [getTypeDesc(p.module, t), d.r])
genBracedInit(p, n, isConst = true, t, p.module.s[cfsData])
p.module.s[cfsData].addf(";$n", [])
var data = "static NIM_CONST $1 $2 = " % [getTypeDesc(p.module, t), d.r]
# bug #23627; when generating const object fields, it's likely that
# we need to generate type infos for the object, which may be an object with
# custom hooks. We need to generate potential consts in the hooks first.
genBracedInit(p, n, isConst = true, t, data)
data.addf(";$n", [])
p.module.s[cfsData].add data

proc handleConstExpr(p: BProc, n: PNode, d: var TLoc): bool =
if d.k == locNone and n.len > ord(n.kind == nkObjConstr) and n.isDeepConstExpr:
Expand Down
34 changes: 29 additions & 5 deletions tests/arc/tarcmisc.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
discard """
output: '''
Destructor for TestTestObj
=destroy called
123xyzabc
destroyed: false
Expand Down Expand Up @@ -36,23 +37,46 @@ destroying variable: 20
destroying variable: 10
closed
'''
cmd: "nim c --gc:arc --deepcopy:on -d:nimAllocPagesViaMalloc $file"
cmd: "nim c --mm:arc --deepcopy:on -d:nimAllocPagesViaMalloc $file"
"""

block: # bug #23627
type
TestObj = object of RootObj

Test2 = object of RootObj
foo: TestObj

TestTestObj = object of RootObj
shit: TestObj

proc `=destroy`(x: TestTestObj) =
echo "Destructor for TestTestObj"
let test = Test2(foo: TestObj())

proc testCaseT() =
let tt1 {.used.} = TestTestObj(shit: TestObj())


proc main() =
testCaseT()

main()


# bug #9401

type
MyObj = object
len: int
data: ptr UncheckedArray[float]

proc `=destroy`*(m: var MyObj) =
proc `=destroy`*(m: MyObj) =

echo "=destroy called"

if m.data != nil:
deallocShared(m.data)
m.data = nil

type
MyObjDistinct = distinct MyObj
Expand Down Expand Up @@ -104,7 +128,7 @@ bbb("123")
type Variable = ref object
value: int

proc `=destroy`(self: var typeof(Variable()[])) =
proc `=destroy`(self: typeof(Variable()[])) =
echo "destroying variable: ",self.value

proc newVariable(value: int): Variable =
Expand Down Expand Up @@ -158,7 +182,7 @@ type
B = ref object of A
x: int

proc `=destroy`(x: var AObj) =
proc `=destroy`(x: AObj) =
close(x.io)
echo "closed"

Expand Down
Loading