Make any accidental "run-time" usage of declval an error

This commit is contained in:
Zahary Karadjov 2020-04-22 16:34:11 +03:00
parent 4acc3866ed
commit 1eed7a72b7
No known key found for this signature in database
GPG Key ID: C8936F8A3073D609
2 changed files with 15 additions and 0 deletions

View File

@ -42,6 +42,8 @@ func declval*(T: type): T {.compileTime.} =
## 2. It will work for types that lack a valid default value due
## to `not nil` or `requiresInit` requirements.
##
doAssert false,
"declvar should be used only in `typeof` expressions and concepts"
default(ptr T)[]
when not compiles(len((1, 2))):

View File

@ -5,6 +5,8 @@ import
when defined(nimHasUsed):
{.used.}
{.experimental: "notnil".}
suite "Objects":
test "baseType":
type
@ -36,19 +38,30 @@ suite "Objects":
f(bar)
test "declval":
type
Bar = object
x: RootRef not nil
proc foo(x: int): string =
discard
proc foo(x: var int): float =
discard
proc foo(x: Bar): int =
discard
type
T1 = typeof foo(declval(int))
T2 = typeof foo(declval(var int))
T3 = typeof foo(declval(lent int))
T4 = typeof foo(declval(Bar))
T5 = typeof foo(declval(var Bar))
check:
T1 is string
T2 is float
T3 is string
T4 is int
T5 is int