Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
cooldome authored and Araq committed Dec 6, 2019
1 parent 1db2172 commit 7213969
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
15 changes: 10 additions & 5 deletions compiler/enumtostr.nim
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,26 @@ proc genEnumToStrProc*(t: PType; info: TLineInfo; g: ModuleGraph): PSym =
result.ast = n
incl result.flags, sfFromGeneric

proc searchObjCase(obj: PNode; field: PSym): PNode =
proc searchObjCaseImpl(obj: PNode; field: PSym): PNode =
case obj.kind
of nkSym:
result = nil
of nkElse, nkOfBranch:
result = searchObjCase(obj.lastSon, field)
result = searchObjCaseImpl(obj.lastSon, field)
else:
if obj.kind == nkRecCase and obj[0].kind == nkSym and obj[0].sym == field:
result = obj
else:
for x in obj:
result = searchObjCase(x, field)
result = searchObjCaseImpl(x, field)
if result != nil: break

proc searchObjCase(t: PType; field: PSym): PNode =
result = searchObjCaseImpl(t.n, field)
if result == nil and t.len > 0:
result = searchObjCase(t[0].skipTypes({tyAlias, tyGenericInst, tyRef, tyPtr}), field)
doAssert result != nil

proc genCaseObjDiscMapping*(t: PType; field: PSym; info: TLineInfo; g: ModuleGraph): PSym =
result = newSym(skProc, getIdent(g.cache, "objDiscMapping"), t.owner, info)

Expand All @@ -75,8 +81,7 @@ proc genCaseObjDiscMapping*(t: PType; field: PSym; info: TLineInfo; g: ModuleGra
var caseStmt = newNodeI(nkCaseStmt, info)
caseStmt.add(newSymNode dest)

let subObj = searchObjCase(t.n, field)
doAssert subObj != nil
let subObj = searchObjCase(t, field)
for i in 1..<subObj.len:
let ofBranch = subObj[i]
var newBranch = newNodeI(ofBranch.kind, ofBranch.info)
Expand Down
16 changes: 16 additions & 0 deletions tests/destructor/tcaseobj_transitions.nim
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,19 @@ var y = MyCaseObjectB(kind: A)
y.x = 1
y.kind = C
echo "no crash"


#################
## bug #12821

type
RefBaseObject* = ref object of RootObj
case kind: bool
of true: a: int
of false: b: float

MyRefObject = ref object of RefBaseObject
x: float

let z = new(MyRefObject)
z.kind = false

0 comments on commit 7213969

Please sign in to comment.