From 249dd7027302c913513cf577beed83512236fc57 Mon Sep 17 00:00:00 2001 From: Zahary Karadjov Date: Thu, 6 Mar 2014 23:03:02 +0200 Subject: [PATCH] test cases for the new handling of iterators by the `is` operator --- compiler/semexprs.nim | 5 ----- compiler/sigmatch.nim | 12 +++++++++--- tests/types/tisopr.nim | 28 +++++++++++++++++++++++++++- 3 files changed, 36 insertions(+), 9 deletions(-) diff --git a/compiler/semexprs.nim b/compiler/semexprs.nim index ebdd79a8c..203a51816 100644 --- a/compiler/semexprs.nim +++ b/compiler/semexprs.nim @@ -323,11 +323,6 @@ proc isOpImpl(c: PContext, n: PNode): PNode = result = newIntNode(nkIntLit, ord(t.kind == tyProc and t.callConv == ccClosure and 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: var t2 = n[2].typ.skipTypes({tyTypeDesc}) let lifted = liftParamType(c, skType, newNodeI(nkArgList, n.info), diff --git a/compiler/sigmatch.nim b/compiler/sigmatch.nim index 6a3c9267e..c0898ef26 100644 --- a/compiler/sigmatch.nim +++ b/compiler/sigmatch.nim @@ -410,7 +410,10 @@ proc procTypeRel(c: var TCandidate, f, a: PType): TTypeRelation = return isNone when useEffectSystem: 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 proc typeRangeRel(f, a: PType): TTypeRelation {.noinline.} = @@ -923,8 +926,11 @@ proc typeRel(c: var TCandidate, f, aOrig: PType, doBind = true): TTypeRelation = result = isNone of tyIter: - if a.kind == f.kind: result = typeRel(c, f.base, a.base) - else: result = isNone + if a.kind == tyIter or + (a.kind == tyProc and tfIterator in a.flags): + result = typeRel(c, f.base, a.base) + else: + result = isNone of tyStmt: result = isGeneric diff --git a/tests/types/tisopr.nim b/tests/types/tisopr.nim index 6d3c51749..f173b93f4 100644 --- a/tests/types/tisopr.nim +++ b/tests/types/tisopr.nim @@ -1,5 +1,8 @@ discard """ - output: "true true false yes" + output: '''true +true +false +yes''' """ proc IsVoid[T](): string = @@ -11,3 +14,26 @@ proc IsVoid[T](): string = const x = int is int 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] +