isLastRead regression fix (#10463)

* fixes #10462

* add a test
This commit is contained in:
cooldome
2019-01-28 07:32:14 +00:00
committed by Andreas Rumpf
parent 5fa8a61a23
commit 690f21043d
3 changed files with 24 additions and 10 deletions

View File

@@ -139,7 +139,7 @@ proc isLastRead(s: PSym; c: var Con; pc, comesFrom: int): int =
of def:
if c.g[pc].sym == s:
# the path lead to a redefinition of 's' --> abandon it.
return pc
return high(int)
inc pc
of use:
if c.g[pc].sym == s:
@@ -150,14 +150,16 @@ proc isLastRead(s: PSym; c: var Con; pc, comesFrom: int): int =
pc = pc + c.g[pc].dest
of fork:
# every branch must lead to the last read of the location:
let variantA = isLastRead(s, c, pc+1, pc)
var variantA = isLastRead(s, c, pc+1, pc)
if variantA < 0: return -1
let variantB = isLastRead(s, c, pc + c.g[pc].dest, pc)
if variantB < 0: return -1
pc = variantA+1
elif variantA == high(int):
variantA = variantB
pc = variantA
of InstrKind.join:
let dest = pc + c.g[pc].dest
if dest == comesFrom: return pc
if dest == comesFrom: return pc + 1
inc pc
return pc

View File

@@ -74,7 +74,7 @@ proc codeListing(c: ControlFlowGraph, result: var string, start=0; last = -1) =
while i <= last:
if i in jumpTargets: result.add("L" & $i & ":\n")
result.add "\t"
result.add $c[i].kind
result.add ($i & " " & $c[i].kind)
result.add "\t"
case c[i].kind
of def, use:
@@ -540,13 +540,17 @@ proc genTry(c: var Con; n: PNode) =
c.gen(fin.sons[0])
doAssert(c.forks.len == oldLen)
template genNoReturn(c: var Con; n: PNode) =
# leave the graph
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.sons[0])
if c.inTryStmt > 0:
c.tryStmtFixups.add c.gotoI(n)
else:
c.code.add Instr(n: n, kind: goto, dest: high(int) - c.code.len)
genNoReturn(c, n)
proc genImplicitReturn(c: var Con) =
if c.owner.kind in {skProc, skFunc, skMethod, skIterator, skConverter} and resultPos < c.owner.ast.len:
@@ -558,7 +562,7 @@ proc genReturn(c: var Con; n: PNode) =
gen(c, n.sons[0])
else:
genImplicitReturn(c)
c.code.add Instr(n: n, kind: goto, dest: high(int) - c.code.len)
genNoReturn(c, n)
const
InterestingSyms = {skVar, skResult, skLet, skParam}
@@ -612,9 +616,6 @@ proc genMagic(c: var Con; n: PNode; m: TMagic) =
of mNew, mNewFinalize:
genDef(c, n[1])
for i in 2..<n.len: gen(c, n[i])
of mExit:
genCall(c, n)
c.code.add Instr(n: n, kind: goto, dest: high(int) - c.code.len)
else:
genCall(c, n)
@@ -639,6 +640,8 @@ proc gen(c: var Con; n: PNode) =
genMagic(c, n, s.magic)
else:
genCall(c, n)
if sfNoReturn in n.sons[0].sym.flags:
genNoReturn(c, n)
else:
genCall(c, n)
of nkCharLit..nkNilLit: discard

View File

@@ -166,3 +166,12 @@ seq4 =
var ii = 1
let arr2 = [newMySeq(2, 5.0), if i > 1: newMySeq(3, 1.0) else: newMySeq(0, 0.0)]
var seqOfSeq2 = @[newMySeq(2, 5.0), newMySeq(3, 1.0)]
## issue #10462
proc myfuncLoop(x: int): MySeqNonCopyable =
for i in 0..<x:
var cc = newMySeq(i, 5.0)
result = cc
discard myfuncLoop(3)