From 1d1b3f79fdccfdd359781dbde3c5aea3b67bf05b Mon Sep 17 00:00:00 2001 From: hlaaftana <10591326+hlaaftana@users.noreply.github.com> Date: Tue, 1 Dec 2020 14:01:36 +0300 Subject: [PATCH] fix broken => for typed procs with pragmas (#16190) * fix broken => for typed procs with pragmas * add more sugar tests * add ending quote --- lib/pure/sugar.nim | 15 ++++++--------- tests/macros/tclosuremacro.nim | 13 +++++++++++++ 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/lib/pure/sugar.nim b/lib/pure/sugar.nim index d9c98c3608..0471049722 100644 --- a/lib/pure/sugar.nim +++ b/lib/pure/sugar.nim @@ -17,10 +17,7 @@ proc checkPragma(ex, prag: var NimNode) = since (1, 3): if ex.kind == nnkPragmaExpr: prag = ex[1] - if ex[0].kind == nnkPar and ex[0].len == 1: - ex = ex[0][0] - else: - ex = ex[0] + ex = ex[0] proc createProcType(p, b: NimNode): NimNode {.compileTime.} = result = newNimNode(nnkProcTy) @@ -66,12 +63,12 @@ macro `=>`*(p, b: untyped): untyped = type Bot = object - call: proc (): string {.nosideEffect.} + call: proc (name: string): string {.noSideEffect.} var myBot = Bot() - myBot.call = () {.nosideEffect.} => "I'm a bot." - doAssert myBot.call() == "I'm a bot." + myBot.call = (name: string) {.noSideEffect.} => "Hello " & name & ", I'm a bot." + doAssert myBot.call("John") == "Hello John, I'm a bot." var params = @[ident"auto"] @@ -89,7 +86,7 @@ macro `=>`*(p, b: untyped): untyped = checkPragma(p, pragma) # check again after -> transform since (1, 3): - if p.kind == nnkCall: + if p.kind in {nnkCall, nnkObjConstr}: # foo(x, y) => x + y kind = nnkProcDef name = p[0] @@ -129,7 +126,7 @@ macro `=>`*(p, b: untyped): untyped = error("Expected proc type (->) got (" & c[0].strVal & ").", c) break else: - error("Incorrect procedure parameter list.", c) + error("Incorrect procedure parameter.", c) params.add(identDefs) of nnkIdent: var identDefs = newNimNode(nnkIdentDefs) diff --git a/tests/macros/tclosuremacro.nim b/tests/macros/tclosuremacro.nim index 5c41c317a9..2a7847b9ec 100644 --- a/tests/macros/tclosuremacro.nim +++ b/tests/macros/tclosuremacro.nim @@ -71,3 +71,16 @@ m: echo "yes" sugarWithPragma() {.m.} => echo "sugarWithPragma called" + +typedParamAndPragma(x, y: int) {.used.} -> int => x + y +doAssert typedParamAndPragma(1, 2) == 3 + +type + Bot = object + call: proc (): string {.noSideEffect.} + +var myBot = Bot() +myBot.call = () {.noSideEffect.} => "I'm a bot." +doAssert myBot.call() == "I'm a bot." + +