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:
parent
2e4b6b94e6
commit
1d1b3f79fd
2 changed files with 19 additions and 9 deletions
|
|
@ -17,9 +17,6 @@ proc checkPragma(ex, prag: var NimNode) =
|
||||||
since (1, 3):
|
since (1, 3):
|
||||||
if ex.kind == nnkPragmaExpr:
|
if ex.kind == nnkPragmaExpr:
|
||||||
prag = ex[1]
|
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.} =
|
proc createProcType(p, b: NimNode): NimNode {.compileTime.} =
|
||||||
|
|
@ -66,12 +63,12 @@ macro `=>`*(p, b: untyped): untyped =
|
||||||
|
|
||||||
type
|
type
|
||||||
Bot = object
|
Bot = object
|
||||||
call: proc (): string {.nosideEffect.}
|
call: proc (name: string): string {.noSideEffect.}
|
||||||
|
|
||||||
var myBot = Bot()
|
var myBot = Bot()
|
||||||
|
|
||||||
myBot.call = () {.nosideEffect.} => "I'm a bot."
|
myBot.call = (name: string) {.noSideEffect.} => "Hello " & name & ", I'm a bot."
|
||||||
doAssert myBot.call() == "I'm a bot."
|
doAssert myBot.call("John") == "Hello John, I'm a bot."
|
||||||
|
|
||||||
var
|
var
|
||||||
params = @[ident"auto"]
|
params = @[ident"auto"]
|
||||||
|
|
@ -89,7 +86,7 @@ macro `=>`*(p, b: untyped): untyped =
|
||||||
checkPragma(p, pragma) # check again after -> transform
|
checkPragma(p, pragma) # check again after -> transform
|
||||||
|
|
||||||
since (1, 3):
|
since (1, 3):
|
||||||
if p.kind == nnkCall:
|
if p.kind in {nnkCall, nnkObjConstr}:
|
||||||
# foo(x, y) => x + y
|
# foo(x, y) => x + y
|
||||||
kind = nnkProcDef
|
kind = nnkProcDef
|
||||||
name = p[0]
|
name = p[0]
|
||||||
|
|
@ -129,7 +126,7 @@ macro `=>`*(p, b: untyped): untyped =
|
||||||
error("Expected proc type (->) got (" & c[0].strVal & ").", c)
|
error("Expected proc type (->) got (" & c[0].strVal & ").", c)
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
error("Incorrect procedure parameter list.", c)
|
error("Incorrect procedure parameter.", c)
|
||||||
params.add(identDefs)
|
params.add(identDefs)
|
||||||
of nnkIdent:
|
of nnkIdent:
|
||||||
var identDefs = newNimNode(nnkIdentDefs)
|
var identDefs = newNimNode(nnkIdentDefs)
|
||||||
|
|
|
||||||
|
|
@ -71,3 +71,16 @@ m:
|
||||||
echo "yes"
|
echo "yes"
|
||||||
|
|
||||||
sugarWithPragma() {.m.} => echo "sugarWithPragma called"
|
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."
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue