From c40a1bb1206f8aa6a95188e8ce4cdfd7a1b3f844 Mon Sep 17 00:00:00 2001 From: flywind Date: Thu, 10 Feb 2022 11:42:28 +0800 Subject: [PATCH 1/6] [fix #18977] disallow change branch of an object variant in ARC again --- compiler/ccgstmts.nim | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/compiler/ccgstmts.nim b/compiler/ccgstmts.nim index 7fecc1475291..b07814abeb06 100644 --- a/compiler/ccgstmts.nim +++ b/compiler/ccgstmts.nim @@ -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) = From e8918d4f9757ca81806da0ebdbf0f670ad908323 Mon Sep 17 00:00:00 2001 From: flywind Date: Thu, 10 Feb 2022 11:53:50 +0800 Subject: [PATCH 2/6] no need to destroy selector type --- compiler/injectdestructors.nim | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/compiler/injectdestructors.nim b/compiler/injectdestructors.nim index 985f278784b6..00f098631547 100644 --- a/compiler/injectdestructors.nim +++ b/compiler/injectdestructors.nim @@ -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 + 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 = From b964010afb7ff3449da61581813f4dd0ad91a314 Mon Sep 17 00:00:00 2001 From: flywind Date: Thu, 10 Feb 2022 12:04:21 +0800 Subject: [PATCH 3/6] fix tests --- tests/arc/tcaseobj.nim | 11 +++++++---- tests/arc/tcaseobjcopy.nim | 11 +++++++---- tests/destructor/tgotoexceptions7.nim | 10 +++++++--- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/tests/arc/tcaseobj.nim b/tests/arc/tcaseobj.nim index a967a509bc9b..ab34f9e78259 100644 --- a/tests/arc/tcaseobj.nim +++ b/tests/arc/tcaseobj.nim @@ -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() @@ -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 diff --git a/tests/arc/tcaseobjcopy.nim b/tests/arc/tcaseobjcopy.nim index ed07b404e9f3..77e4365e5aba 100644 --- a/tests/arc/tcaseobjcopy.nim +++ b/tests/arc/tcaseobjcopy.nim @@ -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() @@ -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 diff --git a/tests/destructor/tgotoexceptions7.nim b/tests/destructor/tgotoexceptions7.nim index 6e564a04434d..fdc59005dbca 100644 --- a/tests/destructor/tgotoexceptions7.nim +++ b/tests/destructor/tgotoexceptions7.nim @@ -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''' @@ -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)) From 0f5033ce75bc7b0a41f7c63124fd9f7cbcd62c84 Mon Sep 17 00:00:00 2001 From: xflywind Date: Mon, 28 Feb 2022 12:12:05 +0800 Subject: [PATCH 4/6] part --- compiler/injectdestructors.nim | 19 +++++++++---------- tests/arc/tcaseobj.nim | 12 ++++++------ tests/arc/tcaseobjcopy.nim | 11 +++++------ tests/destructor/tgotoexceptions7.nim | 7 +++---- 4 files changed, 23 insertions(+), 26 deletions(-) diff --git a/compiler/injectdestructors.nim b/compiler/injectdestructors.nim index 00f098631547..985f278784b6 100644 --- a/compiler/injectdestructors.nim +++ b/compiler/injectdestructors.nim @@ -396,16 +396,15 @@ It is best to factor out piece of object that needs custom destructor into separ return # generate: if le != tmp: `=destroy`(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 - result.add newTree(nkIfStmt, newTree(nkElifBranch, notExpr, c.genOp(branchDestructor, 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))) result.add newTree(nkFastAsgn, le, tmp) proc genWasMoved(c: var Con, n: PNode): PNode = diff --git a/tests/arc/tcaseobj.nim b/tests/arc/tcaseobj.nim index ab34f9e78259..be8eb92d2b5b 100644 --- a/tests/arc/tcaseobj.nim +++ b/tests/arc/tcaseobj.nim @@ -207,9 +207,8 @@ type proc init(): RocksDBResult[string] = result = RocksDBResult[string](ok: true, value: "ok") - # fixme: use {.cast(uncheckedAssign).} - # result.ok = true - # result.value = "ok" + result.ok = true + result.value = "ok" echo init() @@ -223,9 +222,10 @@ type MyObj = object of true: x1: string var a = MyObj(kind: false, x0: 1234) -# fixme: use {.cast(uncheckedAssign).} -# a.kind = true -# doAssert(a.x1 == "") +{.cast(uncheckedAssign).}: + a.kind = true + doAssert(a.x1 == "") + block: # bug #15532 diff --git a/tests/arc/tcaseobjcopy.nim b/tests/arc/tcaseobjcopy.nim index 77e4365e5aba..173fe431fc8e 100644 --- a/tests/arc/tcaseobjcopy.nim +++ b/tests/arc/tcaseobjcopy.nim @@ -207,9 +207,8 @@ type proc init(): RocksDBResult[string] = result = RocksDBResult[string](ok: true, value: "ok") - # fixme: use {.cast(uncheckedAssign).} - # result.ok = true - # result.value = "ok" + result.ok = true + result.value = "ok" echo init() @@ -223,9 +222,9 @@ type MyObj = object of true: x1: string var a = MyObj(kind: false, x0: 1234) -# fixme: use {.cast(uncheckedAssign).} -# a.kind = true -# doAssert(a.x1 == "") +{.cast(uncheckedAssign).}: + a.kind = true + doAssert(a.x1 == "") block: # bug #15532 diff --git a/tests/destructor/tgotoexceptions7.nim b/tests/destructor/tgotoexceptions7.nim index fdc59005dbca..c703ece45862 100644 --- a/tests/destructor/tgotoexceptions7.nim +++ b/tests/destructor/tgotoexceptions7.nim @@ -1,7 +1,6 @@ discard """ cmd: "nim c --gc:arc --exceptions:goto --panics:off $file" output: ''' -field error prevented prevented! caught AssertionDefect @@ -27,10 +26,10 @@ proc helper = doAssert(false) proc main(i: int) = var obj = Obj(kind: kindA, s: "abc") - try: + {.cast(uncheckedAssign).}: obj.kind = kindB - except FieldDefect: - echo "field error prevented" + obj.i = 2 + try: var objA = ObjA() bplease(ObjB(objA)) From fff1afda2b33f30941521673d0bfb0a3d001a926 Mon Sep 17 00:00:00 2001 From: xflywind Date: Mon, 28 Feb 2022 12:14:02 +0800 Subject: [PATCH 5/6] uncheckedAssign --- tests/arc/tcaseobj.nim | 6 +++--- tests/arc/tcaseobjcopy.nim | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/arc/tcaseobj.nim b/tests/arc/tcaseobj.nim index be8eb92d2b5b..b997e6af9156 100644 --- a/tests/arc/tcaseobj.nim +++ b/tests/arc/tcaseobj.nim @@ -206,9 +206,9 @@ type error*: string proc init(): RocksDBResult[string] = - result = RocksDBResult[string](ok: true, value: "ok") - result.ok = true - result.value = "ok" + {.cast(uncheckedAssign).}: + result.ok = true + result.value = "ok" echo init() diff --git a/tests/arc/tcaseobjcopy.nim b/tests/arc/tcaseobjcopy.nim index 173fe431fc8e..4e5e76036f10 100644 --- a/tests/arc/tcaseobjcopy.nim +++ b/tests/arc/tcaseobjcopy.nim @@ -206,9 +206,9 @@ type error*: string proc init(): RocksDBResult[string] = - result = RocksDBResult[string](ok: true, value: "ok") - result.ok = true - result.value = "ok" + {.cast(uncheckedAssign).}: + result.ok = true + result.value = "ok" echo init() From 8c3c1545c5f650b9df2e9dd3e3b3c39832bb5f3b Mon Sep 17 00:00:00 2001 From: xflywind Date: Tue, 1 Mar 2022 10:03:54 +0800 Subject: [PATCH 6/6] revert --- compiler/ccgstmts.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/ccgstmts.nim b/compiler/ccgstmts.nim index ca9471c2588f..240fa55c8814 100644 --- a/compiler/ccgstmts.nim +++ b/compiler/ccgstmts.nim @@ -1566,7 +1566,7 @@ proc asgnFieldDiscriminant(p: BProc, e: PNode) = initLocExpr(p, e[0], a) getTemp(p, a.t, tmp) expr(p, e[1], tmp) - if p.inUncheckedAssignSection == 0: + if optTinyRtti notin p.config.globalOptions and p.inUncheckedAssignSection == 0: let field = dotExpr[1].sym genDiscriminantCheck(p, a, tmp, dotExpr[0].typ, field) message(p.config, e.info, warnCaseTransition)