-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
new syntax for lvalue references: var b {.byaddr.} = expr
#13508
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1bb288c
new syntax for lvalue references: `var b {.byaddr.} = expr`
timotheecour 52390f2
on type mismatch, `???(0, 0)` not shown anymore
timotheecour 56d7d93
new syntax for lvalue references: `var b {.byaddr.} = expr`
timotheecour 788100f
* compiler now lowers `var a: {.foo.}: MyType = expr` to foo(a, MyTyp…
timotheecour f6d29c7
fixup
timotheecour 08bac12
fixup
timotheecour 8336443
skip `template foo() {.pragma.}`
timotheecour b1e464d
address comments
timotheecour File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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[] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import std/pragmas | ||
|
||
block: | ||
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 | ||
|
||
doAssert not compiles(block: | ||
# redeclaration not allowed | ||
var foo = 0 | ||
var foo {.byaddr.} = s[0]) | ||
|
||
doAssert not compiles(block: | ||
# ditto | ||
var foo {.byaddr.} = s[0] | ||
var foo {.byaddr.} = s[0]) | ||
|
||
block: | ||
var b {.byaddr.} = s[1] # redeclaration ok in sub scope | ||
b = 123 | ||
|
||
doAssert s == @[110,123,12] | ||
|
||
b = b * 10 | ||
doAssert s == @[1100,123,12] | ||
|
||
doAssert not compiles(block: | ||
var b2 {.byaddr.}: float = s[2]) | ||
|
||
doAssert compiles(block: | ||
var b2 {.byaddr.}: int = s[2]) | ||
|
||
## We can define custom pragmas in user code | ||
template byUnsafeAddr(lhs, typ, expr) = | ||
when typ is type(nil): | ||
let tmp = unsafeAddr(expr) | ||
else: | ||
let tmp: ptr typ = unsafeAddr(expr) | ||
template lhs: untyped = tmp[] | ||
|
||
block: | ||
let s = @["foo", "bar"] | ||
let a {.byUnsafeAddr.} = s[0] | ||
doAssert a == "foo" | ||
doAssert a[0].unsafeAddr == s[0][0].unsafeAddr | ||
|
||
block: # nkAccQuoted | ||
# shows using a keyword, which requires nkAccQuoted | ||
template `cast`(lhs, typ, expr) = | ||
when typ is type(nil): | ||
let tmp = unsafeAddr(expr) | ||
else: | ||
let tmp: ptr typ = unsafeAddr(expr) | ||
template lhs: untyped = tmp[] | ||
|
||
block: | ||
let s = @["foo", "bar"] | ||
let a {.`byUnsafeAddr`.} = s[0] | ||
doAssert a == "foo" | ||
doAssert a[0].unsafeAddr == s[0][0].unsafeAddr | ||
|
||
block: | ||
let s = @["foo", "bar"] | ||
let a {.`cast`.} = s[0] | ||
doAssert a == "foo" | ||
doAssert a[0].unsafeAddr == s[0][0].unsafeAddr |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.