macros for proc types, macros for types (#13778)

* new minor feature: macros for proc types, to be documented

* Finished the implementation and added tests

* [skip ci] Describe the new custom pragmas in the manual and the changelog

Co-authored-by: Zahary Karadjov <zahary@gmail.com>
This commit is contained in:
Andreas Rumpf
2020-03-31 21:14:05 +02:00
committed by GitHub
parent 40898871a9
commit 9134bb9cfb
7 changed files with 303 additions and 140 deletions

View File

@@ -1,6 +1,6 @@
{.experimental: "notnil".}
import macros
import macros, asyncmacro, asyncfutures
block:
template myAttr() {.pragma.}
@@ -249,3 +249,89 @@ block:
var e {.fooBar("foo", 123, 'u').}: int
doAssert(hasCustomPragma(e, fooBar))
doAssert(getCustomPragmaVal(e, fooBar).c == 123)
block:
macro expectedAst(expectedRepr: static[string], input: untyped): untyped =
assert input.treeRepr & "\n" == expectedRepr
return input
const procTypeAst = """
ProcTy
FormalParams
Empty
IdentDefs
Ident "x"
Ident "int"
Empty
Pragma
Ident "async"
"""
type
Foo = proc (x: int) {.expectedAst(procTypeAst), async.}
static: assert Foo is proc(x: int): Future[void]
const asyncProcTypeAst = """
ProcTy
FormalParams
BracketExpr
Ident "Future"
Ident "void"
IdentDefs
Ident "s"
Ident "string"
Empty
Pragma
"""
type
Bar = proc (s: string) {.async, expectedAst(asyncProcTypeAst).}
static: assert Bar is proc(x: string): Future[void]
const typeAst = """
TypeDef
PragmaExpr
Ident "Baz"
Pragma
Empty
ObjectTy
Empty
Empty
RecList
IdentDefs
Ident "x"
Ident "string"
Empty
"""
type
Baz {.expectedAst(typeAst).} = object
x: string
static: assert Baz.x is string
const procAst = """
ProcDef
Ident "bar"
Empty
Empty
FormalParams
Ident "string"
IdentDefs
Ident "s"
Ident "string"
Empty
Empty
Empty
StmtList
ReturnStmt
Ident "s"
"""
proc bar(s: string): string {.expectedAst(procAst).} =
return s
static: assert bar("x") == "x"