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

[fix #18977] disallow change branch of an object variant in ARC again #19510

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
7 changes: 3 additions & 4 deletions compiler/ccgstmts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1559,10 +1559,9 @@ proc asgnFieldDiscriminant(p: BProc, e: PNode) =
initLocExpr(p, e[0], a)
getTemp(p, a.t, tmp)
expr(p, e[1], tmp)
if optTinyRtti notin p.config.globalOptions:
let field = dotExpr[1].sym
genDiscriminantCheck(p, a, tmp, dotExpr[0].typ, field)
message(p.config, e.info, warnCaseTransition)
let field = dotExpr[1].sym
genDiscriminantCheck(p, a, tmp, dotExpr[0].typ, field)
message(p.config, e.info, warnCaseTransition)
genAssignment(p, a, tmp, {})

proc genAsgn(p: BProc, e: PNode, fastAsgn: bool) =
Expand Down
19 changes: 10 additions & 9 deletions compiler/injectdestructors.nim
Original file line number Diff line number Diff line change
Expand Up @@ -396,15 +396,16 @@ It is best to factor out piece of object that needs custom destructor into separ
return

# generate: if le != tmp: `=destroy`(le)
let branchDestructor = produceDestructorForDiscriminator(c.graph, objType, leDotExpr[1].sym, n.info, c.idgen)
let cond = newNodeIT(nkInfix, n.info, getSysType(c.graph, unknownLineInfo, tyBool))
cond.add newSymNode(getMagicEqSymForType(c.graph, le.typ, n.info))
cond.add le
cond.add tmp
let notExpr = newNodeIT(nkPrefix, n.info, getSysType(c.graph, unknownLineInfo, tyBool))
notExpr.add newSymNode(createMagic(c.graph, c.idgen, "not", mNot))
notExpr.add cond
result.add newTree(nkIfStmt, newTree(nkElifBranch, notExpr, c.genOp(branchDestructor, le)))
when false:
let branchDestructor = produceDestructorForDiscriminator(c.graph, objType, leDotExpr[1].sym, n.info, c.idgen)
let cond = newNodeIT(nkInfix, n.info, getSysType(c.graph, unknownLineInfo, tyBool))
cond.add newSymNode(getMagicEqSymForType(c.graph, le.typ, n.info))
cond.add le
cond.add tmp
let notExpr = newNodeIT(nkPrefix, n.info, getSysType(c.graph, unknownLineInfo, tyBool))
notExpr.add newSymNode(createMagic(c.graph, c.idgen, "not", mNot))
notExpr.add cond
ringabout marked this conversation as resolved.
Show resolved Hide resolved
result.add newTree(nkIfStmt, newTree(nkElifBranch, notExpr, c.genOp(branchDestructor, le)))
result.add newTree(nkFastAsgn, le, tmp)

proc genWasMoved(c: var Con, n: PNode): PNode =
Expand Down
11 changes: 7 additions & 4 deletions tests/arc/tcaseobj.nim
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,10 @@ type
error*: string

proc init(): RocksDBResult[string] =
result.ok = true
result.value = "ok"
result = RocksDBResult[string](ok: true, value: "ok")
# fixme: use {.cast(uncheckedAssign).}
# result.ok = true
# result.value = "ok"

echo init()

Expand All @@ -221,8 +223,9 @@ type MyObj = object
of true: x1: string

var a = MyObj(kind: false, x0: 1234)
a.kind = true
doAssert(a.x1 == "")
# fixme: use {.cast(uncheckedAssign).}
# a.kind = true
# doAssert(a.x1 == "")

block:
# bug #15532
Expand Down
11 changes: 7 additions & 4 deletions tests/arc/tcaseobjcopy.nim
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,10 @@ type
error*: string

proc init(): RocksDBResult[string] =
result.ok = true
result.value = "ok"
result = RocksDBResult[string](ok: true, value: "ok")
# fixme: use {.cast(uncheckedAssign).}
# result.ok = true
# result.value = "ok"

echo init()

Expand All @@ -221,8 +223,9 @@ type MyObj = object
of true: x1: string

var a = MyObj(kind: false, x0: 1234)
a.kind = true
doAssert(a.x1 == "")
# fixme: use {.cast(uncheckedAssign).}
# a.kind = true
# doAssert(a.x1 == "")

block:
# bug #15532
Expand Down
10 changes: 7 additions & 3 deletions tests/destructor/tgotoexceptions7.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
discard """
cmd: "nim c --gc:arc --exceptions:goto --panics:off $file"
output: '''prevented!
output: '''
field error prevented
prevented!
caught
AssertionDefect
900'''
Expand All @@ -25,8 +27,10 @@ proc helper = doAssert(false)

proc main(i: int) =
var obj = Obj(kind: kindA, s: "abc")
obj.kind = kindB
obj.i = 2
try:
obj.kind = kindB
except FieldDefect:
echo "field error prevented"
try:
var objA = ObjA()
bplease(ObjB(objA))
Expand Down