Fix codegen for mInSet magic and empty sets (#8140)

Fixes #8041
This commit is contained in:
LemonBoy 2018-06-29 15:10:51 +02:00 • committed by Andreas Rumpf
commit 64c84a7d11
2 changed files with 24 additions and 13 deletions

View file

@ -1587,20 +1587,24 @@ proc genInOp(p: BProc, e: PNode, d: var TLoc) =
e.sons[2] e.sons[2]
initLocExpr(p, ea, a) initLocExpr(p, ea, a)
initLoc(b, locExpr, e, OnUnknown) initLoc(b, locExpr, e, OnUnknown)
b.r = rope("(")
var length = sonsLen(e.sons[1]) var length = sonsLen(e.sons[1])
for i in countup(0, length - 1): if length > 0:
let it = e.sons[1].sons[i] b.r = rope("(")
if it.kind == nkRange: for i in countup(0, length - 1):
initLocExpr(p, it.sons[0], x) let it = e.sons[1].sons[i]
initLocExpr(p, it.sons[1], y) if it.kind == nkRange:
addf(b.r, "$1 >= $2 && $1 <= $3", initLocExpr(p, it.sons[0], x)
[rdCharLoc(a), rdCharLoc(x), rdCharLoc(y)]) initLocExpr(p, it.sons[1], y)
else: addf(b.r, "$1 >= $2 && $1 <= $3",
initLocExpr(p, it, x) [rdCharLoc(a), rdCharLoc(x), rdCharLoc(y)])
addf(b.r, "$1 == $2", [rdCharLoc(a), rdCharLoc(x)]) else:
if i < length - 1: add(b.r, " || ") initLocExpr(p, it, x)
add(b.r, ")") addf(b.r, "$1 == $2", [rdCharLoc(a), rdCharLoc(x)])
if i < length - 1: add(b.r, " || ")
add(b.r, ")")
else:
# handle the case of an empty set
b.r = rope("0")
putIntoDest(p, d, e, b.r) putIntoDest(p, d, e, b.r)
else: else:
assert(e.sons[1].typ != nil) assert(e.sons[1].typ != nil)

7
tests/iter/t8041.nim Normal file
View file

@ -0,0 +1,7 @@
iterator xy[T](a: T, b: set[T]): T =
if a in b:
yield a
for a in xy(1'i8, {}):
for b in xy(a, {}):
echo a