misc fixes: remove forceConst (obsolete by static), add more runnableExamples to system (#17896)
* misc fixes * add runnableExamples for compileOption * add runnableExamples for runnableExamples * move tconsteval => tconst * cleanup
This commit is contained in:
parent
1f1d85bb9c
commit
82996aee3f
5 changed files with 78 additions and 88 deletions
|
|
@ -8,7 +8,7 @@
|
||||||
#
|
#
|
||||||
|
|
||||||
## This module allows querying the compiler about
|
## This module allows querying the compiler about
|
||||||
## diverse configuration settings.
|
## diverse configuration settings. See also `compileOption`.
|
||||||
|
|
||||||
# Note: Only add new enum values at the end to ensure binary compatibility with
|
# Note: Only add new enum values at the end to ensure binary compatibility with
|
||||||
# other Nim compiler versions!
|
# other Nim compiler versions!
|
||||||
|
|
|
||||||
|
|
@ -55,35 +55,61 @@ type
|
||||||
include "system/basic_types"
|
include "system/basic_types"
|
||||||
|
|
||||||
|
|
||||||
|
proc runnableExamples*(rdoccmd = "", body: untyped) {.magic: "RunnableExamples".} =
|
||||||
|
## A section you should use to mark `runnable example`:idx: code with.
|
||||||
|
##
|
||||||
|
## - In normal debug and release builds code within
|
||||||
|
## a `runnableExamples` section is ignored.
|
||||||
|
## - The documentation generator is aware of these examples and considers them
|
||||||
|
## part of the `##` doc comment. As the last step of documentation
|
||||||
|
## generation each runnableExample is put in its own file `$file_examples$i.nim`,
|
||||||
|
## compiled and tested. The collected examples are
|
||||||
|
## put into their own module to ensure the examples do not refer to
|
||||||
|
## non-exported symbols.
|
||||||
|
runnableExamples:
|
||||||
|
proc timesTwo*(x: int): int =
|
||||||
|
## This proc doubles a number.
|
||||||
|
runnableExamples:
|
||||||
|
# at module scope
|
||||||
|
const exported* = 123
|
||||||
|
assert timesTwo(5) == 10
|
||||||
|
block: # at block scope
|
||||||
|
defer: echo "done"
|
||||||
|
runnableExamples "-d:foo -b:cpp":
|
||||||
|
import std/compilesettings
|
||||||
|
assert querySetting(backend) == "cpp"
|
||||||
|
assert defined(foo)
|
||||||
|
runnableExamples "-r:off": ## this one is only compiled
|
||||||
|
import std/browsers
|
||||||
|
openDefaultBrowser "https://forum.nim-lang.org/"
|
||||||
|
2 * x
|
||||||
|
|
||||||
proc compileOption*(option: string): bool {.
|
proc compileOption*(option: string): bool {.
|
||||||
magic: "CompileOption", noSideEffect.}
|
magic: "CompileOption", noSideEffect.} =
|
||||||
## Can be used to determine an `on|off` compile-time option.
|
## Can be used to determine an `on|off` compile-time option.
|
||||||
##
|
##
|
||||||
## See also:
|
## See also:
|
||||||
## * `compileOption <#compileOption,string,string>`_ for enum options
|
## * `compileOption <#compileOption,string,string>`_ for enum options
|
||||||
## * `defined <#defined,untyped>`_
|
## * `defined <#defined,untyped>`_
|
||||||
## * `std/compilesettings module <compilesettings.html>`_
|
## * `std/compilesettings module <compilesettings.html>`_
|
||||||
##
|
runnableExamples("--floatChecks:off"):
|
||||||
## Example:
|
static: doAssert not compileOption("floatchecks")
|
||||||
##
|
{.push floatChecks: on.}
|
||||||
## .. code-block:: Nim
|
static: doAssert compileOption("floatchecks")
|
||||||
## when compileOption("floatchecks"):
|
# floating point NaN and Inf checks enabled in this scope
|
||||||
## echo "compiled with floating point NaN and Inf checks"
|
{.pop.}
|
||||||
|
|
||||||
proc compileOption*(option, arg: string): bool {.
|
proc compileOption*(option, arg: string): bool {.
|
||||||
magic: "CompileOptionArg", noSideEffect.}
|
magic: "CompileOptionArg", noSideEffect.} =
|
||||||
## Can be used to determine an enum compile-time option.
|
## Can be used to determine an enum compile-time option.
|
||||||
##
|
##
|
||||||
## See also:
|
## See also:
|
||||||
## * `compileOption <#compileOption,string>`_ for `on|off` options
|
## * `compileOption <#compileOption,string>`_ for `on|off` options
|
||||||
## * `defined <#defined,untyped>`_
|
## * `defined <#defined,untyped>`_
|
||||||
## * `std/compilesettings module <compilesettings.html>`_
|
## * `std/compilesettings module <compilesettings.html>`_
|
||||||
##
|
runnableExamples:
|
||||||
## Example:
|
when compileOption("opt", "size") and compileOption("gc", "boehm"):
|
||||||
##
|
discard "compiled with optimization for size and uses Boehm's GC"
|
||||||
## .. code-block:: Nim
|
|
||||||
## when compileOption("opt", "size") and compileOption("gc", "boehm"):
|
|
||||||
## echo "compiled with optimization for size and uses Boehm's GC"
|
|
||||||
|
|
||||||
{.push warning[GcMem]: off, warning[Uninit]: off.}
|
{.push warning[GcMem]: off, warning[Uninit]: off.}
|
||||||
{.push hints: off.}
|
{.push hints: off.}
|
||||||
|
|
@ -139,36 +165,6 @@ else:
|
||||||
OrdinalImpl[T] {.magic: Ordinal.}
|
OrdinalImpl[T] {.magic: Ordinal.}
|
||||||
Ordinal* = OrdinalImpl | uint | uint64
|
Ordinal* = OrdinalImpl | uint | uint64
|
||||||
|
|
||||||
proc runnableExamples*(rdoccmd = "", body: untyped) {.magic: "RunnableExamples".}
|
|
||||||
## A section you should use to mark `runnable example`:idx: code with.
|
|
||||||
##
|
|
||||||
## - In normal debug and release builds code within
|
|
||||||
## a `runnableExamples` section is ignored.
|
|
||||||
## - The documentation generator is aware of these examples and considers them
|
|
||||||
## part of the `##` doc comment. As the last step of documentation
|
|
||||||
## generation each runnableExample is put in its own file `$file_examples$i.nim`,
|
|
||||||
## compiled and tested. The collected examples are
|
|
||||||
## put into their own module to ensure the examples do not refer to
|
|
||||||
## non-exported symbols.
|
|
||||||
##
|
|
||||||
## Usage:
|
|
||||||
##
|
|
||||||
## .. code-block:: Nim
|
|
||||||
## proc double*(x: int): int =
|
|
||||||
## ## This proc doubles a number.
|
|
||||||
## runnableExamples:
|
|
||||||
## ## at module scope
|
|
||||||
## assert double(5) == 10
|
|
||||||
## block: ## at block scope
|
|
||||||
## defer: echo "done"
|
|
||||||
## result = 2 * x
|
|
||||||
## runnableExamples "-d:foo -b:cpp":
|
|
||||||
## import std/compilesettings
|
|
||||||
## doAssert querySetting(backend) == "cpp"
|
|
||||||
## runnableExamples "-r:off": ## this one is only compiled
|
|
||||||
## import std/browsers
|
|
||||||
## openDefaultBrowser "https://forum.nim-lang.org/"
|
|
||||||
|
|
||||||
when defined(nimHasDeclaredMagic):
|
when defined(nimHasDeclaredMagic):
|
||||||
proc declared*(x: untyped): bool {.magic: "Declared", noSideEffect, compileTime.}
|
proc declared*(x: untyped): bool {.magic: "Declared", noSideEffect, compileTime.}
|
||||||
## Special compile-time procedure that checks whether `x` is
|
## Special compile-time procedure that checks whether `x` is
|
||||||
|
|
|
||||||
35
tests/vm/tconst.nim
Normal file
35
tests/vm/tconst.nim
Normal file
|
|
@ -0,0 +1,35 @@
|
||||||
|
discard """
|
||||||
|
targets: "c cpp js"
|
||||||
|
"""
|
||||||
|
|
||||||
|
import std/strutils
|
||||||
|
|
||||||
|
template forceConst(a: untyped): untyped =
|
||||||
|
## Force evaluation at CT, but `static(a)` is simpler
|
||||||
|
const ret = a
|
||||||
|
ret
|
||||||
|
|
||||||
|
proc isNimVm(): bool =
|
||||||
|
when nimvm: result = true
|
||||||
|
else: result = false
|
||||||
|
|
||||||
|
block:
|
||||||
|
doAssert forceConst(isNimVm())
|
||||||
|
doAssert not isNimVm()
|
||||||
|
doAssert forceConst(isNimVm()) == static(isNimVm())
|
||||||
|
doAssert forceConst(isNimVm()) == isNimVm().static
|
||||||
|
|
||||||
|
template main() =
|
||||||
|
# xxx merge more const related tests here
|
||||||
|
block:
|
||||||
|
const
|
||||||
|
a = """
|
||||||
|
Version $1|
|
||||||
|
Compiled at: $2, $3
|
||||||
|
""" % [NimVersion & spaces(44-len(NimVersion)), CompileDate, CompileTime]
|
||||||
|
let b = $a
|
||||||
|
doAssert CompileTime in b
|
||||||
|
doAssert NimVersion in b
|
||||||
|
|
||||||
|
static: main()
|
||||||
|
main()
|
||||||
|
|
@ -1,31 +0,0 @@
|
||||||
discard """
|
|
||||||
action: compile
|
|
||||||
"""
|
|
||||||
|
|
||||||
import strutils
|
|
||||||
|
|
||||||
const
|
|
||||||
HelpText = """
|
|
||||||
+-----------------------------------------------------------------+
|
|
||||||
| Maintenance program for Nim |
|
|
||||||
| Version $1|
|
|
||||||
| (c) 2012 Andreas Rumpf |
|
|
||||||
+-----------------------------------------------------------------+
|
|
||||||
Compiled at: $2, $3
|
|
||||||
|
|
||||||
Usage:
|
|
||||||
koch [options] command [options for command]
|
|
||||||
Options:
|
|
||||||
--force, -f, -B, -b forces rebuild
|
|
||||||
--help, -h shows this help and quits
|
|
||||||
Possible Commands:
|
|
||||||
boot [options] bootstraps with given command line options
|
|
||||||
clean cleans Nim project; removes generated files
|
|
||||||
web generates the website
|
|
||||||
csource [options] builds the C sources for installation
|
|
||||||
zip builds the installation ZIP package
|
|
||||||
inno builds the Inno Setup installer
|
|
||||||
""" % [NimVersion & spaces(44-len(NimVersion)),
|
|
||||||
CompileDate, CompileTime]
|
|
||||||
|
|
||||||
echo HelpText
|
|
||||||
|
|
@ -9,16 +9,6 @@ import os
|
||||||
import math
|
import math
|
||||||
import strutils
|
import strutils
|
||||||
|
|
||||||
template forceConst(a: untyped): untyped =
|
|
||||||
## Force evaluation at CT, useful for example here:
|
|
||||||
## `callFoo(forceConst(getBar1()), getBar2())`
|
|
||||||
## instead of:
|
|
||||||
## block:
|
|
||||||
## const a = getBar1()
|
|
||||||
## `callFoo(a, getBar2())`
|
|
||||||
const ret = a
|
|
||||||
ret
|
|
||||||
|
|
||||||
static:
|
static:
|
||||||
# TODO: add more tests
|
# TODO: add more tests
|
||||||
block: #getAppFilename, gorgeEx, gorge
|
block: #getAppFilename, gorgeEx, gorge
|
||||||
|
|
@ -51,6 +41,6 @@ static:
|
||||||
|
|
||||||
block:
|
block:
|
||||||
# Check against bugs like #9176
|
# Check against bugs like #9176
|
||||||
doAssert getCurrentCompilerExe() == forceConst(getCurrentCompilerExe())
|
doAssert getCurrentCompilerExe() == getCurrentCompilerExe().static
|
||||||
if false: #pending #9176
|
if false: #pending #9176
|
||||||
doAssert gorgeEx("unexistant") == forceConst(gorgeEx("unexistant"))
|
doAssert gorgeEx("unexistant") == gorgeEx("unexistant").static
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue