Skip to content

add decls.byLent, turns an expression into a let #14875

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

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@
initial size must be a power of two - this is done internally.
Proc `rightSize` for Tables and HashSets is deprecated, as it is not needed anymore.


- Add `decls.byLent` to turn an argument into a let param, useful for overload resolution.

## Language changes
- In the newruntime it is now allowed to assign to the discriminator field
Expand Down
11 changes: 3 additions & 8 deletions compiler/options.nim
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import

from terminal import isatty
from times import utc, fromUnix, local, getTime, format, DateTime
from std/decls import byLent

const
hasTinyCBackend* = defined(tinyc)
Expand Down Expand Up @@ -619,12 +620,6 @@ proc shortenDir*(conf: ConfigRef; dir: string): string {.
return substr(dir, prefix.len)
result = dir

proc removeTrailingDirSep*(path: string): string =
if (path.len > 0) and (path[^1] == DirSep):
result = substr(path, 0, path.len - 2)
else:
result = path

proc disableNimblePath*(conf: ConfigRef) =
incl conf.globalOptions, optNoNimblePath
conf.lazyPaths.setLen(0)
Expand Down Expand Up @@ -653,7 +648,7 @@ proc getNimcacheDir*(conf: ConfigRef): AbsoluteDir =
(if isDefined(conf, "release") or isDefined(conf, "danger"): "_r" else: "_d"))

proc pathSubs*(conf: ConfigRef; p, config: string): string =
let home = removeTrailingDirSep(os.getHomeDir())
let home = os.getHomeDir().normalizePathEnd
result = unixToNativePath(p % [
"nim", getPrefixDir(conf).string,
"lib", conf.libpath.string,
Expand All @@ -668,7 +663,7 @@ iterator nimbleSubs*(conf: ConfigRef; p: string): string =
let pl = p.toLowerAscii
if "$nimblepath" in pl or "$nimbledir" in pl:
for i in countdown(conf.nimblePaths.len-1, 0):
let nimblePath = removeTrailingDirSep(conf.nimblePaths[i].string)
let nimblePath = conf.nimblePaths[i].string.byLent.normalizePathEnd
yield p % ["nimblepath", nimblePath, "nimbledir", nimblePath]
else:
yield p
Expand Down
20 changes: 18 additions & 2 deletions lib/std/decls.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# see `semLowerLetVarCustomPragma` for compiler support that enables these
# lowerings
#[
keep this module dependency-light to be usable in low level modules.

see `semLowerLetVarCustomPragma` for compiler support that enables these
lowerings
]#

template byaddr*(lhs, typ, ex) =
## Allows a syntax for lvalue reference, exact analog to
Expand All @@ -17,3 +21,15 @@ template byaddr*(lhs, typ, ex) =
else:
let tmp: ptr typ = addr(ex)
template lhs: untyped = tmp[]

proc byLent*[T](a: var T): lent T {.inline.} =
## Transforms `a` into a let param without copying; this is useful for overload
## resolution
runnableExamples:
proc fn(a: int): int = result = a*2
proc fn(a: var int) = a = a*2
var x = 3
# x = fn(x) # would give: Error: expression 'fn(x)' has no type (or is ambiguous)
x = fn(x.byLent) # works
doAssert x == 3*2
result = a # just `a` hits: bug #14420
37 changes: 22 additions & 15 deletions tests/js/taddr.nim
Original file line number Diff line number Diff line change
Expand Up @@ -81,29 +81,36 @@ doAssert(someGlobal == 10)
block:
# issue #14576
# lots of these used to give: Error: internal error: genAddr: 2
proc byLent[T](a: T): lent T = a
proc byLentAny[T](a: T): lent T = a
proc byLentVar[T](a: var T): lent T = a # see also decls.byLent
proc byPtr[T](a: T): ptr T = a.unsafeAddr

block:
let a = (10,11)
let (x,y) = byLent(a)
let (x,y) = byLentAny(a)
doAssert (x,y) == a
block:
var a = (10,11)
let (x,y) = byLentAny(a)
doAssert (x,y) == a

block:
when defined(c) and defined(release):
# bug; pending https://github.com/nim-lang/Nim/issues/14578
discard
else:
let a = 10
doAssert byLent(a) == 10
let a2 = byLent(a)
doAssert a2 == 10
let a = 10
doAssert byLentAny(a) == 10
let a2 = byLentAny(a)
doAssert a2 == 10

block:
let a = [11,12]
doAssert byLent(a) == [11,12]
doAssert byLentAny(a) == [11,12]
let a2 = (11,)
doAssert byLent(a2) == (11,)
doAssert byLentAny(a2) == (11,)

block:
var a = [11,12]
doAssert byLentVar(a) == [11,12]
var a2 = (11,)
doAssert byLentVar(a2) == (11,)

block:
when defined(c) and defined(release):
Expand All @@ -117,9 +124,9 @@ block:
doAssert byPtr(a3)[] == 14

block:
proc byLent2[T](a: seq[T]): lent T = a[1]
proc byLentElem[T](a: seq[T]): lent T = a[1]
var a = @[20,21,22]
doAssert byLent2(a) == 21
doAssert byLentElem(a) == 21

block: # sanity checks
proc bar[T](a: var T): var T = a
Expand All @@ -134,6 +141,6 @@ block:
bar(a2).inc
doAssert a2 == 13

block: # xxx: bug this doesn't work
block: # pending bug #14877
when false:
proc byLent2[T](a: T): lent type(a[0]) = a[0]
56 changes: 32 additions & 24 deletions tests/stdlib/tdecls.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import std/decls

block:
block: # byaddr
var s = @[10,11,12]
var a {.byaddr.} = s[0]
a+=100
Expand Down Expand Up @@ -34,23 +34,9 @@ block:
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) =
block: # custom var pragmas
## We can define custom pragmas in user code
template byUnsafeAddr(lhs, typ, expr) =
when typ is type(nil):
let tmp = unsafeAddr(expr)
else:
Expand All @@ -59,12 +45,34 @@ block: # nkAccQuoted

block:
let s = @["foo", "bar"]
let a {.`byUnsafeAddr`.} = s[0]
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
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

block: # byLent
proc fn(a: int): int = result = a*2
proc fn(a: var int) = a = a*5
var x = 3
x = fn(x.byLent)
doAssert x == 3*2