fix broken => for typed procs with pragmas (#16190)

* fix broken => for typed procs with pragmas

* add more sugar tests

* add ending quote
This commit is contained in:
hlaaftana
2020-12-01 14:01:36 +03:00
committed by GitHub
parent 2e4b6b94e6
commit 1d1b3f79fd
2 changed files with 19 additions and 9 deletions

View File

@@ -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)

View File

@@ -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."