test cases for the new handling of iterators by the is operator

This commit is contained in:
Zahary Karadjov 2014-03-06 23:03:02 +02:00
commit 249dd70273
3 changed files with 36 additions and 9 deletions

View file

@ -323,11 +323,6 @@ proc isOpImpl(c: PContext, n: PNode): PNode =
result = newIntNode(nkIntLit, ord(t.kind == tyProc and result = newIntNode(nkIntLit, ord(t.kind == tyProc and
t.callConv == ccClosure and t.callConv == ccClosure and
tfIterator notin t.flags)) tfIterator notin t.flags))
of "iterator":
let t = skipTypes(t1, abstractRange)
result = newIntNode(nkIntLit, ord(t.kind == tyProc and
t.callConv == ccClosure and
tfIterator in t.flags))
else: else:
var t2 = n[2].typ.skipTypes({tyTypeDesc}) var t2 = n[2].typ.skipTypes({tyTypeDesc})
let lifted = liftParamType(c, skType, newNodeI(nkArgList, n.info), let lifted = liftParamType(c, skType, newNodeI(nkArgList, n.info),

View file

@ -410,7 +410,10 @@ proc procTypeRel(c: var TCandidate, f, a: PType): TTypeRelation =
return isNone return isNone
when useEffectSystem: when useEffectSystem:
if not compatibleEffects(f, a): return isNone if not compatibleEffects(f, a): return isNone
of tyNil: result = f.allowsNil of tyNil:
result = f.allowsNil
of tyIter:
if tfIterator in f.flags: result = typeRel(c, f.base, a.base)
else: discard else: discard
proc typeRangeRel(f, a: PType): TTypeRelation {.noinline.} = proc typeRangeRel(f, a: PType): TTypeRelation {.noinline.} =
@ -923,8 +926,11 @@ proc typeRel(c: var TCandidate, f, aOrig: PType, doBind = true): TTypeRelation =
result = isNone result = isNone
of tyIter: of tyIter:
if a.kind == f.kind: result = typeRel(c, f.base, a.base) if a.kind == tyIter or
else: result = isNone (a.kind == tyProc and tfIterator in a.flags):
result = typeRel(c, f.base, a.base)
else:
result = isNone
of tyStmt: of tyStmt:
result = isGeneric result = isGeneric

View file

@ -1,5 +1,8 @@
discard """ discard """
output: "true true false yes" output: '''true
true
false
yes'''
""" """
proc IsVoid[T](): string = proc IsVoid[T](): string =
@ -11,3 +14,26 @@ proc IsVoid[T](): string =
const x = int is int const x = int is int
echo x, " ", float is float, " ", float is string, " ", IsVoid[void]() echo x, " ", float is float, " ", float is string, " ", IsVoid[void]()
template yes(e: expr): stmt =
static: assert e
template no(e: expr): stmt =
static: assert(not e)
var s = @[1, 2, 3]
yes s.items is iterator
no s.items is proc
yes s.items is iterator: int
no s.items is iterator: float
yes s.items is iterator: TNumber
no s.items is iterator: object
type
Iter[T] = iterator: T
yes s.items is Iter[TNumber]
no s.items is Iter[float]