diff options
author | Zahary Karadjov <zahary@gmail.com> | 2013-11-17 22:50:26 +0200 |
---|---|---|
committer | Zahary Karadjov <zahary@gmail.com> | 2013-11-17 22:50:26 +0200 |
commit | a068aaed3c5ddaf05a104f3f2d0f512bab2861c6 (patch) | |
tree | 4f15fc2599ef28934ac6276ceb9f8a388d63f9a2 /compiler/semstmts.nim | |
parent | 4cea15d2748de610715311497110136ba11c7ce9 (diff) | |
download | Nim-a068aaed3c5ddaf05a104f3f2d0f512bab2861c6.tar.gz |
simple unit test and better documentation for the user defined type classes
Diffstat (limited to 'compiler/semstmts.nim')
-rw-r--r-- | compiler/semstmts.nim | 16 |
1 files changed, 10 insertions, 6 deletions
diff --git a/compiler/semstmts.nim b/compiler/semstmts.nim index ed6787a16..f514a93d7 100644 --- a/compiler/semstmts.nim +++ b/compiler/semstmts.nim @@ -132,7 +132,7 @@ proc fixNilType(n: PNode) = for it in n: fixNilType(it) n.typ = nil -proc discardCheck(result: PNode) = +proc discardCheck(c: PContext, result: PNode) = if result.typ != nil and result.typ.kind notin {tyStmt, tyEmpty}: if result.kind == nkNilLit: result.typ = nil @@ -142,6 +142,10 @@ proc discardCheck(result: PNode) = while n.kind in skipForDiscardable: n = n.lastSon n.typ = nil + elif c.InTypeClass > 0 and result.typ.kind == tyBool: + let verdict = semConstExpr(c, result) + if verdict.intVal == 0: + localError(result.info, "type class predicate failed.") elif result.typ.kind != tyError and gCmd != cmdInteractive: if result.typ.kind == tyNil: fixNilType(result) @@ -169,7 +173,7 @@ proc semIf(c: PContext, n: PNode): PNode = typ = commonType(typ, it.sons[0].typ) else: illFormedAst(it) if isEmptyType(typ) or typ.kind == tyNil or not hasElse: - for it in n: discardCheck(it.lastSon) + for it in n: discardCheck(c, it.lastSon) result.kind = nkIfStmt # propagate any enforced VoidContext: if typ == EnforceVoidContext: result.typ = EnforceVoidContext @@ -230,7 +234,7 @@ proc semCase(c: PContext, n: PNode): PNode = localError(n.info, errNotAllCasesCovered) closeScope(c) if isEmptyType(typ) or typ.kind == tyNil or not hasElse: - for i in 1..n.len-1: discardCheck(n.sons[i].lastSon) + for i in 1..n.len-1: discardCheck(c, n.sons[i].lastSon) # propagate any enforced VoidContext: if typ == EnforceVoidContext: result.typ = EnforceVoidContext @@ -275,8 +279,8 @@ proc semTry(c: PContext, n: PNode): PNode = typ = commonType(typ, a.sons[length-1].typ) dec c.p.inTryStmt if isEmptyType(typ) or typ.kind == tyNil: - discardCheck(n.sons[0]) - for i in 1..n.len-1: discardCheck(n.sons[i].lastSon) + discardCheck(c, n.sons[0]) + for i in 1..n.len-1: discardCheck(c, n.sons[i].lastSon) if typ == EnforceVoidContext: result.typ = EnforceVoidContext else: @@ -1221,7 +1225,7 @@ proc semStmtList(c: PContext, n: PNode): PNode = voidContext = true n.typ = EnforceVoidContext if i != last or voidContext: - discardCheck(n.sons[i]) + discardCheck(c, n.sons[i]) else: n.typ = n.sons[i].typ if not isEmptyType(n.typ): |