diff --git a/compiler/renderer.nim b/compiler/renderer.nim index 30c817df8f..0a73d6ee99 100644 --- a/compiler/renderer.nim +++ b/compiler/renderer.nim @@ -898,16 +898,19 @@ proc accentedName(g: var TSrcGen, n: PNode) = gsub(g, n) proc infixArgument(g: var TSrcGen, n: PNode, i: int) = - if i >= n.len: return - + if i < 1 and i > 2: return var needsParenthesis = false let n_next = n[i].skipHiddenNodes if n_next.kind == nkInfix: if n_next[0].kind in {nkSym, nkIdent} and n[0].kind in {nkSym, nkIdent}: let nextId = if n_next[0].kind == nkSym: n_next[0].sym.name else: n_next[0].ident let nnId = if n[0].kind == nkSym: n[0].sym.name else: n[0].ident - if getPrecedence(nextId) < getPrecedence(nnId): - needsParenthesis = true + if i == 1: + if getPrecedence(nextId) < getPrecedence(nnId): + needsParenthesis = true + elif i == 2: + if getPrecedence(nextId) <= getPrecedence(nnId): + needsParenthesis = true if needsParenthesis: put(g, tkParLe, "(") gsub(g, n, i) diff --git a/tests/macros/tmacrostmt.nim b/tests/macros/tmacrostmt.nim index bf67a9fc34..56a0bf840b 100644 --- a/tests/macros/tmacrostmt.nim +++ b/tests/macros/tmacrostmt.nim @@ -97,6 +97,9 @@ proc fn4(x: int): int = if x mod 2 == 0: return x + 2 else: return 0 +proc fn5(a, b: float): float = + result = - a * a / (b * b) + #------------------------------------ # bug #10807 proc fn_unsafeaddr(x: int): int = @@ -108,12 +111,14 @@ static: let fn2s = "proc fn2(x, y: float): float =\n result = (y + 2 * x) / (x - y)\n" let fn3s = "proc fn3(x, y: int): bool =\n result = ((x and 3) div 4 or x mod (y xor -1)) == 0 or not contains([1, 2], y)\n" let fn4s = "proc fn4(x: int): int =\n if x mod 2 == 0:\n return x + 2\n else:\n return 0\n" + let fn5s = "proc fn5(a, b: float): float =\n result = -a * a / (b * b)\n" let fnAddr = "proc fn_unsafeaddr(x: int): int =\n result = cast[int](unsafeAddr(x))\n" doAssert fn1.repr_to_string == fn1s doAssert fn2.repr_to_string == fn2s doAssert fn3.repr_to_string == fn3s doAssert fn4.repr_to_string == fn4s + doAssert fn5.repr_to_string == fn5s doAssert fn_unsafeaddr.repr_to_string == fnAddr #------------------------------------