Skip to content

Commit

Permalink
add compilesettings.nimVersionCT + friends
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheecour committed Jun 13, 2020
1 parent d149823 commit ee26661
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 3 deletions.
3 changes: 3 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@
- Add `rstgen.rstToLatex` convenience proc for `renderRstToOut` and `initRstGenerator` with `outLatex` output.


- Add `compilesettings.nimVersionCT` to get the stdlib version nim was compiled at; this is the same as the one reported
by `nim dump --dump.format:json . | jq .version` or in `nim -v`, unlike the one in `system.NimVersion`.

## Language changes
- In the newruntime it is now allowed to assign discriminator field without restrictions as long as case object doesn't have custom destructor. Discriminator value doesn't have to be a constant either. If you have custom destructor for case object and you do want to freely assign discriminator fields, it is recommended to refactor object into 2 objects like this:

Expand Down
2 changes: 2 additions & 0 deletions compiler/vmops.nim
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ proc registerAdditionalOps*(c: PCtx) =
setResult(a, querySettingImpl(c.config, getInt(a, 0)))
registerCallback c, "stdlib.compilesettings.querySettingSeq", proc (a: VmArgs) {.nimcall.} =
setResult(a, querySettingSeqImpl(c.config, getInt(a, 0)))
registerCallback c, "stdlib.compilesettings.nimVersionCTImpl", proc (a: VmArgs) {.nimcall.} =
a.setResult (NimMajor, NimMinor, NimPatch).toLit

if defined(nimsuggest) or c.config.cmd == cmdCheck:
discard "don't run staticExec for 'nim suggest'"
Expand Down
52 changes: 52 additions & 0 deletions lib/std/compilesettings.nim
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,55 @@ proc querySettingSeq*(setting: MultipleValueSetting): seq[string] {.
##
## .. code-block:: Nim
## const nimblePaths = compileSettingSeq(MultipleValueSetting.nimblePaths)

type NimVersionType* = object
major*, minor*, patch*: int
# `NimVersion` is already taken by `system.NimVersion`.
# More type-safe than (int,int,int) since procs based on tuples would apply to
# unrelated tuples, eg `proc `$`(a: (int,int,int))` would not be a great idea.

proc nimVersionCTImpl(): (int,int,int) {.compileTime.} = discard

const ver = nimVersionCTImpl()

const nimVersionCT* = NimVersionType(major: ver[0], minor: ver[1], patch: ver[2])
## return the stdlib version nim was compiled with.

const nimVersion* = NimVersionType(major: NimMajor, minor: NimMinor, patch: NimPatch)
## return the stdlib version.

template toTuple*(a: NimVersionType): untyped =
(major: a.major, minor: a.minor, patch: a.patch)

#[
A bit hacky but allows using this before most of system.nim is defined
the only dependency is on NimMajor, NimMinor, NimPatch, `int`
]#

proc `<`(x, y: int): bool {.magic: "LtI", noSideEffect.}

proc `>=`(a, b: tuple[major: int, minor: int, patch: int]): bool =
if b.major < a.major: return true
if a.major < b.major: return false

if b.minor < a.minor: return true
if a.minor < b.minor: return false

if b.patch < a.patch: return true
if a.patch < b.patch: return false
return true

proc `>=`*(a: NimVersionType, b: tuple[major: int, minor: int, patch: int]): bool =
## specialization so it can be used before `<=*[T: tuple]`
## is defined in system.nim, for low level modules (eg: iterators.nim).
## For other comparisons, use `a.toTuple` after `include system/comparisons`.
runnableExamples:
when nimVersionCT >= (1,3,5): discard
doAssert nimVersionCT >= (1,3,5)
doAssert nimVersionCT.toTuple < (99, 0, 0)
doAssert nimVersionCT.toTuple != (99, 0, 0)
a.toTuple >= b

template `$`*(a: NimVersionType): string =
## returns `major.minor.patch`
$a.major & "." & $a.minor & "." & $a.patch
8 changes: 5 additions & 3 deletions lib/system.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2063,17 +2063,19 @@ template unlikely*(val: bool): bool =

const
NimMajor* {.intdefine.}: int = 1
## is the major number of Nim's version. Example:
## is the major number of Nim's version.
## See also `compilesettings.nimVersion,nimVersionCT`.
## Example:
##
## .. code-block:: Nim
## when (NimMajor, NimMinor, NimPatch) >= (1, 3, 1): discard
# see also std/private/since
# See also `std/private/since`

NimMinor* {.intdefine.}: int = 3
## is the minor number of Nim's version.
## Odd for devel, even for releases.

NimPatch* {.intdefine.}: int = 5
NimPatch* {.intdefine.}: int = 7
## is the patch number of Nim's version.
## Odd for devel, even for releases.

Expand Down

0 comments on commit ee26661

Please sign in to comment.