From c682a1da2b4f364718aaa5e895885043a6f7b873 Mon Sep 17 00:00:00 2001 From: Araq Date: Fri, 13 Jan 2012 14:59:58 +0100 Subject: [PATCH] bugfix: invalid indentation for 'else' is detected; optional indentation for 'if' expressions --- compiler/ast.nim | 2 +- compiler/parser.nim | 7 ++++++- tests/reject/tind1.nim | 27 +++++++++++++++++++++++++++ todo.txt | 3 ++- 4 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 tests/reject/tind1.nim diff --git a/compiler/ast.nim b/compiler/ast.nim index b936ec3b40..9e799e0510 100755 --- a/compiler/ast.nim +++ b/compiler/ast.nim @@ -1024,7 +1024,7 @@ proc getStrOrChar*(a: PNode): string = proc isGenericRoutine*(s: PSym): bool = case s.kind - of skProc, skTemplate, skMacro, skIterator: + of skProc, skTemplate, skMacro, skIterator, skMethod: result = s.ast != nil and s.ast[genericParamsPos].kind != nkEmpty else: nil diff --git a/compiler/parser.nim b/compiler/parser.nim index acca5feaf4..a0d8ad44ab 100755 --- a/compiler/parser.nim +++ b/compiler/parser.nim @@ -503,12 +503,15 @@ proc parseIfExpr(p: var TParser): PNode = var branch = newNodeP(nkElifExpr, p) addSon(branch, parseExpr(p)) eat(p, tkColon) + optInd(p, branch) addSon(branch, parseExpr(p)) + optInd(p, branch) addSon(result, branch) if p.tok.tokType != tkElif: break var branch = newNodeP(nkElseExpr, p) eat(p, tkElse) eat(p, tkColon) + optInd(p, branch) addSon(branch, parseExpr(p)) addSon(result, branch) @@ -1368,7 +1371,9 @@ proc parseStmt(p: var TParser): PNode = break else: var a = complexOrSimpleStmt(p) - if a.kind == nkEmpty: break + if a.kind == nkEmpty: + parMessage(p, errInvalidIndentation) + break addSon(result, a) popInd(p.lex) else: diff --git a/tests/reject/tind1.nim b/tests/reject/tind1.nim new file mode 100644 index 0000000000..e14de09ce1 --- /dev/null +++ b/tests/reject/tind1.nim @@ -0,0 +1,27 @@ +discard """ + line: 24 + errormsg: "invalid indentation" +""" + +import macros + +# finally optional indentation in 'if' expressions :-): +var x = if 4 != 5: + "yes" + else: + "no" + +macro mymacro(n: expr): expr = result = n[1][0] + +mymacro: + echo "test" +else: + echo "else part" + + +if 4 == 3: + echo "bug" + else: + echo "no bug" + + diff --git a/todo.txt b/todo.txt index fc09e5400d..ec0c283dba 100755 --- a/todo.txt +++ b/todo.txt @@ -39,13 +39,14 @@ Bugs proc forward[T](x: var T): var T = result = x proc p(): var int = var x: int + # reject this call via alias analysis: result = forward(x) - bug: stress testing basic method example (eval example) without ``-d:release`` leaks memory; good way to figure out how a fixed amount of stack can hold an arbitrary number of GC roots! - bug: temp2.nim triggers weird compiler and except.nim bug -- bug: tsortdev does not run +- bug: tsortdev does not run with native GC version 0.9.XX