new syntax for lvalue references: var b {.byaddr.} = expr (#13508)

* new syntax for lvalue references: `var b {.byaddr.} = expr`
* on type mismatch, `???(0, 0)` not shown anymore
* * compiler now lowers `var a: {.foo.}: MyType = expr` to foo(a, MyType, expr)
* new pragmas.byaddr defined in pure library code exploiting this lowering
* skip `template foo() {.pragma.}`
This commit is contained in:
Timothee Cour
2020-03-23 03:15:45 -07:00
committed by GitHub
parent fa06203e90
commit 913bc95964
4 changed files with 139 additions and 1 deletions

19
lib/std/pragmas.nim Normal file
View File

@@ -0,0 +1,19 @@
# see `semLowerLetVarCustomPragma` for compiler support that enables these
# lowerings
template byaddr*(lhs, typ, expr) =
## Allows a syntax for lvalue reference, exact analog to
## `auto& a = expr;` in C++
runnableExamples:
var s = @[10,11,12]
var a {.byaddr.} = s[0]
a+=100
doAssert s == @[110,11,12]
doAssert a is int
var b {.byaddr.}: int = s[0]
doAssert a.addr == b.addr
when typ is type(nil):
let tmp = addr(expr)
else:
let tmp: ptr typ = addr(expr)
template lhs: untyped = tmp[]