fix #18977; disallow change branch of an object variant in ORC (#21526)

* fix #18977 disallow change branch of an object variant in ORC

* check errors for goto exception

* fixes conditions

* fixes tests

* add a test case for #18977
This commit is contained in:
ringabout
2023-03-16 23:06:26 +08:00
committed by GitHub
parent 6552a27ec1
commit b5ee81fd23
6 changed files with 97 additions and 32 deletions

26
tests/arc/t18977.nim Normal file
View File

@@ -0,0 +1,26 @@
discard """
matrix: "--mm:arc"
"""
type
E = enum
a, b, c, d
X = object
v: int
O = object
case kind: E
of a:
a: int
of {b, c}:
b: float
else:
d: X
proc `=destroy`(x: var X) =
echo "x destroyed"
var o = O(kind: d, d: X(v: 12345))
doAssert o.d.v == 12345
doAssertRaises(FieldDefect):
o.kind = a

View File

@@ -60,7 +60,7 @@ proc `=destroy`(o: var TMyObj) =
o.p = nil
echo "myobj destroyed"
proc `=`(dst: var TMyObj, src: TMyObj) =
proc `=copy`(dst: var TMyObj, src: TMyObj) =
`=destroy`(dst)
dst.p = alloc(src.len)
dst.len = src.len
@@ -170,18 +170,24 @@ proc test_myobject =
x.x1 = "x1"
x.x2 = "x2"
x.y1 = "ljhkjhkjh"
x.kind1 = true
{.cast(uncheckedAssign).}:
x.kind1 = true
x.y2 = @["1", "2"]
x.kind2 = true
{.cast(uncheckedAssign).}:
x.kind2 = true
x.z1 = "yes"
x.kind2 = false
{.cast(uncheckedAssign).}:
x.kind2 = false
x.z2 = @["1", "2"]
x.kind2 = true
{.cast(uncheckedAssign).}:
x.kind2 = true
x.z1 = "yes"
x.kind2 = true # should be no effect
doAssert(x.z1 == "yes")
x.kind2 = false
x.kind1 = x.kind2 # support self assignment with effect
{.cast(uncheckedAssign).}:
x.kind2 = false
{.cast(uncheckedAssign).}:
x.kind1 = x.kind2 # support self assignment with effect
try:
x.kind1 = x.flag # flag is not accesible
@@ -207,8 +213,9 @@ type
error*: string
proc init(): RocksDBResult[string] =
result.ok = true
result.value = "ok"
{.cast(uncheckedAssign).}:
result.ok = true
result.value = "ok"
echo init()
@@ -222,7 +229,8 @@ type MyObj = object
of true: x1: string
var a = MyObj(kind: false, x0: 1234)
a.kind = true
{.cast(uncheckedAssign).}:
a.kind = true
doAssert(a.x1 == "")
block:

View File

@@ -169,18 +169,23 @@ proc test_myobject =
x.x1 = "x1"
x.x2 = "x2"
x.y1 = "ljhkjhkjh"
x.kind1 = true
{.cast(uncheckedAssign).}:
x.kind1 = true
x.y2 = @["1", "2"]
x.kind2 = true
{.cast(uncheckedAssign).}:
x.kind2 = true
x.z1 = "yes"
x.kind2 = false
{.cast(uncheckedAssign).}:
x.kind2 = false
x.z2 = @["1", "2"]
x.kind2 = true
{.cast(uncheckedAssign).}:
x.kind2 = true
x.z1 = "yes"
x.kind2 = true # should be no effect
doAssert(x.z1 == "yes")
x.kind2 = false
x.kind1 = x.kind2 # support self assignment with effect
{.cast(uncheckedAssign).}:
x.kind2 = false
x.kind1 = x.kind2 # support self assignment with effect
try:
x.kind1 = x.flag # flag is not accesible
@@ -206,7 +211,8 @@ type
error*: string
proc init(): RocksDBResult[string] =
result.ok = true
{.cast(uncheckedAssign).}:
result.ok = true
result.value = "ok"
echo init()
@@ -221,7 +227,8 @@ type MyObj = object
of true: x1: string
var a = MyObj(kind: false, x0: 1234)
a.kind = true
{.cast(uncheckedAssign).}:
a.kind = true
doAssert(a.x1 == "")
block:

View File

@@ -25,7 +25,8 @@ proc helper = doAssert(false)
proc main(i: int) =
var obj = Obj(kind: kindA, s: "abc")
obj.kind = kindB
{.cast(uncheckedAssign).}:
obj.kind = kindB
obj.i = 2
try:
var objA = ObjA()