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

destructors: don't produce stupid code for 'cast' #14208

Merged
merged 2 commits into from
May 4, 2020
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
4 changes: 2 additions & 2 deletions compiler/dfa.nim
Original file line number Diff line number Diff line change
Expand Up @@ -541,8 +541,8 @@ template genNoReturn(c: var Con; n: PNode) =
c.code.add Instr(n: n, kind: goto, dest: high(int) - c.code.len)

proc genRaise(c: var Con; n: PNode) =
genJoins(c, n)
gen(c, n[0])
genJoins(c, n)
if c.inTryStmt > 0:
c.tryStmtFixups.add c.gotoI(n)
else:
Expand All @@ -553,11 +553,11 @@ proc genImplicitReturn(c: var Con) =
gen(c, c.owner.ast[resultPos])

proc genReturn(c: var Con; n: PNode) =
genJoins(c, n)
if n[0].kind != nkEmpty:
gen(c, n[0])
else:
genImplicitReturn(c)
genJoins(c, n)
genNoReturn(c, n)

const
Expand Down
8 changes: 6 additions & 2 deletions compiler/injectdestructors.nim
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ proc genDiscriminantAsgn(c: var Con; n: PNode): PNode =
# discriminator is ordinal value that doesn't need sink destroy
# but fields within active case branch might need destruction

# tmp to support self assignments
# tmp to support self assignments
let tmp = getTemp(c, n[1].typ, n.info)
c.addTopVar(tmp)

Expand All @@ -329,7 +329,7 @@ proc genDiscriminantAsgn(c: var Con; n: PNode): PNode =
if hasDestructor(objType):
if objType.attachedOps[attachedDestructor] != nil and
sfOverriden in objType.attachedOps[attachedDestructor].flags:
localError(c.graph.config, n.info, errGenerated, """Assignment to discriminant for object's with user defined destructor is not supported, object must have default destructor.
localError(c.graph.config, n.info, errGenerated, """Assignment to discriminant for object's with user defined destructor is not supported, object must have default destructor.
It is best to factor out piece of object that needs custom destructor into separate object or not use discriminator assignment""")
result.add newTree(nkFastAsgn, le, tmp)
return
Expand Down Expand Up @@ -957,6 +957,10 @@ proc p(n: PNode; c: var Con; mode: ProcessMode): PNode =
for i in 0..<n.len:
result[i] = p(n[i], c, mode)
inc c.hasUnstructuredCf
of nkCast:
result = shallowCopy(n)
result[0] = n[0]
result[1] = p(n[1], c, mode)
else:
result = shallowCopy(n)
for i in 0..<n.len:
Expand Down
14 changes: 14 additions & 0 deletions tests/arc/tarcmisc.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
discard """
output: '''
123xyzabc
destroyed: false
destroyed: false
closed
Expand All @@ -8,6 +9,19 @@ destroying variable
cmd: "nim c --gc:arc $file"
"""

proc takeSink(x: sink string): bool = true

proc b(x: sink string): string =
if takeSink(x):
return x & "abc"

proc bbb(inp: string) =
let y = inp & "xyz"
echo b(y)

bbb("123")


# bug #13691
type Variable = ref object
value: int
Expand Down