From 9524edec60a4222aee1af6650e1fbdc12f9270ff Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Tue, 8 Apr 2025 23:54:31 +0800 Subject: [PATCH] =?UTF-8?q?fixes=20#24850;=20macro-generated=20if/else=20a?= =?UTF-8?q?nd=20when/else=20statements=20have=20m=E2=80=A6=20(#24852)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit …ismatched indentation with repr fixes #24850 (cherry picked from commit 29a2e25d1e47deaa7fbaaf5aaf78ab5be430c731) --- compiler/renderer.nim | 60 ++++++++++++++++++++---------------- tests/arc/topt_cursor.nim | 3 +- tests/arc/topt_no_cursor.nim | 3 +- tests/stdlib/trepr.nim | 24 +++++++++++++++ 4 files changed, 62 insertions(+), 28 deletions(-) diff --git a/compiler/renderer.nim b/compiler/renderer.nim index 4e8511437b..060089482a 100644 --- a/compiler/renderer.nim +++ b/compiler/renderer.nim @@ -557,8 +557,16 @@ proc lsub(g: TSrcGen; n: PNode): int = of nkIfExpr: result = lsub(g, n[0][0]) + lsub(g, n[0][1]) + lsons(g, n, 1) + len("if_:_") - of nkElifExpr: result = lsons(g, n) + len("_elif_:_") - of nkElseExpr: result = lsub(g, n[0]) + len("_else:_") # type descriptions + of nkElifExpr, nkElifBranch: + if isEmptyType(n[1].typ): + result = lsons(g, n) + len("elif_:_") + else: + result = lsons(g, n) + len("_elif_:_") + of nkElseExpr, nkElse: + if isEmptyType(n[0].typ): + result = lsub(g, n[0]) + len("else:_") + else: + result = lsub(g, n[0]) + len("_else:_") # type descriptions of nkTypeOfExpr: result = (if n.len > 0: lsub(g, n[0]) else: 0)+len("typeof()") of nkRefTy: result = (if n.len > 0: lsub(g, n[0])+1 else: 0) + len("ref") of nkPtrTy: result = (if n.len > 0: lsub(g, n[0])+1 else: 0) + len("ptr") @@ -601,8 +609,6 @@ proc lsub(g: TSrcGen; n: PNode): int = of nkCommentStmt: result = n.comment.len of nkOfBranch: result = lcomma(g, n, 0, - 2) + lsub(g, lastSon(n)) + len("of_:_") of nkImportAs: result = lsub(g, n[0]) + len("_as_") + lsub(g, n[1]) - of nkElifBranch: result = lsons(g, n) + len("elif_:_") - of nkElse: result = lsub(g, n[0]) + len("else:_") of nkFinally: result = lsub(g, n[0]) + len("finally:_") of nkGenericParams: result = lcomma(g, n) + 2 of nkFormalParams: @@ -1459,15 +1465,30 @@ proc gsub(g: var TSrcGen, n: PNode, c: TContext, fromStmtList = false) = putWithSpace(g, tkColon, ":") if n.len > 0: gsub(g, n[0], 1) gsons(g, n, emptyContext, 1) - of nkElifExpr: - putWithSpace(g, tkElif, " elif") - gcond(g, n[0]) - putWithSpace(g, tkColon, ":") - gsub(g, n, 1) - of nkElseExpr: - put(g, tkElse, " else") - putWithSpace(g, tkColon, ":") - gsub(g, n, 0) + of nkElifExpr, nkElifBranch: + if isEmptyType(n[1].typ): + optNL(g) + putWithSpace(g, tkElif, "elif") + gsub(g, n, 0) + putWithSpace(g, tkColon, ":") + gcoms(g) + gstmts(g, n[1], c) + else: + putWithSpace(g, tkElif, " elif") + gcond(g, n[0]) + putWithSpace(g, tkColon, ":") + gsub(g, n, 1) + of nkElseExpr, nkElse: + if isEmptyType(n[0].typ): + optNL(g) + put(g, tkElse, "else") + putWithSpace(g, tkColon, ":") + gcoms(g) + gstmts(g, n[0], c) + else: + put(g, tkElse, " else") + putWithSpace(g, tkColon, ":") + gsub(g, n, 0) of nkTypeOfExpr: put(g, tkType, "typeof") put(g, tkParLe, "(") @@ -1729,19 +1750,6 @@ proc gsub(g: var TSrcGen, n: PNode, c: TContext, fromStmtList = false) = of nkMixinStmt: putWithSpace(g, tkMixin, "mixin") gcomma(g, n, c) - of nkElifBranch: - optNL(g) - putWithSpace(g, tkElif, "elif") - gsub(g, n, 0) - putWithSpace(g, tkColon, ":") - gcoms(g) - gstmts(g, n[1], c) - of nkElse: - optNL(g) - put(g, tkElse, "else") - putWithSpace(g, tkColon, ":") - gcoms(g) - gstmts(g, n[0], c) of nkFinally, nkDefer: optNL(g) if n.kind == nkFinally: diff --git a/tests/arc/topt_cursor.nim b/tests/arc/topt_cursor.nim index 7941329219..9a9552c837 100644 --- a/tests/arc/topt_cursor.nim +++ b/tests/arc/topt_cursor.nim @@ -9,7 +9,8 @@ var try: x_cursor = ("hi", 5) if cond: - x_cursor = ("different", 54) else: + x_cursor = ("different", 54) + else: x_cursor = ("string here", 80) echo [ :tmpD = `$$`(x_cursor) diff --git a/tests/arc/topt_no_cursor.nim b/tests/arc/topt_no_cursor.nim index 0a4984a690..3513a78449 100644 --- a/tests/arc/topt_no_cursor.nim +++ b/tests/arc/topt_no_cursor.nim @@ -128,7 +128,8 @@ if dirExists(this.value): var :tmpD par = (dir: :tmpD = `=dup`(this.value) - :tmpD, front: "") else: + :tmpD, front: "") +else: var :tmpD_1 :tmpD_2 diff --git a/tests/stdlib/trepr.nim b/tests/stdlib/trepr.nim index 3956b98f95..d70319a7ed 100644 --- a/tests/stdlib/trepr.nim +++ b/tests/stdlib/trepr.nim @@ -326,3 +326,27 @@ do: static: main() main() + +import std/macros + +# bug #24850 +macro a() = + let + y = quote do: discard + b = nnkIfStmt.newTree( + nnkElifExpr.newTree(ident "true", y), nnkElseExpr.newTree(y)) + d = nnkWhenStmt.newTree( + nnkElifExpr.newTree(ident "true", y), nnkElseExpr.newTree(y)) + doAssert repr(b) == """ +if true: + discard +else: + discard""" + + doAssert repr(d) == """ +when true: + discard +else: + discard""" + +a()