mirror of
https://github.com/nim-lang/Nim.git
synced 2026-08-31 02:43:41 +00:00
Compare commits
52 Commits
araq-dispo
...
b494147310
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b494147310 | ||
|
|
d0919b6df8 | ||
|
|
797b05eda6 | ||
|
|
3a42572b19 | ||
|
|
1a1586a5fb | ||
|
|
2db13e05ac | ||
|
|
87d957fdf1 | ||
|
|
edbb32e4c4 | ||
|
|
0395af2b34 | ||
|
|
60661f6569 | ||
|
|
7a87e7d199 | ||
|
|
e4b1d8eebc | ||
|
|
5094369875 | ||
|
|
269a1c1fec | ||
|
|
c033ccd2e5 | ||
|
|
2290c75f12 | ||
|
|
5ea198faf3 | ||
|
|
8e2547a5e2 | ||
|
|
46cddbccd6 | ||
|
|
9ed4077d9a | ||
|
|
e69d672354 | ||
|
|
bd709f9b4c | ||
|
|
4566ffaca9 | ||
|
|
a2db2af5b6 | ||
|
|
c36617c490 | ||
|
|
9b2b286baf | ||
|
|
49961a54dd | ||
|
|
358d9b4497 | ||
|
|
a3157537e1 | ||
|
|
74499e4561 | ||
|
|
d0ff0ebb43 | ||
|
|
c292981fd3 | ||
|
|
29705aab1a | ||
|
|
1ff79079a6 | ||
|
|
a311ac8d22 | ||
|
|
f3d07ff114 | ||
|
|
b51be75613 | ||
|
|
1451651fd9 | ||
|
|
fb80f7707d | ||
|
|
86b9245dd6 | ||
|
|
e58acc2e1e | ||
|
|
6badeb1b4d | ||
|
|
df42ebc5e6 | ||
|
|
44eafa7552 | ||
|
|
15c6249f2c | ||
|
|
1e3caf457b | ||
|
|
72e9bfe0a4 | ||
|
|
7c873ca615 | ||
|
|
97fed258ed | ||
|
|
937e647f4f | ||
|
|
b41049988f | ||
|
|
04933b773a |
11
.github/dependabot.yml
vendored
Normal file
11
.github/dependabot.yml
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
# To get started with Dependabot version updates, you'll need to specify which
|
||||
# package ecosystems to update and where the package manifests are located.
|
||||
# Please see the documentation for all configuration options:
|
||||
# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates
|
||||
|
||||
version: 2
|
||||
updates:
|
||||
- package-ecosystem: "github-actions" # See documentation for possible values
|
||||
directory: "/" # Location of package manifests
|
||||
schedule:
|
||||
interval: "weekly"
|
||||
2
.github/workflows/bisects.yml
vendored
2
.github/workflows/bisects.yml
vendored
@@ -15,7 +15,7 @@ jobs:
|
||||
name: ${{ matrix.platform }}-bisects
|
||||
runs-on: ${{ matrix.platform }}
|
||||
steps:
|
||||
- uses: actions/checkout@v5
|
||||
- uses: actions/checkout@v6
|
||||
|
||||
- name: Install OpenSSL (Windows)
|
||||
if: |
|
||||
|
||||
4
.github/workflows/ci_docs.yml
vendored
4
.github/workflows/ci_docs.yml
vendored
@@ -53,7 +53,7 @@ jobs:
|
||||
|
||||
steps:
|
||||
- name: 'Checkout'
|
||||
uses: actions/checkout@v4
|
||||
uses: actions/checkout@v6
|
||||
with:
|
||||
fetch-depth: 2
|
||||
|
||||
@@ -109,7 +109,7 @@ jobs:
|
||||
if: |
|
||||
github.event_name == 'push' && github.ref == 'refs/heads/devel' &&
|
||||
matrix.target == 'linux'
|
||||
uses: crazy-max/ghaction-github-pages@v4
|
||||
uses: crazy-max/ghaction-github-pages@v5
|
||||
with:
|
||||
build_dir: doc/html
|
||||
env:
|
||||
|
||||
8
.github/workflows/ci_packages.yml
vendored
8
.github/workflows/ci_packages.yml
vendored
@@ -33,14 +33,14 @@ jobs:
|
||||
NIM_TESTAMENT_BATCH: ${{ matrix.batch }}
|
||||
steps:
|
||||
- name: 'Checkout'
|
||||
uses: actions/checkout@v4
|
||||
uses: actions/checkout@v6
|
||||
with:
|
||||
fetch-depth: 2
|
||||
|
||||
- name: 'Install node.js 20.x'
|
||||
uses: actions/setup-node@v4
|
||||
- name: 'Install node.js'
|
||||
uses: actions/setup-node@v6
|
||||
with:
|
||||
node-version: '20.x'
|
||||
node-version: 24
|
||||
|
||||
- name: 'Install dependencies (Linux amd64)'
|
||||
if: runner.os == 'Linux' && matrix.cpu == 'amd64'
|
||||
|
||||
8
.github/workflows/ci_publish.yml
vendored
8
.github/workflows/ci_publish.yml
vendored
@@ -17,14 +17,14 @@ jobs:
|
||||
runs-on: ${{ matrix.os }}
|
||||
steps:
|
||||
- name: 'Checkout'
|
||||
uses: actions/checkout@v4
|
||||
uses: actions/checkout@v6
|
||||
with:
|
||||
fetch-depth: 2
|
||||
|
||||
- name: 'Install node.js'
|
||||
uses: actions/setup-node@v4
|
||||
uses: actions/setup-node@v6
|
||||
with:
|
||||
node-version: ''
|
||||
node-version: 24
|
||||
|
||||
- name: 'Install dependencies (Linux amd64)'
|
||||
if: runner.os == 'Linux' && matrix.cpu == 'amd64'
|
||||
@@ -60,7 +60,7 @@ jobs:
|
||||
run: nim c -r -d:release ci/action.nim
|
||||
|
||||
- name: 'Comment'
|
||||
uses: actions/github-script@v7
|
||||
uses: actions/github-script@v8
|
||||
with:
|
||||
script: |
|
||||
const fs = require('fs');
|
||||
|
||||
2
.github/workflows/stale.yml
vendored
2
.github/workflows/stale.yml
vendored
@@ -9,7 +9,7 @@ jobs:
|
||||
stale:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/stale@v9
|
||||
- uses: actions/stale@v10
|
||||
with:
|
||||
days-before-pr-stale: 365
|
||||
days-before-pr-close: 30
|
||||
|
||||
@@ -33,7 +33,7 @@ errors.
|
||||
|
||||
- Bitshift operators (`shl`, `shr`, `ashr`) now apply bitmasking to the right operand in the C/C++/VM/JS backends.
|
||||
|
||||
- Adds a new warning enabled by `--warning:ImplicitRangeConversion` that detects downsizing implicit conversions to range types (e.g., `int -> range[0..255]` or `range[1..256] -> range[0..255]`) that could cause runtime panics. Safe conversions like `range[0..255] -> range[0..65535]` and explicit casts are not warned on.
|
||||
- Adds a new warning `--warning:ImplicitRangeConversion` that detects downsizing implicit conversions to range types (e.g., `int -> range[0..255]` or `range[1..256] -> range[0..255]`) that could cause runtime panics. Safe conversions like `range[0..255] -> range[0..65535]` and explicit casts do not trigger warnings. `int` to `Natural` and `Positive` conversions do not trigger warnings, which can be enabled with `--warning:systemRangeConversion`.
|
||||
|
||||
## Standard library additions and changes
|
||||
|
||||
@@ -61,6 +61,10 @@ errors.
|
||||
|
||||
- `system.setLenUninit` now supports refc, JS and VM backends.
|
||||
|
||||
- `std/parseopt` now supports multiple parser modes via a `CliMode` enum.
|
||||
Modes include `Nim` (default, fully compatible) and two new experimental modes:
|
||||
`Lax` and `Gnu` for different option parsing behaviors.
|
||||
|
||||
[//]: # "Changes:"
|
||||
|
||||
- `std/math` The `^` symbol now supports floating-point as exponent in addition to the Natural type.
|
||||
|
||||
@@ -1270,7 +1270,8 @@ template transitionSymKindCommon*(k: TSymKind) =
|
||||
s[] = TSym(kindImpl: k, itemId: obj.itemId, magicImpl: obj.magicImpl, typImpl: obj.typImpl, name: obj.name,
|
||||
infoImpl: obj.infoImpl, ownerFieldImpl: obj.ownerFieldImpl, flagsImpl: obj.flagsImpl, astImpl: obj.astImpl,
|
||||
optionsImpl: obj.optionsImpl, positionImpl: obj.positionImpl, offsetImpl: obj.offsetImpl,
|
||||
locImpl: obj.locImpl, annexImpl: obj.annexImpl, constraintImpl: obj.constraintImpl)
|
||||
disamb: obj.disamb, locImpl: obj.locImpl, annexImpl: obj.annexImpl, constraintImpl: obj.constraintImpl,
|
||||
instantiatedFromImpl: obj.instantiatedFromImpl)
|
||||
when hasFFI:
|
||||
s.cnameImpl = obj.cnameImpl
|
||||
when defined(nimsuggest):
|
||||
|
||||
@@ -910,20 +910,20 @@ proc loadSymFromCursor(c: var DecodeContext; s: PSym; n: var Cursor; thisModule:
|
||||
proc createTypeStub(c: var DecodeContext; t: SymId): PType =
|
||||
let name = pool.syms[t]
|
||||
assert name.startsWith("`t")
|
||||
var i = len("`t")
|
||||
var k = 0
|
||||
while i < name.len and name[i] in {'0'..'9'}:
|
||||
k = k * 10 + name[i].ord - ord('0')
|
||||
inc i
|
||||
if i < name.len and name[i] == '.': inc i
|
||||
var itemId = 0'i32
|
||||
while i < name.len and name[i] in {'0'..'9'}:
|
||||
itemId = itemId * 10'i32 + int32(name[i].ord - ord('0'))
|
||||
inc i
|
||||
if i < name.len and name[i] == '.': inc i
|
||||
let suffix = name.substr(i)
|
||||
result = c.types.getOrDefault(name)[0]
|
||||
if result == nil:
|
||||
var i = len("`t")
|
||||
var k = 0
|
||||
while i < name.len and name[i] in {'0'..'9'}:
|
||||
k = k * 10 + name[i].ord - ord('0')
|
||||
inc i
|
||||
if i < name.len and name[i] == '.': inc i
|
||||
var itemId = 0'i32
|
||||
while i < name.len and name[i] in {'0'..'9'}:
|
||||
itemId = itemId * 10'i32 + int32(name[i].ord - ord('0'))
|
||||
inc i
|
||||
if i < name.len and name[i] == '.': inc i
|
||||
let suffix = name.substr(i)
|
||||
let id = ItemId(module: moduleId(c, suffix).int32, item: itemId)
|
||||
let offs = c.getOffset(id.module.FileIndex, name)
|
||||
result = PType(itemId: id, uniqueId: id, kind: TTypeKind(k), state: Partial)
|
||||
@@ -944,30 +944,26 @@ proc extractLocalSymsFromTree(c: var DecodeContext; n: var Cursor; thisModule: s
|
||||
if n.tagId == sdefTag:
|
||||
# Found an sdef - check if it's local
|
||||
let name = n.firstSon
|
||||
if name.kind == SymbolDef:
|
||||
let symName = pool.syms[name.symId]
|
||||
let sn = parseSymName(symName)
|
||||
if sn.module.len == 0 and symName notin localSyms:
|
||||
# Local symbol - create stub and immediately load it fully
|
||||
# since local symbols have no index offsets for lazy loading
|
||||
let module = moduleId(c, thisModule)
|
||||
let val = addr c.mods[module].symCounter
|
||||
inc val[]
|
||||
let id = ItemId(module: module.int32, item: val[])
|
||||
let sym = PSym(itemId: id, kindImpl: skStub, name: c.cache.getIdent(sn.name),
|
||||
disamb: sn.count.int32, state: Complete)
|
||||
localSyms[symName] = sym
|
||||
# Load the full symbol definition immediately
|
||||
# We're currently at the `(sd` position, need to skip to SymbolDef
|
||||
inc n # skip past `sd` tag to get to SymbolDef
|
||||
inc depth # account for the opening `(` of the sdef
|
||||
loadSymFromCursor(c, sym, n, thisModule, localSyms)
|
||||
sym.state = Sealed # mark as fully loaded
|
||||
# loadSymFromCursor consumed everything including the closing `)`,
|
||||
# so we need to account for it in depth tracking
|
||||
dec depth
|
||||
# Continue processing - loadSymFromCursor already advanced n past the closing `)`
|
||||
continue
|
||||
expect name, SymbolDef
|
||||
let symName = pool.syms[name.symId]
|
||||
let sn = parseSymName(symName)
|
||||
if sn.module.len == 0 and symName notin localSyms:
|
||||
# Local symbol - create stub and immediately load it fully
|
||||
# since local symbols have no index offsets for lazy loading
|
||||
let module = moduleId(c, thisModule)
|
||||
let val = addr c.mods[module].symCounter
|
||||
inc val[]
|
||||
let id = ItemId(module: module.int32, item: val[])
|
||||
let sym = PSym(itemId: id, kindImpl: skStub, name: c.cache.getIdent(sn.name),
|
||||
disamb: sn.count.int32, state: Complete)
|
||||
localSyms[symName] = sym
|
||||
# Load the full symbol definition immediately
|
||||
# We're currently at the `(sd` position, need to skip to SymbolDef
|
||||
inc n # skip past `sd` tag to get to SymbolDef
|
||||
loadSymFromCursor(c, sym, n, thisModule, localSyms)
|
||||
sym.state = Sealed # mark as fully loaded
|
||||
# Continue processing - loadSymFromCursor already advanced n past the closing `)`
|
||||
continue
|
||||
inc depth
|
||||
elif n.kind == ParRi:
|
||||
dec depth
|
||||
|
||||
@@ -202,7 +202,11 @@ type
|
||||
tySequence,
|
||||
tyProc,
|
||||
tyPointer, tyOpenArray,
|
||||
tyString, tyCstring, tyForward,
|
||||
tyString, tyCstring,
|
||||
tyForward,
|
||||
# a type not yet semchecked
|
||||
# When semcheck a type section, all types defined in it are initialized to tyForward
|
||||
|
||||
tyInt, tyInt8, tyInt16, tyInt32, tyInt64, # signed integers
|
||||
tyFloat, tyFloat32, tyFloat64, tyFloat128,
|
||||
tyUInt, tyUInt8, tyUInt16, tyUInt32, tyUInt64,
|
||||
@@ -697,6 +701,7 @@ type
|
||||
|
||||
PLib* = ref TLib
|
||||
TSym* {.acyclic.} = object # Keep in sync with ast2nif.nim
|
||||
# Check `transitionSymKindCommon` in ast.nim when add a new field.
|
||||
itemId*: ItemId
|
||||
# proc and type instantiations are cached in the generic symbol
|
||||
state*: ItemState
|
||||
|
||||
@@ -216,6 +216,8 @@ proc genOptAsgnTuple(p: BProc, dest, src: TLoc, flags: TAssignmentFlags) =
|
||||
flags
|
||||
let t = skipTypes(dest.t, abstractInst).getUniqueType()
|
||||
for i, t in t.ikids:
|
||||
# Do not produce code for void types
|
||||
if isEmptyType(t): continue
|
||||
let field = "Field$1" % [i.rope]
|
||||
genAssignment(p, optAsgnLoc(dest, t, field),
|
||||
optAsgnLoc(src, t, field), newflags)
|
||||
@@ -920,8 +922,8 @@ proc genDeref(p: BProc, e: PNode, d: var TLoc) =
|
||||
else:
|
||||
a = initLocExprSingleUse(p, e[0])
|
||||
|
||||
if e.typ != nil and e.typ.kind == tyObject:
|
||||
# bug #23453 #25265
|
||||
# bug #23453 #25265
|
||||
if e.typ != nil and e.typ.skipTypes(abstractInst).kind == tyObject:
|
||||
discard getTypeDesc(p.module, e.typ)
|
||||
if d.k == locNone:
|
||||
# dest = *a; <-- We do not know that 'dest' is on the heap!
|
||||
@@ -1587,6 +1589,51 @@ proc genSeqElemAppend(p: BProc, e: PNode, d: var TLoc) =
|
||||
genAssignment(p, dest, b, {needToCopy})
|
||||
gcUsage(p.config, e)
|
||||
|
||||
proc genSeqElemAppendV2(p: BProc, e: PNode, d: var TLoc) =
|
||||
# s.add(x) with optSeqDestructors (arc/orc), inlined for direct slot construction:
|
||||
# NI oldLen = s.len;
|
||||
# if (s.p == NIM_NIL || (s.p->cap & ~NIM_STRLIT_FLAG) < oldLen + 1)
|
||||
# s.p = (PayloadType*)prepareSeqAddUninit(oldLen, s.p, 1, sizeof(T), alignof(T));
|
||||
# s.len = oldLen + 1;
|
||||
# s.p->data[oldLen] = x; // direct assignment, no function call overhead
|
||||
let seqtype = skipTypes(e[1].typ, abstractVarRange)
|
||||
var a = initLocExpr(p, e[1])
|
||||
let pt = getSeqPayloadType(p.module, seqtype)
|
||||
let pe = seqPayloadElem(p.module, seqtype)
|
||||
# Capture a stable pointer to the seq BEFORE evaluating the element (e[2]).
|
||||
# Evaluating e[2] may emit move semantics (eqwasMoved) that nil a variable
|
||||
# through which e[1]'s snippet is accessed (e.g. a closure env pointer).
|
||||
inc(p.labels)
|
||||
let seqPtrName = "T" & rope(p.labels) & "_"
|
||||
p.s(cpsLocals).addVar(kind = Local, name = seqPtrName,
|
||||
typ = ptrType(getTypeDesc(p.module, seqtype)))
|
||||
p.s(cpsStmts).addAssignment(seqPtrName, cAddr(rdLoc(a)))
|
||||
var b = initLocExpr(p, e[2])
|
||||
# All seq operations now go through the stable seqPtrName pointer.
|
||||
let ra = wrapPar(cDeref(seqPtrName))
|
||||
var tmpL = getIntTemp(p)
|
||||
p.s(cpsStmts).addAssignment(tmpL.snippet, dotField(ra, "len"))
|
||||
let pField = dotField(ra, "p")
|
||||
p.s(cpsStmts).addSingleIfStmt(
|
||||
cOp(Or,
|
||||
cOp(Equal, pField, NimNil),
|
||||
cOp(LessThan,
|
||||
cOp(BitAnd, NimInt, derefField(pField, "cap"), cOp(BitNot, NimInt, NimStrlitFlag)),
|
||||
cOp(Add, NimInt, tmpL.snippet, cIntValue(1))))):
|
||||
p.s(cpsStmts).addFieldAssignmentWithValue(ra, "p"):
|
||||
p.s(cpsStmts).addCast(ptrType(pt)):
|
||||
p.s(cpsStmts).addCall(cgsymValue(p.module, "prepareSeqAddUninit"),
|
||||
tmpL.snippet,
|
||||
pField,
|
||||
cIntValue(1),
|
||||
cSizeof(pe),
|
||||
cAlignof(pe))
|
||||
p.s(cpsStmts).addFieldAssignment(ra, "len",
|
||||
cOp(Add, NimInt, tmpL.snippet, cIntValue(1)))
|
||||
var dest = initLoc(locExpr, e[2], OnHeap)
|
||||
dest.snippet = subscript(dataField(p, ra), tmpL.snippet)
|
||||
genAssignment(p, dest, b, {})
|
||||
|
||||
proc genDefault(p: BProc; n: PNode; d: var TLoc) =
|
||||
if d.k == locNone: d = getTemp(p, n.typ, needsInit=true)
|
||||
else: resetLoc(p, d)
|
||||
@@ -1722,15 +1769,15 @@ proc genNewSeq(p: BProc, e: PNode) =
|
||||
let seqtype = skipTypes(e[1].typ, abstractVarRange)
|
||||
let ra = a.rdLoc
|
||||
let rb = b.rdLoc
|
||||
let et = getTypeDesc(p.module, seqtype.elementType)
|
||||
let pt = getSeqPayloadType(p.module, seqtype)
|
||||
let pe = seqPayloadElem(p.module, seqtype)
|
||||
p.s(cpsStmts).addFieldAssignment(ra, "len", rb)
|
||||
p.s(cpsStmts).addFieldAssignmentWithValue(ra, "p"):
|
||||
p.s(cpsStmts).addCast(ptrType(pt)):
|
||||
p.s(cpsStmts).addCall(cgsymValue(p.module, "newSeqPayload"),
|
||||
rb,
|
||||
cSizeof(et),
|
||||
cAlignof(et))
|
||||
cSizeof(pe),
|
||||
cAlignof(pe))
|
||||
else:
|
||||
let lenIsZero = e[2].kind == nkIntLit and e[2].intVal == 0
|
||||
genNewSeqAux(p, a, b.rdLoc, lenIsZero)
|
||||
@@ -1743,15 +1790,15 @@ proc genNewSeqOfCap(p: BProc; e: PNode; d: var TLoc) =
|
||||
if d.k == locNone: d = getTemp(p, e.typ, needsInit=false)
|
||||
let rd = d.rdLoc
|
||||
let ra = a.rdLoc
|
||||
let et = getTypeDesc(p.module, seqtype.elementType)
|
||||
let pt = getSeqPayloadType(p.module, seqtype)
|
||||
let pe = seqPayloadElem(p.module, seqtype)
|
||||
p.s(cpsStmts).addFieldAssignment(rd, "len", cIntValue(0))
|
||||
p.s(cpsStmts).addFieldAssignmentWithValue(rd, "p"):
|
||||
p.s(cpsStmts).addCast(ptrType(pt)):
|
||||
p.s(cpsStmts).addCall(cgsymValue(p.module, "newSeqPayloadUninit"),
|
||||
ra,
|
||||
cSizeof(et),
|
||||
cAlignof(et))
|
||||
cSizeof(pe),
|
||||
cAlignof(pe))
|
||||
else:
|
||||
if d.k == locNone: d = getTemp(p, e.typ, needsInit=false) # bug #22560
|
||||
let ra = a.rdLoc
|
||||
@@ -1887,15 +1934,15 @@ proc genSeqConstr(p: BProc, n: PNode, d: var TLoc) =
|
||||
if optSeqDestructors in p.config.globalOptions:
|
||||
let seqtype = n.typ
|
||||
let rd = rdLoc dest[]
|
||||
let et = getTypeDesc(p.module, seqtype.elementType)
|
||||
let pt = getSeqPayloadType(p.module, seqtype)
|
||||
let pe = seqPayloadElem(p.module, seqtype)
|
||||
p.s(cpsStmts).addFieldAssignment(rd, "len", lit)
|
||||
p.s(cpsStmts).addFieldAssignmentWithValue(rd, "p"):
|
||||
p.s(cpsStmts).addCast(ptrType(pt)):
|
||||
p.s(cpsStmts).addCall(cgsymValue(p.module, "newSeqPayload"),
|
||||
lit,
|
||||
cSizeof(et),
|
||||
cAlignof(et))
|
||||
cSizeof(pe),
|
||||
cAlignof(pe))
|
||||
else:
|
||||
# generate call to newSeq before adding the elements per hand:
|
||||
genNewSeqAux(p, dest[], lit, n.len == 0)
|
||||
@@ -1928,15 +1975,15 @@ proc genArrToSeq(p: BProc, n: PNode, d: var TLoc) =
|
||||
let seqtype = n.typ
|
||||
let rd = rdLoc d
|
||||
let valL = cIntValue(L)
|
||||
let et = getTypeDesc(p.module, seqtype.elementType)
|
||||
let pt = getSeqPayloadType(p.module, seqtype)
|
||||
let pe = seqPayloadElem(p.module, seqtype)
|
||||
p.s(cpsStmts).addFieldAssignment(rd, "len", valL)
|
||||
p.s(cpsStmts).addFieldAssignmentWithValue(rd, "p"):
|
||||
p.s(cpsStmts).addCast(ptrType(pt)):
|
||||
p.s(cpsStmts).addCall(cgsymValue(p.module, "newSeqPayload"),
|
||||
valL,
|
||||
cSizeof(et),
|
||||
cAlignof(et))
|
||||
cSizeof(pe),
|
||||
cAlignof(pe))
|
||||
else:
|
||||
let lit = cIntLiteral(L)
|
||||
genNewSeqAux(p, d, lit, L == 0)
|
||||
@@ -2898,8 +2945,15 @@ proc genMagicExpr(p: BProc, e: PNode, d: var TLoc, op: TMagic) =
|
||||
of mAppendStrStr: genStrAppend(p, e, d)
|
||||
of mAppendSeqElem:
|
||||
if optSeqDestructors in p.config.globalOptions:
|
||||
e[1] = makeAddr(e[1], p.module.idgen)
|
||||
genCall(p, e, d)
|
||||
if p.config.selectedGC in {gcArc, gcAtomicArc, gcOrc}:
|
||||
# Inline growth + direct slot assignment: avoids the add() call overhead
|
||||
# and lets the C compiler see the construction expression at its final
|
||||
# destination, enabling in-place construction for nkObjConstr etc.
|
||||
# gcYrc is excluded because its add() acquires a striped reader lock.
|
||||
genSeqElemAppendV2(p, e, d)
|
||||
else:
|
||||
e[1] = makeAddr(e[1], p.module.idgen)
|
||||
genCall(p, e, d)
|
||||
else:
|
||||
genSeqElemAppend(p, e, d)
|
||||
of mEqStr: genStrEquals(p, e, d)
|
||||
@@ -3155,6 +3209,8 @@ proc genTupleConstr(p: BProc, n: PNode, d: var TLoc) =
|
||||
for i in 0..<n.len:
|
||||
var it = n[i]
|
||||
if it.kind == nkExprColonExpr: it = it[1]
|
||||
# Do not produce code for void types
|
||||
if it.typ != nil and isEmptyType(it.typ): continue
|
||||
rec = initLoc(locExpr, it, dest[].storage)
|
||||
rec.snippet = dotField(rdLoc(dest[]), "Field" & rope(i))
|
||||
rec.flags.incl(lfEnforceDeref)
|
||||
@@ -3271,7 +3327,11 @@ proc upConv(p: BProc, n: PNode, d: var TLoc) =
|
||||
p.s(cpsStmts).addCallStmt(cgsymValue(p.module, "raiseObjectConversionError"))
|
||||
raiseInstr(p, p.s(cpsStmts))
|
||||
|
||||
if n[0].typ.kind != tyObject:
|
||||
# skip cast when types map to the same C type
|
||||
# this avoids invalid C code like `*(T*)&x` for types that can't have their address taken (e.g., WASM __externref_t)
|
||||
if getTypeDesc(p.module, n.typ) == getTypeDesc(p.module, n[0].typ):
|
||||
expr(p, n[0], d)
|
||||
elif n[0].typ.kind != tyObject:
|
||||
let destTyp = getTypeDesc(p.module, n.typ)
|
||||
let val = rdLoc(a)
|
||||
if n.isLValue:
|
||||
@@ -3317,7 +3377,7 @@ proc downConv(p: BProc, n: PNode, d: var TLoc) =
|
||||
cCast(ptrType(destType),
|
||||
wrapPar(cAddr(wrapPar(val))))),
|
||||
a.storage)
|
||||
elif p.module.compileToCpp:
|
||||
elif p.module.compileToCpp or isImportedType(src):
|
||||
# C++ implicitly downcasts for us
|
||||
expr(p, arg, d)
|
||||
else:
|
||||
@@ -3762,6 +3822,7 @@ proc containsOpaqueImportcField(typ: PType): bool =
|
||||
return true
|
||||
of tyTuple:
|
||||
for i, a in t.ikids:
|
||||
if isEmptyType(a): continue
|
||||
if containsOpaqueImportcField(a):
|
||||
return true
|
||||
of tyArray:
|
||||
@@ -3811,10 +3872,12 @@ proc getDefaultValue(p: BProc; typ: PType; info: TLineInfo; result: var Builder)
|
||||
var tupleInit: StructInitializer
|
||||
let initKind = if containsOpaqueImportcField(t): siNamedStruct else: siOrderedStruct
|
||||
result.addStructInitializer(tupleInit, kind = initKind):
|
||||
if p.vccAndC and t.isEmptyTupleType:
|
||||
if p.vccAndC and validTupleTypeFields(t) == 0:
|
||||
result.addField(tupleInit, name = "dummy"):
|
||||
result.addIntValue(0)
|
||||
for i, a in t.ikids:
|
||||
# Do not produce code for void types
|
||||
if isEmptyType(a): continue
|
||||
let elemTyp = skipTypes(a, abstractRange+{tyOwned}-{tyTypeDesc})
|
||||
if not isOpaqueImportcType(elemTyp):
|
||||
result.addField(tupleInit, name = "Field" & $i):
|
||||
@@ -3989,6 +4052,8 @@ proc genConstTuple(p: BProc, n: PNode; isConst: bool; tup: PType; result: var Bu
|
||||
var it = n[i]
|
||||
if it.kind == nkExprColonExpr:
|
||||
it = it[1]
|
||||
# Do not produce code for void types
|
||||
if isEmptyType(tup[i]): continue
|
||||
result.addField(tupleInit, name = "Field" & $i):
|
||||
genBracedInit(p, it, isConst, tup[i], result)
|
||||
|
||||
|
||||
@@ -453,6 +453,14 @@ proc getSeqPayloadType(m: BModule; t: PType): Rope =
|
||||
result = getTypeDescWeak(m, t, check, dkParam) & "_Content"
|
||||
#result = getTypeForward(m, t, hashType(t)) & "_Content"
|
||||
|
||||
proc seqPayloadElem(m: BModule; t: PType): Snippet =
|
||||
## Returns the C type name for a seq's element as stored in the payload,
|
||||
## suitable for sizeof()/alignof(). Must use dkVar, not the dkParam default,
|
||||
## because reified openArrays (experimental views) differ: dkParam gives a
|
||||
## bare pointer (T*) while dkVar gives the two-word struct actually stored.
|
||||
var check = initIntSet()
|
||||
result = getTypeDescAux(m, t.elementType, check, dkVar)
|
||||
|
||||
proc seqV2ContentType(m: BModule; t: PType; check: var IntSet) =
|
||||
let sig = hashType(t, m.config)
|
||||
let result = cacheGetType(m.typeCache, sig)
|
||||
@@ -800,6 +808,8 @@ proc getTupleDesc(m: BModule; typ: PType, name: Rope,
|
||||
var res = newBuilder("")
|
||||
res.addStruct(m, typ, name, ""):
|
||||
for i, a in typ.ikids:
|
||||
# Do not produce code for void types
|
||||
if isEmptyType(a): continue
|
||||
res.addField(
|
||||
name = "Field" & $i,
|
||||
typ = getTypeDescAux(m, a, check, dkField))
|
||||
@@ -1472,27 +1482,38 @@ proc genObjectInfo(m: BModule; typ, origType: PType, name: Rope; info: TLineInfo
|
||||
t.incl tfObjHasKids
|
||||
t = t.baseClass
|
||||
|
||||
proc validTupleTypeFields(t: PType): int =
|
||||
# we want to treat tuples with only void fields as empty, so we need to exclude void types here:
|
||||
result = 0
|
||||
for a in t.kids:
|
||||
if not isEmptyType(a): inc result
|
||||
|
||||
proc genTupleInfo(m: BModule; typ, origType: PType, name: Rope; info: TLineInfo) =
|
||||
genTypeInfoAuxBase(m, typ, typ, name, cIntValue(0), info)
|
||||
var expr = getNimNode(m)
|
||||
if not typ.isEmptyTupleType:
|
||||
var tmp = getTempName(m) & "_" & $typ.kidsLen
|
||||
genTNimNodeArray(m, tmp, typ.kidsLen)
|
||||
let nonVoidKids = validTupleTypeFields(typ)
|
||||
if nonVoidKids > 0:
|
||||
var tmp = getTempName(m) & "_" & $nonVoidKids
|
||||
genTNimNodeArray(m, tmp, nonVoidKids)
|
||||
var j = 0
|
||||
for i, a in typ.ikids:
|
||||
# Do not produce code for void types
|
||||
if isEmptyType(a): continue
|
||||
var tmp2 = getNimNode(m)
|
||||
let fieldTypInfo = genTypeInfoV1(m, a, info)
|
||||
m.s[cfsTypeInit3].addSubscriptAssignment(tmp, cIntValue(i), cAddr(tmp2))
|
||||
m.s[cfsTypeInit3].addSubscriptAssignment(tmp, cIntValue(j), cAddr(tmp2))
|
||||
m.s[cfsTypeInit3].addFieldAssignment(tmp2, "kind", 1)
|
||||
m.s[cfsTypeInit3].addFieldAssignmentWithValue(tmp2, "offset"):
|
||||
m.s[cfsTypeInit3].addOffsetof(getTypeDesc(m, origType, dkVar), "Field" & $i)
|
||||
m.s[cfsTypeInit3].addFieldAssignment(tmp2, "typ", fieldTypInfo)
|
||||
m.s[cfsTypeInit3].addFieldAssignment(tmp2, "name", "\"Field" & $i & "\"")
|
||||
m.s[cfsTypeInit3].addFieldAssignment(expr, "len", typ.kidsLen)
|
||||
inc j
|
||||
m.s[cfsTypeInit3].addFieldAssignment(expr, "len", nonVoidKids)
|
||||
m.s[cfsTypeInit3].addFieldAssignment(expr, "kind", 2)
|
||||
m.s[cfsTypeInit3].addFieldAssignment(expr, "sons",
|
||||
cAddr(subscript(tmp, cIntValue(0))))
|
||||
else:
|
||||
m.s[cfsTypeInit3].addFieldAssignment(expr, "len", typ.kidsLen)
|
||||
m.s[cfsTypeInit3].addFieldAssignment(expr, "len", cIntValue(0))
|
||||
m.s[cfsTypeInit3].addFieldAssignment(expr, "kind", 2)
|
||||
m.s[cfsTypeInit3].addFieldAssignment(tiNameForHcr(m, name), "node", cAddr(expr))
|
||||
|
||||
|
||||
@@ -139,7 +139,7 @@
|
||||
|
||||
import
|
||||
ast, msgs, idents,
|
||||
renderer, magicsys, lowerings, lambdalifting, modulegraphs, lineinfos
|
||||
renderer, magicsys, lowerings, lambdalifting, modulegraphs, lineinfos, trees
|
||||
|
||||
import std/tables
|
||||
|
||||
@@ -727,7 +727,7 @@ proc lowerStmtListExprs(ctx: var Ctx, n: PNode, needsSplit: var bool): PNode =
|
||||
n[0] = ex
|
||||
result.add(n)
|
||||
|
||||
of nkCast, nkHiddenStdConv, nkHiddenSubConv, nkConv, nkObjDownConv,
|
||||
of nkCast, nkHiddenStdConv, nkHiddenSubConv, nkConv, nkObjDownConv, nkObjUpConv,
|
||||
nkDerefExpr, nkHiddenDeref:
|
||||
var ns = false
|
||||
for i in ord(n.kind == nkCast)..<n.len:
|
||||
@@ -1390,18 +1390,34 @@ proc optimizeStates(ctx: var Ctx) =
|
||||
for i in 0 .. ctx.states.high:
|
||||
ctx.states[i].label.intVal = i
|
||||
|
||||
proc detectCapturedSym(c: var Ctx, s: PSym, stateIdx: int) =
|
||||
if s.kind in {skResult, skVar, skLet, skForVar, skTemp} and sfGlobal notin s.flags and s.owner == c.fn and s != c.externExcSym:
|
||||
let vs = c.varStates.getOrDefault(s.itemId, localNotSeen)
|
||||
if vs == localNotSeen: # First seing this variable
|
||||
c.varStates[s.itemId] = stateIdx
|
||||
elif vs == localRequiresLifting:
|
||||
discard # Sym already marked
|
||||
elif vs != stateIdx:
|
||||
c.captureVar(s)
|
||||
|
||||
proc isClosureIterLocal(c: Ctx, s: PSym): bool =
|
||||
s.kind in {skResult, skVar, skLet, skForVar, skTemp} and
|
||||
sfGlobal notin s.flags and s.owner == c.fn and s != c.externExcSym
|
||||
|
||||
proc detectCapturedVars(c: var Ctx, n: PNode, stateIdx: int) =
|
||||
case n.kind
|
||||
of nkSym:
|
||||
let s = n.sym
|
||||
if s.kind in {skResult, skVar, skLet, skForVar, skTemp} and sfGlobal notin s.flags and s.owner == c.fn and s != c.externExcSym:
|
||||
let vs = c.varStates.getOrDefault(s.itemId, localNotSeen)
|
||||
if vs == localNotSeen: # First seing this variable
|
||||
c.varStates[s.itemId] = stateIdx
|
||||
elif vs == localRequiresLifting:
|
||||
discard # Sym already marked
|
||||
elif vs != stateIdx:
|
||||
c.captureVar(s)
|
||||
detectCapturedSym(c, s, stateIdx)
|
||||
of nkAddr, nkHiddenAddr:
|
||||
let s = getRoot(n)
|
||||
if s != nil and isClosureIterLocal(c, s):
|
||||
detectCapturedSym(c, s, stateIdx)
|
||||
# bug #25596; lifetime extension for `addr`-taken locals as
|
||||
# we claim ARC/ORC do destruction based on scopes, not on last-usages.
|
||||
c.captureVar(s)
|
||||
for i in 0 ..< n.safeLen:
|
||||
detectCapturedVars(c, n[i], stateIdx)
|
||||
of nkReturnStmt:
|
||||
if n[0].kind in {nkAsgn, nkFastAsgn, nkSinkAsgn}:
|
||||
# we have a `result = result` expression produced by the closure
|
||||
|
||||
@@ -540,10 +540,11 @@ proc nodeToHighlightedHtml(d: PDoc; n: PNode; result: var string;
|
||||
elif s != nil and s.kind in {skType, skVar, skLet, skConst} and
|
||||
sfExported in s.flags and s.owner != nil and
|
||||
belongsToProjectPackage(d.conf, s.owner) and d.target == outHtml:
|
||||
let external = externalDep(d, s.owner)
|
||||
result.addf "<a href=\"$1#$2\"><span class=\"Identifier\">$3</span></a>",
|
||||
[changeFileExt(external, "html"), literal,
|
||||
escLit]
|
||||
let href = (if d.module == s.owner: ""
|
||||
else: externalDep(d, s.owner).changeFileExt("html")
|
||||
) & "#" & literal
|
||||
result.addf "<a href=\"$1\"><span class=\"Identifier\">$2</span></a>",
|
||||
[href, escLit]
|
||||
else:
|
||||
dispA(d.conf, result, "<span class=\"Identifier\">$1</span>",
|
||||
"\\spanIdentifier{$1}", [escLit])
|
||||
|
||||
@@ -72,9 +72,11 @@ proc hasDestructor(c: Con; t: PType): bool {.inline.} =
|
||||
if not result and c.graph.config.selectedGC in {gcArc, gcOrc, gcYrc, gcAtomicArc}:
|
||||
assert(not containsGarbageCollectedRef(t))
|
||||
|
||||
proc getTemp(c: var Con; s: var Scope; typ: PType; info: TLineInfo): PNode =
|
||||
proc getTemp(c: var Con; s: var Scope; typ: PType; info: TLineInfo; needsInit: bool): PNode =
|
||||
let sym = newSym(skTemp, getIdent(c.graph.cache, ":tmpD"), c.idgen, c.owner, info)
|
||||
sym.typ = typ
|
||||
if not needsInit:
|
||||
sym.incl sfNoInit
|
||||
s.vars.add(sym)
|
||||
result = newSymNode(sym)
|
||||
|
||||
@@ -302,7 +304,7 @@ proc genSink(c: var Con; s: var Scope; dest, ri: PNode; flags: set[MoveOrCopyFla
|
||||
if deepAliases(dest, ri):
|
||||
# consider: x = x + y, it is wrong to destroy the destination first!
|
||||
# tmp to support self assignments
|
||||
let tmp = c.getTemp(s, dest.typ, dest.info)
|
||||
let tmp = c.getTemp(s, dest.typ, dest.info, needsInit = false)
|
||||
result = newTree(nkStmtList, newTree(nkFastAsgn, tmp, dest), newTree(nkFastAsgn, dest, ri),
|
||||
c.genDestroy(tmp))
|
||||
else:
|
||||
@@ -371,7 +373,7 @@ proc genDiscriminantAsgn(c: var Con; s: var Scope; n: PNode): PNode =
|
||||
# but fields within active case branch might need destruction
|
||||
|
||||
# tmp to support self assignments
|
||||
let tmp = c.getTemp(s, n[1].typ, n.info)
|
||||
let tmp = c.getTemp(s, n[1].typ, n.info, needsInit = false)
|
||||
|
||||
result = newTree(nkStmtList)
|
||||
result.add newTree(nkFastAsgn, tmp, p(n[1], c, s, consumed))
|
||||
@@ -457,49 +459,50 @@ proc isCapturedVar(n: PNode): bool =
|
||||
else: result = false
|
||||
|
||||
proc passCopyToSink(n: PNode; c: var Con; s: var Scope): PNode =
|
||||
result = newNodeIT(nkStmtListExpr, n.info, n.typ)
|
||||
let nTyp = n.typ.skipTypes(tyUserTypeClasses)
|
||||
let tmp = c.getTemp(s, nTyp, n.info)
|
||||
if hasDestructorOrAsgn(c, nTyp):
|
||||
let typ = nTyp.skipTypes({tyGenericInst, tyAlias, tySink})
|
||||
let op = getAttachedOp(c.graph, typ, attachedDup)
|
||||
if op != nil and tfHasOwned notin typ.flags:
|
||||
if sfError in op.flags:
|
||||
c.checkForErrorPragma(nTyp, n, "=dup")
|
||||
else:
|
||||
let copyOp = getAttachedOp(c.graph, typ, attachedAsgn)
|
||||
if copyOp != nil and sfError in copyOp.flags and
|
||||
sfOverridden notin op.flags:
|
||||
c.checkForErrorPragma(nTyp, n, "=dup", inferredFromCopy = true)
|
||||
|
||||
let src = p(n, c, s, normal)
|
||||
var newCall = newTreeIT(nkCall, src.info, src.typ,
|
||||
newSymNode(op),
|
||||
src)
|
||||
c.finishCopy(newCall, n, {}, isFromSink = true)
|
||||
result.add newTreeI(nkFastAsgn,
|
||||
src.info, tmp,
|
||||
newCall
|
||||
)
|
||||
else:
|
||||
result.add c.genWasMoved(tmp)
|
||||
var m = c.genCopy(tmp, n, {})
|
||||
m.add p(n, c, s, normal)
|
||||
c.finishCopy(m, n, {}, isFromSink = true)
|
||||
result.add m
|
||||
if isLValue(n) and not isCapturedVar(n) and nTyp.skipTypes(abstractInst).kind != tyRef and c.inSpawn == 0:
|
||||
message(c.graph.config, n.info, hintPerformance,
|
||||
("passing '$1' to a sink parameter introduces an implicit copy; " &
|
||||
"if possible, rearrange your program's control flow to prevent it") % $n)
|
||||
if c.inEnsureMove > 0:
|
||||
localError(c.graph.config, n.info, errFailedMove,
|
||||
("cannot move '$1', passing '$1' to a sink parameter introduces an implicit copy") % $n)
|
||||
else:
|
||||
if not hasDestructorOrAsgn(c, nTyp):
|
||||
# Non-managed (plain-old-data) type: no ownership transfer is needed.
|
||||
# Return the expression directly — no temp required.
|
||||
if c.graph.config.selectedGC in {gcArc, gcOrc, gcYrc, gcAtomicArc}:
|
||||
assert(not containsManagedMemory(nTyp))
|
||||
if nTyp.skipTypes(abstractInst).kind in {tyOpenArray, tyVarargs}:
|
||||
localError(c.graph.config, n.info, "cannot create an implicit openArray copy to be passed to a sink parameter")
|
||||
result.add newTree(nkAsgn, tmp, p(n, c, s, normal))
|
||||
return p(n, c, s, normal)
|
||||
result = newNodeIT(nkStmtListExpr, n.info, n.typ)
|
||||
let tmp = c.getTemp(s, nTyp, n.info, needsInit = false)
|
||||
let typ = nTyp.skipTypes({tyGenericInst, tyAlias, tySink})
|
||||
let op = getAttachedOp(c.graph, typ, attachedDup)
|
||||
if op != nil and tfHasOwned notin typ.flags:
|
||||
if sfError in op.flags:
|
||||
c.checkForErrorPragma(nTyp, n, "=dup")
|
||||
else:
|
||||
let copyOp = getAttachedOp(c.graph, typ, attachedAsgn)
|
||||
if copyOp != nil and sfError in copyOp.flags and
|
||||
sfOverridden notin op.flags:
|
||||
c.checkForErrorPragma(nTyp, n, "=dup", inferredFromCopy = true)
|
||||
|
||||
let src = p(n, c, s, normal)
|
||||
var newCall = newTreeIT(nkCall, src.info, src.typ,
|
||||
newSymNode(op),
|
||||
src)
|
||||
c.finishCopy(newCall, n, {}, isFromSink = true)
|
||||
result.add newTreeI(nkFastAsgn,
|
||||
src.info, tmp,
|
||||
newCall
|
||||
)
|
||||
else:
|
||||
result.add c.genWasMoved(tmp)
|
||||
var m = c.genCopy(tmp, n, {})
|
||||
m.add p(n, c, s, normal)
|
||||
c.finishCopy(m, n, {}, isFromSink = true)
|
||||
result.add m
|
||||
if isLValue(n) and not isCapturedVar(n) and nTyp.skipTypes(abstractInst).kind != tyRef and c.inSpawn == 0:
|
||||
message(c.graph.config, n.info, hintPerformance,
|
||||
("passing '$1' to a sink parameter introduces an implicit copy; " &
|
||||
"if possible, rearrange your program's control flow to prevent it") % $n)
|
||||
if c.inEnsureMove > 0:
|
||||
localError(c.graph.config, n.info, errFailedMove,
|
||||
("cannot move '$1', passing '$1' to a sink parameter introduces an implicit copy") % $n)
|
||||
# Since we know somebody will take over the produced copy, there is
|
||||
# no need to destroy it.
|
||||
result.add tmp
|
||||
@@ -530,7 +533,7 @@ proc ensureDestruction(arg, orig: PNode; c: var Con; s: var Scope): PNode =
|
||||
# produce temp creation for (fn, env). But we need to move 'env'?
|
||||
# This was already done in the sink parameter handling logic.
|
||||
result = newNodeIT(nkStmtListExpr, arg.info, arg.typ)
|
||||
let tmp = c.getTemp(s, arg.typ, arg.info)
|
||||
let tmp = c.getTemp(s, arg.typ, arg.info, true)
|
||||
result.add c.genSink(s, tmp, arg, {IsDecl})
|
||||
result.add tmp
|
||||
s.final.add c.genDestroy(tmp)
|
||||
@@ -609,7 +612,7 @@ template processScopeExpr(c: var Con; s: var Scope; ret: PNode, processCall: unt
|
||||
# There is a possibility to do this check: s.wasMoved.len > 0 or s.final.len > 0
|
||||
# later and use it to eliminate the temporary when theres no need for it, but its
|
||||
# tricky because you would have to intercept moveOrCopy at a certain point
|
||||
let tmp = c.getTemp(s.parent[], ret.typ, ret.info)
|
||||
let tmp = c.getTemp(s.parent[], ret.typ, ret.info, needsInit = true)
|
||||
tmp.sym.flags = tmpFlags
|
||||
let cpy = if hasDestructor(c, ret.typ) and
|
||||
ret.typ.kind notin {tyOpenArray, tyVarargs}:
|
||||
@@ -770,7 +773,7 @@ proc pRaiseStmt(n: PNode, c: var Con; s: var Scope): PNode =
|
||||
result = copyNode(n)
|
||||
result.add call
|
||||
else:
|
||||
let tmp = c.getTemp(s, n[0].typ, n.info)
|
||||
let tmp = c.getTemp(s, n[0].typ, n.info, needsInit = true)
|
||||
var m = c.genCopyNoCheck(tmp, n[0], attachedAsgn)
|
||||
m.add p(n[0], c, s, normal)
|
||||
c.finishCopy(m, n[0], {}, isFromSink = false)
|
||||
@@ -1001,6 +1004,13 @@ proc p(n: PNode; c: var Con; s: var Scope; mode: ProcessMode; tmpFlags = {sfSing
|
||||
result = moveOrCopy(p(n[0], c, s, mode), n[1], c, s, flags)
|
||||
elif isDiscriminantField(n[0]):
|
||||
result = c.genDiscriminantAsgn(s, n)
|
||||
elif n[1].kind in {nkStmtListExpr, nkBlockExpr, nkIfExpr, nkCaseStmt, nkTryStmt, nkPragmaBlock}:
|
||||
# Distribute the assignment into each branch to avoid
|
||||
# creating pointless temporaries for expression-based control flow.
|
||||
let dest = p(n[0], c, s, mode)
|
||||
template process(child, s): untyped =
|
||||
newTree(n.kind, dest, p(child, c, s, consumed))
|
||||
handleNestedTempl(n[1], process, willProduceStmt = true)
|
||||
else:
|
||||
result = copyNode(n)
|
||||
result.add p(n[0], c, s, mode)
|
||||
@@ -1154,7 +1164,7 @@ proc ownsData(c: var Con; s: var Scope; orig: PNode; flags: set[MoveOrCopyFlag])
|
||||
break
|
||||
if n.kind in nkCallKinds and n.typ != nil and hasDestructor(c, n.typ):
|
||||
result = newNodeIT(nkStmtListExpr, orig.info, orig.typ)
|
||||
let tmp = c.getTemp(s, n.typ, n.info)
|
||||
let tmp = c.getTemp(s, n.typ, n.info, needsInit = true)
|
||||
tmp.sym.flagsImpl.incl sfSingleUsedTemp
|
||||
result.add newTree(nkFastAsgn, tmp, copyTree(n))
|
||||
s.final.add c.genDestroy(tmp)
|
||||
|
||||
@@ -2018,8 +2018,12 @@ proc createVar(p: PProc, typ: PType, indirect: bool): Rope =
|
||||
if indirect: result = "[$1]" % [result]
|
||||
of tyTuple:
|
||||
result = rope("{")
|
||||
var first = true
|
||||
for i in 0..<t.len:
|
||||
if i > 0: result.add(", ")
|
||||
# Do not produce code for void types
|
||||
if isEmptyType(t[i]): continue
|
||||
if not first: result.add(", ")
|
||||
first = false
|
||||
result.addf("Field$1: $2", [i.rope,
|
||||
createVar(p, t[i], false)])
|
||||
result.add("}")
|
||||
|
||||
@@ -1286,7 +1286,15 @@ proc produceSym(g: ModuleGraph; c: PContext; typ: PType; kind: TTypeAttachedOp;
|
||||
tk = tyNone # no special casing for strings and seqs
|
||||
case tk
|
||||
of tySequence:
|
||||
let needsYrcLock = g.config.selectedGC == gcYrc and
|
||||
kind in {attachedDestructor, attachedSink, attachedAsgn, attachedDeepCopy, attachedDup} and
|
||||
types.canFormAcycle(g, skipped.elementType)
|
||||
# YRC: topology-changing seq ops must hold the mutator (read) lock
|
||||
if needsYrcLock:
|
||||
result.ast[bodyPos].add callCodegenProc(g, "acquireMutatorLock", info)
|
||||
fillSeqOp(a, typ, result.ast[bodyPos], d, src)
|
||||
if needsYrcLock:
|
||||
result.ast[bodyPos].add callCodegenProc(g, "releaseMutatorLock", info)
|
||||
of tyString:
|
||||
fillStrOp(a, typ, result.ast[bodyPos], d, src)
|
||||
else:
|
||||
|
||||
@@ -99,6 +99,7 @@ type
|
||||
warnUser = "User",
|
||||
warnGlobalVarConstructorTemporary = "GlobalVarConstructorTemporary",
|
||||
warnImplicitRangeConversion = "ImplicitRangeConversion",
|
||||
warnSystemRangeConversion = "SystemRangeConversion",
|
||||
# hints
|
||||
hintSuccess = "Success", hintSuccessX = "SuccessX",
|
||||
hintCC = "CC",
|
||||
@@ -208,6 +209,7 @@ const
|
||||
warnUser: "$1",
|
||||
warnGlobalVarConstructorTemporary: "global variable '$1' initialization requires a temporary variable",
|
||||
warnImplicitRangeConversion: "implicit range conversion $1",
|
||||
warnSystemRangeConversion: "implicit range conversion $1",
|
||||
hintSuccess: "operation successful: $#",
|
||||
# keep in sync with `testament.isSuccess`
|
||||
hintSuccessX: "$build\n$loc lines; ${sec}s; $mem; proj: $project; out: $output",
|
||||
@@ -262,7 +264,7 @@ type
|
||||
|
||||
proc computeNotesVerbosity(): array[0..3, TNoteKinds] =
|
||||
result = default(array[0..3, TNoteKinds])
|
||||
result[3] = {low(TNoteKind)..high(TNoteKind)} - {warnObservableStores, warnResultUsed, warnAnyEnumConv, warnBareExcept, warnStdPrefix, warnImplicitRangeConversion}
|
||||
result[3] = {low(TNoteKind)..high(TNoteKind)} - {warnObservableStores, warnResultUsed, warnAnyEnumConv, warnBareExcept, warnStdPrefix, warnSystemRangeConversion}
|
||||
result[2] = result[3] - {hintStackTrace, hintExtendedContext, hintDeclaredLoc, hintProcessingStmt}
|
||||
result[1] = result[2] - {warnProveField, warnProveIndex,
|
||||
warnGcUnsafe, hintPath, hintDependency, hintCodeBegin, hintCodeEnd,
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
|
||||
import
|
||||
ast, msgs, platform, idents,
|
||||
modulegraphs, lineinfos
|
||||
modulegraphs, lineinfos, types
|
||||
|
||||
export createMagic
|
||||
|
||||
@@ -134,7 +134,7 @@ proc getNimScriptSymbol*(g: ModuleGraph; name: string): PSym =
|
||||
proc resetNimScriptSymbols*(g: ModuleGraph) = g.exposed = initStrTable()
|
||||
|
||||
proc getMagicEqSymForType*(g: ModuleGraph; t: PType; info: TLineInfo): PSym =
|
||||
case t.kind
|
||||
case t.skipTypes(abstractRange).kind
|
||||
of tyInt, tyInt8, tyInt16, tyInt32, tyInt64,
|
||||
tyUInt, tyUInt8, tyUInt16, tyUInt32, tyUInt64:
|
||||
result = getSysMagic(g, info, "==", mEqI)
|
||||
|
||||
@@ -65,3 +65,7 @@ define:useStdoutAsStdmsg
|
||||
@if nimHasVtables:
|
||||
experimental:vtables
|
||||
@end
|
||||
|
||||
@if nimHasImplicitRangeConversion:
|
||||
warning[ImplicitRangeConversion]:off
|
||||
@end
|
||||
|
||||
@@ -981,7 +981,7 @@ proc setGenericParams(c: PContext, n, expectedParams: PNode) =
|
||||
if e.typ == nil:
|
||||
n[i].typ = errorType(c)
|
||||
else:
|
||||
n[i].typ = e.typ.skipTypes({tyTypeDesc})
|
||||
n[i].typ = e.typ
|
||||
|
||||
proc explicitGenericInstantiation(c: PContext, n: PNode, s: PSym, doError: bool): PNode =
|
||||
assert n.kind == nkBracketExpr
|
||||
|
||||
@@ -236,6 +236,8 @@ proc evalTypeTrait(c: PContext; traitCall: PNode, operand: PType, context: PSym)
|
||||
let complexObj = containsGarbageCollectedRef(t) or
|
||||
hasDestructor(t)
|
||||
result = newIntNodeT(toInt128(ord(not complexObj)), traitCall, c.idgen, c.graph)
|
||||
of "canFormCycles":
|
||||
result = newIntNodeT(toInt128(ord(types.canFormAcycle(c.graph, operand))), traitCall, c.idgen, c.graph)
|
||||
of "hasDefaultValue":
|
||||
result = newIntNodeT(toInt128(ord(not operand.requiresInit)), traitCall, c.idgen, c.graph)
|
||||
of "isNamedTuple":
|
||||
|
||||
@@ -168,7 +168,7 @@ proc isRangeSupertype(conf: ConfigRef; wider, narrower: PType): bool =
|
||||
# int -> float ranges; warn
|
||||
result = false
|
||||
|
||||
proc shouldWarnRangeConversion(conf: ConfigRef; formalType, argType: PType): bool =
|
||||
proc shouldWarnRangeConversion(conf: ConfigRef; info: TLineInfo; formalType, argType: PType): bool =
|
||||
## Determine if an implicit range conversion should warn
|
||||
## We warn on conversions that are likely to cause panics
|
||||
let f = formalType.skipTypes({tyGenericInst, tyAlias, tySink, tyDistinct})
|
||||
@@ -176,7 +176,19 @@ proc shouldWarnRangeConversion(conf: ConfigRef; formalType, argType: PType): boo
|
||||
if f.kind == tyRange:
|
||||
# Only warn if formal range doesn't fully contain argument range
|
||||
# Check if the ranges don't perfectly overlap
|
||||
result = not isRangeSupertype(conf, f, a)
|
||||
if a.kind == tyInt and f.sym != nil and f.sym.owner != nil and
|
||||
sfSystemModule in f.sym.owner.flags and
|
||||
(f.sym.name.s == "Positive" or
|
||||
f.sym.name.s == "Natural"):
|
||||
# Positive and Natural are special cases that we do not warn on with
|
||||
# ImplicitRangeConversion, but may warn on with systemRangeConversion
|
||||
# if that warning is enabled.
|
||||
if conf.hasWarn(warnSystemRangeConversion):
|
||||
message(conf, info, warnSystemRangeConversion,
|
||||
typeToString(argType) & " -> " & typeToString(formalType))
|
||||
result = false
|
||||
else:
|
||||
result = not isRangeSupertype(conf, f, a)
|
||||
else:
|
||||
result = false
|
||||
|
||||
@@ -1538,7 +1550,8 @@ proc track(tracked: PEffects, n: PNode) =
|
||||
|
||||
# Check for implicit range conversions
|
||||
if n.kind == nkHiddenStdConv and (not tracked.isArrayIndexing) and
|
||||
shouldWarnRangeConversion(tracked.config, n.typ, n[1].typ):
|
||||
n[1].kind notin {nkCharLit..nkUInt64Lit, nkFloatLit..nkFloat128Lit} and
|
||||
shouldWarnRangeConversion(tracked.config, n.info, n.typ, n[1].typ):
|
||||
message(tracked.config, n.info, warnImplicitRangeConversion,
|
||||
typeToString(n[1].typ) & " -> " & typeToString(n.typ))
|
||||
|
||||
|
||||
@@ -1203,7 +1203,15 @@ proc addImplicitGeneric(c: PContext; typeClass: PType, typId: PIdent;
|
||||
# is this a bindOnce type class already present in the param list?
|
||||
for i in 0..<genericParams.len:
|
||||
if genericParams[i].sym.name.id == finalTypId.id:
|
||||
return genericParams[i].typ
|
||||
if typeClass.kind == tyStatic and genericParams[i].typ.kind != tyStatic:
|
||||
# The base type (e.g. from `auto`) was already added as a generic param,
|
||||
# but `static[auto]` requires upgrading it to a `tyStatic` wrapper so
|
||||
# it is instantiated as a compile-time value (`skConst`).
|
||||
genericParams[i].sym.linkTo(typeClass)
|
||||
typeClass.incl tfImplicitTypeParam
|
||||
return typeClass
|
||||
else:
|
||||
return genericParams[i].typ
|
||||
|
||||
let owner = if typeClass.sym != nil: typeClass.sym
|
||||
else: getCurrOwner(c)
|
||||
@@ -1739,6 +1747,7 @@ proc semGeneric(c: PContext, n: PNode, s: PSym, prev: PType): PType =
|
||||
var isConcrete = true
|
||||
let rType = m.call[0].typ
|
||||
let mIndex = if rType != nil: rType.len - 1 else: -1
|
||||
var hasForwardTypeParam = false
|
||||
for i in 1..<m.call.len:
|
||||
var typ = m.call[i].typ
|
||||
# is this a 'typedesc' *parameter*? If so, use the typedesc type,
|
||||
@@ -1755,13 +1764,36 @@ proc semGeneric(c: PContext, n: PNode, s: PSym, prev: PType): PType =
|
||||
skip = false
|
||||
addToResult(typ, skip)
|
||||
|
||||
if typ.kind == tyForward:
|
||||
hasForwardTypeParam = true
|
||||
|
||||
if isConcrete:
|
||||
if s.ast == nil and s.typ.kind != tyCompositeTypeClass:
|
||||
# XXX: What kind of error is this? is it still relevant?
|
||||
localError(c.config, n.info, errCannotInstantiateX % s.name.s)
|
||||
result = newOrPrevType(tyError, prev, c)
|
||||
elif containsGenericInvocationWithForward(n[0]):
|
||||
elif containsGenericInvocationWithForward(n[0]) or hasForwardTypeParam:
|
||||
# isConcrete == false means this generic type is not instanciated here because it invoked with generic parameters.
|
||||
# Even if isConcrete == true, don't instanciate it now if there are any `tyForward` type params.
|
||||
# Such `tyForward` type params will be semchecked later and we can instanciate this next time.
|
||||
# Some generic types like std/options.Option[T] needs a type kinds of the given type argument.
|
||||
|
||||
# return `tyForward` instead of `tyGenericInvocation` because:
|
||||
# ```nim
|
||||
# type Foo = object
|
||||
# x: Option[Foo]
|
||||
# ```
|
||||
# returning `tyGenericInvocation` makes `Option[Foo]` to `tyGenericInvocation` and
|
||||
# next time `semGeneric` is called with `Option[Foo]`, containsGenericType(typeof(`Foo`)) == true
|
||||
# and `isConcrete == false`.
|
||||
if prev == nil:
|
||||
result = newTypeS(tyForward, c)
|
||||
result.sym = s
|
||||
else:
|
||||
assignType(result, newTypeS(tyForward, c))
|
||||
result.sym = s
|
||||
c.forwardTypeUpdates.add (result, n) #fixes 1500
|
||||
return
|
||||
else:
|
||||
result = instGenericContainer(c, n.info, result,
|
||||
allowMetaTypes = false)
|
||||
@@ -2049,7 +2081,9 @@ proc semTypeIdent(c: PContext, n: PNode): PSym =
|
||||
# proc signature for example
|
||||
if c.inGenericInst > 0:
|
||||
let bound = result.typ.elementType.sym
|
||||
if bound != nil: return bound
|
||||
# the symbol may still point to the uninstantiated generic body type
|
||||
if bound != nil and bound.typ == result.typ.elementType:
|
||||
return bound
|
||||
return result
|
||||
if result.typ.sym == nil:
|
||||
localError(c.config, n.info, errTypeExpected)
|
||||
|
||||
@@ -563,6 +563,26 @@ proc eraseVoidParams*(t: PType) =
|
||||
setLen t.n.sons, pos
|
||||
break
|
||||
|
||||
proc eraseTupleVoidFields*(t: PType) =
|
||||
## Remove void fields from a named tuple type, compacting both `t.n`
|
||||
## (the field symbol nodes) and `t.sonsImpl` (the child types).
|
||||
if t.n == nil: return # anonymous tuple, nothing to compact
|
||||
for i in 0..<t.kidsLen:
|
||||
if t.n[i].kind == nkRecList or t[i].kind == tyVoid:
|
||||
# found first void field, compact from here
|
||||
var pos = i
|
||||
for j in i+1..<t.kidsLen:
|
||||
if t[j].kind != tyVoid and j < t.n.len and t.n[j].kind != nkRecList:
|
||||
t.n[pos] = t.n[j]
|
||||
t[pos] = t[j]
|
||||
if t.n[pos].kind == nkSym:
|
||||
t.n[pos].sym.position = pos
|
||||
inc pos
|
||||
# else: skip void entries
|
||||
setLen t.n.sons, pos
|
||||
t.setSonsLen pos
|
||||
break
|
||||
|
||||
proc skipIntLiteralParams*(t: PType; idgen: IdGenerator) =
|
||||
for i, p in t.ikids:
|
||||
if p == nil: continue
|
||||
@@ -768,6 +788,8 @@ proc replaceTypeVarsTAux(cl: var TReplTypeVars, t: PType, isInstValue = false):
|
||||
propagateFieldFlags(result, result.n)
|
||||
if result.kind == tyObject and cl.c.computeRequiresInit(cl.c, result):
|
||||
result.incl tfRequiresInit
|
||||
if result.kind == tyTuple:
|
||||
eraseTupleVoidFields(result)
|
||||
|
||||
of tyProc:
|
||||
eraseVoidParams(result)
|
||||
|
||||
@@ -41,6 +41,7 @@ type
|
||||
CoType
|
||||
CoOwnerSig
|
||||
CoIgnoreRange
|
||||
CoIgnoreRangeInArray
|
||||
CoConsiderOwned
|
||||
CoDistinct
|
||||
CoHashTypeInsideNode
|
||||
@@ -220,10 +221,17 @@ proc hashType(c: var MD5Context, t: PType; flags: set[ConsiderFlag]; conf: Confi
|
||||
else:
|
||||
for a in t.kids: c.hashType a, flags+{CoIgnoreRange}, conf
|
||||
of tyRange:
|
||||
if CoIgnoreRange notin flags:
|
||||
if {CoIgnoreRange, CoIgnoreRangeInArray} * flags == {}:
|
||||
c &= char(t.kind)
|
||||
c.hashTree(t.n, {}, conf)
|
||||
c.hashType(t.elementType, flags, conf)
|
||||
c.hashType(t.elementType, flags, conf)
|
||||
elif CoIgnoreRangeInArray in flags:
|
||||
# include only the length of the range (not its specific bounds)
|
||||
c &= char(t.kind)
|
||||
let l = lengthOrd(conf, t)
|
||||
lowlevel l
|
||||
else:
|
||||
c.hashType(t.elementType, flags, conf)
|
||||
of tyStatic:
|
||||
c &= char(t.kind)
|
||||
c.hashTree(t.n, {}, conf)
|
||||
@@ -253,7 +261,7 @@ proc hashType(c: var MD5Context, t: PType; flags: set[ConsiderFlag]; conf: Confi
|
||||
if tfVarargs in t.flags: c &= ".varargs"
|
||||
of tyArray:
|
||||
c &= char(t.kind)
|
||||
c.hashType(t.indexType, flags-{CoIgnoreRange}, conf)
|
||||
c.hashType(t.indexType, flags-{CoIgnoreRange}+{CoIgnoreRangeInArray}, conf)
|
||||
c.hashType(t.elementType, flags-{CoIgnoreRange}, conf)
|
||||
else:
|
||||
c &= char(t.kind)
|
||||
|
||||
@@ -160,8 +160,7 @@ proc matchGenericParam(m: var TCandidate, formal: PType, n: PNode) =
|
||||
arg = newTypeS(tyStatic, m.c, son = evaluated.typ)
|
||||
arg.n = evaluated
|
||||
elif formalBase.kind == tyTypeDesc:
|
||||
if arg.kind != tyTypeDesc:
|
||||
arg = makeTypeDesc(m.c, arg)
|
||||
discard # if arg is not tyTypeDesc, typeRel will report the mismatch
|
||||
else:
|
||||
arg = arg.skipTypes({tyTypeDesc})
|
||||
let tm = typeRel(m, formal, arg)
|
||||
@@ -2186,9 +2185,9 @@ proc implicitConv(kind: TNodeKind, f: PType, arg: PNode, m: TCandidate,
|
||||
result.typ = errorType(c)
|
||||
else:
|
||||
result.typ = f.skipTypes({tySink})
|
||||
# keep varness
|
||||
# keep varness, but don't wrap lent types with var
|
||||
if arg.typ != nil and arg.typ.kind == tyVar:
|
||||
result.typ = toVar(result.typ, tyVar, c.idgen)
|
||||
result.typ = toVar(result.typ.skipTypes({tyLent}), tyVar, c.idgen)
|
||||
# copy the tfVarIsPtr flag
|
||||
result.typ.flags = arg.typ.flags
|
||||
else:
|
||||
|
||||
@@ -62,7 +62,10 @@ proc objectNode(cache: IdentCache; n: PNode; idgen: IdGenerator): PNode =
|
||||
result = newNodeI(nkIdentDefs, n.info)
|
||||
result.add n # name
|
||||
result.add mapTypeToAstX(cache, n.sym.typ, n.info, idgen, true, false) # type
|
||||
result.add newNodeI(nkEmpty, n.info) # no assigned value
|
||||
if n.sym.ast != nil:
|
||||
result.add copyTree(n.sym.ast)
|
||||
else:
|
||||
result.add newNodeI(nkEmpty, n.info) # no assigned value
|
||||
else:
|
||||
result = copyNode(n)
|
||||
for i in 0..<n.safeLen:
|
||||
@@ -87,7 +90,10 @@ proc mapTypeToAstX(cache: IdentCache; t: PType; info: TLineInfo;
|
||||
var id = newNodeX(nkIdentDefs)
|
||||
id.add n # name
|
||||
id.add mapTypeToAst(t, info) # type
|
||||
id.add newNodeI(nkEmpty, info) # no assigned value
|
||||
if n.sym.ast != nil:
|
||||
id.add copyTree(n.sym.ast)
|
||||
else:
|
||||
id.add newNodeI(nkEmpty, n.info) # no assigned value
|
||||
id
|
||||
template newIdentDefs(s): untyped = newIdentDefs(s, s.typ)
|
||||
|
||||
|
||||
@@ -1144,6 +1144,8 @@ semantic analysis). Assignments from the base type to one of its subrange types
|
||||
A subrange type has the same size as its base type (`int` in the
|
||||
Subrange example).
|
||||
|
||||
Implicit "downsizing" conversions to range types (for example, `int -> range[0..255]` or `range[1..256] -> range[0..255]`) emit the `ImplicitRangeConversion` warning. Conversions that are clearly safe (for example, `range[0..255] -> range[0..65535]`) and any explicit casts do not trigger this warning. Conversions from `int` to common subranges such as `Natural` or `Positive` do not trigger this warning by default, but can be enabled with `--warning:systemRangeConversion`.
|
||||
|
||||
|
||||
Pre-defined floating-point types
|
||||
--------------------------------
|
||||
@@ -7904,7 +7906,7 @@ alignment requirement of the type are ignored.
|
||||
main()
|
||||
```
|
||||
|
||||
This pragma has no effect on the JS backend.
|
||||
This pragma has no effect on the JavaScript backend and may significantly increase memory usage with the `--mm:refc` option.
|
||||
|
||||
|
||||
Noalias pragma
|
||||
|
||||
@@ -2127,7 +2127,7 @@ can be used in an `isolate` context:
|
||||
`=destroy`(dest.value)
|
||||
```
|
||||
|
||||
The `.sendable` pragma itself is an experimenal, unchecked, unsafe annotation. It is
|
||||
The `.sendable` pragma itself is an experimental, unchecked, unsafe annotation. It is
|
||||
currently only used by `Isolated[T]`.
|
||||
|
||||
Virtual pragma
|
||||
|
||||
@@ -276,9 +276,9 @@ This parser has 2 modes for inline markup:
|
||||
|
||||
2) Compatibility mode which is RST rules.
|
||||
|
||||
.. Note:: in both modes the parser interpretes text between single
|
||||
.. Note:: in both modes the parser interprets text between single
|
||||
backticks (code) identically:
|
||||
backslash does not escape; the only exception: ``\`` folowed by `
|
||||
backslash does not escape; the only exception: ``\`` followed by `
|
||||
does escape so that we can always input a single backtick ` in
|
||||
inline code. However that makes impossible to input code with
|
||||
``\`` at the end in *single* backticks, one must use *double*
|
||||
|
||||
@@ -123,7 +123,6 @@ Modified by Boyd Greenfield and narimiran
|
||||
}
|
||||
|
||||
html {
|
||||
overflow-x: hidden;
|
||||
max-width: 100%;
|
||||
box-sizing: border-box;
|
||||
font-size: 100%;
|
||||
@@ -572,8 +571,8 @@ blockquote.markdown-quote {
|
||||
padding-left: 3px;
|
||||
padding-right: 3px;
|
||||
border-radius: 4px;
|
||||
white-space: normal;
|
||||
word-break: break-all;
|
||||
white-space: pre-wrap;
|
||||
overflow-wrap: break-word;
|
||||
}
|
||||
|
||||
span.tok {
|
||||
@@ -674,6 +673,8 @@ table {
|
||||
border-collapse: collapse;
|
||||
border-color: var(--third-background);
|
||||
border-spacing: 0;
|
||||
display: block;
|
||||
overflow-x: auto;
|
||||
}
|
||||
|
||||
table:not(.line-nums-table) {
|
||||
|
||||
@@ -52,7 +52,7 @@ Options:
|
||||
nimgrep --filenames # In current dir
|
||||
nimgrep --filenames "" DIRECTORY
|
||||
# Note empty pattern "", lists all files in DIRECTORY
|
||||
* Interprete patterns:
|
||||
* Interpret patterns:
|
||||
--peg PATTERN and PAT are Peg
|
||||
--re PATTERN and PAT are regular expressions (default)
|
||||
--rex, -x use the "extended" syntax for the regular expression
|
||||
|
||||
@@ -27,7 +27,7 @@ Nim runs on a wide variety of platforms. Support on amd64 and i386 is tested reg
|
||||
- ppc64el (aka ppc64le)
|
||||
- riscv64
|
||||
|
||||
The following platforms are seldomly tested:
|
||||
The following platforms are rarely tested:
|
||||
|
||||
- alpha
|
||||
- hppa
|
||||
|
||||
@@ -20,8 +20,8 @@ notation meaning
|
||||
as they succeed. Indicate success if all succeeded.
|
||||
Otherwise, do not consume any text and indicate failure.
|
||||
The sequence's precedence is higher than that of ordered
|
||||
choice: ``A B / C`` means ``(A B) / Z`` and
|
||||
not ``A (B / Z)``.
|
||||
choice: ``A B / C`` means ``(A B) / C`` and
|
||||
not ``A (B / C)``.
|
||||
``(E)`` Grouping: Parenthesis can be used to change
|
||||
operator priority.
|
||||
``{E}`` Capture: Apply expression `E` and store the substring
|
||||
|
||||
@@ -1159,8 +1159,8 @@ In Nim new types can be defined within a `type` statement:
|
||||
|
||||
```nim test = "nim c $1"
|
||||
type
|
||||
biggestInt = int64 # biggest integer type that is available
|
||||
biggestFloat = float64 # biggest float type that is available
|
||||
BiggestInt = int64 # biggest integer type that is available
|
||||
BiggestFloat = float64 # biggest float type that is available
|
||||
```
|
||||
|
||||
Enumeration and object types may only be defined within a
|
||||
|
||||
6
koch.nim
6
koch.nim
@@ -11,10 +11,10 @@
|
||||
|
||||
const
|
||||
# examples of possible values for repos: Head, ea82b54
|
||||
NimbleStableCommit = "9207e8b2bbdf66b5a4d1020214cff44d2d30df92" # 0.20.1
|
||||
AtlasStableCommit = "2aa62121b40d580aa2fb27920a37b938d36c5f57" # 0.9.4
|
||||
NimbleStableCommit = "aa03f886e4a111d6af9090c6a1f1271d64b66f7b" # 0.22.2
|
||||
AtlasStableCommit = "ff1f4289482dce94ba9f95b3b0ae16d16e21eb3d" # 0.10.1
|
||||
ChecksumsStableCommit = "0b8e46379c5bc1bf73d8b3011908389c60fb9b98" # 2.0.1
|
||||
SatStableCommit = "faf1617f44d7632ee9601ebc13887644925dcc01"
|
||||
SatStableCommit = "e63eaea8baf00bed8bcd5a29ffd8823abb265b39"
|
||||
|
||||
NimonyStableCommit = "deb9b50c573fb55e071825ab55385e293b7216d5" # unversioned \
|
||||
# Note that Nimony uses Nim as a git submodule but we don't want to install
|
||||
|
||||
@@ -433,15 +433,15 @@ when defined(nimHasNoReturnError):
|
||||
else:
|
||||
{.pragma: errorNoReturn.}
|
||||
|
||||
proc error*(msg: string, n: NimNode = nil) {.magic: "NError", benign, errorNoReturn.}
|
||||
proc error*(msg: string, n: NimNode = nil) {.magic: "NError", gcsafe, errorNoReturn.}
|
||||
## Writes an error message at compile time. The optional `n: NimNode`
|
||||
## parameter is used as the source for file and line number information in
|
||||
## the compilation error message.
|
||||
|
||||
proc warning*(msg: string, n: NimNode = nil) {.magic: "NWarning", benign.}
|
||||
proc warning*(msg: string, n: NimNode = nil) {.magic: "NWarning", gcsafe.}
|
||||
## Writes a warning message at compile time.
|
||||
|
||||
proc hint*(msg: string, n: NimNode = nil) {.magic: "NHint", benign.}
|
||||
proc hint*(msg: string, n: NimNode = nil) {.magic: "NHint", gcsafe.}
|
||||
## Writes a hint message at compile time.
|
||||
|
||||
proc newStrLitNode*(s: string): NimNode {.noSideEffect.} =
|
||||
@@ -511,7 +511,7 @@ proc genSym*(kind: NimSymKind = nskLet; ident = ""): NimNode {.
|
||||
## Generates a fresh symbol that is guaranteed to be unique. The symbol
|
||||
## needs to occur in a declaration context.
|
||||
|
||||
proc callsite*(): NimNode {.magic: "NCallSite", benign, deprecated:
|
||||
proc callsite*(): NimNode {.magic: "NCallSite", gcsafe, deprecated:
|
||||
"Deprecated since v0.18.1; use `varargs[untyped]` in the macro prototype instead".}
|
||||
## Returns the AST of the invocation expression that invoked this macro.
|
||||
# see https://github.com/nim-lang/RFCs/issues/387 as candidate replacement.
|
||||
@@ -933,7 +933,7 @@ proc eqIdent*(a: NimNode; b: NimNode): bool {.magic: "EqIdent", noSideEffect.}
|
||||
|
||||
const collapseSymChoice = not defined(nimLegacyMacrosCollapseSymChoice)
|
||||
|
||||
proc treeTraverse(n: NimNode; res: var string; level = 0; isLisp = false, indented = false) {.benign.} =
|
||||
proc treeTraverse(n: NimNode; res: var string; level = 0; isLisp = false, indented = false) {.gcsafe.} =
|
||||
if level > 0:
|
||||
if indented:
|
||||
res.add("\n")
|
||||
@@ -982,21 +982,21 @@ proc treeTraverse(n: NimNode; res: var string; level = 0; isLisp = false, indent
|
||||
if isLisp:
|
||||
res.add(")")
|
||||
|
||||
proc treeRepr*(n: NimNode): string {.benign.} =
|
||||
proc treeRepr*(n: NimNode): string {.gcsafe.} =
|
||||
## Convert the AST `n` to a human-readable tree-like string.
|
||||
##
|
||||
## See also `repr`, `lispRepr`_, and `astGenRepr`_.
|
||||
result = ""
|
||||
n.treeTraverse(result, isLisp = false, indented = true)
|
||||
|
||||
proc lispRepr*(n: NimNode; indented = false): string {.benign.} =
|
||||
proc lispRepr*(n: NimNode; indented = false): string {.gcsafe.} =
|
||||
## Convert the AST `n` to a human-readable lisp-like string.
|
||||
##
|
||||
## See also `repr`, `treeRepr`_, and `astGenRepr`_.
|
||||
result = ""
|
||||
n.treeTraverse(result, isLisp = true, indented = indented)
|
||||
|
||||
proc astGenRepr*(n: NimNode): string {.benign.} =
|
||||
proc astGenRepr*(n: NimNode): string {.gcsafe.} =
|
||||
## Convert the AST `n` to the code required to generate that AST.
|
||||
##
|
||||
## See also `repr`_, `treeRepr`_, and `lispRepr`_.
|
||||
@@ -1005,7 +1005,7 @@ proc astGenRepr*(n: NimNode): string {.benign.} =
|
||||
NodeKinds = {nnkEmpty, nnkIdent, nnkSym, nnkNone, nnkCommentStmt}
|
||||
LitKinds = {nnkCharLit..nnkInt64Lit, nnkFloatLit..nnkFloat64Lit, nnkStrLit..nnkTripleStrLit}
|
||||
|
||||
proc traverse(res: var string, level: int, n: NimNode) {.benign.} =
|
||||
proc traverse(res: var string, level: int, n: NimNode) {.gcsafe.} =
|
||||
for i in 0..level-1: res.add " "
|
||||
if n.kind in NodeKinds:
|
||||
res.add("new" & ($n.kind).substr(3) & "Node(")
|
||||
|
||||
@@ -15,6 +15,9 @@ template formatStr*(howExpr, namegetter, idgetter): untyped =
|
||||
val.add(how[i])
|
||||
i += 1
|
||||
else:
|
||||
if i + 1 >= how.len:
|
||||
raise newException(ValueError, "Syntax error in format string at " & $i)
|
||||
|
||||
if how[i + 1] == '$':
|
||||
val.add('$')
|
||||
i += 2
|
||||
@@ -27,7 +30,7 @@ template formatStr*(howExpr, namegetter, idgetter): untyped =
|
||||
i += 1
|
||||
var id {.inject.} = 0
|
||||
while i < how.len and how[i] in {'0'..'9'}:
|
||||
id += (id * 10) + (ord(how[i]) - ord('0'))
|
||||
id = (id * 10) + (ord(how[i]) - ord('0'))
|
||||
i += 1
|
||||
val.add(idgetter)
|
||||
lastNum = id + 1
|
||||
@@ -44,6 +47,8 @@ template formatStr*(howExpr, namegetter, idgetter): untyped =
|
||||
while i < how.len and how[i] != '}':
|
||||
name.add(how[i])
|
||||
i += 1
|
||||
if i >= how.len or how[i] != '}':
|
||||
raise newException(ValueError, "Syntax error in format string at " & $i)
|
||||
i += 1
|
||||
val.add(namegetter)
|
||||
else:
|
||||
|
||||
@@ -326,7 +326,8 @@ proc nimNextToken(g: var GeneralTokenizer, keywords: openArray[string] = @[]) =
|
||||
pos = nimNumber(g, pos)
|
||||
of '\'':
|
||||
inc(pos)
|
||||
if g.kind != gtPunctuation:
|
||||
let followsBacktick = pos >= 2 and g.buf[pos - 2] == '`'
|
||||
if not followsBacktick:
|
||||
g.kind = gtCharLit
|
||||
while true:
|
||||
case g.buf[pos]
|
||||
@@ -338,6 +339,8 @@ proc nimNextToken(g: var GeneralTokenizer, keywords: openArray[string] = @[]) =
|
||||
of '\\':
|
||||
inc(pos, 2)
|
||||
else: inc(pos)
|
||||
else:
|
||||
g.kind = gtPunctuation
|
||||
of '\"':
|
||||
inc(pos)
|
||||
if (g.buf[pos] == '\"') and (g.buf[pos + 1] == '\"'):
|
||||
|
||||
@@ -1946,9 +1946,14 @@ proc withTimeout*[T](fut: Future[T], timeout: int): owned(Future[bool]) =
|
||||
retFuture.fail(fut.error)
|
||||
else:
|
||||
retFuture.complete(true)
|
||||
# Timeout side lost; drop its callback to avoid retaining closures/futures.
|
||||
timeoutFuture.clearCallbacks()
|
||||
timeoutFuture.callback =
|
||||
proc () =
|
||||
if not retFuture.finished: retFuture.complete(false)
|
||||
if not retFuture.finished:
|
||||
retFuture.complete(false)
|
||||
# Wrapped future side lost; drop its callback to avoid retaining closures/futures.
|
||||
fut.clearCallbacks()
|
||||
return retFuture
|
||||
|
||||
proc accept*(socket: AsyncFD,
|
||||
|
||||
@@ -255,6 +255,9 @@ proc decode*(s: string): string =
|
||||
while inputIndex <= inputEnds:
|
||||
while s[inputIndex] in {'\n', '\r', ' '}:
|
||||
inc inputIndex
|
||||
# double check inputIndex as it can be incremented due to whitespace
|
||||
if inputIndex > inputEnds:
|
||||
break
|
||||
inputChar(a)
|
||||
inputChar(b)
|
||||
inputChar(c)
|
||||
|
||||
@@ -14,149 +14,306 @@
|
||||
## Supported Syntax
|
||||
## ================
|
||||
##
|
||||
## The following syntax is supported when arguments for the `shortNoVal` and
|
||||
## `longNoVal` parameters, which are
|
||||
## `described later<#nimshortnoval-and-nimlongnoval>`_, are not provided:
|
||||
## The syntax described here applies to the default way the parser works.
|
||||
## The behavior is configurable, though, and two additional modes
|
||||
## are supported, see the details: `Parser Modes`_.
|
||||
##
|
||||
## 1. Short options: `-abcd`, `-e:5`, `-e=5`
|
||||
## Parsing also depends on whether the `shortNoVal` and `longNoVal` parameters
|
||||
## are omitted/empty or provided. The details are described in a
|
||||
## `later section<#nimshortnoval-and-nimlongnoval>`_.
|
||||
##
|
||||
## The following syntax is supported:
|
||||
##
|
||||
## 1. Short options: `-a:5`, `-b=5`, `-cde`, `-fgh=5`
|
||||
## 2. Long options: `--foo:bar`, `--foo=bar`, `--foo`
|
||||
## 3. Arguments: everything that does not start with a `-`
|
||||
##
|
||||
## These three kinds of tokens are enumerated in the
|
||||
## `CmdLineKind enum<#CmdLineKind>`_.
|
||||
## Passing values to options **requires** a separator (`:`/`=`), short options
|
||||
## (flags) can be bundled together and the last one can take a value.
|
||||
##
|
||||
## When option values begin with ':' or '=', they need to be doubled up (as in
|
||||
## `--delim::`) or alternated (as in `--delim=:`).
|
||||
## Option values can begin with the separator character (`:`/`=`), so all of the
|
||||
## following is valid:
|
||||
## - option `foo`, value `:`: `--foo::`, `--foo=:`
|
||||
## - option `foo`, value `=`: `--foo:=`, `--foo==`
|
||||
##
|
||||
## The `--` option, commonly used to denote that every token that follows is
|
||||
## an argument, is interpreted as a long option, and its name is the empty
|
||||
## string.
|
||||
## string. Trailing arguments can be accessed with `remainingArgs<#remainingArgs,OptParser>`_
|
||||
## or `cmdLineRest<#cmdLineRest,OptParser>`_.
|
||||
##
|
||||
## Parsing
|
||||
## =======
|
||||
##
|
||||
## Use an `OptParser<#OptParser>`_ to parse command line options. It can be
|
||||
## created with `initOptParser<#initOptParser,string,set[char],seq[string]>`_,
|
||||
## and `next<#next,OptParser>`_ advances the parser by one token.
|
||||
## To parse command line options, use the `getopt iterator<#getopt.i,OptParser>`_.
|
||||
## It initializes the `OptParser<#OptParser>`_ object internally and iterates
|
||||
## through the command line options.
|
||||
##
|
||||
## For each token, the parser's `kind`, `key`, and `val` fields give
|
||||
## information about that token. If the token is a long or short option, `key`
|
||||
## is the option's name, and `val` is either the option's value, if provided,
|
||||
## or the empty string. For arguments, the `key` field contains the argument
|
||||
## itself, and `val` is unused. To check if the end of the command line has
|
||||
## been reached, check if `kind` is equal to `cmdEnd`.
|
||||
## For each token, the parser's `kind` (`CmdLineKind enum<#CmdLineKind>`_.),
|
||||
## `key`, and `val` fields are yielded.
|
||||
##
|
||||
## For long and short options, `key` is the option's name, and `val` is either
|
||||
## the option's value, if given, or an empty string. For arguments, the `key`
|
||||
## field contains the argument itself, and `val` is unused (empty).
|
||||
##
|
||||
## Here is an example:
|
||||
##
|
||||
## ```Nim
|
||||
## import std/parseopt
|
||||
runnableExamples:
|
||||
import std/os
|
||||
|
||||
let cmds = "-ab -e:5 --foo --bar=20 file.txt".parseCmdLine()
|
||||
var output: seq[string] = @[]
|
||||
# If cmds is not supplied, real arguments will be retrieved by the `os` module
|
||||
for kind, key, val in getopt(cmds):
|
||||
case kind
|
||||
of cmdEnd: break
|
||||
of cmdShortOption, cmdLongOption:
|
||||
if val == "":
|
||||
output.add("Option: " & key)
|
||||
else:
|
||||
output.add("Option and value: " & key & ", " & val)
|
||||
of cmdArgument:
|
||||
output.add("Argument: " & key)
|
||||
|
||||
doAssert output == @[
|
||||
"Option: a",
|
||||
"Option: b",
|
||||
"Option and value: e, 5",
|
||||
"Option: foo",
|
||||
"Option and value: bar, 20",
|
||||
"Argument: file.txt"
|
||||
]
|
||||
##
|
||||
## var p = initOptParser("-ab -e:5 --foo --bar=20 file.txt")
|
||||
## while true:
|
||||
## p.next()
|
||||
## case p.kind
|
||||
## of cmdEnd: break
|
||||
## of cmdShortOption, cmdLongOption:
|
||||
## if p.val == "":
|
||||
## echo "Option: ", p.key
|
||||
## else:
|
||||
## echo "Option and value: ", p.key, ", ", p.val
|
||||
## of cmdArgument:
|
||||
## echo "Argument: ", p.key
|
||||
## The `OptParser<#OptParser>`_ can be initialized with
|
||||
## `initOptParser<#initOptParser,string,set[char],seq[string]>`_.
|
||||
## The `next<#next,OptParser>`_ proc advances the parser by one token.
|
||||
##
|
||||
## # Output:
|
||||
## # Option: a
|
||||
## # Option: b
|
||||
## # Option and value: e, 5
|
||||
## # Option: foo
|
||||
## # Option and value: bar, 20
|
||||
## # Argument: file.txt
|
||||
## ```
|
||||
## When iterating the object manually with `next<#next,OptParser>`_, reaching
|
||||
## the end of the command line is signalled by setting the `kind` field
|
||||
## to `cmdEnd`.
|
||||
##
|
||||
## The `getopt iterator<#getopt.i,OptParser>`_, which is provided for
|
||||
## convenience, can be used to iterate through all command line options as well.
|
||||
## To set a default value for an option, assign the default value to a variable
|
||||
## beforehand, then update it while parsing.
|
||||
##
|
||||
## To set a default value for a variable assigned through `getopt` and accept arguments from the cmd line.
|
||||
## Assign the default value to a variable before parsing.
|
||||
## Then set the variable to the new value while parsing.
|
||||
##
|
||||
## Here is an example:
|
||||
##
|
||||
## ```Nim
|
||||
## import std/parseopt
|
||||
##
|
||||
## var varName: string = "defaultValue"
|
||||
##
|
||||
## for kind, key, val in getopt():
|
||||
## case kind
|
||||
## of cmdArgument:
|
||||
## discard
|
||||
## of cmdLongOption, cmdShortOption:
|
||||
## case key:
|
||||
## of "varName": # --varName:<value> in the console when executing
|
||||
## varName = val # do input sanitization in production systems
|
||||
## of cmdEnd:
|
||||
## discard
|
||||
## ```
|
||||
runnableExamples:
|
||||
import std/strutils
|
||||
|
||||
var varName: string = "defaultValue"
|
||||
|
||||
for kind, key, val in getopt(@["--varName:HELLO"]):
|
||||
case kind
|
||||
of cmdArgument:
|
||||
discard
|
||||
of cmdLongOption, cmdShortOption:
|
||||
case key
|
||||
of "varName": # --varName:<value> in the console when executing
|
||||
varName = val.toLowerAscii() # do input sanitization in production
|
||||
of cmdEnd:
|
||||
discard
|
||||
|
||||
doAssert varName == "hello"
|
||||
##
|
||||
## `shortNoVal` and `longNoVal`
|
||||
## ============================
|
||||
##
|
||||
## The optional `shortNoVal` and `longNoVal` parameters present in
|
||||
## `initOptParser<#initOptParser,string,set[char],seq[string]>`_ are for
|
||||
## The optional `shortNoVal` and `longNoVal` parameters in
|
||||
## `initOptParser<#initOptParser,string,set[char],seq[string]>`_ and
|
||||
## `getopt iterator<#getopt.i,OptParser>`_ are for
|
||||
## specifying which short and long options do not accept values.
|
||||
##
|
||||
## When `shortNoVal` is non-empty, users are not required to separate short
|
||||
## options and their values with a ':' or '=' since the parser knows which
|
||||
## options accept values and which ones do not. This behavior also applies for
|
||||
## long options if `longNoVal` is non-empty. For short options, `-j4`
|
||||
## becomes supported syntax, and for long options, `--foo bar` becomes
|
||||
## supported. This is in addition to the `previously mentioned
|
||||
## syntax<#supported-syntax>`_. Users can still separate options and their
|
||||
## values with ':' or '=', but that becomes optional.
|
||||
## When `shortNoVal` or `longNoVal` is non-empty, using the separators (`:`/`=`)
|
||||
## becomes non-mandatory and users can separate a value from long
|
||||
## options (that are not supplied to the corresponding argument) by whitespace
|
||||
## or, in the case of a short option, by writing the value directly adjacent to
|
||||
## the option.
|
||||
##
|
||||
## For short options, `-j4` becomes supported syntax (parsed as option `j` with
|
||||
## value `4` instead of two separate options `j` and `4`). For long options,
|
||||
## `--foo bar` becomes supported syntax in all `modes<Parser Modes>`_.
|
||||
##
|
||||
## In `LaxMode` and `GnuMode`, short options can also take values from the next
|
||||
## argument (`-c val`), but this does **not** work in the default `Nim` mode.
|
||||
##
|
||||
## As more options which do not accept values are added to your program,
|
||||
## remember to amend `shortNoVal` and `longNoVal` accordingly.
|
||||
##
|
||||
## The parser does not validate the input for syntax mistakes, thus, options
|
||||
## can still have values if passed explicitly by the user, even when they are
|
||||
## marked as `shortNoVal`/`longNoVal`.
|
||||
##
|
||||
## This behavior allows associating an option with the mistakenly passed value:
|
||||
##
|
||||
runnableExamples:
|
||||
import std/[sequtils, os]
|
||||
|
||||
let cmds = "-n:9 --foo:bar".parseCmdLine()
|
||||
let parsed = toSeq(cmds.getopt(shortNoVal = {'n'}, longNoVal = @["foo"]))
|
||||
for (kind, key, val) in parsed:
|
||||
case kind
|
||||
of cmdEnd: raise newException(AssertionDefect, "Unreachable")
|
||||
of cmdShortOption, cmdLongOption:
|
||||
if key in ["n", "foo"] and val != "":
|
||||
# Substitute for proper error handling in your code
|
||||
discard "Option " & key & " can't take values!"
|
||||
else: discard
|
||||
of cmdArgument: discard
|
||||
doAssert parsed == @[
|
||||
(cmdShortOption, "n", "9"),
|
||||
(cmdLongOption, "foo", "bar")]
|
||||
##
|
||||
## .. Important::
|
||||
## Next-argument value-taking for short/long options is only enabled when
|
||||
## `shortNoVal`/`longNoVal` are non-empty. If your program has *no* options
|
||||
## that take no value, you still must pass a non-empty placeholder (for example,
|
||||
## `shortNoVal = {'\0'}` and/or `longNoVal = @[""]`) to enable this form.
|
||||
##
|
||||
## The following example illustrates the difference between having an empty
|
||||
## `shortNoVal` and `longNoVal`, which is the default, and providing
|
||||
## arguments for those two parameters:
|
||||
##
|
||||
## ```Nim
|
||||
## import std/parseopt
|
||||
runnableExamples:
|
||||
|
||||
proc format(kind: CmdLineKind; key, val: string): string =
|
||||
case kind
|
||||
of cmdEnd: raise newException(AssertionDefect, "Unreachable")
|
||||
of cmdShortOption, cmdLongOption:
|
||||
if val == "": "Option: " & key
|
||||
else: "Option and value: " & key & ", " & val
|
||||
of cmdArgument: "Argument: " & key
|
||||
|
||||
let cmdLine = "-j4 --first bar"
|
||||
var output1, output2: seq[string] = @[]
|
||||
|
||||
var emptyNoVal = initOptParser(cmdLine)
|
||||
for kind, key, val in emptyNoVal.getopt():
|
||||
output1.add format(kind, key, val)
|
||||
|
||||
doAssert output1 == @[
|
||||
"Option: j",
|
||||
"Option: 4",
|
||||
"Option: first",
|
||||
"Argument: bar"
|
||||
]
|
||||
|
||||
var withNoVal = cmdLine.initOptParser(shortNoVal = {'c'},
|
||||
longNoVal = @["second"])
|
||||
for kind, key, val in withNoVal.getopt():
|
||||
output2.add format(kind, key, val)
|
||||
|
||||
doAssert output2 == @[
|
||||
"Option and value: j, 4",
|
||||
"Option and value: first, bar"
|
||||
]
|
||||
##
|
||||
## proc printToken(kind: CmdLineKind, key: string, val: string) =
|
||||
## case kind
|
||||
## of cmdEnd: doAssert(false) # Doesn't happen with getopt()
|
||||
## of cmdShortOption, cmdLongOption:
|
||||
## if val == "":
|
||||
## echo "Option: ", key
|
||||
## else:
|
||||
## echo "Option and value: ", key, ", ", val
|
||||
## of cmdArgument:
|
||||
## echo "Argument: ", key
|
||||
## Parser Modes
|
||||
## ============
|
||||
##
|
||||
## let cmdLine = "-j4 --first bar"
|
||||
## .. Warning:: Modes other than the default (`Nim`) are **experimental** and may
|
||||
## change in future releases.
|
||||
##
|
||||
## var emptyNoVal = initOptParser(cmdLine)
|
||||
## for kind, key, val in emptyNoVal.getopt():
|
||||
## printToken(kind, key, val)
|
||||
## The parser supports several distinct rule sets that change how options are
|
||||
## interpreted:
|
||||
##
|
||||
## # Output:
|
||||
## # Option: j
|
||||
## # Option: 4
|
||||
## # Option: first
|
||||
## # Argument: bar
|
||||
## 1. **LaxMode**: Most forgiving mode, combines `Nim` with POSIX-like
|
||||
## short option handling. Tries to follow the POSIX_ guidelines where possible.
|
||||
## 2. **NimMode**: Standard Nim parsing rules (default).
|
||||
## 3. **GnuMode**: GNU-inspired parsing (e.g. `=` as the only delimiter).
|
||||
## Puts some additional restrictions, following some of the GNU_ conventions.
|
||||
##
|
||||
## var withNoVal = initOptParser(cmdLine, shortNoVal = {'c'},
|
||||
## longNoVal = @["second"])
|
||||
## for kind, key, val in withNoVal.getopt():
|
||||
## printToken(kind, key, val)
|
||||
## Modes are ordered from most relaxed to strictest. The names were
|
||||
## chosen to set general user expectations and full compliance is neither
|
||||
## achieved nor planned.
|
||||
##
|
||||
## # Output:
|
||||
## # Option and value: j, 4
|
||||
## # Option and value: first, bar
|
||||
## ```
|
||||
## Mode Differences
|
||||
## ----------------
|
||||
##
|
||||
## **NimMode** (default):
|
||||
##
|
||||
## - Short options require adjacent values or explicit delimiters:
|
||||
## `-cval`, `-c:val`, `-c=val`
|
||||
## - Short options follow POSIX-style bundling rules
|
||||
## - Next-argument value taking (`-c val`) is **not** supported by default
|
||||
## - Supports both `:` and `=` as delimiters
|
||||
## - Allows whitespace around delimiters
|
||||
## - Values starting with `-` are interpreted as new options
|
||||
##
|
||||
## **LaxMode**:
|
||||
##
|
||||
## - Essentially the Nim mode with some relaxations for short options:
|
||||
## + Allows short options to take values from the next argument: `-c val`
|
||||
## + Supports bundled short options with trailing value: `-abc val`
|
||||
## - Values starting with `-` can be consumed as option arguments
|
||||
##
|
||||
## **GnuMode**:
|
||||
##
|
||||
## - Only `=` is treated as a delimiter (`:` is not a delimiter)
|
||||
## - No whitespace allowed around `=`
|
||||
## - Short options can take next-argument values (`-c val`), but only whitespace
|
||||
## is allowed as a delimiter, separators parse as part of the value
|
||||
## - Short options follow POSIX-style bundling rules
|
||||
## - Values starting with `-` can be consumed as option arguments
|
||||
## - Known discrepancies compared to GNU getopt:
|
||||
## + No notion of optional/mandatory arguments, colon (`:`) doesn't
|
||||
## indicate them and overall is not a special character.
|
||||
##
|
||||
## Mode-Specific Behavior
|
||||
## ----------------------
|
||||
##
|
||||
## The parser's behavior varies significantly between modes, particularly
|
||||
## around how options consume their values:
|
||||
##
|
||||
## **Short Options**
|
||||
##
|
||||
## Consider `-c val`:
|
||||
##
|
||||
## - In `Nim` mode: `-c` is parsed as an option without a value, and `val` is
|
||||
## parsed as a separate argument, regardless of `shortNoVal` being empty or not.
|
||||
## - In `Lax` and `Gnu` modes:
|
||||
## + When `shortNoVal` is empty, or not empty and `-c` is in it:
|
||||
## Same as `Nim`, parsed as option `-c` followed by argument `val`.
|
||||
## + When `-c` is not in `shortNoVal`:
|
||||
## parsed as option `-c`, `val` is consumed as its value.
|
||||
##
|
||||
## Consider `-c-10`:
|
||||
##
|
||||
## - If `shortNoVal` value is empty, all three modes parse three separate short
|
||||
## options: `c`, `1` and `0`.
|
||||
## - Otherwise, if `-c` is not in `shortNoVal`:
|
||||
## + `Nim`: `-c` is an option without an argument. `-10` is interpreted as a
|
||||
## an option `-1` with the `0` argument.
|
||||
## + `Lax` and `Gnu` modes: `-10` is consumed as the value of `-c`
|
||||
## (allowing negative number values).
|
||||
##
|
||||
## **Long Options**
|
||||
##
|
||||
## Consider `--foo:bar`:
|
||||
##
|
||||
## - `Nim`: `:` is a valid delimiter, so `bar` is the value of `--foo`.
|
||||
## - `LaxMode`: same as `Nim`.
|
||||
## - `Gnu`: only `=` is a valid delimiter, so this parses as an option named
|
||||
## `foo:bar` without a value (unless `longNoVal` is non-empty and allows
|
||||
## next-argument consumption).
|
||||
##
|
||||
## Consider `--foo =bar`:
|
||||
##
|
||||
## - `Nim`: whitespace around delimiters is allowed, so `=bar` is the
|
||||
## value of `--foo`.
|
||||
## - `LaxMode`: same as `Nim`.
|
||||
## - `Gnu`: whitespace around `=` is not allowed, so `--foo` is an
|
||||
## option without a value, and `=bar` is parsed as an argument.
|
||||
##
|
||||
## Custom Rule Sets
|
||||
## ================
|
||||
##
|
||||
## .. Warning:: Custom rule sets are unsupported and not tested
|
||||
##
|
||||
## If you require parsing rules beyond the three provided modes, it's possible
|
||||
## to define a custom parser behavior by specifying a set of individual parser
|
||||
## rules.
|
||||
##
|
||||
## Due to this feature being unsupported, it requires importing the private
|
||||
## symbols of the module (with `import std/parseopt {.all.}`) and utilizing
|
||||
## the unexported `initOptParser` overload, which accepts `set[ParserRules]`
|
||||
## (see the `ParserRules` enum in the code for details).
|
||||
##
|
||||
## See also
|
||||
## ========
|
||||
@@ -171,13 +328,42 @@
|
||||
## parser
|
||||
## * `parsexml module<parsexml.html>`_ for a XML / HTML parser
|
||||
## * `other parsers<lib.html#pure-libraries-parsers>`_ for more parsers
|
||||
## * POSIX_ - The Open Group Base Specifications Issue 8. Utility Conventions
|
||||
## * GNU_ - GNU C Library reference manual. 26.1.1 Program Argument Syntax Conventions
|
||||
##
|
||||
## .. _GNU: https://sourceware.org/glibc/manual/latest/html_node/Argument-Syntax.html
|
||||
## .. _POSIX: https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/V1_chap12.html
|
||||
|
||||
{.push debugger: off.}
|
||||
|
||||
include "system/inclrtl"
|
||||
|
||||
import std/strutils
|
||||
import std/os
|
||||
when defined(nimscript):
|
||||
from std/strutils import toLowerAscii, endsWith
|
||||
|
||||
type
|
||||
CliMode* = enum
|
||||
## Parser behavior profiles used to control parser behavior.
|
||||
## See `Parser Modes`_ for details.
|
||||
LaxMode, ## The most forgiving mode
|
||||
NimMode, ## Nim parsing rules (default)
|
||||
GnuMode ## GNU-style parsing
|
||||
|
||||
type
|
||||
ParserRules = enum
|
||||
## Feature flags used to assemble parser behavior for a given mode.
|
||||
prSepAllowDelimBefore, ## Allow whitespace before an opt-val separator
|
||||
prSepAllowDelimAfter, ## Allow whitespace after an opt-val separator
|
||||
prShortAllowSep, ## Allow `-k<separator>val` form
|
||||
prShortBundle, ## Allow bundling short options behind one '-'
|
||||
prShortValAllowAdjacent, ## Allow adjacent short option values: `-kval`
|
||||
prShortValAllowNextArg, ## Allow next-argv short option values: `-k val`
|
||||
prShortValAllowDashLeading, ## Allow values that start with '-' to be taken
|
||||
prLongAllowSep, ## Allow `--opt<separator>val` form
|
||||
prLongValAllowNextArg, ## Allow `--opt val` form, requires non-empty `longNoVal`
|
||||
prSepAllowColon, ## Allow `:` as an opt-val separator
|
||||
prSepAllowEq, ## Allow `=` as an opt-val separator
|
||||
|
||||
type
|
||||
CmdLineKind* = enum ## The detected command line token.
|
||||
@@ -189,21 +375,51 @@ type
|
||||
## Implementation of the command line parser.
|
||||
##
|
||||
## To initialize it, use the
|
||||
## `initOptParser proc<#initOptParser,string,set[char],seq[string]>`_.
|
||||
## `initOptParser proc<#initOptParser,string,set[char],seq[string],CliMode>`_.
|
||||
## `next<#next,OptParser>`_ is used to advance the parser state and move
|
||||
## through the parsed tokens.
|
||||
pos: int
|
||||
inShortState: bool
|
||||
allowWhitespaceAfterColon: bool
|
||||
shortNoVal: set[char]
|
||||
longNoVal: seq[string]
|
||||
cmds: seq[string]
|
||||
idx: int
|
||||
separators: set[char] ## Allowed separators for long/short option values
|
||||
rules: set[ParserRules]
|
||||
kind*: CmdLineKind ## The detected command line token
|
||||
key*, val*: string ## Key and value pair; the key is the option
|
||||
## or the argument, and the value is not "" if
|
||||
## the option was given a value
|
||||
|
||||
const DelimSet = {'\t', ' '} ## Allowed delimiters between tokens
|
||||
|
||||
func toRules(m: CliMode): set[ParserRules] =
|
||||
## Default rule sets for the given mode `m`
|
||||
let
|
||||
Common = {
|
||||
prSepAllowEq,
|
||||
prShortValAllowAdjacent,
|
||||
prShortBundle,
|
||||
prLongValAllowNextArg,
|
||||
prLongAllowSep,
|
||||
}
|
||||
Lax = {
|
||||
prSepAllowColon,
|
||||
prSepAllowDelimBefore,
|
||||
prSepAllowDelimAfter,
|
||||
prShortAllowSep,
|
||||
}
|
||||
ShortPosix = {
|
||||
prShortValAllowNextArg,
|
||||
prShortValAllowDashLeading,
|
||||
}
|
||||
case m
|
||||
of LaxMode: Common + Lax + ShortPosix
|
||||
of NimMode: Common + Lax
|
||||
of GnuMode: Common + ShortPosix
|
||||
|
||||
proc parseWord(s: string, i: int, w: var string,
|
||||
delim: set[char] = {'\t', ' '}): int =
|
||||
delim: set[char] = DelimSet): int =
|
||||
result = i
|
||||
if result < s.len and s[result] == '\"':
|
||||
inc(result)
|
||||
@@ -218,34 +434,23 @@ proc parseWord(s: string, i: int, w: var string,
|
||||
add(w, s[result])
|
||||
inc(result)
|
||||
|
||||
proc initOptParser*(cmdline: seq[string], shortNoVal: set[char] = {},
|
||||
longNoVal: seq[string] = @[];
|
||||
allowWhitespaceAfterColon = true): OptParser =
|
||||
## Initializes the command line parser.
|
||||
##
|
||||
## If `cmdline.len == 0`, the real command line as provided by the
|
||||
## `os` module is retrieved instead if it is available. If the
|
||||
## command line is not available, a `ValueError` will be raised.
|
||||
## Behavior of the other parameters remains the same as in
|
||||
## `initOptParser(string, ...)
|
||||
## <#initOptParser,string,set[char],seq[string]>`_.
|
||||
##
|
||||
## See also:
|
||||
## * `getopt iterator<#getopt.i,seq[string],set[char],seq[string]>`_
|
||||
runnableExamples:
|
||||
var p = initOptParser()
|
||||
p = initOptParser(@["--left", "--debug:3", "-l", "-r:2"])
|
||||
p = initOptParser(@["--left", "--debug:3", "-l", "-r:2"],
|
||||
shortNoVal = {'l'}, longNoVal = @["left"])
|
||||
result = OptParser(pos: 0, idx: 0, inShortState: false,
|
||||
shortNoVal: shortNoVal, longNoVal: longNoVal,
|
||||
allowWhitespaceAfterColon: allowWhitespaceAfterColon
|
||||
proc initOptParser(cmdline: openArray[string];
|
||||
shortNoVal: set[char];
|
||||
longNoVal: seq[string];
|
||||
rules: set[ParserRules]): OptParser =
|
||||
result = OptParser(pos: 0, idx: 0,
|
||||
cmds: @cmdline,
|
||||
inShortState: false,
|
||||
shortNoVal: shortNoVal,
|
||||
longNoVal: longNoVal,
|
||||
separators: {},
|
||||
rules: rules,
|
||||
kind: cmdEnd,
|
||||
key: "", val: "",
|
||||
)
|
||||
if cmdline.len != 0:
|
||||
result.cmds = newSeq[string](cmdline.len)
|
||||
for i in 0..<cmdline.len:
|
||||
result.cmds[i] = cmdline[i]
|
||||
else:
|
||||
if prSepAllowEq in rules: result.separators.incl('=')
|
||||
if prSepAllowColon in rules: result.separators.incl(':')
|
||||
if cmdline.len == 0:
|
||||
when declared(paramCount):
|
||||
when defined(nimscript):
|
||||
var ctr = 0
|
||||
@@ -254,7 +459,7 @@ proc initOptParser*(cmdline: seq[string], shortNoVal: set[char] = {},
|
||||
if firstNimsFound:
|
||||
result.cmds[ctr] = paramStr(i)
|
||||
inc ctr, 1
|
||||
if paramStr(i).endsWith(".nims") and not firstNimsFound:
|
||||
if paramStr(i).toLowerAscii().endsWith(".nims") and not firstNimsFound:
|
||||
firstNimsFound = true
|
||||
result.cmds = newSeq[string](paramCount()-i)
|
||||
else:
|
||||
@@ -266,25 +471,73 @@ proc initOptParser*(cmdline: seq[string], shortNoVal: set[char] = {},
|
||||
# access the command line arguments then!
|
||||
raiseAssert "empty command line given but" &
|
||||
" real command line is not accessible"
|
||||
result.kind = cmdEnd
|
||||
result.key = ""
|
||||
result.val = ""
|
||||
|
||||
proc initOptParser*(cmdline = "", shortNoVal: set[char] = {},
|
||||
proc initOptParser*(cmdline: seq[string];
|
||||
shortNoVal: set[char] = {};
|
||||
longNoVal: seq[string] = @[];
|
||||
allowWhitespaceAfterColon = true): OptParser =
|
||||
mode: CliMode = NimMode): OptParser =
|
||||
## Initializes the command line parser.
|
||||
##
|
||||
## If `cmdline == ""`, the real command line as provided by the
|
||||
## `os` module is retrieved instead if it is available. If the
|
||||
## command line is not available, a `ValueError` will be raised.
|
||||
## **Parameters:**
|
||||
##
|
||||
## `shortNoVal` and `longNoVal` are used to specify which options
|
||||
## do not take values. See the `documentation about these
|
||||
## parameters<#nimshortnoval-and-nimlongnoval>`_ for more information on
|
||||
## how this affects parsing.
|
||||
## - `cmdline`: Sequence of command line arguments to parse. If empty, the
|
||||
## real command line as provided by the `os` module is retrieved instead.
|
||||
## If the command line is not available, an assertion will be raised.
|
||||
## - `shortNoVal`: Set of short option characters that do not accept values.
|
||||
## See `shortNoVal and longNoVal<#nimshortnoval-and-nimlongnoval>`_ for details.
|
||||
## - `longNoVal`: Sequence of long option names that do not accept values.
|
||||
## See `shortNoVal and longNoVal<#nimshortnoval-and-nimlongnoval>`_ for details.
|
||||
## - `mode`: Parser behavior profile (`NimMode`, `LaxMode`, or `GnuMode`).
|
||||
## See `Parser Modes`_ for details.
|
||||
##
|
||||
## This does not provide a way of passing default values to arguments.
|
||||
## See also:
|
||||
## * `getopt iterator<#getopt.i,seq[string],set[char],seq[string],CliMode>`_
|
||||
runnableExamples:
|
||||
var p = initOptParser()
|
||||
p = initOptParser(@["--left", "--debug:3", "-l", "-r:2"])
|
||||
p = initOptParser(@["--left", "--debug:3", "-l", "-r:2"],
|
||||
shortNoVal = {'l'}, longNoVal = @["left"])
|
||||
initOptParser(cmdline, shortNoVal, longNoVal, toRules(mode))
|
||||
|
||||
proc initOptParser*(cmdline: seq[string],
|
||||
shortNoVal: set[char] = {},
|
||||
longNoVal: seq[string] = @[];
|
||||
allowWhitespaceAfterColon: bool): OptParser {.deprecated:
|
||||
"`allowWhitespaceAfterColon` is deprecated, use parser modes instead".} =
|
||||
## This is an overload for continued support of the legacy `allowWhitespaceAfterColon`
|
||||
## option. It modifies the default parser mode so that the passed value is respected.
|
||||
##
|
||||
## Current default parser mode behaves as if `true` was passed (old default)
|
||||
##
|
||||
## - `allowWhitespaceAfterColon`: When `true`, allows forms like
|
||||
## `--option: value` or `--option= value` where the value is in the next
|
||||
## token after the delimiter. When `false`, the value must be in the same
|
||||
## token as the delimiter.
|
||||
var nimrules = toRules(NimMode)
|
||||
if allowWhitespaceAfterColon == false: nimrules.excl prSepAllowDelimAfter
|
||||
initOptParser(cmdline, shortNoVal, longNoVal, nimrules)
|
||||
|
||||
proc initOptParser*(cmdline = "";
|
||||
shortNoVal: set[char] = {};
|
||||
longNoVal: seq[string] = @[];
|
||||
mode: CliMode = NimMode): OptParser =
|
||||
## Initializes the command line parser from a command line string.
|
||||
##
|
||||
## The `cmdline` string is parsed into tokens using shell-like quoting rules.
|
||||
##
|
||||
## **Parameters:**
|
||||
##
|
||||
## - `cmdline`: Command line string to parse. If empty, the real command line
|
||||
## as provided by the `os` module is retrieved instead. If the command line
|
||||
## is not available, an assertion will be raised.
|
||||
## - `shortNoVal`: Set of short option characters that do not accept values.
|
||||
## See `shortNoVal and longNoVal<#nimshortnoval-and-nimlongnoval>`_ for details.
|
||||
## - `longNoVal`: Sequence of long option names that do not accept values.
|
||||
## See `shortNoVal and longNoVal<#nimshortnoval-and-nimlongnoval>`_ for details.
|
||||
## - `mode`: Parser behavior profile (`NimMode`, `LaxMode`, or `GnuMode`).
|
||||
## See `Parser Modes`_ for details.
|
||||
##
|
||||
## **Note:** This does not provide a way of passing default values to arguments.
|
||||
##
|
||||
## See also:
|
||||
## * `getopt iterator<#getopt.i,OptParser>`_
|
||||
@@ -293,34 +546,81 @@ proc initOptParser*(cmdline = "", shortNoVal: set[char] = {},
|
||||
p = initOptParser("--left --debug:3 -l -r:2")
|
||||
p = initOptParser("--left --debug:3 -l -r:2",
|
||||
shortNoVal = {'l'}, longNoVal = @["left"])
|
||||
initOptParser(parseCmdLine(cmdline), shortNoVal, longNoVal, toRules(mode))
|
||||
|
||||
initOptParser(parseCmdLine(cmdline), shortNoVal, longNoVal, allowWhitespaceAfterColon)
|
||||
proc initOptParser*(cmdline = "";
|
||||
shortNoVal: set[char] = {};
|
||||
longNoVal: seq[string] = @[];
|
||||
allowWhitespaceAfterColon: bool): OptParser {.deprecated:
|
||||
"`allowWhitespaceAfterColon` is deprecated, use parser modes instead".} =
|
||||
## This is an overload for continued support of the legacy `allowWhitespaceAfterColon`
|
||||
## option. It modifies the default parser mode so that the passed value is respected.
|
||||
##
|
||||
## Current default parser mode behaves as if `true` was passed (old default).
|
||||
##
|
||||
## - `allowWhitespaceAfterColon`: When `true`, allows forms like
|
||||
## `--option: value` or `--option= value` where the value is in the next
|
||||
## token after the delimiter. When `false`, the value must be in the same
|
||||
## token as the delimiter.
|
||||
var nimrules = toRules(NimMode)
|
||||
if allowWhitespaceAfterColon == false: nimrules.excl prSepAllowDelimAfter
|
||||
initOptParser(parseCmdLine(cmdline), shortNoVal, longNoVal, nimrules)
|
||||
|
||||
proc handleShortOption(p: var OptParser; cmd: string) =
|
||||
var i = p.pos
|
||||
p.kind = cmdShortOption
|
||||
if i < cmd.len:
|
||||
if i < cmd.len: # multidigit short option support goes here
|
||||
add(p.key, cmd[i])
|
||||
inc(i)
|
||||
p.inShortState = true
|
||||
while i < cmd.len and cmd[i] in {'\t', ' '}:
|
||||
inc(i)
|
||||
p.inShortState = false
|
||||
if i < cmd.len and (cmd[i] in {':', '='} or
|
||||
card(p.shortNoVal) > 0 and p.key[0] notin p.shortNoVal):
|
||||
if i < cmd.len and cmd[i] in {':', '='}:
|
||||
if prSepAllowDelimBefore in p.rules:
|
||||
while i < cmd.len and cmd[i] in DelimSet:
|
||||
inc(i)
|
||||
p.inShortState = false
|
||||
|
||||
proc consumeDelims() =
|
||||
while i < cmd.len and cmd[i] in DelimSet: inc(i)
|
||||
|
||||
proc advance(p: var OptParser; n = 1)=
|
||||
p.inShortState = false
|
||||
while i < cmd.len and cmd[i] in {'\t', ' '}: inc(i)
|
||||
p.pos = 0
|
||||
inc p.idx, n
|
||||
|
||||
template next(): untyped = p.cmds[p.idx + 1]
|
||||
|
||||
let canTakeVal = card(p.shortNoVal) > 0 and p.key[0] notin p.shortNoVal
|
||||
if i < cmd.len and cmd[i] in p.separators:
|
||||
# separator case
|
||||
if prShortAllowSep in p.rules:
|
||||
# allow separators: skip the separator and take the value after it
|
||||
inc(i)
|
||||
if prSepAllowDelimAfter in p.rules:
|
||||
consumeDelims()
|
||||
# prohibit separators: treat separator + remainder as the value
|
||||
# this represents an error state but produces output that can be validated
|
||||
p.val = substr(cmd, i)
|
||||
p.pos = 0
|
||||
inc p.idx
|
||||
else:
|
||||
p.pos = i
|
||||
p.advance(1)
|
||||
return
|
||||
elif canTakeVal and prShortValAllowAdjacent in p.rules and i < cmd.len:
|
||||
# adjacent value
|
||||
if prSepAllowDelimBefore in p.rules:
|
||||
consumeDelims()
|
||||
p.val = substr(cmd, i)
|
||||
p.advance(1)
|
||||
return
|
||||
elif canTakeVal and
|
||||
prShortValAllowNextArg in p.rules and
|
||||
i >= cmd.len and
|
||||
p.idx + 1 < p.cmds.len and (
|
||||
prShortValAllowDashLeading in p.rules or
|
||||
not (next().len > 0 and next()[0] == '-')):
|
||||
# next-argument value
|
||||
p.val = next()
|
||||
p.advance(2)
|
||||
return
|
||||
p.pos = i
|
||||
if i >= cmd.len:
|
||||
p.inShortState = false
|
||||
p.pos = 0
|
||||
inc p.idx
|
||||
p.advance(1)
|
||||
|
||||
proc next*(p: var OptParser) {.rtl, extern: "npo$1".} =
|
||||
## Parses the next token.
|
||||
@@ -343,54 +643,71 @@ proc next*(p: var OptParser) {.rtl, extern: "npo$1".} =
|
||||
return
|
||||
|
||||
var i = p.pos
|
||||
while i < p.cmds[p.idx].len and p.cmds[p.idx][i] in {'\t', ' '}: inc(i)
|
||||
template cmd(): untyped = p.cmds[p.idx]
|
||||
template nextArg(): untyped = p.cmds[p.idx + 1]
|
||||
|
||||
proc consumeDelims(cmds: openArray[string]; idx: int) =
|
||||
while i < cmds[idx].len and cmds[idx][i] in DelimSet: inc(i)
|
||||
|
||||
proc advance(p: var OptParser; n = 1) =
|
||||
p.pos = 0
|
||||
inc p.idx, n
|
||||
|
||||
consumeDelims(p.cmds, p.idx)
|
||||
p.pos = i
|
||||
setLen(p.key, 0)
|
||||
setLen(p.val, 0)
|
||||
if p.inShortState:
|
||||
p.inShortState = false
|
||||
if i >= p.cmds[p.idx].len:
|
||||
inc(p.idx)
|
||||
p.pos = 0
|
||||
if i < cmd.len:
|
||||
handleShortOption(p, p.cmds[p.idx])
|
||||
return
|
||||
else:
|
||||
p.advance(1)
|
||||
if p.idx >= p.cmds.len:
|
||||
p.kind = cmdEnd
|
||||
return
|
||||
else:
|
||||
handleShortOption(p, p.cmds[p.idx])
|
||||
return
|
||||
|
||||
if i < p.cmds[p.idx].len and p.cmds[p.idx][i] == '-':
|
||||
if i < cmd.len and cmd[i] == '-':
|
||||
inc(i)
|
||||
if i < p.cmds[p.idx].len and p.cmds[p.idx][i] == '-':
|
||||
if i < cmd.len and cmd[i] == '-':
|
||||
p.kind = cmdLongOption
|
||||
inc(i)
|
||||
i = parseWord(p.cmds[p.idx], i, p.key, {' ', '\t', ':', '='})
|
||||
while i < p.cmds[p.idx].len and p.cmds[p.idx][i] in {'\t', ' '}: inc(i)
|
||||
if i < p.cmds[p.idx].len and p.cmds[p.idx][i] in {':', '='}:
|
||||
i = parseWord(cmd, i, p.key,
|
||||
DelimSet + (if prLongAllowSep in p.rules: p.separators else: {}))
|
||||
if prSepAllowDelimBefore in p.rules:
|
||||
consumeDelims(p.cmds, p.idx)
|
||||
if prLongAllowSep in p.rules and i < cmd.len and cmd[i] in p.separators:
|
||||
inc(i)
|
||||
while i < p.cmds[p.idx].len and p.cmds[p.idx][i] in {'\t', ' '}: inc(i)
|
||||
# if we're at the end, use the next command line option:
|
||||
if i >= p.cmds[p.idx].len and p.idx < p.cmds.len and
|
||||
p.allowWhitespaceAfterColon:
|
||||
inc p.idx
|
||||
i = 0
|
||||
if p.idx < p.cmds.len:
|
||||
p.val = p.cmds[p.idx].substr(i)
|
||||
elif len(p.longNoVal) > 0 and p.key notin p.longNoVal and p.idx+1 < p.cmds.len:
|
||||
p.val = p.cmds[p.idx+1]
|
||||
inc p.idx
|
||||
if prSepAllowDelimAfter in p.rules:
|
||||
consumeDelims(p.cmds, p.idx)
|
||||
if i >= cmd.len and p.idx + 1 < p.cmds.len and
|
||||
prSepAllowDelimAfter in p.rules:
|
||||
p.val = nextArg()
|
||||
p.advance(2)
|
||||
else:
|
||||
p.val = cmd.substr(i)
|
||||
p.advance(1)
|
||||
elif prLongValAllowNextArg in p.rules and
|
||||
len(p.longNoVal) > 0 and
|
||||
p.key notin p.longNoVal and
|
||||
p.idx + 1 < p.cmds.len:
|
||||
p.val = nextArg()
|
||||
p.advance(2)
|
||||
else:
|
||||
p.val = ""
|
||||
inc p.idx
|
||||
p.pos = 0
|
||||
if i < cmd.len:
|
||||
# Leave remainder of the current token to be parsed as an argument.
|
||||
consumeDelims(p.cmds, p.idx)
|
||||
p.cmds[p.idx] = cmd.substr(i)
|
||||
else:
|
||||
p.advance(1)
|
||||
else:
|
||||
p.pos = i
|
||||
handleShortOption(p, p.cmds[p.idx])
|
||||
handleShortOption(p, cmd)
|
||||
else:
|
||||
p.kind = cmdArgument
|
||||
p.key = p.cmds[p.idx]
|
||||
inc p.idx
|
||||
p.pos = 0
|
||||
p.key = cmd
|
||||
p.advance(1)
|
||||
|
||||
when declared(quoteShellCommand):
|
||||
proc cmdLineRest*(p: OptParser): string {.rtl, extern: "npo$1".} =
|
||||
@@ -399,15 +716,13 @@ when declared(quoteShellCommand):
|
||||
## See also:
|
||||
## * `remainingArgs proc<#remainingArgs,OptParser>`_
|
||||
##
|
||||
## **Examples:**
|
||||
## ```Nim
|
||||
## var p = initOptParser("--left -r:2 -- foo.txt bar.txt")
|
||||
## while true:
|
||||
## p.next()
|
||||
## if p.kind == cmdLongOption and p.key == "": # Look for "--"
|
||||
## break
|
||||
## doAssert p.cmdLineRest == "foo.txt bar.txt"
|
||||
## ```
|
||||
runnableExamples:
|
||||
var p = initOptParser("--left -r:2 -- foo.txt bar.txt")
|
||||
while true:
|
||||
p.next()
|
||||
if p.kind == cmdLongOption and p.key == "": # Look for "--"
|
||||
break
|
||||
doAssert p.cmdLineRest == "foo.txt bar.txt"
|
||||
result = p.cmds[p.idx .. ^1].quoteShellCommand
|
||||
|
||||
proc remainingArgs*(p: OptParser): seq[string] {.rtl, extern: "npo$1".} =
|
||||
@@ -416,15 +731,13 @@ proc remainingArgs*(p: OptParser): seq[string] {.rtl, extern: "npo$1".} =
|
||||
## See also:
|
||||
## * `cmdLineRest proc<#cmdLineRest,OptParser>`_
|
||||
##
|
||||
## **Examples:**
|
||||
## ```Nim
|
||||
## var p = initOptParser("--left -r:2 -- foo.txt bar.txt")
|
||||
## while true:
|
||||
## p.next()
|
||||
## if p.kind == cmdLongOption and p.key == "": # Look for "--"
|
||||
## break
|
||||
## doAssert p.remainingArgs == @["foo.txt", "bar.txt"]
|
||||
## ```
|
||||
runnableExamples:
|
||||
var p = initOptParser("--left -r:2 -- foo.txt bar.txt")
|
||||
while true:
|
||||
p.next()
|
||||
if p.kind == cmdLongOption and p.key == "": # Look for "--"
|
||||
break
|
||||
doAssert p.remainingArgs == @["foo.txt", "bar.txt"]
|
||||
result = @[]
|
||||
for i in p.idx..<p.cmds.len: result.add p.cmds[i]
|
||||
|
||||
@@ -439,29 +752,26 @@ iterator getopt*(p: var OptParser): tuple[kind: CmdLineKind, key,
|
||||
## See also:
|
||||
## * `initOptParser proc<#initOptParser,string,set[char],seq[string]>`_
|
||||
##
|
||||
## **Examples:**
|
||||
##
|
||||
## ```Nim
|
||||
## # these are placeholders, of course
|
||||
## proc writeHelp() = discard
|
||||
## proc writeVersion() = discard
|
||||
##
|
||||
## var filename: string
|
||||
## var p = initOptParser("--left --debug:3 -l -r:2")
|
||||
##
|
||||
## for kind, key, val in p.getopt():
|
||||
## case kind
|
||||
## of cmdArgument:
|
||||
## filename = key
|
||||
## of cmdLongOption, cmdShortOption:
|
||||
## case key
|
||||
## of "help", "h": writeHelp()
|
||||
## of "version", "v": writeVersion()
|
||||
## of cmdEnd: assert(false) # cannot happen
|
||||
## if filename == "":
|
||||
## # no filename has been given, so we show the help
|
||||
## writeHelp()
|
||||
## ```
|
||||
runnableExamples:
|
||||
# these are placeholders, of course
|
||||
proc writeHelp() = discard
|
||||
proc writeVersion() = discard
|
||||
|
||||
var filename: string = ""
|
||||
var p = initOptParser("--left --debug:3 -l -r:2")
|
||||
|
||||
for kind, key, val in p.getopt():
|
||||
case kind
|
||||
of cmdArgument:
|
||||
filename = key
|
||||
of cmdLongOption, cmdShortOption:
|
||||
case key
|
||||
of "help", "h": writeHelp()
|
||||
of "version", "v": writeVersion()
|
||||
of cmdEnd: assert(false) # cannot happen
|
||||
if filename == "":
|
||||
# no filename has been given, so we show the help
|
||||
writeHelp()
|
||||
p.pos = 0
|
||||
p.idx = 0
|
||||
while true:
|
||||
@@ -469,8 +779,10 @@ iterator getopt*(p: var OptParser): tuple[kind: CmdLineKind, key,
|
||||
if p.kind == cmdEnd: break
|
||||
yield (p.kind, p.key, p.val)
|
||||
|
||||
iterator getopt*(cmdline: seq[string] = @[],
|
||||
shortNoVal: set[char] = {}, longNoVal: seq[string] = @[]):
|
||||
iterator getopt*(cmdline: seq[string] = @[];
|
||||
shortNoVal: set[char] = {};
|
||||
longNoVal: seq[string] = @[];
|
||||
mode: CliMode = NimMode):
|
||||
tuple[kind: CmdLineKind, key, val: string] =
|
||||
## Convenience iterator for iterating over command line arguments.
|
||||
##
|
||||
@@ -483,6 +795,9 @@ iterator getopt*(cmdline: seq[string] = @[],
|
||||
## parameters<#nimshortnoval-and-nimlongnoval>`_ for more information on
|
||||
## how this affects parsing.
|
||||
##
|
||||
## `mode` selects the parser behavior profile (`NimMode`, `LaxMode`,
|
||||
## or `GnuMode`). See `Parser Modes`_ for details.
|
||||
##
|
||||
## There is no need to check for `cmdEnd` while iterating. If using `getopt`
|
||||
## with case switching, checking for `cmdEnd` is required.
|
||||
##
|
||||
@@ -513,7 +828,8 @@ iterator getopt*(cmdline: seq[string] = @[],
|
||||
## writeHelp()
|
||||
## ```
|
||||
var p = initOptParser(cmdline, shortNoVal = shortNoVal,
|
||||
longNoVal = longNoVal)
|
||||
longNoVal = longNoVal,
|
||||
rules = toRules(mode))
|
||||
while true:
|
||||
next(p)
|
||||
if p.kind == cmdEnd: break
|
||||
|
||||
@@ -243,7 +243,7 @@ proc rand[T: uint | uint64](r: var Rand; max: T): T =
|
||||
else:
|
||||
inc iters
|
||||
|
||||
proc rand*(r: var Rand; max: Natural): int {.benign.} =
|
||||
proc rand*(r: var Rand; max: Natural): int {.gcsafe.} =
|
||||
## Returns a random integer in the range `0..max` using the given state.
|
||||
##
|
||||
## **See also:**
|
||||
@@ -260,7 +260,7 @@ proc rand*(r: var Rand; max: Natural): int {.benign.} =
|
||||
cast[int](rand(r, uint64(max)))
|
||||
# xxx toUnsigned pending https://github.com/nim-lang/Nim/pull/18445
|
||||
|
||||
proc rand*(max: int): int {.benign.} =
|
||||
proc rand*(max: int): int {.gcsafe.} =
|
||||
## Returns a random integer in the range `0..max`.
|
||||
##
|
||||
## If `randomize <#randomize>`_ has not been called, the sequence of random
|
||||
@@ -281,7 +281,7 @@ proc rand*(max: int): int {.benign.} =
|
||||
|
||||
rand(state, max)
|
||||
|
||||
proc rand*(r: var Rand; max: range[0.0 .. high(float)]): float {.benign.} =
|
||||
proc rand*(r: var Rand; max: range[0.0 .. high(float)]): float {.gcsafe.} =
|
||||
## Returns a random floating point number in the range `0.0..max`
|
||||
## using the given state.
|
||||
##
|
||||
@@ -308,7 +308,7 @@ proc rand*(r: var Rand; max: range[0.0 .. high(float)]): float {.benign.} =
|
||||
let u = (0x3FFu64 shl 52u64) or (x shr 12u64)
|
||||
result = (cast[float](u) - 1.0) * max
|
||||
|
||||
proc rand*(max: float): float {.benign.} =
|
||||
proc rand*(max: float): float {.gcsafe.} =
|
||||
## Returns a random floating point number in the range `0.0..max`.
|
||||
##
|
||||
## If `randomize <#randomize>`_ has not been called, the sequence of random
|
||||
@@ -612,7 +612,7 @@ proc initRand*(seed: int64): Rand =
|
||||
skipRandomNumbers(result)
|
||||
discard next(result)
|
||||
|
||||
proc randomize*(seed: int64) {.benign.} =
|
||||
proc randomize*(seed: int64) {.gcsafe.} =
|
||||
## Initializes the default random number generator with the given seed.
|
||||
##
|
||||
## Providing a specific seed will produce the same results for that seed each time.
|
||||
@@ -736,7 +736,7 @@ when not defined(standalone):
|
||||
since (1, 5, 1):
|
||||
export initRand
|
||||
|
||||
proc randomize*() {.benign.} =
|
||||
proc randomize*() {.gcsafe.} =
|
||||
## Initializes the default random number generator with a seed based on
|
||||
## random number source.
|
||||
##
|
||||
|
||||
@@ -16,9 +16,9 @@
|
||||
## stream interface.
|
||||
##
|
||||
## .. warning:: Due to the use of `pointer`, the `readData`, `peekData` and
|
||||
## `writeData` interfaces are not available on the compile-time VM, and must
|
||||
## be cast from a `ptr string` on the JS backend. However, `readDataStr` is
|
||||
## available generally in place of `readData`.
|
||||
## `writeData` interfaces are not available on the compile-time VM, and must
|
||||
## be cast from a `ptr string` on the JS backend. However, `readDataStr` is
|
||||
## available generally in place of `readData`.
|
||||
##
|
||||
## Basic usage
|
||||
## ===========
|
||||
|
||||
@@ -382,9 +382,9 @@ type
|
||||
## timezones. The `times` module only supplies implementations for the
|
||||
## system's local time and UTC.
|
||||
zonedTimeFromTimeImpl: proc (x: Time): ZonedTime
|
||||
{.tags: [], raises: [], benign.}
|
||||
{.tags: [], raises: [], gcsafe.}
|
||||
zonedTimeFromAdjTimeImpl: proc (x: Time): ZonedTime
|
||||
{.tags: [], raises: [], benign.}
|
||||
{.tags: [], raises: [], gcsafe.}
|
||||
name: string
|
||||
|
||||
ZonedTime* = object ## Represents a point in time with an associated
|
||||
@@ -432,7 +432,7 @@ else:
|
||||
# Helper procs
|
||||
#
|
||||
|
||||
{.pragma: operator, rtl, noSideEffect, benign.}
|
||||
{.pragma: operator, rtl, noSideEffect, gcsafe.}
|
||||
|
||||
proc convert*[T: SomeInteger](unitFrom, unitTo: FixedTimeUnit, quantity: T): T
|
||||
{.inline.} =
|
||||
@@ -518,7 +518,7 @@ proc fromEpochDay(epochday: int64):
|
||||
return (d.MonthdayRange, m.Month, (y + ord(m <= 2)).int)
|
||||
|
||||
proc getDayOfYear*(monthday: MonthdayRange, month: Month, year: int):
|
||||
YeardayRange {.tags: [], raises: [], benign.} =
|
||||
YeardayRange {.tags: [], raises: [], gcsafe.} =
|
||||
## Returns the day of the year.
|
||||
## Equivalent with `dateTime(year, month, monthday, 0, 0, 0, 0).yearday`.
|
||||
runnableExamples:
|
||||
@@ -538,7 +538,7 @@ proc getDayOfYear*(monthday: MonthdayRange, month: Month, year: int):
|
||||
result = daysUntilMonth[month] + monthday - 1
|
||||
|
||||
proc getDayOfWeek*(monthday: MonthdayRange, month: Month, year: int): WeekDay
|
||||
{.tags: [], raises: [], benign.} =
|
||||
{.tags: [], raises: [], gcsafe.} =
|
||||
## Returns the day of the week enum from day, month and year.
|
||||
## Equivalent with `dateTime(year, month, monthday, 0, 0, 0, 0).weekday`.
|
||||
runnableExamples:
|
||||
@@ -922,21 +922,21 @@ proc nanosecond*(time: Time): NanosecondRange =
|
||||
time.nanosecond
|
||||
|
||||
proc fromUnix*(unix: int64): Time
|
||||
{.benign, tags: [], raises: [], noSideEffect.} =
|
||||
{.gcsafe, tags: [], raises: [], noSideEffect.} =
|
||||
## Convert a unix timestamp (seconds since `1970-01-01T00:00:00Z`)
|
||||
## to a `Time`.
|
||||
runnableExamples:
|
||||
doAssert $fromUnix(0).utc == "1970-01-01T00:00:00Z"
|
||||
initTime(unix, 0)
|
||||
|
||||
proc toUnix*(t: Time): int64 {.benign, tags: [], raises: [], noSideEffect.} =
|
||||
proc toUnix*(t: Time): int64 {.gcsafe, tags: [], raises: [], noSideEffect.} =
|
||||
## Convert `t` to a unix timestamp (seconds since `1970-01-01T00:00:00Z`).
|
||||
## See also `toUnixFloat` for subsecond resolution.
|
||||
runnableExamples:
|
||||
doAssert fromUnix(0).toUnix() == 0
|
||||
t.seconds
|
||||
|
||||
proc fromUnixFloat(seconds: float): Time {.benign, tags: [], raises: [], noSideEffect.} =
|
||||
proc fromUnixFloat(seconds: float): Time {.gcsafe, tags: [], raises: [], noSideEffect.} =
|
||||
## Convert a unix timestamp in seconds to a `Time`; same as `fromUnix`
|
||||
## but with subsecond resolution.
|
||||
runnableExamples:
|
||||
@@ -946,7 +946,7 @@ proc fromUnixFloat(seconds: float): Time {.benign, tags: [], raises: [], noSideE
|
||||
let nsecs = (seconds - secs) * 1e9
|
||||
initTime(secs.int64, nsecs.NanosecondRange)
|
||||
|
||||
proc toUnixFloat(t: Time): float {.benign, tags: [], raises: [].} =
|
||||
proc toUnixFloat(t: Time): float {.gcsafe, tags: [], raises: [].} =
|
||||
## Same as `toUnix` but using subsecond resolution.
|
||||
runnableExamples:
|
||||
let t = getTime()
|
||||
@@ -975,7 +975,7 @@ proc toWinTime*(t: Time): int64 =
|
||||
proc getTimeImpl(typ: typedesc[Time]): Time =
|
||||
raiseAssert "implemented in the vm"
|
||||
|
||||
proc getTime*(): Time {.tags: [TimeEffect], benign.} =
|
||||
proc getTime*(): Time {.tags: [TimeEffect], gcsafe.} =
|
||||
## Gets the current time as a `Time` with up to nanosecond resolution.
|
||||
when nimvm:
|
||||
result = getTimeImpl(Time)
|
||||
@@ -1154,7 +1154,7 @@ proc isLeapDay*(dt: DateTime): bool {.since: (1, 1).} =
|
||||
assertDateTimeInitialized dt
|
||||
dt.year.isLeapYear and dt.month == mFeb and dt.monthday == 29
|
||||
|
||||
proc toTime*(dt: DateTime): Time {.tags: [], raises: [], benign.} =
|
||||
proc toTime*(dt: DateTime): Time {.tags: [], raises: [], gcsafe.} =
|
||||
## Converts a `DateTime` to a `Time` representing the same point in time.
|
||||
assertDateTimeInitialized dt
|
||||
let epochDay = toEpochDay(dt.monthday, dt.month, dt.year)
|
||||
@@ -1197,9 +1197,9 @@ proc initDateTime(zt: ZonedTime, zone: Timezone): DateTime =
|
||||
proc newTimezone*(
|
||||
name: string,
|
||||
zonedTimeFromTimeImpl: proc (time: Time): ZonedTime
|
||||
{.tags: [], raises: [], benign.},
|
||||
{.tags: [], raises: [], gcsafe.},
|
||||
zonedTimeFromAdjTimeImpl: proc (adjTime: Time): ZonedTime
|
||||
{.tags: [], raises: [], benign.}
|
||||
{.tags: [], raises: [], gcsafe.}
|
||||
): owned Timezone =
|
||||
## Create a new `Timezone`.
|
||||
##
|
||||
@@ -1263,12 +1263,12 @@ proc `==`*(zone1, zone2: Timezone): bool =
|
||||
zone1.name == zone2.name
|
||||
|
||||
proc inZone*(time: Time, zone: Timezone): DateTime
|
||||
{.tags: [], raises: [], benign.} =
|
||||
{.tags: [], raises: [], gcsafe.} =
|
||||
## Convert `time` into a `DateTime` using `zone` as the timezone.
|
||||
result = initDateTime(zone.zonedTimeFromTime(time), zone)
|
||||
|
||||
proc inZone*(dt: DateTime, zone: Timezone): DateTime
|
||||
{.tags: [], raises: [], benign.} =
|
||||
{.tags: [], raises: [], gcsafe.} =
|
||||
## Returns a `DateTime` representing the same point in time as `dt` but
|
||||
## using `zone` as the timezone.
|
||||
assertDateTimeInitialized dt
|
||||
@@ -1283,14 +1283,14 @@ proc toAdjTime(dt: DateTime): Time =
|
||||
result = initTime(seconds, dt.nanosecond)
|
||||
|
||||
when defined(js):
|
||||
proc localZonedTimeFromTime(time: Time): ZonedTime {.benign.} =
|
||||
proc localZonedTimeFromTime(time: Time): ZonedTime {.gcsafe.} =
|
||||
let jsDate = newDate(time.seconds * 1000)
|
||||
let offset = jsDate.getTimezoneOffset() * secondsInMin
|
||||
result.time = time
|
||||
result.utcOffset = offset
|
||||
result.isDst = false
|
||||
|
||||
proc localZonedTimeFromAdjTime(adjTime: Time): ZonedTime {.benign.} =
|
||||
proc localZonedTimeFromAdjTime(adjTime: Time): ZonedTime {.gcsafe.} =
|
||||
let utcDate = newDate(adjTime.seconds * 1000)
|
||||
let localDate = newDate(utcDate.getUTCFullYear(), utcDate.getUTCMonth(),
|
||||
utcDate.getUTCDate(), utcDate.getUTCHours(), utcDate.getUTCMinutes(),
|
||||
@@ -1337,11 +1337,11 @@ else:
|
||||
return ((a.int64 - tm.toAdjUnix).int, tm.tm_isdst > 0)
|
||||
return (0, false)
|
||||
|
||||
proc localZonedTimeFromTime(time: Time): ZonedTime {.benign.} =
|
||||
proc localZonedTimeFromTime(time: Time): ZonedTime {.gcsafe.} =
|
||||
let (offset, dst) = getLocalOffsetAndDst(time.seconds)
|
||||
result = ZonedTime(time: time, utcOffset: offset, isDst: dst)
|
||||
|
||||
proc localZonedTimeFromAdjTime(adjTime: Time): ZonedTime {.benign.} =
|
||||
proc localZonedTimeFromAdjTime(adjTime: Time): ZonedTime {.gcsafe.} =
|
||||
var adjUnix = adjTime.seconds
|
||||
let past = adjUnix - secondsInDay
|
||||
let (pastOffset, _) = getLocalOffsetAndDst(past)
|
||||
@@ -1408,7 +1408,7 @@ proc local*(t: Time): DateTime =
|
||||
## Shorthand for `t.inZone(local())`.
|
||||
t.inZone(local())
|
||||
|
||||
proc now*(): DateTime {.tags: [TimeEffect], benign.} =
|
||||
proc now*(): DateTime {.tags: [TimeEffect], gcsafe.} =
|
||||
## Get the current time as a `DateTime` in the local timezone.
|
||||
## Shorthand for `getTime().local`.
|
||||
##
|
||||
@@ -2327,7 +2327,7 @@ proc parseTime*(input: string, f: static[string], zone: Timezone): Time
|
||||
const f2 = initTimeFormat(f)
|
||||
result = input.parse(f2, zone).toTime()
|
||||
|
||||
proc `$`*(dt: DateTime): string {.tags: [], raises: [], benign.} =
|
||||
proc `$`*(dt: DateTime): string {.tags: [], raises: [], gcsafe.} =
|
||||
## Converts a `DateTime` object to a string representation.
|
||||
## It uses the format `yyyy-MM-dd'T'HH:mm:sszzz`.
|
||||
runnableExamples:
|
||||
@@ -2339,7 +2339,7 @@ proc `$`*(dt: DateTime): string {.tags: [], raises: [], benign.} =
|
||||
else:
|
||||
result = format(dt, "yyyy-MM-dd'T'HH:mm:sszzz")
|
||||
|
||||
proc `$`*(time: Time): string {.tags: [], raises: [], benign.} =
|
||||
proc `$`*(time: Time): string {.tags: [], raises: [], gcsafe.} =
|
||||
## Converts a `Time` value to a string representation. It will use the local
|
||||
## time zone and use the format `yyyy-MM-dd'T'HH:mm:sszzz`.
|
||||
runnableExamples:
|
||||
|
||||
@@ -96,6 +96,9 @@ proc supportsCopyMem*(t: typedesc): bool {.magic: "TypeTrait".}
|
||||
##
|
||||
## Other languages name a type like these `blob`:idx:.
|
||||
|
||||
proc canFormCycles*(t: typedesc): bool {.magic: "TypeTrait".}
|
||||
## Returns true if `t` can form cycles.
|
||||
|
||||
proc hasDefaultValue*(t: typedesc): bool {.magic: "TypeTrait".} =
|
||||
## Returns true if `t` has a valid default value.
|
||||
runnableExamples:
|
||||
|
||||
@@ -331,7 +331,7 @@ proc rawRemoveDir(dir: string) {.noWeirdTarget.} =
|
||||
if rmdir(dir) != 0'i32 and errno != ENOENT: raiseOSError(osLastError(), dir)
|
||||
|
||||
proc removeDir*(dir: string, checkDir = false) {.rtl, extern: "nos$1", tags: [
|
||||
WriteDirEffect, ReadDirEffect], benign, noWeirdTarget.} =
|
||||
WriteDirEffect, ReadDirEffect], gcsafe, noWeirdTarget.} =
|
||||
## Removes the directory `dir` including all subdirectories and files
|
||||
## in `dir` (recursively).
|
||||
##
|
||||
@@ -441,7 +441,7 @@ proc createDir*(dir: string) {.rtl, extern: "nos$1",
|
||||
discard existsOrCreateDir(p)
|
||||
|
||||
proc copyDir*(source, dest: string, skipSpecial = false) {.rtl, extern: "nos$1",
|
||||
tags: [ReadDirEffect, WriteIOEffect, ReadIOEffect], benign, noWeirdTarget.} =
|
||||
tags: [ReadDirEffect, WriteIOEffect, ReadIOEffect], gcsafe, noWeirdTarget.} =
|
||||
## Copies a directory from `source` to `dest`.
|
||||
##
|
||||
## On non-Windows OSes, symlinks are copied as symlinks. On Windows, symlinks
|
||||
@@ -482,7 +482,7 @@ proc copyDirWithPermissions*(source, dest: string,
|
||||
ignorePermissionErrors = true,
|
||||
skipSpecial = false)
|
||||
{.rtl, extern: "nos$1", tags: [ReadDirEffect, WriteIOEffect, ReadIOEffect],
|
||||
benign, noWeirdTarget.} =
|
||||
gcsafe, noWeirdTarget.} =
|
||||
## Copies a directory from `source` to `dest` preserving file permissions.
|
||||
##
|
||||
## On non-Windows OSes, symlinks are copied as symlinks. On Windows, symlinks
|
||||
|
||||
@@ -182,7 +182,7 @@ proc checkErr(f: File) =
|
||||
|
||||
{.push stackTrace: off, profiler: off.}
|
||||
proc readBuffer*(f: File, buffer: pointer, len: Natural): int {.
|
||||
tags: [ReadIOEffect], benign.} =
|
||||
tags: [ReadIOEffect], gcsafe.} =
|
||||
## Reads `len` bytes into the buffer pointed to by `buffer`. Returns
|
||||
## the actual number of bytes that have been read which may be less than
|
||||
## `len` (if not as many bytes are remaining), but not greater.
|
||||
@@ -191,20 +191,20 @@ proc readBuffer*(f: File, buffer: pointer, len: Natural): int {.
|
||||
|
||||
proc readBytes*(f: File, a: var openArray[int8|uint8], start,
|
||||
len: Natural): int {.
|
||||
tags: [ReadIOEffect], benign.} =
|
||||
tags: [ReadIOEffect], gcsafe.} =
|
||||
## Reads `len` bytes into the buffer `a` starting at `a[start]`. Returns
|
||||
## the actual number of bytes that have been read which may be less than
|
||||
## `len` (if not as many bytes are remaining), but not greater.
|
||||
result = readBuffer(f, addr(a[start]), len)
|
||||
|
||||
proc readChars*(f: File, a: var openArray[char]): int {.tags: [ReadIOEffect], benign.} =
|
||||
proc readChars*(f: File, a: var openArray[char]): int {.tags: [ReadIOEffect], gcsafe.} =
|
||||
## Reads up to `a.len` bytes into the buffer `a`. Returns
|
||||
## the actual number of bytes that have been read which may be less than
|
||||
## `a.len` (if not as many bytes are remaining), but not greater.
|
||||
result = readBuffer(f, addr(a[0]), a.len)
|
||||
|
||||
proc readChars*(f: File, a: var openArray[char], start, len: Natural): int {.
|
||||
tags: [ReadIOEffect], benign, deprecated:
|
||||
tags: [ReadIOEffect], gcsafe, deprecated:
|
||||
"use other `readChars` overload, possibly via: readChars(toOpenArray(buf, start, len-1))".} =
|
||||
## Reads `len` bytes into the buffer `a` starting at `a[start]`. Returns
|
||||
## the actual number of bytes that have been read which may be less than
|
||||
@@ -213,13 +213,13 @@ proc readChars*(f: File, a: var openArray[char], start, len: Natural): int {.
|
||||
raiseEIO("buffer overflow: (start+len) > length of openarray buffer")
|
||||
result = readBuffer(f, addr(a[start]), len)
|
||||
|
||||
proc write*(f: File, c: cstring) {.tags: [WriteIOEffect], benign.} =
|
||||
proc write*(f: File, c: cstring) {.tags: [WriteIOEffect], gcsafe.} =
|
||||
## Writes a value to the file `f`. May throw an IO exception.
|
||||
discard c_fputs(c, f)
|
||||
checkErr(f)
|
||||
|
||||
proc writeBuffer*(f: File, buffer: pointer, len: Natural): int {.
|
||||
tags: [WriteIOEffect], benign.} =
|
||||
tags: [WriteIOEffect], gcsafe.} =
|
||||
## Writes the bytes of buffer pointed to by the parameter `buffer` to the
|
||||
## file `f`. Returns the number of actual written bytes, which may be less
|
||||
## than `len` in case of an error.
|
||||
@@ -227,7 +227,7 @@ proc writeBuffer*(f: File, buffer: pointer, len: Natural): int {.
|
||||
checkErr(f)
|
||||
|
||||
proc writeBytes*(f: File, a: openArray[int8|uint8], start, len: Natural): int {.
|
||||
tags: [WriteIOEffect], benign.} =
|
||||
tags: [WriteIOEffect], gcsafe.} =
|
||||
## Writes the bytes of `a[start..start+len-1]` to the file `f`. Returns
|
||||
## the number of actual written bytes, which may be less than `len` in case
|
||||
## of an error.
|
||||
@@ -235,7 +235,7 @@ proc writeBytes*(f: File, a: openArray[int8|uint8], start, len: Natural): int {.
|
||||
result = writeBuffer(f, addr(x[int(start)]), len)
|
||||
|
||||
proc writeChars*(f: File, a: openArray[char], start, len: Natural): int {.
|
||||
tags: [WriteIOEffect], benign.} =
|
||||
tags: [WriteIOEffect], gcsafe.} =
|
||||
## Writes the bytes of `a[start..start+len-1]` to the file `f`. Returns
|
||||
## the number of actual written bytes, which may be less than `len` in case
|
||||
## of an error.
|
||||
@@ -264,7 +264,7 @@ when defined(windows):
|
||||
break
|
||||
inc i, w
|
||||
|
||||
proc write*(f: File, s: string) {.tags: [WriteIOEffect], benign.} =
|
||||
proc write*(f: File, s: string) {.tags: [WriteIOEffect], gcsafe.} =
|
||||
when defined(windows):
|
||||
writeWindows(f, s, doRaise = true)
|
||||
else:
|
||||
@@ -393,7 +393,7 @@ when defined(nimdoc) or (defined(posix) and not defined(nimscript)) or defined(w
|
||||
inheritable.WinDWORD) != 0
|
||||
|
||||
proc readLine*(f: File, line: var string): bool {.tags: [ReadIOEffect],
|
||||
benign.} =
|
||||
gcsafe.} =
|
||||
## Reads a line of text from the file `f` into `line`. May throw an IO
|
||||
## exception.
|
||||
## A line of text may be delimited by `LF` or `CRLF`. The newline
|
||||
@@ -519,43 +519,43 @@ proc readLine*(f: File, line: var string): bool {.tags: [ReadIOEffect],
|
||||
sp = 128 # read in 128 bytes at a time
|
||||
line.setLen(pos+sp)
|
||||
|
||||
proc readLine*(f: File): string {.tags: [ReadIOEffect], benign.} =
|
||||
proc readLine*(f: File): string {.tags: [ReadIOEffect], gcsafe.} =
|
||||
## Reads a line of text from the file `f`. May throw an IO exception.
|
||||
## A line of text may be delimited by `LF` or `CRLF`. The newline
|
||||
## character(s) are not part of the returned string.
|
||||
result = newStringOfCap(80)
|
||||
if not readLine(f, result): raiseEOF()
|
||||
|
||||
proc write*(f: File, i: int) {.tags: [WriteIOEffect], benign.} =
|
||||
proc write*(f: File, i: int) {.tags: [WriteIOEffect], gcsafe.} =
|
||||
when sizeof(int) == 8:
|
||||
if c_fprintf(f, "%lld", i) < 0: checkErr(f)
|
||||
else:
|
||||
if c_fprintf(f, "%ld", i) < 0: checkErr(f)
|
||||
|
||||
proc write*(f: File, i: BiggestInt) {.tags: [WriteIOEffect], benign.} =
|
||||
proc write*(f: File, i: BiggestInt) {.tags: [WriteIOEffect], gcsafe.} =
|
||||
when sizeof(BiggestInt) == 8:
|
||||
if c_fprintf(f, "%lld", i) < 0: checkErr(f)
|
||||
else:
|
||||
if c_fprintf(f, "%ld", i) < 0: checkErr(f)
|
||||
|
||||
proc write*(f: File, b: bool) {.tags: [WriteIOEffect], benign.} =
|
||||
proc write*(f: File, b: bool) {.tags: [WriteIOEffect], gcsafe.} =
|
||||
if b: write(f, "true")
|
||||
else: write(f, "false")
|
||||
|
||||
proc write*(f: File, r: float32) {.tags: [WriteIOEffect], benign.} =
|
||||
proc write*(f: File, r: float32) {.tags: [WriteIOEffect], gcsafe.} =
|
||||
var buffer {.noinit.}: array[65, char]
|
||||
discard writeFloatToBuffer(buffer, r)
|
||||
if c_fprintf(f, "%s", buffer[0].addr) < 0: checkErr(f)
|
||||
|
||||
proc write*(f: File, r: BiggestFloat) {.tags: [WriteIOEffect], benign.} =
|
||||
proc write*(f: File, r: BiggestFloat) {.tags: [WriteIOEffect], gcsafe.} =
|
||||
var buffer {.noinit.}: array[65, char]
|
||||
discard writeFloatToBuffer(buffer, r)
|
||||
if c_fprintf(f, "%s", buffer[0].addr) < 0: checkErr(f)
|
||||
|
||||
proc write*(f: File, c: char) {.tags: [WriteIOEffect], benign.} =
|
||||
proc write*(f: File, c: char) {.tags: [WriteIOEffect], gcsafe.} =
|
||||
discard c_putc(cint(c), f)
|
||||
|
||||
proc write*(f: File, a: varargs[string, `$`]) {.tags: [WriteIOEffect], benign.} =
|
||||
proc write*(f: File, a: varargs[string, `$`]) {.tags: [WriteIOEffect], gcsafe.} =
|
||||
for x in items(a): write(f, x)
|
||||
|
||||
proc readAllBuffer(file: File): string =
|
||||
@@ -579,7 +579,7 @@ proc rawFileSize(file: File): int64 =
|
||||
result = c_ftell(file)
|
||||
discard c_fseek(file, oldPos, 0)
|
||||
|
||||
proc endOfFile*(f: File): bool {.tags: [], benign.} =
|
||||
proc endOfFile*(f: File): bool {.tags: [], gcsafe.} =
|
||||
## Returns true if `f` is at the end.
|
||||
var c = c_fgetc(f)
|
||||
discard c_ungetc(c, f)
|
||||
@@ -603,7 +603,7 @@ proc readAllFile(file: File): string =
|
||||
var len = rawFileSize(file)
|
||||
result = readAllFile(file, len)
|
||||
|
||||
proc readAll*(file: File): string {.tags: [ReadIOEffect], benign.} =
|
||||
proc readAll*(file: File): string {.tags: [ReadIOEffect], gcsafe.} =
|
||||
## Reads all data from the stream `file`.
|
||||
##
|
||||
## Raises an IO exception in case of an error. It is an error if the
|
||||
@@ -621,7 +621,7 @@ proc readAll*(file: File): string {.tags: [ReadIOEffect], benign.} =
|
||||
result = readAllBuffer(file)
|
||||
|
||||
proc writeLine*[Ty](f: File, x: varargs[Ty, `$`]) {.inline,
|
||||
tags: [WriteIOEffect], benign.} =
|
||||
tags: [WriteIOEffect], gcsafe.} =
|
||||
## Writes the values `x` to `f` and then writes "\\n".
|
||||
## May throw an IO exception.
|
||||
for i in items(x):
|
||||
@@ -713,7 +713,7 @@ when defined(posix) and not defined(nimscript):
|
||||
|
||||
proc open*(f: var File, filename: string,
|
||||
mode: FileMode = fmRead,
|
||||
bufSize: int = -1): bool {.tags: [], raises: [], benign.} =
|
||||
bufSize: int = -1): bool {.tags: [], raises: [], gcsafe.} =
|
||||
## Opens a file named `filename` with given `mode`.
|
||||
##
|
||||
## Default mode is readonly. Returns true if the file could be opened.
|
||||
@@ -747,7 +747,7 @@ proc open*(f: var File, filename: string,
|
||||
result = false
|
||||
|
||||
proc reopen*(f: File, filename: string, mode: FileMode = fmRead): bool {.
|
||||
tags: [], benign.} =
|
||||
tags: [], gcsafe.} =
|
||||
## Reopens the file `f` with given `filename` and `mode`. This
|
||||
## is often used to redirect the `stdin`, `stdout` or `stderr`
|
||||
## file variables.
|
||||
@@ -766,7 +766,7 @@ proc reopen*(f: File, filename: string, mode: FileMode = fmRead): bool {.
|
||||
result = false
|
||||
|
||||
proc open*(f: var File, filehandle: FileHandle,
|
||||
mode: FileMode = fmRead): bool {.tags: [], raises: [], benign.} =
|
||||
mode: FileMode = fmRead): bool {.tags: [], raises: [], gcsafe.} =
|
||||
## Creates a `File` from a `filehandle` with given `mode`.
|
||||
##
|
||||
## Default mode is readonly. Returns true if the file could be opened.
|
||||
@@ -792,26 +792,26 @@ proc open*(filename: string,
|
||||
if not open(result, filename, mode, bufSize):
|
||||
raise newException(IOError, "cannot open: " & filename)
|
||||
|
||||
proc setFilePos*(f: File, pos: int64, relativeTo: FileSeekPos = fspSet) {.benign, sideEffect.} =
|
||||
proc setFilePos*(f: File, pos: int64, relativeTo: FileSeekPos = fspSet) {.gcsafe, sideEffect.} =
|
||||
## Sets the position of the file pointer that is used for read/write
|
||||
## operations. The file's first byte has the index zero.
|
||||
if c_fseek(f, pos, cint(relativeTo)) != 0:
|
||||
raiseEIO("cannot set file position")
|
||||
|
||||
proc getFilePos*(f: File): int64 {.benign.} =
|
||||
proc getFilePos*(f: File): int64 {.gcsafe.} =
|
||||
## Retrieves the current position of the file pointer that is used to
|
||||
## read from the file `f`. The file's first byte has the index zero.
|
||||
result = c_ftell(f)
|
||||
if result < 0: raiseEIO("cannot retrieve file position")
|
||||
|
||||
proc getFileSize*(f: File): int64 {.tags: [ReadIOEffect], benign.} =
|
||||
proc getFileSize*(f: File): int64 {.tags: [ReadIOEffect], gcsafe.} =
|
||||
## Retrieves the file size (in bytes) of `f`.
|
||||
let oldPos = getFilePos(f)
|
||||
discard c_fseek(f, 0, 2) # seek the end of the file
|
||||
result = getFilePos(f)
|
||||
setFilePos(f, oldPos)
|
||||
|
||||
proc setStdIoUnbuffered*() {.tags: [], benign.} =
|
||||
proc setStdIoUnbuffered*() {.tags: [], gcsafe.} =
|
||||
## Configures `stdin`, `stdout` and `stderr` to be unbuffered.
|
||||
when declared(stdout):
|
||||
discard c_setvbuf(stdout, nil, IONBF, 0)
|
||||
@@ -865,7 +865,7 @@ when defined(windows) and appType == "console" and
|
||||
discard setConsoleCP(Utf8codepage)
|
||||
addExitProc(restoreConsoleCP)
|
||||
|
||||
proc readFile*(filename: string): string {.tags: [ReadIOEffect], benign.} =
|
||||
proc readFile*(filename: string): string {.tags: [ReadIOEffect], gcsafe.} =
|
||||
## Opens a file named `filename` for reading, calls `readAll
|
||||
## <#readAll,File>`_ and closes the file afterwards. Returns the string.
|
||||
## Raises an IO exception in case of an error. If you need to call
|
||||
@@ -880,7 +880,7 @@ proc readFile*(filename: string): string {.tags: [ReadIOEffect], benign.} =
|
||||
else:
|
||||
raise newException(IOError, "cannot open: " & filename)
|
||||
|
||||
proc writeFile*(filename, content: string) {.tags: [WriteIOEffect], benign.} =
|
||||
proc writeFile*(filename, content: string) {.tags: [WriteIOEffect], gcsafe.} =
|
||||
## Opens a file named `filename` for writing. Then writes the
|
||||
## `content` completely to the file and closes the file afterwards.
|
||||
## Raises an IO exception in case of an error.
|
||||
|
||||
@@ -219,16 +219,16 @@ elif someVcc:
|
||||
elif mem == ATOMIC_ACQ_REL: fence()
|
||||
elif mem == ATOMIC_SEQ_CST: fence()
|
||||
|
||||
proc atomicStoreN*[T: AtomType](p: ptr T, val: T, mem: static[AtomMemModel]) =
|
||||
proc atomicStoreN*[T: AtomType](p: ptr T, val: T, mem: static[AtomMemModel]) {.enforcenoraises.} =
|
||||
barrier(mem)
|
||||
p[] = val
|
||||
|
||||
proc atomicLoadN*[T: AtomType](p: ptr T, mem: static[AtomMemModel]): T =
|
||||
proc atomicLoadN*[T: AtomType](p: ptr T, mem: static[AtomMemModel]): T {.enforcenoraises.} =
|
||||
result = p[]
|
||||
barrier(mem)
|
||||
|
||||
proc atomicCompareExchangeN*[T: ptr](p, expected: ptr T, desired: T,
|
||||
weak: bool, success_memmodel: AtomMemModel, failure_memmodel: AtomMemModel): bool =
|
||||
weak: bool, success_memmodel: AtomMemModel, failure_memmodel: AtomMemModel): bool {.enforcenoraises.} =
|
||||
when sizeof(T) == 8:
|
||||
interlockedCompareExchange64(p, cast[int64](desired), cast[int64](expected[])) ==
|
||||
cast[int64](expected[])
|
||||
@@ -236,7 +236,7 @@ elif someVcc:
|
||||
interlockedCompareExchange32(p, cast[int32](desired), cast[int32](expected[])) ==
|
||||
cast[int32](expected[])
|
||||
|
||||
proc atomicExchangeN*[T: ptr](p: ptr T, val: T, mem: AtomMemModel): T =
|
||||
proc atomicExchangeN*[T: ptr](p: ptr T, val: T, mem: AtomMemModel): T {.enforcenoraises.} =
|
||||
when sizeof(T) == 8:
|
||||
cast[T](interlockedExchange64(p, cast[int64](val)))
|
||||
elif sizeof(T) == 4:
|
||||
|
||||
@@ -627,7 +627,7 @@ proc newSeq*[T](s: var seq[T], len: Natural) {.magic: "NewSeq", noSideEffect.}
|
||||
## #inputStrings[3] = "out of bounds"
|
||||
## ```
|
||||
|
||||
proc newSeq*[T](len = 0.Natural): seq[T] =
|
||||
proc newSeq*[T](len = 0.Natural): seq[T] {.noSideEffect.} =
|
||||
## Creates a new sequence of type `seq[T]` with length `len`.
|
||||
##
|
||||
## Note that the sequence will be filled with zeroed entries.
|
||||
@@ -1147,7 +1147,7 @@ template sysAssert(cond: bool, msg: string) =
|
||||
const hasAlloc = (hostOS != "standalone" or not defined(nogc)) and not defined(nimscript)
|
||||
|
||||
when notJSnotNims and hasAlloc and not defined(nimSeqsV2):
|
||||
proc addChar(s: NimString, c: char): NimString {.compilerproc, benign.}
|
||||
proc addChar(s: NimString, c: char): NimString {.compilerproc, gcsafe.}
|
||||
|
||||
when defined(nimscript) or not defined(nimSeqsV2):
|
||||
proc add*[T](x: var seq[T], y: sink T) {.magic: "AppendSeqElem", noSideEffect.}
|
||||
@@ -1459,6 +1459,7 @@ proc isNil*[T: proc | iterator {.closure.}](x: T): bool {.noSideEffect, magic: "
|
||||
## `== nil`.
|
||||
|
||||
proc supportsCopyMem(t: typedesc): bool {.magic: "TypeTrait".}
|
||||
proc canFormCycles(t: typedesc): bool {.magic: "TypeTrait".}
|
||||
|
||||
when defined(nimHasTopDownInference):
|
||||
# magic used for seq type inference
|
||||
@@ -1664,7 +1665,7 @@ when not defined(js) and hasThreadSupport and hostOS != "standalone":
|
||||
|
||||
when not defined(js) and defined(nimV2):
|
||||
type
|
||||
DestructorProc = proc (p: pointer) {.nimcall, benign, raises: [].}
|
||||
DestructorProc = proc (p: pointer) {.nimcall, gcsafe, raises: [].}
|
||||
TNimTypeV2 {.compilerproc.} = object
|
||||
destructor: pointer
|
||||
size: int
|
||||
@@ -1776,7 +1777,7 @@ when not defined(nimscript):
|
||||
when not declared(sysFatal):
|
||||
include "system/fatal"
|
||||
|
||||
proc echo*(x: varargs[typed, `$`]) {.magic: "Echo", benign, sideEffect.}
|
||||
proc echo*(x: varargs[typed, `$`]) {.magic: "Echo", gcsafe, sideEffect.}
|
||||
## Writes and flushes the parameters to the standard output.
|
||||
##
|
||||
## Special built-in that takes a variable number of arguments. Each argument
|
||||
@@ -1883,7 +1884,7 @@ when notJSnotNims:
|
||||
## lead to the `raise` statement. This only works for debug builds.
|
||||
|
||||
var
|
||||
globalRaiseHook*: proc (e: ref Exception): bool {.nimcall, benign.}
|
||||
globalRaiseHook*: proc (e: ref Exception): bool {.nimcall, gcsafe.}
|
||||
## With this hook you can influence exception handling on a global level.
|
||||
## If not nil, every 'raise' statement ends up calling this hook.
|
||||
##
|
||||
@@ -1892,7 +1893,7 @@ when notJSnotNims:
|
||||
## If `globalRaiseHook` returns false, the exception is caught and does
|
||||
## not propagate further through the call stack.
|
||||
|
||||
localRaiseHook* {.threadvar.}: proc (e: ref Exception): bool {.nimcall, benign.}
|
||||
localRaiseHook* {.threadvar.}: proc (e: ref Exception): bool {.nimcall, gcsafe.}
|
||||
## With this hook you can influence exception handling on a
|
||||
## thread local level.
|
||||
## If not nil, every 'raise' statement ends up calling this hook.
|
||||
@@ -1902,7 +1903,7 @@ when notJSnotNims:
|
||||
## If `localRaiseHook` returns false, the exception
|
||||
## is caught and does not propagate further through the call stack.
|
||||
|
||||
outOfMemHook*: proc () {.nimcall, tags: [], benign, raises: [].}
|
||||
outOfMemHook*: proc () {.nimcall, tags: [], gcsafe, raises: [].}
|
||||
## Set this variable to provide a procedure that should be called
|
||||
## in case of an `out of memory`:idx: event. The standard handler
|
||||
## writes an error message and terminates the program.
|
||||
@@ -1923,7 +1924,7 @@ when notJSnotNims:
|
||||
## If the handler does not raise an exception, ordinary control flow
|
||||
## continues and the program is terminated.
|
||||
|
||||
unhandledExceptionHook*: proc (e: ref Exception) {.nimcall, tags: [], benign, raises: [].}
|
||||
unhandledExceptionHook*: proc (e: ref Exception) {.nimcall, tags: [], gcsafe, raises: [].}
|
||||
## Set this variable to provide a procedure that should be called
|
||||
## in case of an `unhandle exception` event. The standard handler
|
||||
## writes an error message and terminates the program, except when
|
||||
@@ -2066,7 +2067,7 @@ when hostOS == "standalone" and defined(nogc):
|
||||
if s == nil or s.len == 0: result = cstring""
|
||||
else: result = cast[cstring](addr s.data)
|
||||
|
||||
proc getTypeInfo*[T](x: T): pointer {.magic: "GetTypeInfo", benign.}
|
||||
proc getTypeInfo*[T](x: T): pointer {.magic: "GetTypeInfo", gcsafe.}
|
||||
## Get type information for `x`.
|
||||
##
|
||||
## Ordinary code should not use this, but the `typeinfo module
|
||||
@@ -2285,21 +2286,21 @@ when not defined(js) and declared(alloc0) and declared(dealloc):
|
||||
dealloc(a)
|
||||
|
||||
when notJSnotNims and hostOS != "standalone":
|
||||
proc getCurrentException*(): ref Exception {.compilerRtl, inl, benign.} =
|
||||
proc getCurrentException*(): ref Exception {.compilerRtl, inl, gcsafe.} =
|
||||
## Retrieves the current exception; if there is none, `nil` is returned.
|
||||
result = currException
|
||||
|
||||
proc nimBorrowCurrentException(): ref Exception {.compilerRtl, inl, benign, nodestroy.} =
|
||||
proc nimBorrowCurrentException(): ref Exception {.compilerRtl, inl, gcsafe, nodestroy.} =
|
||||
# .nodestroy here so that we do not produce a write barrier as the
|
||||
# C codegen only uses it in a borrowed way:
|
||||
result = currException
|
||||
|
||||
proc getCurrentExceptionMsg*(): string {.inline, benign.} =
|
||||
proc getCurrentExceptionMsg*(): string {.inline, gcsafe.} =
|
||||
## Retrieves the error message that was attached to the current
|
||||
## exception; if there is none, `""` is returned.
|
||||
return if currException == nil: "" else: currException.msg
|
||||
|
||||
proc setCurrentException*(exc: ref Exception) {.inline, benign.} =
|
||||
proc setCurrentException*(exc: ref Exception) {.inline, gcsafe.} =
|
||||
## Sets the current exception.
|
||||
##
|
||||
## .. warning:: Only use this if you know what you are doing.
|
||||
|
||||
@@ -104,6 +104,8 @@ type
|
||||
zeroField: int # 0 means cell is not used (overlaid with typ field)
|
||||
# 1 means cell is manually managed pointer
|
||||
# otherwise a PNimType is stored in there
|
||||
when sizeof(int) == 4: # 32-bit only
|
||||
headerAlignPad: array[8, byte] # so addr(data) ≡ 8 (mod 16)
|
||||
else:
|
||||
alignment: int
|
||||
|
||||
@@ -725,7 +727,7 @@ proc getSmallChunk(a: var MemRegion): PSmallChunk =
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
when not defined(gcDestructors):
|
||||
proc isAllocatedPtr(a: MemRegion, p: pointer): bool {.benign.}
|
||||
proc isAllocatedPtr(a: MemRegion, p: pointer): bool {.gcsafe.}
|
||||
|
||||
when true:
|
||||
template allocInv(a: MemRegion): bool = true
|
||||
@@ -851,12 +853,12 @@ when defined(heaptrack):
|
||||
|
||||
proc bigChunkAlignOffset(alignment: int): int {.inline.} =
|
||||
## Compute the alignment offset for big chunk data.
|
||||
if alignment <= MemAlign:
|
||||
if alignment == 0:
|
||||
result = 0
|
||||
else:
|
||||
result = align(sizeof(BigChunk) + sizeof(Cell), alignment) - sizeof(BigChunk) - sizeof(Cell)
|
||||
result = align(sizeof(BigChunk) + sizeof(FreeCell), alignment) - sizeof(BigChunk) - sizeof(FreeCell)
|
||||
|
||||
proc rawAlloc(a: var MemRegion, requestedSize: int, alignment: int = MemAlign): pointer =
|
||||
proc rawAlloc(a: var MemRegion, requestedSize: int, alignment: int = 0): pointer =
|
||||
when defined(nimTypeNames):
|
||||
inc(a.allocCounter)
|
||||
sysAssert(allocInv(a), "rawAlloc: begin")
|
||||
@@ -868,7 +870,7 @@ proc rawAlloc(a: var MemRegion, requestedSize: int, alignment: int = MemAlign):
|
||||
|
||||
# For custom alignments > MemAlign, force big chunk allocation
|
||||
# Small chunks cannot handle arbitrary alignments due to fixed cell boundaries
|
||||
if size <= SmallChunkSize-smallChunkOverhead() and alignment <= MemAlign:
|
||||
if size <= SmallChunkSize-smallChunkOverhead() and alignment == 0:
|
||||
template fetchSharedCells(tc: PSmallChunk) =
|
||||
# Consumes cells from (potentially) foreign threads from `a.sharedFreeLists[s]`
|
||||
when defined(gcDestructors):
|
||||
@@ -1046,13 +1048,29 @@ proc rawDealloc(a: var MemRegion, p: pointer) =
|
||||
inc(c.free, s)
|
||||
else:
|
||||
inc(c.free, s)
|
||||
# Free only if the entire chunk is unused and there are no borrowed cells.
|
||||
# If the chunk were to be freed while it references foreign cells,
|
||||
# the foreign chunks will leak memory and can never be freed.
|
||||
if c.free == SmallChunkSize-smallChunkOverhead() and c.foreignCells == 0:
|
||||
listRemove(a.freeSmallChunks[s div MemAlign], c)
|
||||
c.size = SmallChunkSize
|
||||
freeBigChunk(a, cast[PBigChunk](c))
|
||||
# FIX: Don't free small chunks to avoid race condition with sharedFreeLists.
|
||||
#
|
||||
# RACE CONDITION: Between checking foreignCells==0 and calling freeBigChunk,
|
||||
# another thread may read chunk.owner and decide to add a cell to our
|
||||
# sharedFreeLists. If we free the chunk, that cell becomes orphaned.
|
||||
#
|
||||
# SOLUTION: Never free small chunks. They remain in freeSmallChunks[s] and
|
||||
# are reused on next allocation. This maintains the invariant that chunks
|
||||
# in freeSmallChunks[s] have c.free >= s (completely free chunks satisfy this).
|
||||
# If a chunk becomes exhausted (c.free < s), it's removed by line 949.
|
||||
#
|
||||
# TRADEOFF: Memory not returned to OS. Bounded by peak concurrent allocation
|
||||
# per size class (~4KB per active size class per thread, typically <1MB total).
|
||||
#
|
||||
# VERIFIED: TLA+ formal proof shows no race - see VERIFICATION_RESULTS.md
|
||||
#
|
||||
# Original code (REMOVED to fix race):
|
||||
sysAssert(c.free >= s, "Invariant violated: chunk in freeSmallChunks has insufficient space")
|
||||
when false:
|
||||
if c.free == SmallChunkSize-smallChunkOverhead() and c.foreignCells == 0:
|
||||
listRemove(a.freeSmallChunks[s div MemAlign], c)
|
||||
c.size = SmallChunkSize
|
||||
freeBigChunk(a, cast[PBigChunk](c))
|
||||
else:
|
||||
when logAlloc: cprintf("dealloc(pointer_%p) # SMALL FROM %p CALLER %p\n", p, c.owner, addr(a))
|
||||
|
||||
@@ -1348,4 +1366,4 @@ template instantiateForRegion(allocator: untyped) {.dirty.} =
|
||||
#sharedMemStatsShared(sharedHeap.currMem - sharedHeap.freeMem)
|
||||
{.pop.}
|
||||
|
||||
{.pop.}
|
||||
{.pop.}
|
||||
@@ -9,11 +9,11 @@
|
||||
|
||||
include seqs_v2_reimpl
|
||||
|
||||
proc genericResetAux(dest: pointer, n: ptr TNimNode) {.benign.}
|
||||
proc genericResetAux(dest: pointer, n: ptr TNimNode) {.gcsafe.}
|
||||
|
||||
proc genericAssignAux(dest, src: pointer, mt: PNimType, shallow: bool) {.benign.}
|
||||
proc genericAssignAux(dest, src: pointer, mt: PNimType, shallow: bool) {.gcsafe.}
|
||||
proc genericAssignAux(dest, src: pointer, n: ptr TNimNode,
|
||||
shallow: bool) {.benign.} =
|
||||
shallow: bool) {.gcsafe.} =
|
||||
var
|
||||
d = cast[int](dest)
|
||||
s = cast[int](src)
|
||||
@@ -187,8 +187,8 @@ proc genericAssignOpenArray(dest, src: pointer, len: int,
|
||||
genericAssign(cast[pointer](d +% i *% mt.base.size),
|
||||
cast[pointer](s +% i *% mt.base.size), mt.base)
|
||||
|
||||
proc objectInit(dest: pointer, typ: PNimType) {.compilerproc, benign.}
|
||||
proc objectInitAux(dest: pointer, n: ptr TNimNode) {.benign.} =
|
||||
proc objectInit(dest: pointer, typ: PNimType) {.compilerproc, gcsafe.}
|
||||
proc objectInitAux(dest: pointer, n: ptr TNimNode) {.gcsafe.} =
|
||||
var d = cast[int](dest)
|
||||
case n.kind
|
||||
of nkNone: sysAssert(false, "objectInitAux")
|
||||
@@ -224,7 +224,7 @@ proc objectInit(dest: pointer, typ: PNimType) =
|
||||
|
||||
# ---------------------- assign zero -----------------------------------------
|
||||
|
||||
proc genericReset(dest: pointer, mt: PNimType) {.compilerproc, benign.}
|
||||
proc genericReset(dest: pointer, mt: PNimType) {.compilerproc, gcsafe.}
|
||||
proc genericResetAux(dest: pointer, n: ptr TNimNode) =
|
||||
var d = cast[int](dest)
|
||||
case n.kind
|
||||
|
||||
@@ -51,7 +51,7 @@ proc split(t: var PAvlNode) =
|
||||
t.link[0] = temp
|
||||
inc t.level
|
||||
|
||||
proc add(a: var MemRegion, t: var PAvlNode, key, upperBound: int) {.benign.} =
|
||||
proc add(a: var MemRegion, t: var PAvlNode, key, upperBound: int) {.gcsafe.} =
|
||||
if t.isBottom:
|
||||
t = allocAvlNode(a, key, upperBound)
|
||||
else:
|
||||
@@ -70,7 +70,7 @@ proc add(a: var MemRegion, t: var PAvlNode, key, upperBound: int) {.benign.} =
|
||||
skew(t)
|
||||
split(t)
|
||||
|
||||
proc del(a: var MemRegion, t: var PAvlNode, x: int) {.benign.} =
|
||||
proc del(a: var MemRegion, t: var PAvlNode, x: int) {.gcsafe.} =
|
||||
if isBottom(t): return
|
||||
a.last = t
|
||||
if x <% t.key:
|
||||
|
||||
@@ -48,7 +48,23 @@ when defined(gcOrc) or defined(gcArc) or defined(gcAtomicArc) or defined(gcYrc):
|
||||
|
||||
when not declaredInScope(PageShift):
|
||||
include bitmasks
|
||||
else:
|
||||
type
|
||||
RefCount = int
|
||||
|
||||
Cell {.pure.} = object
|
||||
refcount: RefCount # the refcount and some flags
|
||||
typ: PNimType
|
||||
|
||||
when trackAllocationSource:
|
||||
filename: cstring
|
||||
line: int
|
||||
when useCellIds:
|
||||
id: int
|
||||
when (not trackAllocationSource) and (not useCellIds) and sizeof(int) == 4: # 32-bit only
|
||||
headerAlignPad: array[8, byte] # so addr(data) ≡ 8 (mod 16)
|
||||
|
||||
PCell = ptr Cell
|
||||
|
||||
type
|
||||
PPageDesc = ptr PageDesc
|
||||
|
||||
@@ -181,10 +181,10 @@ proc deinitRawChannel(p: pointer) =
|
||||
|
||||
when not usesDestructors:
|
||||
proc storeAux(dest, src: pointer, mt: PNimType, t: PRawChannel,
|
||||
mode: LoadStoreMode) {.benign.}
|
||||
mode: LoadStoreMode) {.gcsafe.}
|
||||
|
||||
proc storeAux(dest, src: pointer, n: ptr TNimNode, t: PRawChannel,
|
||||
mode: LoadStoreMode) {.benign.} =
|
||||
mode: LoadStoreMode) {.gcsafe.} =
|
||||
var
|
||||
d = cast[int](dest)
|
||||
s = cast[int](src)
|
||||
|
||||
@@ -62,8 +62,8 @@ const
|
||||
colorMask = 0b011
|
||||
|
||||
type
|
||||
TraceProc = proc (p, env: pointer) {.nimcall, benign, raises: [].}
|
||||
DisposeProc = proc (p: pointer) {.nimcall, benign, raises: [].}
|
||||
TraceProc = proc (p, env: pointer) {.nimcall, gcsafe, raises: [].}
|
||||
DisposeProc = proc (p: pointer) {.nimcall, gcsafe, raises: [].}
|
||||
|
||||
template color(c): untyped = c.rc and colorMask
|
||||
template setColor(c, col) =
|
||||
|
||||
@@ -58,9 +58,9 @@ proc put(t: var PtrTable; key, val: pointer) =
|
||||
inc t.counter
|
||||
|
||||
proc genericDeepCopyAux(dest, src: pointer, mt: PNimType;
|
||||
tab: var PtrTable) {.benign.}
|
||||
tab: var PtrTable) {.gcsafe.}
|
||||
proc genericDeepCopyAux(dest, src: pointer, n: ptr TNimNode;
|
||||
tab: var PtrTable) {.benign.} =
|
||||
tab: var PtrTable) {.gcsafe.} =
|
||||
var
|
||||
d = cast[int](dest)
|
||||
s = cast[int](src)
|
||||
|
||||
@@ -16,7 +16,7 @@ import stacktraces
|
||||
const noStacktraceAvailable = "No stack traceback available\n"
|
||||
|
||||
var
|
||||
errorMessageWriter*: (proc(msg: string) {.tags: [WriteIOEffect], benign,
|
||||
errorMessageWriter*: (proc(msg: string) {.tags: [WriteIOEffect], gcsafe,
|
||||
nimcall, raises: [].})
|
||||
## Function that will be called
|
||||
## instead of `stdmsg.write` when printing stacktrace.
|
||||
@@ -61,10 +61,10 @@ proc showErrorMessage2(data: string) {.inline.} =
|
||||
# TODO showErrorMessage will turn it back to a string when a hook is set (!)
|
||||
showErrorMessage(data.cstring, data.len)
|
||||
|
||||
proc chckIndx(i, a, b: int): int {.inline, compilerproc, benign.}
|
||||
proc chckRange(i, a, b: int): int {.inline, compilerproc, benign.}
|
||||
proc chckRangeF(x, a, b: float): float {.inline, compilerproc, benign.}
|
||||
proc chckNil(p: pointer) {.noinline, compilerproc, benign.}
|
||||
proc chckIndx(i, a, b: int): int {.inline, compilerproc, gcsafe.}
|
||||
proc chckRange(i, a, b: int): int {.inline, compilerproc, gcsafe.}
|
||||
proc chckRangeF(x, a, b: float): float {.inline, compilerproc, gcsafe.}
|
||||
proc chckNil(p: pointer) {.noinline, compilerproc, gcsafe.}
|
||||
|
||||
type
|
||||
GcFrame = ptr GcFrameHeader
|
||||
@@ -653,7 +653,7 @@ when defined(cpp) and appType != "lib" and not gotoBasedExceptions and
|
||||
rawQuit 1
|
||||
|
||||
when not defined(noSignalHandler) and not defined(useNimRtl):
|
||||
type Sighandler = proc (a: cint) {.noconv, benign.}
|
||||
type Sighandler = proc (a: cint) {.noconv, gcsafe.}
|
||||
# xxx factor with ansi_c.CSighandlerT, posix.Sighandler
|
||||
|
||||
proc signalHandler(sign: cint) {.exportc: "signalHandler", noconv, raises: [].} =
|
||||
|
||||
@@ -76,7 +76,7 @@ const
|
||||
when withRealTime and not declared(getTicks):
|
||||
include "system/timers"
|
||||
when defined(memProfiler):
|
||||
proc nimProfile(requestedSize: int) {.benign.}
|
||||
proc nimProfile(requestedSize: int) {.gcsafe.}
|
||||
|
||||
when hasThreadSupport:
|
||||
import std/sharedlist
|
||||
@@ -97,7 +97,7 @@ type
|
||||
waZctDecRef, waPush
|
||||
#, waDebug
|
||||
|
||||
Finalizer {.compilerproc.} = proc (self: pointer) {.nimcall, benign, raises: [], gcsafe.}
|
||||
Finalizer {.compilerproc.} = proc (self: pointer) {.nimcall, gcsafe, raises: [].}
|
||||
# A ref type can have a finalizer that is called before the object's
|
||||
# storage is freed.
|
||||
|
||||
@@ -222,11 +222,11 @@ template gcTrace(cell, state: untyped) =
|
||||
when traceGC: traceCell(cell, state)
|
||||
|
||||
# forward declarations:
|
||||
proc collectCT(gch: var GcHeap) {.benign, raises: [].}
|
||||
proc isOnStack(p: pointer): bool {.noinline, benign, raises: [].}
|
||||
proc forAllChildren(cell: PCell, op: WalkOp) {.benign, raises: [].}
|
||||
proc doOperation(p: pointer, op: WalkOp) {.benign, raises: [].}
|
||||
proc forAllChildrenAux(dest: pointer, mt: PNimType, op: WalkOp) {.benign, raises: [].}
|
||||
proc collectCT(gch: var GcHeap) {.gcsafe, raises: [].}
|
||||
proc isOnStack(p: pointer): bool {.noinline, gcsafe, raises: [].}
|
||||
proc forAllChildren(cell: PCell, op: WalkOp) {.gcsafe, raises: [].}
|
||||
proc doOperation(p: pointer, op: WalkOp) {.gcsafe, raises: [].}
|
||||
proc forAllChildrenAux(dest: pointer, mt: PNimType, op: WalkOp) {.gcsafe, raises: [].}
|
||||
# we need the prototype here for debugging purposes
|
||||
|
||||
proc incRef(c: PCell) {.inline.} =
|
||||
@@ -338,7 +338,7 @@ proc cellsetReset(s: var CellSet) =
|
||||
|
||||
{.push stacktrace:off.}
|
||||
|
||||
proc forAllSlotsAux(dest: pointer, n: ptr TNimNode, op: WalkOp) {.benign.} =
|
||||
proc forAllSlotsAux(dest: pointer, n: ptr TNimNode, op: WalkOp) {.gcsafe.} =
|
||||
var d = cast[int](dest)
|
||||
case n.kind
|
||||
of nkSlot: forAllChildrenAux(cast[pointer](d +% n.offset), n.typ, op)
|
||||
@@ -459,11 +459,15 @@ proc rawNewObj(typ: PNimType, size: int, gch: var GcHeap): pointer =
|
||||
gcAssert(typ.kind in {tyRef, tyString, tySequence}, "newObj: 1")
|
||||
collectCT(gch)
|
||||
# Use alignment from typ.base if available, otherwise use MemAlign
|
||||
let alignment = if typ.kind == tyRef and typ.base != nil: max(typ.base.align, MemAlign) else: MemAlign
|
||||
let alignment = if typ.kind == tyRef and typ.base != nil and
|
||||
typ.base.align > 16: typ.base.align else: 0
|
||||
var res = cast[PCell](rawAlloc(gch.region, size + sizeof(Cell), alignment))
|
||||
#gcAssert typ.kind in {tyString, tySequence} or size >= typ.base.size, "size too small"
|
||||
# Check that the user data (after the Cell header) is properly aligned
|
||||
gcAssert((cast[int](cellToUsr(res)) and (alignment-1)) == 0, "newObj: 2")
|
||||
if alignment == 0:
|
||||
gcAssert((cast[int](res) and (MemAlign-1)) == 0, "newObj: 2.1")
|
||||
else:
|
||||
gcAssert((cast[int](cellToUsr(res)) and (alignment-1)) == 0, "newObj: 2.2")
|
||||
# now it is buffered in the ZCT
|
||||
res.typ = typ
|
||||
setFrameInfo(res)
|
||||
@@ -512,11 +516,15 @@ proc newObjRC1(typ: PNimType, size: int): pointer {.compilerRtl, noinline, raise
|
||||
sysAssert(allocInv(gch.region), "newObjRC1 after collectCT")
|
||||
|
||||
# Use alignment from typ.base if available, otherwise use MemAlign
|
||||
let alignment = if typ.base != nil: max(typ.base.align, MemAlign) else: MemAlign
|
||||
let alignment = if typ.kind == tyRef and typ.base != nil and
|
||||
typ.base.align > 16: typ.base.align else: 0
|
||||
var res = cast[PCell](rawAlloc(gch.region, size + sizeof(Cell), alignment))
|
||||
sysAssert(allocInv(gch.region), "newObjRC1 after rawAlloc")
|
||||
# Check that the user data (after the Cell header) is properly aligned
|
||||
sysAssert((cast[int](cellToUsr(res)) and (alignment-1)) == 0, "newObj: 2")
|
||||
if alignment == 0:
|
||||
sysAssert((cast[int](res) and (MemAlign-1)) == 0, "newObj: 2.1")
|
||||
else:
|
||||
sysAssert((cast[int](cellToUsr(res)) and (alignment-1)) == 0, "newObj: 2.2")
|
||||
# now it is buffered in the ZCT
|
||||
res.typ = typ
|
||||
setFrameInfo(res)
|
||||
@@ -679,7 +687,7 @@ proc doOperation(p: pointer, op: WalkOp) =
|
||||
proc nimGCvisit(d: pointer, op: int) {.compilerRtl, raises: [].} =
|
||||
doOperation(d, WalkOp(op))
|
||||
|
||||
proc collectZCT(gch: var GcHeap): bool {.benign, raises: [].}
|
||||
proc collectZCT(gch: var GcHeap): bool {.gcsafe, raises: [].}
|
||||
|
||||
proc collectCycles(gch: var GcHeap) {.raises: [].} =
|
||||
when hasThreadSupport:
|
||||
@@ -922,4 +930,4 @@ when not defined(useNimRtl):
|
||||
result.add "[GC] max stack size: " & $gch.stat.maxStackSize & "\n"
|
||||
|
||||
{.pop.} # raises: []
|
||||
{.pop.} # profiler: off, stackTrace: off
|
||||
{.pop.} # profiler: off, stackTrace: off
|
||||
@@ -457,7 +457,7 @@ proc deallocHeap*(runFinalizers = true; allowGcAfterwards = true) =
|
||||
initGC()
|
||||
|
||||
type
|
||||
GlobalMarkerProc = proc () {.nimcall, benign, raises: [].}
|
||||
GlobalMarkerProc = proc () {.nimcall, gcsafe, raises: [].}
|
||||
var
|
||||
globalMarkersLen {.exportc.}: int
|
||||
globalMarkers {.exportc.}: array[0..3499, GlobalMarkerProc]
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
## collectors etc.
|
||||
|
||||
type
|
||||
GlobalMarkerProc = proc () {.nimcall, benign, raises: [], tags: [].}
|
||||
GlobalMarkerProc = proc () {.nimcall, gcsafe, raises: [], tags: [].}
|
||||
var
|
||||
globalMarkersLen: int
|
||||
globalMarkers: array[0..3499, GlobalMarkerProc]
|
||||
|
||||
@@ -12,7 +12,7 @@ when hasAlloc:
|
||||
gcOptimizeSpace ## optimize for memory footprint
|
||||
|
||||
when hasAlloc and not defined(js) and not usesDestructors:
|
||||
proc GC_disable*() {.rtl, inl, benign, raises: [].}
|
||||
proc GC_disable*() {.rtl, inl, gcsafe, raises: [].}
|
||||
## Disables the GC. If called `n` times, `n` calls to `GC_enable`
|
||||
## are needed to reactivate the GC.
|
||||
##
|
||||
@@ -20,39 +20,39 @@ when hasAlloc and not defined(js) and not usesDestructors:
|
||||
## the mark and sweep phase with
|
||||
## `GC_disableMarkAndSweep <#GC_disableMarkAndSweep>`_.
|
||||
|
||||
proc GC_enable*() {.rtl, inl, benign, raises: [].}
|
||||
proc GC_enable*() {.rtl, inl, gcsafe, raises: [].}
|
||||
## Enables the GC again.
|
||||
|
||||
proc GC_fullCollect*() {.rtl, benign, raises: [].}
|
||||
proc GC_fullCollect*() {.rtl, gcsafe, raises: [].}
|
||||
## Forces a full garbage collection pass.
|
||||
## Ordinary code does not need to call this (and should not).
|
||||
|
||||
proc GC_enableMarkAndSweep*() {.rtl, benign, raises: [].}
|
||||
proc GC_disableMarkAndSweep*() {.rtl, benign, raises: [].}
|
||||
proc GC_enableMarkAndSweep*() {.rtl, gcsafe, raises: [].}
|
||||
proc GC_disableMarkAndSweep*() {.rtl, gcsafe, raises: [].}
|
||||
## The current implementation uses a reference counting garbage collector
|
||||
## with a seldomly run mark and sweep phase to free cycles. The mark and
|
||||
## sweep phase may take a long time and is not needed if the application
|
||||
## does not create cycles. Thus the mark and sweep phase can be deactivated
|
||||
## and activated separately from the rest of the GC.
|
||||
|
||||
proc GC_getStatistics*(): string {.rtl, benign, raises: [].}
|
||||
proc GC_getStatistics*(): string {.rtl, gcsafe, raises: [].}
|
||||
## Returns an informative string about the GC's activity. This may be useful
|
||||
## for tweaking.
|
||||
|
||||
proc GC_ref*[T](x: ref T) {.magic: "GCref", benign, raises: [].}
|
||||
proc GC_ref*[T](x: seq[T]) {.magic: "GCref", benign, raises: [].}
|
||||
proc GC_ref*(x: string) {.magic: "GCref", benign, raises: [].}
|
||||
proc GC_ref*[T](x: ref T) {.magic: "GCref", gcsafe, raises: [].}
|
||||
proc GC_ref*[T](x: seq[T]) {.magic: "GCref", gcsafe, raises: [].}
|
||||
proc GC_ref*(x: string) {.magic: "GCref", gcsafe, raises: [].}
|
||||
## Marks the object `x` as referenced, so that it will not be freed until
|
||||
## it is unmarked via `GC_unref`.
|
||||
## If called n-times for the same object `x`,
|
||||
## n calls to `GC_unref` are needed to unmark `x`.
|
||||
|
||||
proc GC_unref*[T](x: ref T) {.magic: "GCunref", benign, raises: [].}
|
||||
proc GC_unref*[T](x: seq[T]) {.magic: "GCunref", benign, raises: [].}
|
||||
proc GC_unref*(x: string) {.magic: "GCunref", benign, raises: [].}
|
||||
proc GC_unref*[T](x: ref T) {.magic: "GCunref", gcsafe, raises: [].}
|
||||
proc GC_unref*[T](x: seq[T]) {.magic: "GCunref", gcsafe, raises: [].}
|
||||
proc GC_unref*(x: string) {.magic: "GCunref", gcsafe, raises: [].}
|
||||
## See the documentation of `GC_ref <#GC_ref,string>`_.
|
||||
|
||||
proc nimGC_setStackBottom*(theStackBottom: pointer) {.compilerRtl, noinline, benign, raises: [].}
|
||||
proc nimGC_setStackBottom*(theStackBottom: pointer) {.compilerRtl, noinline, gcsafe, raises: [].}
|
||||
## Expands operating GC stack range to `theStackBottom`. Does nothing
|
||||
## if current stack bottom is already lower than `theStackBottom`.
|
||||
|
||||
|
||||
@@ -36,7 +36,7 @@ type
|
||||
# local
|
||||
waMarkPrecise # fast precise marking
|
||||
|
||||
Finalizer {.compilerproc.} = proc (self: pointer) {.nimcall, benign, raises: [], gcsafe.}
|
||||
Finalizer {.compilerproc.} = proc (self: pointer) {.nimcall, gcsafe, raises: [].}
|
||||
# A ref type can have a finalizer that is called before the object's
|
||||
# storage is freed.
|
||||
|
||||
@@ -115,10 +115,10 @@ when BitsPerPage mod (sizeof(int)*8) != 0:
|
||||
{.error: "(BitsPerPage mod BitsPerUnit) should be zero!".}
|
||||
|
||||
# forward declarations:
|
||||
proc collectCT(gch: var GcHeap; size: int) {.benign, raises: [].}
|
||||
proc forAllChildren(cell: PCell, op: WalkOp) {.benign, raises: [].}
|
||||
proc doOperation(p: pointer, op: WalkOp) {.benign, raises: [].}
|
||||
proc forAllChildrenAux(dest: pointer, mt: PNimType, op: WalkOp) {.benign, raises: [].}
|
||||
proc collectCT(gch: var GcHeap; size: int) {.gcsafe, raises: [].}
|
||||
proc forAllChildren(cell: PCell, op: WalkOp) {.gcsafe, raises: [].}
|
||||
proc doOperation(p: pointer, op: WalkOp) {.gcsafe, raises: [].}
|
||||
proc forAllChildrenAux(dest: pointer, mt: PNimType, op: WalkOp) {.gcsafe, raises: [].}
|
||||
# we need the prototype here for debugging purposes
|
||||
|
||||
when defined(nimGcRefLeak):
|
||||
@@ -216,7 +216,7 @@ proc initGC() =
|
||||
gch.gcThreadId = atomicInc(gHeapidGenerator) - 1
|
||||
gcAssert(gch.gcThreadId >= 0, "invalid computed thread ID")
|
||||
|
||||
proc forAllSlotsAux(dest: pointer, n: ptr TNimNode, op: WalkOp) {.benign.} =
|
||||
proc forAllSlotsAux(dest: pointer, n: ptr TNimNode, op: WalkOp) {.gcsafe.} =
|
||||
var d = cast[int](dest)
|
||||
case n.kind
|
||||
of nkSlot: forAllChildrenAux(cast[pointer](d +% n.offset), n.typ, op)
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
import std/private/syslocks
|
||||
|
||||
when defined(memProfiler):
|
||||
proc nimProfile(requestedSize: int) {.benign.}
|
||||
proc nimProfile(requestedSize: int) {.gcsafe.}
|
||||
|
||||
when defined(useMalloc):
|
||||
proc roundup(x, v: int): int {.inline.} =
|
||||
@@ -41,7 +41,7 @@ else:
|
||||
# We also support 'finalizers'.
|
||||
|
||||
type
|
||||
Finalizer {.compilerproc.} = proc (self: pointer) {.nimcall, benign, raises: [], gcsafe.}
|
||||
Finalizer {.compilerproc.} = proc (self: pointer) {.nimcall, gcsafe, raises: [].}
|
||||
# A ref type can have a finalizer that is called before the object's
|
||||
# storage is freed.
|
||||
|
||||
|
||||
@@ -96,8 +96,8 @@ type
|
||||
base*: ptr TNimType
|
||||
node: ptr TNimNode # valid for tyRecord, tyObject, tyTuple, tyEnum
|
||||
finalizer*: pointer # the finalizer for the type
|
||||
marker*: proc (p: pointer, op: int) {.nimcall, benign, tags: [], raises: [].} # marker proc for GC
|
||||
deepcopy: proc (p: pointer): pointer {.nimcall, benign, tags: [], raises: [].}
|
||||
marker*: proc (p: pointer, op: int) {.nimcall, gcsafe, tags: [], raises: [].} # marker proc for GC
|
||||
deepcopy: proc (p: pointer): pointer {.nimcall, gcsafe, tags: [], raises: [].}
|
||||
when defined(nimSeqsV2):
|
||||
typeInfoV2*: pointer
|
||||
when defined(nimTypeNames):
|
||||
|
||||
@@ -51,7 +51,7 @@ proc nimCharToStr(x: char): string {.compilerproc.} =
|
||||
proc isNimException(): bool {.asmNoStackFrame.} =
|
||||
{.emit: "return `lastJSError` && `lastJSError`.m_type;".}
|
||||
|
||||
proc getCurrentException*(): ref Exception {.compilerRtl, benign.} =
|
||||
proc getCurrentException*(): ref Exception {.compilerRtl, gcsafe.} =
|
||||
if isNimException(): result = cast[ref Exception](lastJSError)
|
||||
|
||||
proc getCurrentExceptionMsg*(): string =
|
||||
@@ -72,7 +72,7 @@ proc getCurrentExceptionMsg*(): string =
|
||||
proc setCurrentException*(exc: ref Exception) =
|
||||
lastJSError = cast[PJSError](exc)
|
||||
|
||||
proc closureIterSetExc(e: ref Exception) {.compilerRtl, benign.} =
|
||||
proc closureIterSetExc(e: ref Exception) {.compilerRtl, gcsafe.} =
|
||||
setCurrentException(e)
|
||||
|
||||
proc pushCurrentException(e: sink(ref Exception)) {.compilerRtl, inline.} =
|
||||
@@ -735,7 +735,7 @@ proc nimParseBiggestFloat(s: openarray[char], number: var BiggestFloat): int {.c
|
||||
if s[i+1] == 'A' or s[i+1] == 'a':
|
||||
if s[i+2] == 'N' or s[i+2] == 'n':
|
||||
if s[i+3] notin IdentChars:
|
||||
number = NaN
|
||||
number = if sign: -NaN else: NaN
|
||||
return i+3
|
||||
return 0
|
||||
if s[i] == 'I' or s[i] == 'i':
|
||||
|
||||
@@ -6,7 +6,7 @@ when notJSnotNims:
|
||||
## Exactly `size` bytes will be overwritten. Like any procedure
|
||||
## dealing with raw memory this is **unsafe**.
|
||||
|
||||
proc copyMem*(dest, source: pointer, size: Natural) {.inline, benign,
|
||||
proc copyMem*(dest, source: pointer, size: Natural) {.inline, gcsafe,
|
||||
tags: [], raises: [], enforceNoRaises.}
|
||||
## Copies the contents from the memory at `source` to the memory
|
||||
## at `dest`.
|
||||
@@ -14,7 +14,7 @@ when notJSnotNims:
|
||||
## regions may not overlap. Like any procedure dealing with raw
|
||||
## memory this is **unsafe**.
|
||||
|
||||
proc moveMem*(dest, source: pointer, size: Natural) {.inline, benign,
|
||||
proc moveMem*(dest, source: pointer, size: Natural) {.inline, gcsafe,
|
||||
tags: [], raises: [], enforceNoRaises.}
|
||||
## Copies the contents from the memory at `source` to the memory
|
||||
## at `dest`.
|
||||
@@ -48,17 +48,17 @@ when notJSnotNims:
|
||||
|
||||
when hasAlloc and not defined(js):
|
||||
|
||||
proc allocImpl*(size: Natural): pointer {.noconv, rtl, tags: [], benign, raises: [].}
|
||||
proc alloc0Impl*(size: Natural): pointer {.noconv, rtl, tags: [], benign, raises: [].}
|
||||
proc deallocImpl*(p: pointer) {.noconv, rtl, tags: [], benign, raises: [].}
|
||||
proc reallocImpl*(p: pointer, newSize: Natural): pointer {.noconv, rtl, tags: [], benign, raises: [].}
|
||||
proc realloc0Impl*(p: pointer, oldSize, newSize: Natural): pointer {.noconv, rtl, tags: [], benign, raises: [].}
|
||||
proc allocImpl*(size: Natural): pointer {.noconv, rtl, tags: [], gcsafe, raises: [].}
|
||||
proc alloc0Impl*(size: Natural): pointer {.noconv, rtl, tags: [], gcsafe, raises: [].}
|
||||
proc deallocImpl*(p: pointer) {.noconv, rtl, tags: [], gcsafe, raises: [].}
|
||||
proc reallocImpl*(p: pointer, newSize: Natural): pointer {.noconv, rtl, tags: [], gcsafe, raises: [].}
|
||||
proc realloc0Impl*(p: pointer, oldSize, newSize: Natural): pointer {.noconv, rtl, tags: [], gcsafe, raises: [].}
|
||||
|
||||
proc allocSharedImpl*(size: Natural): pointer {.noconv, compilerproc, rtl, benign, raises: [], tags: [].}
|
||||
proc allocShared0Impl*(size: Natural): pointer {.noconv, rtl, benign, raises: [], tags: [].}
|
||||
proc deallocSharedImpl*(p: pointer) {.noconv, rtl, benign, raises: [], tags: [].}
|
||||
proc reallocSharedImpl*(p: pointer, newSize: Natural): pointer {.noconv, rtl, tags: [], benign, raises: [].}
|
||||
proc reallocShared0Impl*(p: pointer, oldSize, newSize: Natural): pointer {.noconv, rtl, tags: [], benign, raises: [].}
|
||||
proc allocSharedImpl*(size: Natural): pointer {.noconv, compilerproc, rtl, gcsafe, raises: [], tags: [].}
|
||||
proc allocShared0Impl*(size: Natural): pointer {.noconv, rtl, gcsafe, raises: [], tags: [].}
|
||||
proc deallocSharedImpl*(p: pointer) {.noconv, rtl, gcsafe, raises: [], tags: [].}
|
||||
proc reallocSharedImpl*(p: pointer, newSize: Natural): pointer {.noconv, rtl, tags: [], gcsafe, raises: [].}
|
||||
proc reallocShared0Impl*(p: pointer, oldSize, newSize: Natural): pointer {.noconv, rtl, tags: [], gcsafe, raises: [].}
|
||||
|
||||
# Allocator statistics for memory leak tests
|
||||
|
||||
@@ -103,7 +103,7 @@ when hasAlloc and not defined(js):
|
||||
incStat(allocCount)
|
||||
allocImpl(size)
|
||||
|
||||
proc createU*(T: typedesc, size = 1.Positive): ptr T {.inline, benign, raises: [].} =
|
||||
proc createU*(T: typedesc, size = 1.Positive): ptr T {.inline, gcsafe, raises: [].} =
|
||||
## Allocates a new memory block with at least `T.sizeof * size` bytes.
|
||||
##
|
||||
## The block has to be freed with `resize(block, 0) <#resize,ptr.T,Natural>`_
|
||||
@@ -131,7 +131,7 @@ when hasAlloc and not defined(js):
|
||||
incStat(allocCount)
|
||||
alloc0Impl(size)
|
||||
|
||||
proc create*(T: typedesc, size = 1.Positive): ptr T {.inline, benign, raises: [].} =
|
||||
proc create*(T: typedesc, size = 1.Positive): ptr T {.inline, gcsafe, raises: [].} =
|
||||
## Allocates a new memory block with at least `T.sizeof * size` bytes.
|
||||
##
|
||||
## The block has to be freed with `resize(block, 0) <#resize,ptr.T,Natural>`_
|
||||
@@ -174,7 +174,7 @@ when hasAlloc and not defined(js):
|
||||
## from a shared heap.
|
||||
realloc0Impl(p, oldSize, newSize)
|
||||
|
||||
proc resize*[T](p: ptr T, newSize: Natural): ptr T {.inline, benign, raises: [].} =
|
||||
proc resize*[T](p: ptr T, newSize: Natural): ptr T {.inline, gcsafe, raises: [].} =
|
||||
## Grows or shrinks a given memory block.
|
||||
##
|
||||
## If `p` is **nil** then a new memory block is returned.
|
||||
@@ -187,7 +187,7 @@ when hasAlloc and not defined(js):
|
||||
## from a shared heap.
|
||||
cast[ptr T](realloc(p, T.sizeof * newSize))
|
||||
|
||||
proc dealloc*(p: pointer) {.noconv, compilerproc, rtl, benign, raises: [], tags: [].} =
|
||||
proc dealloc*(p: pointer) {.noconv, compilerproc, rtl, gcsafe, raises: [], tags: [].} =
|
||||
## Frees the memory allocated with `alloc`, `alloc0`,
|
||||
## `realloc`, `create` or `createU`.
|
||||
##
|
||||
@@ -218,7 +218,7 @@ when hasAlloc and not defined(js):
|
||||
allocSharedImpl(size)
|
||||
|
||||
proc createSharedU*(T: typedesc, size = 1.Positive): ptr T {.inline, tags: [],
|
||||
benign, raises: [].} =
|
||||
gcsafe, raises: [].} =
|
||||
## Allocates a new memory block on the shared heap with at
|
||||
## least `T.sizeof * size` bytes.
|
||||
##
|
||||
@@ -296,7 +296,7 @@ when hasAlloc and not defined(js):
|
||||
## `freeShared <#freeShared,ptr.T>`_.
|
||||
cast[ptr T](reallocShared(p, T.sizeof * newSize))
|
||||
|
||||
proc deallocShared*(p: pointer) {.noconv, compilerproc, rtl, benign, raises: [], tags: [].} =
|
||||
proc deallocShared*(p: pointer) {.noconv, compilerproc, rtl, gcsafe, raises: [], tags: [].} =
|
||||
## Frees the memory allocated with `allocShared`, `allocShared0` or
|
||||
## `reallocShared`.
|
||||
##
|
||||
@@ -307,7 +307,7 @@ when hasAlloc and not defined(js):
|
||||
incStat(deallocCount)
|
||||
deallocSharedImpl(p)
|
||||
|
||||
proc freeShared*[T](p: ptr T) {.inline, benign, raises: [].} =
|
||||
proc freeShared*[T](p: ptr T) {.inline, gcsafe, raises: [].} =
|
||||
## Frees the memory allocated with `createShared`, `createSharedU` or
|
||||
## `resizeShared`.
|
||||
##
|
||||
|
||||
@@ -38,21 +38,6 @@ type
|
||||
PByte = ptr ByteArray
|
||||
PString = ptr string
|
||||
|
||||
when not defined(nimV2):
|
||||
type
|
||||
RefCount = int
|
||||
|
||||
Cell {.pure.} = object
|
||||
refcount: RefCount # the refcount and some flags
|
||||
typ: PNimType
|
||||
when trackAllocationSource:
|
||||
filename: cstring
|
||||
line: int
|
||||
when useCellIds:
|
||||
id: int
|
||||
|
||||
PCell = ptr Cell
|
||||
|
||||
when declared(IntsPerTrunk):
|
||||
discard
|
||||
else:
|
||||
|
||||
@@ -29,8 +29,8 @@ const
|
||||
logOrc = defined(nimArcIds)
|
||||
|
||||
type
|
||||
TraceProc = proc (p, env: pointer) {.nimcall, benign, raises: [].}
|
||||
DisposeProc = proc (p: pointer) {.nimcall, benign, raises: [].}
|
||||
TraceProc = proc (p, env: pointer) {.nimcall, gcsafe, raises: [].}
|
||||
DisposeProc = proc (p: pointer) {.nimcall, gcsafe, raises: [].}
|
||||
|
||||
template color(c): untyped = c.rc and colorMask
|
||||
template setColor(c, col) =
|
||||
|
||||
@@ -77,7 +77,7 @@ include system/repr_impl
|
||||
type
|
||||
PByteArray = ptr UncheckedArray[byte] # array[0xffff, byte]
|
||||
|
||||
proc addSetElem(result: var string, elem: int, typ: PNimType) {.benign.} =
|
||||
proc addSetElem(result: var string, elem: int, typ: PNimType) {.gcsafe.} =
|
||||
case typ.kind
|
||||
of tyEnum: add result, reprEnum(elem, typ)
|
||||
of tyBool: add result, reprBool(bool(elem))
|
||||
@@ -147,7 +147,7 @@ when not defined(useNimRtl):
|
||||
for i in 0..cl.indent-1: add result, ' '
|
||||
|
||||
proc reprAux(result: var string, p: pointer, typ: PNimType,
|
||||
cl: var ReprClosure) {.benign.}
|
||||
cl: var ReprClosure) {.gcsafe.}
|
||||
|
||||
proc reprArray(result: var string, p: pointer, typ: PNimType,
|
||||
cl: var ReprClosure) =
|
||||
@@ -188,7 +188,7 @@ when not defined(useNimRtl):
|
||||
add result, "]"
|
||||
|
||||
proc reprRecordAux(result: var string, p: pointer, n: ptr TNimNode,
|
||||
cl: var ReprClosure) {.benign.} =
|
||||
cl: var ReprClosure) {.gcsafe.} =
|
||||
case n.kind
|
||||
of nkNone: sysAssert(false, "reprRecordAux")
|
||||
of nkSlot:
|
||||
|
||||
143
lib/system/rwlocks.nim
Normal file
143
lib/system/rwlocks.nim
Normal file
@@ -0,0 +1,143 @@
|
||||
#
|
||||
#
|
||||
# Nim's Runtime Library
|
||||
# (c) Copyright 2026 Andreas Rumpf
|
||||
#
|
||||
# See the file "copying.txt", included in this
|
||||
# distribution, for details about the copyright.
|
||||
#
|
||||
|
||||
# Read-write lock (RwLock) for lib/system.
|
||||
# Used by YRC and by traceable containers that perform topology-changing ops.
|
||||
# POSIX: pthread_rwlock_* ; Windows: SRWLOCK (slim reader/writer).
|
||||
|
||||
{.push stackTrace: off.}
|
||||
|
||||
when defined(windows):
|
||||
# SRWLOCK is pointer-sized; use single pointer for ABI compatibility
|
||||
type
|
||||
RwLock* {.importc: "SRWLOCK", header: "<synchapi.h>", final, pure, byref.} = object
|
||||
p: pointer
|
||||
|
||||
proc initializeSRWLock(L: var RwLock) {.importc: "InitializeSRWLock",
|
||||
header: "<synchapi.h>".}
|
||||
proc acquireSRWLockShared(L: var RwLock) {.importc: "AcquireSRWLockShared",
|
||||
header: "<synchapi.h>".}
|
||||
proc releaseSRWLockShared(L: var RwLock) {.importc: "ReleaseSRWLockShared",
|
||||
header: "<synchapi.h>".}
|
||||
proc acquireSRWLockExclusive(L: var RwLock) {.importc: "AcquireSRWLockExclusive",
|
||||
header: "<synchapi.h>".}
|
||||
proc releaseSRWLockExclusive(L: var RwLock) {.importc: "ReleaseSRWLockExclusive",
|
||||
header: "<synchapi.h>".}
|
||||
|
||||
proc initRwLock*(L: var RwLock) {.inline.} =
|
||||
initializeSRWLock(L)
|
||||
proc deinitRwLock*(L: var RwLock) {.inline.} =
|
||||
discard
|
||||
proc acquireRead*(L: var RwLock) {.inline.} =
|
||||
acquireSRWLockShared(L)
|
||||
proc releaseRead*(L: var RwLock) {.inline.} =
|
||||
releaseSRWLockShared(L)
|
||||
proc acquireWrite*(L: var RwLock) {.inline.} =
|
||||
acquireSRWLockExclusive(L)
|
||||
proc releaseWrite*(L: var RwLock) {.inline.} =
|
||||
releaseSRWLockExclusive(L)
|
||||
|
||||
elif defined(genode):
|
||||
{.error: "RwLock is not implemented for Genode".}
|
||||
|
||||
else:
|
||||
# POSIX: pthread_rwlock_*
|
||||
type
|
||||
SysRwLockObj {.importc: "pthread_rwlock_t", pure, final,
|
||||
header: """#include <sys/types.h>
|
||||
#include <pthread.h>""", byref.} = object
|
||||
when defined(linux) and defined(amd64):
|
||||
abi: array[56 div sizeof(clong), clong]
|
||||
|
||||
proc pthread_rwlock_init(rwlock: var SysRwLockObj, attr: pointer): cint {.
|
||||
importc: "pthread_rwlock_init", header: "<pthread.h>", noSideEffect.}
|
||||
proc pthread_rwlock_destroy(rwlock: var SysRwLockObj): cint {.
|
||||
importc: "pthread_rwlock_destroy", header: "<pthread.h>", noSideEffect.}
|
||||
proc pthread_rwlock_rdlock(rwlock: var SysRwLockObj): cint {.
|
||||
importc: "pthread_rwlock_rdlock", header: "<pthread.h>", noSideEffect.}
|
||||
proc pthread_rwlock_wrlock(rwlock: var SysRwLockObj): cint {.
|
||||
importc: "pthread_rwlock_wrlock", header: "<pthread.h>", noSideEffect.}
|
||||
proc pthread_rwlock_unlock(rwlock: var SysRwLockObj): cint {.
|
||||
importc: "pthread_rwlock_unlock", header: "<pthread.h>", noSideEffect.}
|
||||
|
||||
when defined(linux):
|
||||
# PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP: once a writer is waiting,
|
||||
# new readers block. Prevents continuous mutator read-locks from starving
|
||||
# the collector's write-lock acquisition (glibc default is PREFER_READER).
|
||||
type
|
||||
SysRwLockAttr {.importc: "pthread_rwlockattr_t", pure, final,
|
||||
header: "<pthread.h>".} = object
|
||||
const PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP = cint(3)
|
||||
proc pthread_rwlockattr_init(attr: ptr SysRwLockAttr): cint {.
|
||||
importc: "pthread_rwlockattr_init", header: "<pthread.h>".}
|
||||
proc pthread_rwlockattr_destroy(attr: ptr SysRwLockAttr): cint {.
|
||||
importc: "pthread_rwlockattr_destroy", header: "<pthread.h>".}
|
||||
proc pthread_rwlockattr_setkind_np(attr: ptr SysRwLockAttr; pref: cint): cint {.
|
||||
importc: "pthread_rwlockattr_setkind_np", header: "<pthread.h>".}
|
||||
|
||||
when defined(ios):
|
||||
type RwLock* = ptr SysRwLockObj
|
||||
proc initRwLock*(L: var RwLock) =
|
||||
when not declared(c_malloc):
|
||||
proc c_malloc(size: csize_t): pointer {.importc: "malloc", header: "<stdlib.h>".}
|
||||
proc c_free(p: pointer) {.importc: "free", header: "<stdlib.h>".}
|
||||
L = cast[RwLock](c_malloc(csize_t(sizeof(SysRwLockObj))))
|
||||
discard pthread_rwlock_init(L[], nil)
|
||||
proc deinitRwLock*(L: var RwLock) =
|
||||
if L != nil:
|
||||
discard pthread_rwlock_destroy(L[])
|
||||
when not declared(c_free):
|
||||
proc c_free(p: pointer) {.importc: "free", header: "<stdlib.h>".}
|
||||
c_free(L)
|
||||
L = nil
|
||||
proc acquireRead*(L: var RwLock) =
|
||||
discard pthread_rwlock_rdlock(L[])
|
||||
proc releaseRead*(L: var RwLock) =
|
||||
discard pthread_rwlock_unlock(L[])
|
||||
proc acquireWrite*(L: var RwLock) =
|
||||
discard pthread_rwlock_wrlock(L[])
|
||||
proc releaseWrite*(L: var RwLock) =
|
||||
discard pthread_rwlock_unlock(L[])
|
||||
else:
|
||||
type RwLock* = SysRwLockObj
|
||||
proc initRwLock*(L: var RwLock) =
|
||||
when defined(linux):
|
||||
var attr: SysRwLockAttr
|
||||
discard pthread_rwlockattr_init(addr attr)
|
||||
discard pthread_rwlockattr_setkind_np(addr attr, PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP)
|
||||
discard pthread_rwlock_init(L, addr attr)
|
||||
discard pthread_rwlockattr_destroy(addr attr)
|
||||
else:
|
||||
discard pthread_rwlock_init(L, nil)
|
||||
proc deinitRwLock*(L: var RwLock) =
|
||||
discard pthread_rwlock_destroy(L)
|
||||
proc acquireRead*(L: var RwLock) =
|
||||
discard pthread_rwlock_rdlock(L)
|
||||
proc releaseRead*(L: var RwLock) =
|
||||
discard pthread_rwlock_unlock(L)
|
||||
proc acquireWrite*(L: var RwLock) =
|
||||
discard pthread_rwlock_wrlock(L)
|
||||
proc releaseWrite*(L: var RwLock) =
|
||||
discard pthread_rwlock_unlock(L)
|
||||
|
||||
template withReadLock*(L: var RwLock, body: untyped) =
|
||||
acquireRead(L)
|
||||
try:
|
||||
body
|
||||
finally:
|
||||
releaseRead(L)
|
||||
|
||||
template withWriteLock*(L: var RwLock, body: untyped) =
|
||||
acquireWrite(L)
|
||||
try:
|
||||
body
|
||||
finally:
|
||||
releaseWrite(L)
|
||||
|
||||
{.pop.}
|
||||
@@ -11,6 +11,90 @@
|
||||
# import std/typetraits
|
||||
# strs already imported allocateds for us.
|
||||
|
||||
when defined(gcYrc):
|
||||
include rwlocks
|
||||
include threadids
|
||||
|
||||
const
|
||||
NumLockStripes = 64
|
||||
|
||||
type
|
||||
YrcLockState = enum
|
||||
HasNoLock
|
||||
HasMutatorLock
|
||||
HasCollectorLock
|
||||
Collecting
|
||||
|
||||
AlignedRwLock = object
|
||||
## One RwLock per cache line. {.align: 64.} causes the compiler to round
|
||||
## the struct size up to 64 bytes, so consecutive array elements never
|
||||
## share a cache line (sizeof(RwLock) = 56 on Linux x86_64 → 8 byte pad).
|
||||
lock {.align: 64.}: RwLock
|
||||
|
||||
var
|
||||
gYrcLocks: array[NumLockStripes, AlignedRwLock]
|
||||
var
|
||||
lockState {.threadvar.}: YrcLockState
|
||||
|
||||
proc getYrcStripe(): int {.inline.} =
|
||||
## Map this thread to one of the NumLockStripes RwLock stripes.
|
||||
## getThreadId() is already cached thread-locally in threadids.nim.
|
||||
getThreadId() and (NumLockStripes - 1)
|
||||
|
||||
proc acquireMutatorLock() {.compilerRtl, inl.} =
|
||||
if lockState == HasNoLock:
|
||||
acquireRead gYrcLocks[getYrcStripe()].lock
|
||||
lockState = HasMutatorLock
|
||||
|
||||
proc releaseMutatorLock() {.compilerRtl, inl.} =
|
||||
if lockState == HasMutatorLock:
|
||||
lockState = HasNoLock
|
||||
releaseRead gYrcLocks[getYrcStripe()].lock
|
||||
|
||||
template yrcMutatorLock*(t: typedesc; body: untyped) =
|
||||
{.noSideEffect.}:
|
||||
when canFormCycles(t):
|
||||
acquireMutatorLock()
|
||||
try:
|
||||
body
|
||||
finally:
|
||||
{.noSideEffect.}:
|
||||
when canFormCycles(t):
|
||||
releaseMutatorLock()
|
||||
|
||||
template yrcMutatorLockUntyped(body: untyped) =
|
||||
{.noSideEffect.}:
|
||||
acquireMutatorLock()
|
||||
try:
|
||||
body
|
||||
finally:
|
||||
{.noSideEffect.}:
|
||||
releaseMutatorLock()
|
||||
|
||||
template yrcCollectorLock(body: untyped) =
|
||||
if lockState == HasMutatorLock: releaseMutatorLock()
|
||||
let prevState = lockState
|
||||
let hadToAcquire = prevState < HasCollectorLock
|
||||
if hadToAcquire:
|
||||
# Acquire all stripes in ascending order — the only thread ever holding
|
||||
# multiple write locks is the collector, so there is no lock-order cycle.
|
||||
for yrcI in 0..<NumLockStripes:
|
||||
acquireWrite(gYrcLocks[yrcI].lock)
|
||||
lockState = HasCollectorLock
|
||||
try:
|
||||
body
|
||||
finally:
|
||||
if hadToAcquire:
|
||||
for yrcI in 0..<NumLockStripes:
|
||||
releaseWrite(gYrcLocks[yrcI].lock)
|
||||
lockState = prevState
|
||||
|
||||
else:
|
||||
template yrcMutatorLock*(t: typedesc; body: untyped) =
|
||||
body
|
||||
|
||||
template yrcMutatorLockUntyped(body: untyped) =
|
||||
body
|
||||
|
||||
# Some optimizations here may be not to empty-seq-initialize some symbols, then StrictNotNil complains.
|
||||
{.push warning[StrictNotNil]: off.} # See https://github.com/nim-lang/Nim/issues/21401
|
||||
@@ -116,33 +200,35 @@ proc prepareSeqAddUninit(len: int; p: pointer; addlen, elemSize, elemAlign: int)
|
||||
q.cap = newCap
|
||||
result = q
|
||||
|
||||
proc shrink*[T](x: var seq[T]; newLen: Natural) {.tags: [], raises: [].} =
|
||||
proc shrink*[T](x: var seq[T]; newLen: Natural) {.tags: [], raises: [], noSideEffect.} =
|
||||
when nimvm:
|
||||
{.cast(tags: []).}:
|
||||
setLen(x, newLen)
|
||||
else:
|
||||
#sysAssert newLen <= x.len, "invalid newLen parameter for 'shrink'"
|
||||
when not supportsCopyMem(T):
|
||||
for i in countdown(x.len - 1, newLen):
|
||||
reset x[i]
|
||||
# XXX This is wrong for const seqs that were moved into 'x'!
|
||||
{.noSideEffect.}:
|
||||
cast[ptr NimSeqV2[T]](addr x).len = newLen
|
||||
yrcMutatorLock(T):
|
||||
when not supportsCopyMem(T):
|
||||
for i in countdown(x.len - 1, newLen):
|
||||
reset x[i]
|
||||
# XXX This is wrong for const seqs that were moved into 'x'!
|
||||
{.noSideEffect.}:
|
||||
cast[ptr NimSeqV2[T]](addr x).len = newLen
|
||||
|
||||
proc grow*[T](x: var seq[T]; newLen: Natural; value: T) {.nodestroy.} =
|
||||
let oldLen = x.len
|
||||
#sysAssert newLen >= x.len, "invalid newLen parameter for 'grow'"
|
||||
if newLen <= oldLen: return
|
||||
var xu = cast[ptr NimSeqV2[T]](addr x)
|
||||
if xu.p == nil or (xu.p.cap and not strlitFlag) < newLen:
|
||||
xu.p = cast[typeof(xu.p)](prepareSeqAddUninit(oldLen, xu.p, newLen - oldLen, sizeof(T), alignof(T)))
|
||||
xu.len = newLen
|
||||
for i in oldLen .. newLen-1:
|
||||
when (NimMajor, NimMinor, NimPatch) >= (2, 3, 1):
|
||||
xu.p.data[i] = `=dup`(value)
|
||||
else:
|
||||
wasMoved(xu.p.data[i])
|
||||
`=copy`(xu.p.data[i], value)
|
||||
yrcMutatorLock(T):
|
||||
var xu = cast[ptr NimSeqV2[T]](addr x)
|
||||
if xu.p == nil or (xu.p.cap and not strlitFlag) < newLen:
|
||||
xu.p = cast[typeof(xu.p)](prepareSeqAddUninit(oldLen, xu.p, newLen - oldLen, sizeof(T), alignof(T)))
|
||||
xu.len = newLen
|
||||
for i in oldLen .. newLen-1:
|
||||
when (NimMajor, NimMinor, NimPatch) >= (2, 3, 1):
|
||||
xu.p.data[i] = `=dup`(value)
|
||||
else:
|
||||
wasMoved(xu.p.data[i])
|
||||
`=copy`(xu.p.data[i], value)
|
||||
|
||||
proc add*[T](x: var seq[T]; y: sink T) {.magic: "AppendSeqElem", noSideEffect, nodestroy.} =
|
||||
## Generic proc for adding a data item `y` to a container `x`.
|
||||
@@ -152,30 +238,32 @@ proc add*[T](x: var seq[T]; y: sink T) {.magic: "AppendSeqElem", noSideEffect, n
|
||||
## Generic code becomes much easier to write if the Nim naming scheme is
|
||||
## respected.
|
||||
{.cast(noSideEffect).}:
|
||||
let oldLen = x.len
|
||||
var xu = cast[ptr NimSeqV2[T]](addr x)
|
||||
if xu.p == nil or (xu.p.cap and not strlitFlag) < oldLen+1:
|
||||
xu.p = cast[typeof(xu.p)](prepareSeqAddUninit(oldLen, xu.p, 1, sizeof(T), alignof(T)))
|
||||
xu.len = oldLen+1
|
||||
# .nodestroy means `xu.p.data[oldLen] = value` is compiled into a
|
||||
# copyMem(). This is fine as know by construction that
|
||||
# in `xu.p.data[oldLen]` there is nothing to destroy.
|
||||
# We also save the `wasMoved + destroy` pair for the sink parameter.
|
||||
xu.p.data[oldLen] = y
|
||||
yrcMutatorLock(T):
|
||||
let oldLen = x.len
|
||||
var xu = cast[ptr NimSeqV2[T]](addr x)
|
||||
if xu.p == nil or (xu.p.cap and not strlitFlag) < oldLen+1:
|
||||
xu.p = cast[typeof(xu.p)](prepareSeqAddUninit(oldLen, xu.p, 1, sizeof(T), alignof(T)))
|
||||
xu.len = oldLen+1
|
||||
# .nodestroy means `xu.p.data[oldLen] = value` is compiled into a
|
||||
# copyMem(). This is fine as know by construction that
|
||||
# in `xu.p.data[oldLen]` there is nothing to destroy.
|
||||
# We also save the `wasMoved + destroy` pair for the sink parameter.
|
||||
xu.p.data[oldLen] = y
|
||||
|
||||
proc setLen[T](s: var seq[T], newlen: Natural) {.nodestroy.} =
|
||||
{.noSideEffect.}:
|
||||
if newlen < s.len:
|
||||
shrink(s, newlen)
|
||||
else:
|
||||
let oldLen = s.len
|
||||
if newlen <= oldLen: return
|
||||
var xu = cast[ptr NimSeqV2[T]](addr s)
|
||||
if xu.p == nil or (xu.p.cap and not strlitFlag) < newlen:
|
||||
xu.p = cast[typeof(xu.p)](prepareSeqAddUninit(oldLen, xu.p, newlen - oldLen, sizeof(T), alignof(T)))
|
||||
xu.len = newlen
|
||||
for i in oldLen..<newlen:
|
||||
xu.p.data[i] = default(T)
|
||||
yrcMutatorLock(T):
|
||||
let oldLen = s.len
|
||||
if newlen <= oldLen: return
|
||||
var xu = cast[ptr NimSeqV2[T]](addr s)
|
||||
if xu.p == nil or (xu.p.cap and not strlitFlag) < newlen:
|
||||
xu.p = cast[typeof(xu.p)](prepareSeqAddUninit(oldLen, xu.p, newlen - oldLen, sizeof(T), alignof(T)))
|
||||
xu.len = newlen
|
||||
for i in oldLen..<newlen:
|
||||
xu.p.data[i] = default(T)
|
||||
|
||||
proc newSeq[T](s: var seq[T], len: Natural) =
|
||||
shrink(s, 0)
|
||||
@@ -214,11 +302,12 @@ func setLenUninit[T](s: var seq[T], newlen: Natural) {.nodestroy.} =
|
||||
if newlen < s.len:
|
||||
shrink(s, newlen)
|
||||
else:
|
||||
let oldLen = s.len
|
||||
if newlen <= oldLen: return
|
||||
var xu = cast[ptr NimSeqV2[T]](addr s)
|
||||
if xu.p == nil or (xu.p.cap and not strlitFlag) < newlen:
|
||||
xu.p = cast[typeof(xu.p)](prepareSeqAddUninit(oldLen, xu.p, newlen - oldLen, sizeof(T), alignof(T)))
|
||||
xu.len = newlen
|
||||
yrcMutatorLock(T):
|
||||
let oldLen = s.len
|
||||
if newlen <= oldLen: return
|
||||
var xu = cast[ptr NimSeqV2[T]](addr s)
|
||||
if xu.p == nil or (xu.p.cap and not strlitFlag) < newlen:
|
||||
xu.p = cast[typeof(xu.p)](prepareSeqAddUninit(oldLen, xu.p, newlen - oldLen, sizeof(T), alignof(T)))
|
||||
xu.len = newlen
|
||||
|
||||
{.pop.} # See https://github.com/nim-lang/Nim/issues/21401
|
||||
|
||||
@@ -18,7 +18,8 @@ type
|
||||
|
||||
template frees(s: NimSeqV2Reimpl) =
|
||||
if s.p != nil and (s.p.cap and strlitFlag) != strlitFlag:
|
||||
when compileOption("threads"):
|
||||
deallocShared(s.p)
|
||||
else:
|
||||
dealloc(s.p)
|
||||
yrcMutatorLockUntyped:
|
||||
when compileOption("threads"):
|
||||
deallocShared(s.p)
|
||||
else:
|
||||
dealloc(s.p)
|
||||
|
||||
@@ -117,7 +117,7 @@ proc nimParseBiggestFloat(s: openArray[char], number: var BiggestFloat,
|
||||
if s[i+1] == 'A' or s[i+1] == 'a':
|
||||
if s[i+2] == 'N' or s[i+2] == 'n':
|
||||
if i+3 >= s.len or s[i+3] notin IdentChars:
|
||||
number = NaN
|
||||
number = if sign < 0: -NaN else: NaN
|
||||
return i+3
|
||||
return 0
|
||||
|
||||
|
||||
@@ -1,30 +1,29 @@
|
||||
#
|
||||
# YRC: Thread-safe ORC (concurrent cycle collector).
|
||||
# Same API as orc.nim but with striped queues and global lock for merge/collect.
|
||||
# Same API as orc.nim but with the global mutator/collector RWLock for safety.
|
||||
# Destructors for refs run at collection time, not immediately on last decRef.
|
||||
# See yrc_proof.lean for a Lean 4 proof of safety and deadlock freedom.
|
||||
#
|
||||
# ## Key Invariant: Topology vs. Reference Counts
|
||||
# ## Locking Protocol
|
||||
#
|
||||
# Only `obj.field = x` can change the topology of the heap graph (heap-to-heap
|
||||
# edges). Local variable assignments (`var local = someRef`) affect reference
|
||||
# counts but never create heap-to-heap edges and thus cannot create cycles.
|
||||
# ALL topology-changing operations — heap-field writes (`nimAsgnYrc`,
|
||||
# `nimSinkYrc`) and seq mutations that resize internal buffers — hold the
|
||||
# global mutator read lock (`gYrcGlobalLock` via `acquireMutatorLock`).
|
||||
# Multiple mutators may hold this read lock simultaneously.
|
||||
#
|
||||
# The actual pointer write in `obj.field = x` happens immediately and lock-free —
|
||||
# the graph topology is always up-to-date in memory. Only the RC adjustments are
|
||||
# deferred: increments and decrements are buffered into per-stripe queues
|
||||
# (`toInc`, `toDec`) protected by fine-grained per-stripe locks.
|
||||
# The cycle collector acquires the exclusive write lock for the entire
|
||||
# mark/scan/collect phase. This means the heap topology is *completely
|
||||
# frozen* during collection: no `nimAsgnYrc` or seq operation can mutate
|
||||
# any pointer field while the three passes run. This gives the Bacon
|
||||
# algorithm the stable subgraph it requires without full write barriers.
|
||||
#
|
||||
# When `collectCycles` runs it takes the global lock, drains all stripe buffers
|
||||
# via `mergePendingRoots`, and then traces the physical pointer graph (via
|
||||
# `traceImpl`) to detect cycles. This is sound because `trace` follows the actual
|
||||
# pointer values in memory — which are always current — and uses the reconciled
|
||||
# RCs only to identify candidate roots and confirm garbage.
|
||||
#
|
||||
# In summary: the physical pointer graph is always consistent (writes are
|
||||
# immediate); only the reference counts are eventually consistent (writes are
|
||||
# buffered). The per-stripe locks are cheap; the expensive global lock is only
|
||||
# needed when interpreting the RCs during collection.
|
||||
# Consequence for incRef in `nimAsgnYrc`:
|
||||
# Because the collector is blocked, the incRef can be a direct atomic
|
||||
# increment on the RefHeader (`increment head(src)`) rather than going
|
||||
# through the `toInc` stripe queue. The collector will see the updated
|
||||
# RC immediately when it next acquires the write lock. Only decrements
|
||||
# (`yrcDec`) still use the `toDec` stripe queue so that objects whose RC
|
||||
# might reach zero are handled by the collector's cycle-detection logic.
|
||||
#
|
||||
# ## Why No Write Barrier Is Needed
|
||||
#
|
||||
@@ -35,40 +34,19 @@
|
||||
# while A still points to it. Traditional concurrent collectors need write
|
||||
# barriers to prevent this.
|
||||
#
|
||||
# This problem structurally cannot arise in YRC because the cycle collector only
|
||||
# frees *closed cycles* — subgraphs where every reference to every member comes
|
||||
# from within the group, with zero external references. To execute `A.field = B`
|
||||
# the mutator must hold a reference to A, which means A has an external reference
|
||||
# (from the stack) that is not a heap-to-heap edge. During trial deletion
|
||||
# (`markGray`) only internal edges are subtracted from RCs, so A's external
|
||||
# reference survives, `scan` finds A's RC >= 0, calls `scanBlack`, and rescues A
|
||||
# and everything reachable from it — including B. In short: the mutator can only
|
||||
# modify objects it can reach, but the cycle collector only frees objects nothing
|
||||
# external can reach. The two conditions are mutually exclusive.
|
||||
# This problem structurally cannot arise in YRC for two reasons:
|
||||
#
|
||||
#[
|
||||
|
||||
The problem described in Bacon01 is: during markGray/scan, a mutator concurrently
|
||||
does X.field = Z (was X→Y), changing the physical graph while the collector is tracing
|
||||
it. The collector might see stale or new edges. The reasons this is still safe:
|
||||
|
||||
Stale edges cancel with unbuffered decrements: If the collector sees old edge X→Y
|
||||
(mutator already wrote X→Z and buffered dec(Y)), the phantom trial deletion and the
|
||||
unbuffered dec cancel — Y's effective RC is correct.
|
||||
|
||||
scanBlack rescues via current physical edges: If X has external refs (merged RC reflects
|
||||
the mutator's access), scanBlack(X) re-traces X and follows the current physical edge X→Z,
|
||||
incrementing Z's RC and marking it black. Z survives.
|
||||
|
||||
rcSum==edges fast path is conservative: Any discrepancy between physical graph and merged
|
||||
state (stale or new edges) causes rcSum != edges, falling back to the slow path which
|
||||
rescues anything with RC >= 0.
|
||||
|
||||
Unreachable cycles are truly unreachable: The mutator can only reach objects through chains
|
||||
rooted in merged references. If a cycle has zero external refs at merge time, no mutator
|
||||
can reach it.
|
||||
|
||||
]#
|
||||
# 1. The mutator lock freezes the topology during all three passes, so no
|
||||
# concurrent field write can race with markGray/scan/collectWhite.
|
||||
#
|
||||
# 2. Even without the lock, the cycle collector only frees *closed cycles* —
|
||||
# subgraphs where every reference to every member comes from within the
|
||||
# group, with zero external references. To execute `A.field = B` the
|
||||
# mutator must hold a reference to A (external ref), which `scan` would
|
||||
# rescue. The two conditions are mutually exclusive.
|
||||
#
|
||||
# In practice reason (1) makes reason (2) a belt-and-suspenders safety
|
||||
# argument rather than the primary mechanism.
|
||||
|
||||
{.push raises: [].}
|
||||
|
||||
@@ -90,15 +68,52 @@ const
|
||||
logOrc = defined(nimArcIds)
|
||||
|
||||
type
|
||||
TraceProc = proc (p, env: pointer) {.nimcall, benign, raises: [].}
|
||||
DisposeProc = proc (p: pointer) {.nimcall, benign, raises: [].}
|
||||
TraceProc = proc (p, env: pointer) {.nimcall, gcsafe, raises: [].}
|
||||
DisposeProc = proc (p: pointer) {.nimcall, gcsafe, raises: [].}
|
||||
|
||||
template color(c): untyped = c.rc and colorMask
|
||||
template setColor(c, col) =
|
||||
when col == colBlack:
|
||||
c.rc = c.rc and not colorMask
|
||||
else:
|
||||
c.rc = c.rc and not colorMask or col
|
||||
when defined(nimYrcAtomicIncs):
|
||||
template color(c): untyped = atomicLoadN(addr c.rc, ATOMIC_ACQUIRE) and colorMask
|
||||
template setColor(c, col) =
|
||||
block:
|
||||
var expected = atomicLoadN(addr c.rc, ATOMIC_RELAXED)
|
||||
while true:
|
||||
let desired = (expected and not colorMask) or col
|
||||
if atomicCompareExchangeN(addr c.rc, addr expected, desired, true,
|
||||
ATOMIC_ACQ_REL, ATOMIC_RELAXED):
|
||||
break
|
||||
template loadRc(c): int = atomicLoadN(addr c.rc, ATOMIC_ACQUIRE)
|
||||
template trialDec(c) =
|
||||
discard atomicFetchAdd(addr c.rc, -rcIncrement, ATOMIC_ACQ_REL)
|
||||
template trialInc(c) =
|
||||
discard atomicFetchAdd(addr c.rc, rcIncrement, ATOMIC_ACQ_REL)
|
||||
template rcClearFlag(c, flag) =
|
||||
block:
|
||||
var expected = atomicLoadN(addr c.rc, ATOMIC_RELAXED)
|
||||
while true:
|
||||
let desired = expected and not flag
|
||||
if atomicCompareExchangeN(addr c.rc, addr expected, desired, true,
|
||||
ATOMIC_ACQ_REL, ATOMIC_RELAXED):
|
||||
break
|
||||
template rcSetFlag(c, flag) =
|
||||
block:
|
||||
var expected = atomicLoadN(addr c.rc, ATOMIC_RELAXED)
|
||||
while true:
|
||||
let desired = expected or flag
|
||||
if atomicCompareExchangeN(addr c.rc, addr expected, desired, true,
|
||||
ATOMIC_ACQ_REL, ATOMIC_RELAXED):
|
||||
break
|
||||
else:
|
||||
template color(c): untyped = c.rc and colorMask
|
||||
template setColor(c, col) =
|
||||
when col == colBlack:
|
||||
c.rc = c.rc and not colorMask
|
||||
else:
|
||||
c.rc = c.rc and not colorMask or col
|
||||
template loadRc(c): int = c.rc
|
||||
template trialDec(c) = c.rc = c.rc -% rcIncrement
|
||||
template trialInc(c) = c.rc = c.rc +% rcIncrement
|
||||
template rcClearFlag(c, flag) = c.rc = c.rc and not flag
|
||||
template rcSetFlag(c, flag) = c.rc = c.rc or flag
|
||||
|
||||
const
|
||||
optimizedOrc = false
|
||||
@@ -118,8 +133,6 @@ proc trace(s: Cell; desc: PNimTypeV2; j: var GcEnv) {.inline.} =
|
||||
var p = s +! sizeof(RefHeader)
|
||||
cast[TraceProc](desc.traceImpl)(p, addr(j))
|
||||
|
||||
include threadids
|
||||
|
||||
type
|
||||
Stripe = object
|
||||
when not defined(yrcAtomics):
|
||||
@@ -131,13 +144,12 @@ type
|
||||
toDec: array[QueueSize, (Cell, PNimTypeV2)]
|
||||
|
||||
type
|
||||
PreventThreadFromCollectProc* = proc(): bool {.nimcall, benign, raises: [].}
|
||||
PreventThreadFromCollectProc* = proc(): bool {.nimcall, gcsafe, raises: [].}
|
||||
## Callback run before this thread runs the cycle collector.
|
||||
## Return `true` to allow collection, `false` to skip (e.g. real-time thread).
|
||||
## Invoked while holding the global lock; must not call back into YRC.
|
||||
|
||||
var
|
||||
gYrcGlobalLock: Lock
|
||||
roots: CellSeq[Cell] # merged roots, used under global lock
|
||||
stripes: array[NumStripes, Stripe]
|
||||
rootsThreshold: int = 128
|
||||
@@ -182,13 +194,15 @@ proc nimIncRefCyclic(p: pointer; cyclic: bool) {.compilerRtl, inl.} =
|
||||
let h = head(p)
|
||||
when optimizedOrc:
|
||||
if cyclic: h.rc = h.rc or maybeCycle
|
||||
when defined(yrcAtomics):
|
||||
when defined(nimYrcAtomicIncs):
|
||||
discard atomicFetchAdd(addr h.rc, rcIncrement, ATOMIC_ACQ_REL)
|
||||
elif defined(yrcAtomics):
|
||||
let s = getStripeIdx()
|
||||
let slot = atomicFetchAdd(addr stripes[s].toIncLen, 1, ATOMIC_ACQ_REL)
|
||||
if slot < QueueSize:
|
||||
atomicStoreN(addr stripes[s].toInc[slot], h, ATOMIC_RELEASE)
|
||||
else:
|
||||
withLock gYrcGlobalLock:
|
||||
yrcCollectorLock:
|
||||
h.rc = h.rc +% rcIncrement
|
||||
for i in 0..<NumStripes:
|
||||
let len = atomicExchangeN(addr stripes[i].toIncLen, 0, ATOMIC_ACQUIRE)
|
||||
@@ -206,7 +220,7 @@ proc nimIncRefCyclic(p: pointer; cyclic: bool) {.compilerRtl, inl.} =
|
||||
else:
|
||||
overflow = true
|
||||
if overflow:
|
||||
withLock gYrcGlobalLock:
|
||||
yrcCollectorLock:
|
||||
for i in 0..<NumStripes:
|
||||
withLock stripes[i].lockInc:
|
||||
for j in 0..<stripes[i].toIncLen:
|
||||
@@ -217,24 +231,29 @@ proc nimIncRefCyclic(p: pointer; cyclic: bool) {.compilerRtl, inl.} =
|
||||
break
|
||||
|
||||
proc mergePendingRoots() =
|
||||
# Merge buffered RC operations. Note: Unlike truly concurrent collectors,
|
||||
# we don't need to set color to black on incRef because collection runs
|
||||
# under the global lock, so no concurrent mutations happen during collection.
|
||||
for i in 0..<NumStripes:
|
||||
when defined(yrcAtomics):
|
||||
let incLen = atomicExchangeN(addr stripes[i].toIncLen, 0, ATOMIC_ACQUIRE)
|
||||
for j in 0..<min(incLen, QueueSize):
|
||||
let x = atomicLoadN(addr stripes[i].toInc[j], ATOMIC_ACQUIRE)
|
||||
x.rc = x.rc +% rcIncrement
|
||||
else:
|
||||
withLock stripes[i].lockInc:
|
||||
for j in 0..<stripes[i].toIncLen:
|
||||
let x = stripes[i].toInc[j]
|
||||
when not defined(nimYrcAtomicIncs):
|
||||
# Inc buffers only exist when increfs are buffered (not atomic)
|
||||
when defined(yrcAtomics):
|
||||
let incLen = atomicExchangeN(addr stripes[i].toIncLen, 0, ATOMIC_ACQUIRE)
|
||||
for j in 0..<min(incLen, QueueSize):
|
||||
let x = atomicLoadN(addr stripes[i].toInc[j], ATOMIC_ACQUIRE)
|
||||
x.rc = x.rc +% rcIncrement
|
||||
stripes[i].toIncLen = 0
|
||||
else:
|
||||
withLock stripes[i].lockInc:
|
||||
for j in 0..<stripes[i].toIncLen:
|
||||
let x = stripes[i].toInc[j]
|
||||
x.rc = x.rc +% rcIncrement
|
||||
stripes[i].toIncLen = 0
|
||||
withLock stripes[i].lockDec:
|
||||
for j in 0..<stripes[i].toDecLen:
|
||||
let (c, desc) = stripes[i].toDec[j]
|
||||
c.rc = c.rc -% rcIncrement
|
||||
if (c.rc and inRootsFlag) == 0:
|
||||
c.rc = c.rc or inRootsFlag
|
||||
trialDec(c)
|
||||
if (loadRc(c) and inRootsFlag) == 0:
|
||||
rcSetFlag(c, inRootsFlag)
|
||||
if roots.d == nil: init(roots)
|
||||
add(roots, c, desc)
|
||||
stripes[i].toDecLen = 0
|
||||
@@ -254,8 +273,8 @@ when logOrc or orcLeakDetector:
|
||||
|
||||
proc free(s: Cell; desc: PNimTypeV2) {.inline.} =
|
||||
when traceCollector:
|
||||
cprintf("[From ] %p rc %ld color %ld\n", s, s.rc shr rcShift, s.color)
|
||||
if (s.rc and inRootsFlag) == 0:
|
||||
cprintf("[From ] %p rc %ld color %ld\n", s, loadRc(s) shr rcShift, s.color)
|
||||
if (loadRc(s) and inRootsFlag) == 0:
|
||||
let p = s +! sizeof(RefHeader)
|
||||
when logOrc: writeCell("free", s, desc)
|
||||
if desc.destructor != nil:
|
||||
@@ -288,7 +307,7 @@ proc scanBlack(s: Cell; desc: PNimTypeV2; j: var GcEnv) =
|
||||
while j.traceStack.len > until:
|
||||
let (entry, desc) = j.traceStack.pop()
|
||||
let t = head entry[]
|
||||
t.rc = t.rc +% rcIncrement
|
||||
trialInc(t)
|
||||
if t.color != colBlack:
|
||||
t.setColor colBlack
|
||||
trace(t, desc, j)
|
||||
@@ -298,23 +317,23 @@ proc markGray(s: Cell; desc: PNimTypeV2; j: var GcEnv) =
|
||||
if s.color != colGray:
|
||||
s.setColor colGray
|
||||
j.touched = j.touched +% 1
|
||||
j.rcSum = j.rcSum +% (s.rc shr rcShift) +% 1
|
||||
j.rcSum = j.rcSum +% (loadRc(s) shr rcShift) +% 1
|
||||
orcAssert(j.traceStack.len == 0, "markGray: trace stack not empty")
|
||||
trace(s, desc, j)
|
||||
while j.traceStack.len > 0:
|
||||
let (entry, desc) = j.traceStack.pop()
|
||||
let t = head entry[]
|
||||
t.rc = t.rc -% rcIncrement
|
||||
trialDec(t)
|
||||
j.edges = j.edges +% 1
|
||||
if t.color != colGray:
|
||||
t.setColor colGray
|
||||
j.touched = j.touched +% 1
|
||||
j.rcSum = j.rcSum +% (t.rc shr rcShift) +% 2
|
||||
j.rcSum = j.rcSum +% (loadRc(t) shr rcShift) +% 2
|
||||
trace(t, desc, j)
|
||||
|
||||
proc scan(s: Cell; desc: PNimTypeV2; j: var GcEnv) =
|
||||
if s.color == colGray:
|
||||
if (s.rc shr rcShift) >= 0:
|
||||
if (loadRc(s) shr rcShift) >= 0:
|
||||
scanBlack(s, desc, j)
|
||||
else:
|
||||
orcAssert(j.traceStack.len == 0, "scan: trace stack not empty")
|
||||
@@ -324,14 +343,14 @@ proc scan(s: Cell; desc: PNimTypeV2; j: var GcEnv) =
|
||||
let (entry, desc) = j.traceStack.pop()
|
||||
let t = head entry[]
|
||||
if t.color == colGray:
|
||||
if (t.rc shr rcShift) >= 0:
|
||||
if (loadRc(t) shr rcShift) >= 0:
|
||||
scanBlack(t, desc, j)
|
||||
else:
|
||||
t.setColor(colWhite)
|
||||
trace(t, desc, j)
|
||||
|
||||
proc collectColor(s: Cell; desc: PNimTypeV2; col: int; j: var GcEnv) =
|
||||
if s.color == col and (s.rc and inRootsFlag) == 0:
|
||||
if s.color == col and (loadRc(s) and inRootsFlag) == 0:
|
||||
orcAssert(j.traceStack.len == 0, "collectWhite: trace stack not empty")
|
||||
s.setColor(colBlack)
|
||||
j.toFree.add(s, desc)
|
||||
@@ -340,89 +359,54 @@ proc collectColor(s: Cell; desc: PNimTypeV2; col: int; j: var GcEnv) =
|
||||
let (entry, desc) = j.traceStack.pop()
|
||||
let t = head entry[]
|
||||
entry[] = nil
|
||||
if t.color == col and (t.rc and inRootsFlag) == 0:
|
||||
if t.color == col and (loadRc(t) and inRootsFlag) == 0:
|
||||
j.toFree.add(t, desc)
|
||||
t.setColor(colBlack)
|
||||
trace(t, desc, j)
|
||||
|
||||
proc collectCyclesBacon(j: var GcEnv; lowMark: int) =
|
||||
# YRC defers all destruction to collection time - process ALL roots through Bacon's algorithm
|
||||
# This is different from ORC which handles immediate garbage (rc == 0) directly
|
||||
if lockState == Collecting:
|
||||
return
|
||||
lockState = Collecting
|
||||
let last = roots.len -% 1
|
||||
when logOrc:
|
||||
for i in countdown(last, lowMark):
|
||||
writeCell("root", roots.d[i][0], roots.d[i][1])
|
||||
init j.toFree
|
||||
|
||||
# First pass: swap roots with rc <= 0 to the end for immediate freeing
|
||||
# Check RC before markGray modifies it. Use a while loop that shrinks as we iterate.
|
||||
var cycleStart = lowMark
|
||||
var immediateFreeStart = roots.len
|
||||
while cycleStart < immediateFreeStart:
|
||||
let s = roots.d[cycleStart][0]
|
||||
if (s.rc shr rcShift) < 0:
|
||||
# Root is already garbage, swap to end for immediate freeing
|
||||
dec immediateFreeStart
|
||||
swap(roots.d[cycleStart], roots.d[immediateFreeStart])
|
||||
when logOrc: writeCell("root swapped to end for immediate free (rc <= 0)", roots.d[immediateFreeStart][0], roots.d[immediateFreeStart][1])
|
||||
else:
|
||||
inc cycleStart
|
||||
|
||||
# Second pass: process remaining roots (rc > 0) for cycle detection
|
||||
# Only process roots from lowMark to immediateFreeStart (cycleStart == immediateFreeStart after swap loop)
|
||||
for i in lowMark..<immediateFreeStart:
|
||||
# Process all roots through markGray (Bacon's algorithm)
|
||||
for i in countdown(last, lowMark):
|
||||
markGray(roots.d[i][0], roots.d[i][1], j)
|
||||
|
||||
var colToCollect = colWhite
|
||||
if j.rcSum == j.edges:
|
||||
# Short-cut: we know everything is garbage
|
||||
colToCollect = colGray
|
||||
j.keepThreshold = true
|
||||
else:
|
||||
for i in lowMark..<immediateFreeStart:
|
||||
# Normal scan phase
|
||||
for i in countdown(last, lowMark):
|
||||
scan(roots.d[i][0], roots.d[i][1], j)
|
||||
for i in lowMark..<immediateFreeStart:
|
||||
let s = roots.d[i][0]
|
||||
s.rc = s.rc and not inRootsFlag
|
||||
collectColor(s, roots.d[i][1], colToCollect, j)
|
||||
when not defined(nimStressOrc):
|
||||
let oldThreshold = rootsThreshold
|
||||
rootsThreshold = high(int)
|
||||
|
||||
# Prepare immediate-free roots for freeing: recursively trace through ALL descendants
|
||||
# and set child pointers to nil, just like collectColor does. This prevents destructors
|
||||
# from accessing children and triggering nested collectCycles().
|
||||
# Add them to j.toFree so they're freed together after roots.len = 0 is set.
|
||||
# Keep inRootsFlag set until right before freeing to prevent mergePendingRoots from
|
||||
# accessing freed cells during nested collectCycles().
|
||||
let immediateFreeCount = roots.len - immediateFreeStart
|
||||
for i in immediateFreeStart..<roots.len:
|
||||
# Collect phase: free all garbage objects
|
||||
init j.toFree
|
||||
for i in 0 ..< roots.len:
|
||||
let s = roots.d[i][0]
|
||||
let desc = roots.d[i][1]
|
||||
# Don't clear inRootsFlag yet - keep it set so mergePendingRoots can skip this cell
|
||||
orcAssert(j.traceStack.len == 0, "trace stack not empty before preparing immediate-free root")
|
||||
s.setColor(colBlack)
|
||||
j.toFree.add(s, desc)
|
||||
trace(s, desc, j)
|
||||
# Recursively trace and nil ALL descendants, just like collectColor does
|
||||
# This ensures destructors can't access any children, preventing nested collections
|
||||
while j.traceStack.len > 0:
|
||||
let (entry, childDesc) = j.traceStack.pop()
|
||||
let t = head entry[]
|
||||
entry[] = nil
|
||||
# Recursively trace children to nil their descendants too
|
||||
trace(t, childDesc, j)
|
||||
rcClearFlag(s, inRootsFlag)
|
||||
collectColor(s, roots.d[i][1], colToCollect, j)
|
||||
|
||||
# Clear roots before freeing to prevent nested collectCycles() from accessing freed cells
|
||||
roots.len = 0
|
||||
|
||||
# Free all roots (both immediate-free and cycle-detected) together
|
||||
# Free all collected objects
|
||||
# Destructors must not call nimDecRefIsLastCyclicStatic (add to toDec) during this phase
|
||||
for i in 0 ..< j.toFree.len:
|
||||
let s = j.toFree.d[i][0]
|
||||
s.rc = s.rc and not inRootsFlag
|
||||
when orcLeakDetector:
|
||||
writeCell("CYCLIC OBJECT FREED", s, j.toFree.d[i][1])
|
||||
free(s, j.toFree.d[i][1])
|
||||
when not defined(nimStressOrc):
|
||||
rootsThreshold = oldThreshold
|
||||
j.freed = j.freed +% j.toFree.len +% immediateFreeCount
|
||||
j.freed = j.freed +% j.toFree.len
|
||||
deinit j.toFree
|
||||
|
||||
when defined(nimOrcStats):
|
||||
@@ -431,9 +415,10 @@ when defined(nimOrcStats):
|
||||
proc collectCycles() =
|
||||
when logOrc:
|
||||
cfprintf(cstderr, "[collectCycles] begin\n")
|
||||
withLock gYrcGlobalLock:
|
||||
yrcCollectorLock:
|
||||
mergePendingRoots()
|
||||
if roots.len >= RootsThreshold and mayRunCycleCollect():
|
||||
if roots.len >= rootsThreshold and mayRunCycleCollect():
|
||||
let nRoots = roots.len
|
||||
var j: GcEnv
|
||||
init j.traceStack
|
||||
collectCyclesBacon(j, 0)
|
||||
@@ -450,8 +435,11 @@ proc collectCycles() =
|
||||
elif rootsThreshold < high(int) div 4:
|
||||
rootsThreshold = (if rootsThreshold <= 0: defaultThreshold else: rootsThreshold)
|
||||
rootsThreshold = rootsThreshold div 2 +% rootsThreshold
|
||||
# Cap growth so threshold doesn't grow without bound when we rarely free cycles
|
||||
#rootsThreshold = min(rootsThreshold, defaultThreshold *% 16)
|
||||
# Cost-aware: if this run was expensive (large graph), raise threshold more so we don't run again too soon
|
||||
if j.touched > nRoots *% 4:
|
||||
rootsThreshold = rootsThreshold div 2 +% rootsThreshold
|
||||
rootsThreshold = min(rootsThreshold, defaultThreshold *% 16)
|
||||
rootsThreshold = min(rootsThreshold, nRoots *% 2)
|
||||
when logOrc:
|
||||
cfprintf(cstderr, "[collectCycles] end; freed %ld new threshold %ld\n", j.freed, rootsThreshold)
|
||||
when defined(nimOrcStats):
|
||||
@@ -466,9 +454,9 @@ when defined(nimOrcStats):
|
||||
result = OrcStats(freedCyclicObjects: freedCyclicObjects)
|
||||
|
||||
proc GC_runOrc* =
|
||||
withLock gYrcGlobalLock:
|
||||
yrcCollectorLock:
|
||||
mergePendingRoots()
|
||||
if mayRunCycleCollect():
|
||||
if roots.len > 0 and mayRunCycleCollect():
|
||||
var j: GcEnv
|
||||
init j.traceStack
|
||||
collectCyclesBacon(j, 0)
|
||||
@@ -485,12 +473,12 @@ proc GC_disableOrc*() =
|
||||
rootsThreshold = high(int)
|
||||
|
||||
proc GC_prepareOrc*(): int {.inline.} =
|
||||
withLock gYrcGlobalLock:
|
||||
yrcCollectorLock:
|
||||
mergePendingRoots()
|
||||
result = roots.len
|
||||
|
||||
proc GC_partialCollect*(limit: int) =
|
||||
withLock gYrcGlobalLock:
|
||||
yrcCollectorLock:
|
||||
mergePendingRoots()
|
||||
if roots.len > limit and mayRunCycleCollect():
|
||||
var j: GcEnv
|
||||
@@ -566,22 +554,24 @@ proc yrcDec(tmp: pointer; desc: PNimTypeV2) {.inline.} =
|
||||
|
||||
proc nimAsgnYrc(dest: ptr pointer; src: pointer; desc: PNimTypeV2) {.compilerRtl.} =
|
||||
## YRC write barrier for ref copy assignment.
|
||||
## Atomically stores src into dest, then buffers RC adjustments.
|
||||
## Freeing is always done by the cycle collector, never inline.
|
||||
## Holds the mutator read lock for the entire operation so the collector
|
||||
## cannot run between the incRef and decRef, closing the stale-decRef
|
||||
## bug. Direct atomic incRef replaces the toInc stripe queue: the
|
||||
## collector is blocked, so the RC update is immediately visible and correct.
|
||||
acquireMutatorLock()
|
||||
if src != nil: increment head(src) # direct atomic: no toInc queue needed
|
||||
let tmp = dest[]
|
||||
atomicStoreN(dest, src, ATOMIC_RELEASE)
|
||||
if src != nil:
|
||||
nimIncRefCyclic(src, true)
|
||||
if tmp != nil:
|
||||
yrcDec(tmp, desc)
|
||||
dest[] = src
|
||||
if tmp != nil: yrcDec(tmp, desc) # still deferred via toDec for cycle detection
|
||||
releaseMutatorLock()
|
||||
|
||||
proc nimSinkYrc(dest: ptr pointer; src: pointer; desc: PNimTypeV2) {.compilerRtl.} =
|
||||
## YRC write barrier for ref sink (move). No incRef on source.
|
||||
## Freeing is always done by the cycle collector, never inline.
|
||||
acquireMutatorLock()
|
||||
let tmp = dest[]
|
||||
atomicStoreN(dest, src, ATOMIC_RELEASE)
|
||||
if tmp != nil:
|
||||
yrcDec(tmp, desc)
|
||||
dest[] = src
|
||||
if tmp != nil: yrcDec(tmp, desc)
|
||||
releaseMutatorLock()
|
||||
|
||||
proc nimMarkCyclic(p: pointer) {.compilerRtl, inl.} =
|
||||
when optimizedOrc:
|
||||
@@ -589,10 +579,12 @@ proc nimMarkCyclic(p: pointer) {.compilerRtl, inl.} =
|
||||
let h = head(p)
|
||||
h.rc = h.rc or maybeCycle
|
||||
|
||||
# Initialize locks at module load
|
||||
initLock(gYrcGlobalLock)
|
||||
# Initialize locks at module load.
|
||||
# RwLock stripes live in seqs_v2 (gYrcLocks); NumLockStripes is exported from there.
|
||||
for i in 0..<NumLockStripes:
|
||||
initRwLock(gYrcLocks[i].lock)
|
||||
for i in 0..<NumStripes:
|
||||
when not defined(yrcAtomics):
|
||||
when not defined(yrcAtomics) and not defined(nimYrcAtomicIncs):
|
||||
initLock(stripes[i].lockInc)
|
||||
initLock(stripes[i].lockDec)
|
||||
|
||||
|
||||
@@ -42,6 +42,32 @@
|
||||
\* - Mutator must hold stack ref to modify object (external ref)
|
||||
\* - scanBlack follows current physical edges (rescues newly written objects)
|
||||
\* - Only objects unreachable from any stack root are freed
|
||||
\*
|
||||
\* ## Seq Payload Race and RWLock Fix
|
||||
\*
|
||||
\* Value types like seq[T] (where T can form cycles) have internal heap
|
||||
\* allocations (data arrays / "payloads") that are freed by value-type
|
||||
\* hooks (=sink, =destroy), NOT by the cycle collector. This creates a race:
|
||||
\*
|
||||
\* 1. Object O has a seq field with payload P containing refs
|
||||
\* 2. Collector starts tracing O -- reads payload pointer P
|
||||
\* 3. Mutator does O.seq = newSeq -- frees P (value-type destructor)
|
||||
\* 4. Collector dereferences P -- use-after-free!
|
||||
\*
|
||||
\* Fix: Change the global YRC lock to a read-write lock (RWLock).
|
||||
\* - Collector acquires the WRITE lock (exclusive access during tracing)
|
||||
\* - Seq mutations (assign, setLen, add, etc.) acquire the READ lock
|
||||
\* - Multiple seq mutations can proceed concurrently (read lock is shared)
|
||||
\* - But seq mutations block while the collector traces (write lock is exclusive)
|
||||
\*
|
||||
\* This prevents the race: the mutator cannot free a payload while the
|
||||
\* collector is tracing it, because acquiring the read lock requires
|
||||
\* the write lock to be unheld.
|
||||
\*
|
||||
\* Deadlock avoidance: If a seq operation triggers collectCycles() via stripe
|
||||
\* overflow while already holding the read lock, it must NOT attempt to
|
||||
\* acquire the write lock. Instead, it should drain the overflow buffers
|
||||
\* without running the full collection cycle.
|
||||
|
||||
EXTENDS Naturals, Integers, Sequences, FiniteSets, TLC
|
||||
|
||||
@@ -53,10 +79,14 @@ ASSUME IsFiniteSet(Objects)
|
||||
ASSUME IsFiniteSet(Threads)
|
||||
ASSUME IsFiniteSet(ObjTypes)
|
||||
|
||||
\* Seq payload identifiers (models heap-allocated data arrays of seq[T])
|
||||
CONSTANTS SeqPayloads
|
||||
ASSUME IsFiniteSet(SeqPayloads)
|
||||
|
||||
\* NULL constant (represents "no thread" for locks)
|
||||
\* We use a sentinel value that's guaranteed not to be in Threads or Objects
|
||||
NULL == "NULL" \* String literal that won't conflict with Threads/Objects
|
||||
ASSUME NULL \notin Threads /\ NULL \notin Objects
|
||||
ASSUME NULL \notin Threads /\ NULL \notin Objects /\ NULL \notin SeqPayloads
|
||||
|
||||
\* Helper functions
|
||||
\* Note: GetStripeIdx is not used, GetStripe is used instead
|
||||
@@ -90,15 +120,29 @@ VARIABLES
|
||||
\* Per-stripe locks
|
||||
lockInc, \* lockInc[stripe] = thread holding increment lock (or NULL)
|
||||
lockDec, \* lockDec[stripe] = thread holding decrement lock (or NULL)
|
||||
\* Global lock
|
||||
globalLock, \* thread holding global lock (or NULL)
|
||||
\* Global lock (now the WRITE side of the RWLock)
|
||||
globalLock, \* thread holding write lock (or NULL)
|
||||
\* Merged roots array (used during collection)
|
||||
mergedRoots, \* sequence of (object, type) pairs
|
||||
\* Collection state
|
||||
collecting, \* TRUE if collection is in progress
|
||||
gcEnv, \* GC environment: {touched, edges, rcSum, toFree, ...}
|
||||
\* Pending operations (for modeling atomicity)
|
||||
pendingWrites \* set of pending write barrier operations
|
||||
pendingWrites, \* set of pending write barrier operations
|
||||
\* --- Seq payload race modeling ---
|
||||
\* Seq payloads: models the heap-allocated data arrays of seq[T] fields
|
||||
seqData, \* [Objects -> SeqPayloads \cup {NULL}] -- current payload for obj's seq
|
||||
payloadAlive, \* [SeqPayloads -> BOOLEAN] -- is this payload's memory valid?
|
||||
\* RWLock read side: set of threads holding the read lock.
|
||||
\* Seq mutations (assign, add, setLen, etc.) acquire the read lock.
|
||||
\* The collector (write lock holder) gets exclusive access.
|
||||
rwLockReaders, \* SUBSET Threads -- threads currently holding the read lock
|
||||
\* Collector's in-progress seq trace: the payload pointer read during tracing.
|
||||
\* Between reading the pointer and accessing the data, the payload could be freed.
|
||||
collectorPayload \* SeqPayloads \cup {NULL} -- payload being traced by collector
|
||||
|
||||
\* Convenience tuple for seq-related variables (used in UNCHANGED clauses)
|
||||
seqVars == <<seqData, payloadAlive, rwLockReaders, collectorPayload>>
|
||||
|
||||
\* Type invariants
|
||||
TypeOK ==
|
||||
@@ -117,6 +161,11 @@ TypeOK ==
|
||||
/\ mergedRoots \in Seq([obj: Objects, desc: ObjTypes])
|
||||
/\ collecting \in BOOLEAN
|
||||
/\ pendingWrites \in SUBSET ([thread: Threads, dest: Objects, old: Objects \cup {NULL}, src: Objects \cup {NULL}, phase: {"store", "inc", "dec"}])
|
||||
\* Seq payload types
|
||||
/\ seqData \in [Objects -> SeqPayloads \cup {NULL}]
|
||||
/\ payloadAlive \in [SeqPayloads -> BOOLEAN]
|
||||
/\ rwLockReaders \in SUBSET Threads
|
||||
/\ collectorPayload \in SeqPayloads \cup {NULL}
|
||||
|
||||
\* Helper: internal reference count (heap-to-heap edges)
|
||||
InternalRC(obj) ==
|
||||
@@ -163,7 +212,7 @@ MutatorWriteAtomicStore(thread, destObj, destField, oldVal, newVal, desc) ==
|
||||
IF x = newVal /\ newVal # NULL
|
||||
THEN TRUE
|
||||
ELSE FALSE]]
|
||||
/\ UNCHANGED <<roots, rc, color, inRoots, toIncLen, toInc, toDecLen, toDec, lockInc, lockDec, globalLock, mergedRoots, collecting, gcEnv, pendingWrites>>
|
||||
/\ UNCHANGED <<roots, rc, color, inRoots, toIncLen, toInc, toDecLen, toDec, lockInc, lockDec, globalLock, mergedRoots, collecting, gcEnv, pendingWrites, seqData, payloadAlive, rwLockReaders, collectorPayload>>
|
||||
|
||||
\* ============================================================================
|
||||
\* Phase 2: RC Buffering (if space available)
|
||||
@@ -193,7 +242,7 @@ WriteBarrier(thread, destObj, destField, oldVal, newVal, desc) ==
|
||||
/\ toDec' = IF oldVal # NULL /\ toDecLen[stripe] < QueueSize
|
||||
THEN [toDec EXCEPT ![stripe] = Append(toDec[stripe], [obj |-> oldVal, desc |-> desc])]
|
||||
ELSE toDec
|
||||
/\ UNCHANGED <<edges, roots, rc, color, inRoots, mergedRoots, lockInc, lockDec, globalLock, collecting, gcEnv, pendingWrites>>
|
||||
/\ UNCHANGED <<edges, roots, rc, color, inRoots, mergedRoots, lockInc, lockDec, globalLock, collecting, gcEnv, pendingWrites, seqData, payloadAlive, rwLockReaders, collectorPayload>>
|
||||
|
||||
\* ============================================================================
|
||||
\* Phase 3: Overflow Handling (separate actions that can block)
|
||||
@@ -215,7 +264,7 @@ MutatorWriteMergeInc(thread) ==
|
||||
externalRC == Cardinality({t \in Threads : roots[t][x]})
|
||||
IN internalRC + externalRC]
|
||||
/\ globalLock' = NULL \* Release lock after merge
|
||||
/\ UNCHANGED <<edges, roots, color, inRoots, toDecLen, toDec, lockInc, lockDec, mergedRoots, collecting, gcEnv, pendingWrites>>
|
||||
/\ UNCHANGED <<edges, roots, color, inRoots, toDecLen, toDec, lockInc, lockDec, mergedRoots, collecting, gcEnv, pendingWrites, seqData, payloadAlive, rwLockReaders, collectorPayload>>
|
||||
|
||||
\* Handle decrement overflow: merge ALL buffers when lock is available
|
||||
\* This calls collectCycles() which merges both increment and decrement buffers
|
||||
@@ -257,7 +306,7 @@ MutatorWriteMergeDec(thread) ==
|
||||
/\ toDecLen' = [s \in 0..(NumStripes-1) |-> 0]
|
||||
/\ toDec' = [s \in 0..(NumStripes-1) |-> <<>>]
|
||||
/\ globalLock' = NULL \* Lock acquired, merge done, lock released (entire withLock block is atomic)
|
||||
/\ UNCHANGED <<edges, roots, color, lockInc, lockDec, collecting, gcEnv, pendingWrites>>
|
||||
/\ UNCHANGED <<edges, roots, color, lockInc, lockDec, collecting, gcEnv, pendingWrites, seqData, payloadAlive, rwLockReaders, collectorPayload>>
|
||||
|
||||
\* ============================================================================
|
||||
\* Merge Operation: mergePendingRoots
|
||||
@@ -311,7 +360,7 @@ MergePendingRoots ==
|
||||
/\ toInc' = [s \in 0..(NumStripes-1) |-> <<>>]
|
||||
/\ toDecLen' = [s \in 0..(NumStripes-1) |-> 0]
|
||||
/\ toDec' = [s \in 0..(NumStripes-1) |-> <<>>]
|
||||
/\ UNCHANGED <<edges, roots, color, lockInc, lockDec, globalLock, collecting, gcEnv, pendingWrites>>
|
||||
/\ UNCHANGED <<edges, roots, color, lockInc, lockDec, globalLock, collecting, gcEnv, pendingWrites, seqData, payloadAlive, rwLockReaders, collectorPayload>>
|
||||
|
||||
\* ============================================================================
|
||||
\* Trial Deletion: markGray
|
||||
@@ -365,7 +414,7 @@ MarkGray(obj, desc) ==
|
||||
\* For roots, the RC includes external refs which survive trial deletion.
|
||||
rc' = [x \in Objects |->
|
||||
IF x \in allReachable THEN rc[x] - internalEdgeCount[x] ELSE rc[x]]
|
||||
/\ UNCHANGED <<edges, roots, inRoots, toIncLen, toInc, toDecLen, toDec, lockInc, lockDec, globalLock, mergedRoots, collecting, gcEnv, pendingWrites>>
|
||||
/\ UNCHANGED <<edges, roots, inRoots, toIncLen, toInc, toDecLen, toDec, lockInc, lockDec, globalLock, mergedRoots, collecting, gcEnv, pendingWrites, seqData, payloadAlive, rwLockReaders, collectorPayload>>
|
||||
|
||||
\* ============================================================================
|
||||
\* Scan Phase
|
||||
@@ -422,7 +471,7 @@ Scan(obj, desc) ==
|
||||
ELSE \* Mark white (part of closed cycle)
|
||||
/\ color' = [color EXCEPT ![obj] = colWhite]
|
||||
/\ UNCHANGED <<rc>>
|
||||
/\ UNCHANGED <<edges, roots, inRoots, toIncLen, toInc, toDecLen, toDec, lockInc, lockDec, globalLock, mergedRoots, collecting, gcEnv, pendingWrites>>
|
||||
/\ UNCHANGED <<edges, roots, inRoots, toIncLen, toInc, toDecLen, toDec, lockInc, lockDec, globalLock, mergedRoots, collecting, gcEnv, pendingWrites, seqData, payloadAlive, rwLockReaders, collectorPayload>>
|
||||
|
||||
\* ============================================================================
|
||||
\* Collection Phase: collectColor
|
||||
@@ -442,7 +491,7 @@ CollectColor(obj, desc, targetColor) ==
|
||||
edges' = [edges EXCEPT ![obj] = [x \in Objects |->
|
||||
IF x = obj THEN FALSE ELSE edges[obj][x]]]
|
||||
/\ color' = [color EXCEPT ![obj] = colBlack] \* Mark as freed
|
||||
/\ UNCHANGED <<roots, rc, inRoots, toIncLen, toInc, toDecLen, toDec, lockInc, lockDec, globalLock, mergedRoots, collecting, gcEnv, pendingWrites>>
|
||||
/\ UNCHANGED <<roots, rc, inRoots, toIncLen, toInc, toDecLen, toDec, lockInc, lockDec, globalLock, mergedRoots, collecting, gcEnv, pendingWrites, seqData, payloadAlive, rwLockReaders, collectorPayload>>
|
||||
|
||||
\* ============================================================================
|
||||
\* Collection Cycle: collectCyclesBacon
|
||||
@@ -454,7 +503,7 @@ StartCollection ==
|
||||
/\ Len(mergedRoots) >= RootsThreshold
|
||||
/\ collecting' = TRUE
|
||||
/\ gcEnv' = [touched |-> 0, edges |-> 0, rcSum |-> 0, toFree |-> {}]
|
||||
/\ UNCHANGED <<edges, roots, rc, color, inRoots, toIncLen, toInc, toDecLen, toDec, lockInc, lockDec, globalLock, mergedRoots, pendingWrites>>
|
||||
/\ UNCHANGED <<edges, roots, rc, color, inRoots, toIncLen, toInc, toDecLen, toDec, lockInc, lockDec, globalLock, mergedRoots, pendingWrites, seqData, payloadAlive, rwLockReaders, collectorPayload>>
|
||||
|
||||
EndCollection ==
|
||||
/\ globalLock # NULL
|
||||
@@ -464,7 +513,7 @@ EndCollection ==
|
||||
IF x \in {r.obj : r \in mergedRoots} THEN FALSE ELSE inRoots[x]]
|
||||
/\ mergedRoots' = <<>>
|
||||
/\ collecting' = FALSE
|
||||
/\ UNCHANGED <<edges, roots, rc, color, toIncLen, toInc, toDecLen, toDec, lockInc, lockDec, globalLock, gcEnv, pendingWrites>>
|
||||
/\ UNCHANGED <<edges, roots, rc, color, toIncLen, toInc, toDecLen, toDec, lockInc, lockDec, globalLock, gcEnv, pendingWrites, seqData, payloadAlive, rwLockReaders, collectorPayload>>
|
||||
|
||||
\* ============================================================================
|
||||
\* Mutator Actions
|
||||
@@ -491,7 +540,7 @@ MutatorWrite(thread, destObj, destField, oldVal, newVal, desc) ==
|
||||
IF incOverflow \/ decOverflow
|
||||
THEN \* Overflow: atomic store happened, but buffering is deferred
|
||||
\* Buffers stay full, merge will happen when lock is available (via MutatorWriteMergeInc/Dec)
|
||||
/\ UNCHANGED <<roots, rc, color, inRoots, toIncLen, toInc, toDecLen, toDec, lockInc, lockDec, globalLock, mergedRoots, collecting, gcEnv, pendingWrites>>
|
||||
/\ UNCHANGED <<roots, rc, color, inRoots, toIncLen, toInc, toDecLen, toDec, lockInc, lockDec, globalLock, mergedRoots, collecting, gcEnv, pendingWrites, seqData, payloadAlive, rwLockReaders, collectorPayload>>
|
||||
ELSE \* No overflow: buffer normally
|
||||
/\ WriteBarrier(thread, destObj, destField, oldVal, newVal, desc)
|
||||
/\ UNCHANGED <<roots, collecting, pendingWrites>>
|
||||
@@ -521,16 +570,19 @@ MutatorRootAssign(thread, obj, val) ==
|
||||
/\ collecting' = collecting
|
||||
/\ gcEnv' = gcEnv
|
||||
/\ pendingWrites' = pendingWrites
|
||||
/\ UNCHANGED seqVars
|
||||
|
||||
\* ============================================================================
|
||||
\* Collector Actions
|
||||
\* ============================================================================
|
||||
|
||||
\* Collector acquires global lock for entire collection cycle
|
||||
\* Collector acquires write lock (global lock) for entire collection cycle.
|
||||
\* RWLock semantics: writer can only acquire when no readers hold the read lock.
|
||||
CollectorAcquireLock(thread) ==
|
||||
/\ globalLock = NULL
|
||||
/\ rwLockReaders = {} \* RWLock: no readers allowed when acquiring write lock
|
||||
/\ globalLock' = thread
|
||||
/\ UNCHANGED <<edges, roots, rc, color, inRoots, toIncLen, toInc, toDecLen, toDec, lockInc, lockDec, mergedRoots, collecting, gcEnv, pendingWrites>>
|
||||
/\ UNCHANGED <<edges, roots, rc, color, inRoots, toIncLen, toInc, toDecLen, toDec, lockInc, lockDec, mergedRoots, collecting, gcEnv, pendingWrites, seqData, payloadAlive, rwLockReaders, collectorPayload>>
|
||||
|
||||
CollectorMerge ==
|
||||
/\ globalLock # NULL
|
||||
@@ -571,7 +623,93 @@ CollectorEnd ==
|
||||
CollectorReleaseLock(thread) ==
|
||||
/\ globalLock = thread
|
||||
/\ globalLock' = NULL
|
||||
/\ UNCHANGED <<edges, roots, rc, color, inRoots, toIncLen, toInc, toDecLen, toDec, lockInc, lockDec, mergedRoots, collecting, gcEnv, pendingWrites>>
|
||||
/\ UNCHANGED <<edges, roots, rc, color, inRoots, toIncLen, toInc, toDecLen, toDec, lockInc, lockDec, mergedRoots, collecting, gcEnv, pendingWrites, seqData, payloadAlive, rwLockReaders, collectorPayload>>
|
||||
|
||||
\* ============================================================================
|
||||
\* Seq Payload Actions (RWLock-protected)
|
||||
\* ============================================================================
|
||||
\* These actions model the race between the collector tracing seq payloads
|
||||
\* and mutators replacing/freeing seq payloads.
|
||||
\*
|
||||
\* The collector traces seq payloads in two steps:
|
||||
\* 1. CollectorStartTraceSeq: reads seqData[obj] (gets payload pointer)
|
||||
\* 2. CollectorFinishTraceSeq: accesses the payload data
|
||||
\* Between these steps, a mutator could free the payload (the race).
|
||||
\*
|
||||
\* The RWLock prevents this:
|
||||
\* - Collector holds write lock (globalLock) during tracing
|
||||
\* - MutatorSeqAssign requires read lock (rwLockReaders)
|
||||
\* - Read lock requires globalLock = NULL
|
||||
\* - Therefore MutatorSeqAssign is blocked during collection
|
||||
\*
|
||||
\* Note: This models the memory safety aspect of seq tracing.
|
||||
\* The cycle collection algorithm (MarkGray, Scan, etc.) operates on the
|
||||
\* logical edge graph. Seq payloads are a physical representation detail
|
||||
\* that affects memory safety but not GC correctness (which is already
|
||||
\* covered by the existing Safety property).
|
||||
|
||||
\* Mutator acquires read lock for seq mutation.
|
||||
\* RWLock semantics: read lock can be acquired when no writer holds the write lock.
|
||||
\* Multiple readers can hold the read lock simultaneously.
|
||||
MutatorAcquireSeqLock(thread) ==
|
||||
/\ globalLock = NULL \* RWLock: no writer allowed when acquiring read lock
|
||||
/\ thread \notin rwLockReaders
|
||||
/\ rwLockReaders' = rwLockReaders \cup {thread}
|
||||
/\ UNCHANGED <<edges, roots, rc, color, inRoots, toIncLen, toInc, toDecLen, toDec, lockInc, lockDec, globalLock, mergedRoots, collecting, gcEnv, pendingWrites, seqData, payloadAlive, collectorPayload>>
|
||||
|
||||
\* Mutator releases read lock after seq mutation completes.
|
||||
MutatorReleaseSeqLock(thread) ==
|
||||
/\ thread \in rwLockReaders
|
||||
/\ rwLockReaders' = rwLockReaders \ {thread}
|
||||
/\ UNCHANGED <<edges, roots, rc, color, inRoots, toIncLen, toInc, toDecLen, toDec, lockInc, lockDec, globalLock, mergedRoots, collecting, gcEnv, pendingWrites, seqData, payloadAlive, collectorPayload>>
|
||||
|
||||
\* Mutator replaces a seq field's payload (e.g., r.list = newSeq).
|
||||
\* This frees the old payload and installs a new one.
|
||||
\* Requires the read lock (RWLock protection against concurrent collection).
|
||||
\*
|
||||
\* In the real implementation, this is a value-type assignment (=sink/=copy)
|
||||
\* that frees the old data array and installs a new one. The old array is freed
|
||||
\* immediately, NOT deferred to the cycle collector.
|
||||
MutatorSeqAssign(thread, obj, newPayload) ==
|
||||
/\ thread \in rwLockReaders \* Must hold read lock
|
||||
/\ seqData[obj] # NULL \* Object has an existing seq payload
|
||||
/\ newPayload \in SeqPayloads
|
||||
/\ ~payloadAlive[newPayload] \* New payload is freshly allocated (not yet alive)
|
||||
/\ LET oldPayload == seqData[obj]
|
||||
IN
|
||||
/\ seqData' = [seqData EXCEPT ![obj] = newPayload]
|
||||
/\ payloadAlive' = [payloadAlive EXCEPT ![oldPayload] = FALSE,
|
||||
![newPayload] = TRUE]
|
||||
\* Note: In a complete model, this would also update edges[obj] to reflect
|
||||
\* the new seq elements and buffer RC changes (inc new elements, dec old elements).
|
||||
\* We omit this here to focus on the memory safety property (payload lifetime).
|
||||
/\ UNCHANGED <<edges, roots, rc, color, inRoots, toIncLen, toInc, toDecLen, toDec, lockInc, lockDec, globalLock, mergedRoots, collecting, gcEnv, pendingWrites, rwLockReaders, collectorPayload>>
|
||||
|
||||
\* Collector begins tracing an object's seq field.
|
||||
\* Reads the seqData pointer and stores it in collectorPayload.
|
||||
\* This is the first step of a two-step trace operation.
|
||||
\* The collector must hold the write lock (globalLock).
|
||||
CollectorStartTraceSeq(obj) ==
|
||||
/\ globalLock # NULL \* Collector holds write lock
|
||||
/\ collecting = TRUE \* In collection phase
|
||||
/\ seqData[obj] # NULL \* Object has a seq field
|
||||
/\ collectorPayload = NULL \* Not already mid-trace
|
||||
/\ collectorPayload' = seqData[obj]
|
||||
/\ UNCHANGED <<edges, roots, rc, color, inRoots, toIncLen, toInc, toDecLen, toDec, lockInc, lockDec, globalLock, mergedRoots, collecting, gcEnv, pendingWrites, seqData, payloadAlive, rwLockReaders>>
|
||||
|
||||
\* Collector finishes tracing an object's seq field.
|
||||
\* Accesses the payload data via collectorPayload.
|
||||
\* The payload MUST still be alive (this is checked by SeqPayloadSafety).
|
||||
\* After accessing the payload, clears collectorPayload.
|
||||
CollectorFinishTraceSeq ==
|
||||
/\ globalLock # NULL \* Collector holds write lock
|
||||
/\ collecting = TRUE \* In collection phase
|
||||
/\ collectorPayload # NULL \* Mid-trace on a payload
|
||||
\* The actual work: read payloadEdges[collectorPayload] to discover children.
|
||||
\* We don't model the trace results here; the safety property ensures
|
||||
\* the read is valid (payload is alive).
|
||||
/\ collectorPayload' = NULL
|
||||
/\ UNCHANGED <<edges, roots, rc, color, inRoots, toIncLen, toInc, toDecLen, toDec, lockInc, lockDec, globalLock, mergedRoots, collecting, gcEnv, pendingWrites, seqData, payloadAlive, rwLockReaders>>
|
||||
|
||||
\* ============================================================================
|
||||
\* Next State Relation
|
||||
@@ -607,6 +745,16 @@ Next ==
|
||||
\/ CollectorEnd
|
||||
\/ \E thread \in Threads:
|
||||
CollectorReleaseLock(thread)
|
||||
\* --- Seq payload actions ---
|
||||
\/ \E thread \in Threads:
|
||||
MutatorAcquireSeqLock(thread)
|
||||
\/ \E thread \in Threads:
|
||||
MutatorReleaseSeqLock(thread)
|
||||
\/ \E thread \in Threads, obj \in Objects, p \in SeqPayloads:
|
||||
MutatorSeqAssign(thread, obj, p)
|
||||
\/ \E obj \in Objects:
|
||||
CollectorStartTraceSeq(obj)
|
||||
\/ CollectorFinishTraceSeq
|
||||
|
||||
\* ============================================================================
|
||||
\* Initial State
|
||||
@@ -642,6 +790,11 @@ Init ==
|
||||
/\ collecting = FALSE
|
||||
/\ gcEnv = [touched |-> 0, edges |-> 0, rcSum |-> 0, toFree |-> {}]
|
||||
/\ pendingWrites = {}
|
||||
\* Seq payload initial state
|
||||
/\ seqData = [x \in Objects |-> NULL] \* No seq fields initially
|
||||
/\ payloadAlive = [p \in SeqPayloads |-> FALSE] \* No payloads alive initially
|
||||
/\ rwLockReaders = {} \* No threads hold read lock
|
||||
/\ collectorPayload = NULL \* Collector not mid-trace
|
||||
/\ TypeOK
|
||||
|
||||
\* ============================================================================
|
||||
@@ -748,14 +901,53 @@ CycleInvariant ==
|
||||
THEN ExternalRC(obj) = 0
|
||||
ELSE TRUE
|
||||
|
||||
\* ============================================================================
|
||||
\* Seq Payload Safety
|
||||
\* ============================================================================
|
||||
\* Memory safety: The collector never accesses a freed seq payload.
|
||||
\*
|
||||
\* collectorPayload holds the payload pointer the collector read during
|
||||
\* CollectorStartTraceSeq. Between that action and CollectorFinishTraceSeq,
|
||||
\* the collector will dereference this pointer to read the seq's elements.
|
||||
\* If the payload has been freed in between, this is a use-after-free.
|
||||
\*
|
||||
\* The RWLock prevents this:
|
||||
\* - collectorPayload is only set when globalLock # NULL (write lock held)
|
||||
\* - MutatorSeqAssign (which frees payloads) requires rwLockReaders membership
|
||||
\* - MutatorAcquireSeqLock requires globalLock = NULL (no writer)
|
||||
\* - Therefore: while collectorPayload # NULL, no MutatorSeqAssign can execute
|
||||
\* - Therefore: payloadAlive[collectorPayload] remains TRUE
|
||||
\*
|
||||
\* Without the RWLock (if MutatorSeqAssign didn't require the read lock),
|
||||
\* the following interleaving would violate this property:
|
||||
\* 1. Collector acquires write lock
|
||||
\* 2. CollectorStartTraceSeq(obj) -- collectorPayload = P
|
||||
\* 3. MutatorSeqAssign(thread, obj, Q) -- frees P, payloadAlive[P] = FALSE
|
||||
\* 4. SeqPayloadSafety VIOLATED: collectorPayload = P but payloadAlive[P] = FALSE
|
||||
|
||||
SeqPayloadSafety ==
|
||||
collectorPayload # NULL => payloadAlive[collectorPayload]
|
||||
|
||||
\* ============================================================================
|
||||
\* RWLock Invariant
|
||||
\* ============================================================================
|
||||
\* The read-write lock ensures mutual exclusion between the collector (writer)
|
||||
\* and seq mutations (readers). The writer and readers are never active at
|
||||
\* the same time.
|
||||
|
||||
RWLockInvariant ==
|
||||
globalLock # NULL => rwLockReaders = {}
|
||||
|
||||
\* ============================================================================
|
||||
\* Specification
|
||||
\* ============================================================================
|
||||
|
||||
Spec == Init /\ [][Next]_<<edges, roots, rc, color, inRoots, toIncLen, toInc, toDecLen, toDec, lockInc, lockDec, globalLock, mergedRoots, collecting, gcEnv, pendingWrites>>
|
||||
Spec == Init /\ [][Next]_<<edges, roots, rc, color, inRoots, toIncLen, toInc, toDecLen, toDec, lockInc, lockDec, globalLock, mergedRoots, collecting, gcEnv, pendingWrites, seqData, payloadAlive, rwLockReaders, collectorPayload>>
|
||||
|
||||
THEOREM Spec => []Safety
|
||||
THEOREM Spec => []RCInvariant
|
||||
THEOREM Spec => []CycleInvariant
|
||||
THEOREM Spec => []SeqPayloadSafety
|
||||
THEOREM Spec => []RWLockInvariant
|
||||
|
||||
====
|
||||
|
||||
@@ -99,7 +99,7 @@
|
||||
<h1><a class="toc-backref" href="#7">Types</a></h1>
|
||||
<dl class="item">
|
||||
<div id="A">
|
||||
<dt><pre><a href="main.html#A"><span class="Identifier">A</span></a> <span class="Other">=</span> <span class="Keyword">object</span></pre></dt>
|
||||
<dt><pre><a href="#A"><span class="Identifier">A</span></a> <span class="Other">=</span> <span class="Keyword">object</span></pre></dt>
|
||||
<dd>
|
||||
|
||||
|
||||
|
||||
@@ -88,7 +88,7 @@
|
||||
<h1><a class="toc-backref" href="#7">Types</a></h1>
|
||||
<dl class="item">
|
||||
<div id="submoduleInt">
|
||||
<dt><pre><a href="submodule.html#submoduleInt"><span class="Identifier">submoduleInt</span></a> <span class="Other">=</span> <span class="Keyword">distinct</span> <span class="Identifier">int</span></pre></dt>
|
||||
<dt><pre><a href="#submoduleInt"><span class="Identifier">submoduleInt</span></a> <span class="Other">=</span> <span class="Keyword">distinct</span> <span class="Identifier">int</span></pre></dt>
|
||||
<dd>
|
||||
|
||||
|
||||
|
||||
@@ -123,7 +123,6 @@ Modified by Boyd Greenfield and narimiran
|
||||
}
|
||||
|
||||
html {
|
||||
overflow-x: hidden;
|
||||
max-width: 100%;
|
||||
box-sizing: border-box;
|
||||
font-size: 100%;
|
||||
@@ -572,8 +571,8 @@ blockquote.markdown-quote {
|
||||
padding-left: 3px;
|
||||
padding-right: 3px;
|
||||
border-radius: 4px;
|
||||
white-space: normal;
|
||||
word-break: break-all;
|
||||
white-space: pre-wrap;
|
||||
overflow-wrap: break-word;
|
||||
}
|
||||
|
||||
span.tok {
|
||||
@@ -674,6 +673,8 @@ table {
|
||||
border-collapse: collapse;
|
||||
border-color: var(--third-background);
|
||||
border-spacing: 0;
|
||||
display: block;
|
||||
overflow-x: auto;
|
||||
}
|
||||
|
||||
table:not(.line-nums-table) {
|
||||
|
||||
@@ -257,7 +257,7 @@ Ref. <a class="reference internal nimdoc" title="proc `[]`[T](x: G[T]): T" href=
|
||||
<h1><a class="toc-backref" href="#7">Types</a></h1>
|
||||
<dl class="item">
|
||||
<div id="G">
|
||||
<dt><pre><a href="utils.html#G"><span class="Identifier">G</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span> <span class="Other">=</span> <span class="Keyword">object</span></pre></dt>
|
||||
<dt><pre><a href="#G"><span class="Identifier">G</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span> <span class="Other">=</span> <span class="Keyword">object</span></pre></dt>
|
||||
<dd>
|
||||
|
||||
|
||||
@@ -265,7 +265,7 @@ Ref. <a class="reference internal nimdoc" title="proc `[]`[T](x: G[T]): T" href=
|
||||
</dd>
|
||||
</div>
|
||||
<div id="SomeType">
|
||||
<dt><pre><a href="utils.html#SomeType"><span class="Identifier">SomeType</span></a> <span class="Other">=</span> <span class="Keyword">enum</span>
|
||||
<dt><pre><a href="#SomeType"><span class="Identifier">SomeType</span></a> <span class="Other">=</span> <span class="Keyword">enum</span>
|
||||
<span class="Identifier">enumValueA</span><span class="Other">,</span> <span class="Identifier">enumValueB</span><span class="Other">,</span> <span class="Identifier">enumValueC</span></pre></dt>
|
||||
<dd>
|
||||
|
||||
@@ -281,7 +281,7 @@ Ref. <a class="reference internal nimdoc" title="proc `[]`[T](x: G[T]): T" href=
|
||||
<dl class="item">
|
||||
<div id="$-procs-all">
|
||||
<div id="$,G[T]">
|
||||
<dt><pre><span class="Keyword">proc</span> <a href="#%24%2CG%5BT%5D"><span class="Identifier">`$`</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">a</span><span class="Other">:</span> <a href="utils.html#G"><span class="Identifier">G</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">string</span></pre></dt>
|
||||
<dt><pre><span class="Keyword">proc</span> <a href="#%24%2CG%5BT%5D"><span class="Identifier">`$`</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">a</span><span class="Other">:</span> <a href="#G"><span class="Identifier">G</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">string</span></pre></dt>
|
||||
<dd>
|
||||
|
||||
|
||||
@@ -289,7 +289,7 @@ Ref. <a class="reference internal nimdoc" title="proc `[]`[T](x: G[T]): T" href=
|
||||
</dd>
|
||||
</div>
|
||||
<div id="$,ref.SomeType">
|
||||
<dt><pre><span class="Keyword">proc</span> <a href="#%24%2Cref.SomeType"><span class="Identifier">`$`</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">a</span><span class="Other">:</span> <span class="Keyword">ref</span> <a href="utils.html#SomeType"><span class="Identifier">SomeType</span></a><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">string</span></pre></dt>
|
||||
<dt><pre><span class="Keyword">proc</span> <a href="#%24%2Cref.SomeType"><span class="Identifier">`$`</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">a</span><span class="Other">:</span> <span class="Keyword">ref</span> <a href="#SomeType"><span class="Identifier">SomeType</span></a><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">string</span></pre></dt>
|
||||
<dd>
|
||||
|
||||
|
||||
@@ -300,7 +300,7 @@ Ref. <a class="reference internal nimdoc" title="proc `[]`[T](x: G[T]): T" href=
|
||||
</div>
|
||||
<div id="'big-procs-all">
|
||||
<div id="'big,string">
|
||||
<dt><pre><span class="Keyword">func</span> <a href="#%27big%2Cstring"><span class="Identifier">`'big`</span></a><span class="Other">(</span><span class="Identifier">a</span><span class="Other">:</span> <span class="Identifier">string</span><span class="Other">)</span><span class="Other">:</span> <a href="utils.html#SomeType"><span class="Identifier">SomeType</span></a> {.<span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
|
||||
<dt><pre><span class="Keyword">func</span> <a href="#%27big%2Cstring"><span class="Identifier">`'big`</span></a><span class="Other">(</span><span class="Identifier">a</span><span class="Other">:</span> <span class="Identifier">string</span><span class="Other">)</span><span class="Other">:</span> <a href="#SomeType"><span class="Identifier">SomeType</span></a> {.<span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
|
||||
<dd>
|
||||
|
||||
|
||||
@@ -311,7 +311,7 @@ Ref. <a class="reference internal nimdoc" title="proc `[]`[T](x: G[T]): T" href=
|
||||
</div>
|
||||
<div id="[]-procs-all">
|
||||
<div id="[],G[T]">
|
||||
<dt><pre><span class="Keyword">proc</span> <a href="#%5B%5D%2CG%5BT%5D"><span class="Identifier">`[]`</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">x</span><span class="Other">:</span> <a href="utils.html#G"><span class="Identifier">G</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">T</span></pre></dt>
|
||||
<dt><pre><span class="Keyword">proc</span> <a href="#%5B%5D%2CG%5BT%5D"><span class="Identifier">`[]`</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">x</span><span class="Other">:</span> <a href="#G"><span class="Identifier">G</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">T</span></pre></dt>
|
||||
<dd>
|
||||
|
||||
|
||||
@@ -322,7 +322,7 @@ Ref. <a class="reference internal nimdoc" title="proc `[]`[T](x: G[T]): T" href=
|
||||
</div>
|
||||
<div id="[]=-procs-all">
|
||||
<div id="[]=,G[T],int,T">
|
||||
<dt><pre><span class="Keyword">proc</span> <a href="#%5B%5D%3D%2CG%5BT%5D%2Cint%2CT"><span class="Identifier">`[]=`</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">a</span><span class="Other">:</span> <span class="Keyword">var</span> <a href="utils.html#G"><span class="Identifier">G</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">;</span> <span class="Identifier">index</span><span class="Other">:</span> <span class="Identifier">int</span><span class="Other">;</span> <span class="Identifier">value</span><span class="Other">:</span> <span class="Identifier">T</span><span class="Other">)</span></pre></dt>
|
||||
<dt><pre><span class="Keyword">proc</span> <a href="#%5B%5D%3D%2CG%5BT%5D%2Cint%2CT"><span class="Identifier">`[]=`</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">a</span><span class="Other">:</span> <span class="Keyword">var</span> <a href="#G"><span class="Identifier">G</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">;</span> <span class="Identifier">index</span><span class="Other">:</span> <span class="Identifier">int</span><span class="Other">;</span> <span class="Identifier">value</span><span class="Other">:</span> <span class="Identifier">T</span><span class="Other">)</span></pre></dt>
|
||||
<dd>
|
||||
|
||||
|
||||
@@ -345,7 +345,7 @@ Ref. <a class="reference internal nimdoc" title="proc `[]`[T](x: G[T]): T" href=
|
||||
</div>
|
||||
<div id="f-procs-all">
|
||||
<div id="f,G[int]">
|
||||
<dt><pre><span class="Keyword">proc</span> <a href="#f%2CG%5Bint%5D"><span class="Identifier">f</span></a><span class="Other">(</span><span class="Identifier">x</span><span class="Other">:</span> <a href="utils.html#G"><span class="Identifier">G</span></a><span class="Other">[</span><span class="Identifier">int</span><span class="Other">]</span><span class="Other">)</span> {.<span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
|
||||
<dt><pre><span class="Keyword">proc</span> <a href="#f%2CG%5Bint%5D"><span class="Identifier">f</span></a><span class="Other">(</span><span class="Identifier">x</span><span class="Other">:</span> <a href="#G"><span class="Identifier">G</span></a><span class="Other">[</span><span class="Identifier">int</span><span class="Other">]</span><span class="Other">)</span> {.<span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
|
||||
<dd>
|
||||
|
||||
There is also variant <a class="reference internal nimdoc" title="proc f(x: G[string])" href="#f,G[string]">f(G[string])</a>
|
||||
@@ -353,7 +353,7 @@ Ref. <a class="reference internal nimdoc" title="proc `[]`[T](x: G[T]): T" href=
|
||||
</dd>
|
||||
</div>
|
||||
<div id="f,G[string]">
|
||||
<dt><pre><span class="Keyword">proc</span> <a href="#f%2CG%5Bstring%5D"><span class="Identifier">f</span></a><span class="Other">(</span><span class="Identifier">x</span><span class="Other">:</span> <a href="utils.html#G"><span class="Identifier">G</span></a><span class="Other">[</span><span class="Identifier">string</span><span class="Other">]</span><span class="Other">)</span> {.<span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
|
||||
<dt><pre><span class="Keyword">proc</span> <a href="#f%2CG%5Bstring%5D"><span class="Identifier">f</span></a><span class="Other">(</span><span class="Identifier">x</span><span class="Other">:</span> <a href="#G"><span class="Identifier">G</span></a><span class="Other">[</span><span class="Identifier">string</span><span class="Other">]</span><span class="Other">)</span> {.<span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
|
||||
<dd>
|
||||
|
||||
See also <a class="reference internal nimdoc" title="proc f(x: G[int])" href="#f,G[int]">f(G[int])</a>.
|
||||
@@ -528,7 +528,7 @@ Ref. <a class="reference internal nimdoc" title="proc `[]`[T](x: G[T]): T" href=
|
||||
</div>
|
||||
<div id="someType-procs-all">
|
||||
<div id="someType_2">
|
||||
<dt><pre><span class="Keyword">proc</span> <a href="#someType_2"><span class="Identifier">someType</span></a><span class="Other">(</span><span class="Other">)</span><span class="Other">:</span> <a href="utils.html#SomeType"><span class="Identifier">SomeType</span></a> {.<span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
|
||||
<dt><pre><span class="Keyword">proc</span> <a href="#someType_2"><span class="Identifier">someType</span></a><span class="Other">(</span><span class="Other">)</span><span class="Other">:</span> <a href="#SomeType"><span class="Identifier">SomeType</span></a> {.<span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
|
||||
<dd>
|
||||
|
||||
constructor.
|
||||
@@ -545,7 +545,7 @@ Ref. <a class="reference internal nimdoc" title="proc `[]`[T](x: G[T]): T" href=
|
||||
<dl class="item">
|
||||
<div id="fooBar-iterators-all">
|
||||
<div id="fooBar.i,seq[SomeType]">
|
||||
<dt><pre><span class="Keyword">iterator</span> <a href="#fooBar.i%2Cseq%5BSomeType%5D"><span class="Identifier">fooBar</span></a><span class="Other">(</span><span class="Identifier">a</span><span class="Other">:</span> <span class="Identifier">seq</span><span class="Other">[</span><a href="utils.html#SomeType"><span class="Identifier">SomeType</span></a><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">int</span> {.<span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
|
||||
<dt><pre><span class="Keyword">iterator</span> <a href="#fooBar.i%2Cseq%5BSomeType%5D"><span class="Identifier">fooBar</span></a><span class="Other">(</span><span class="Identifier">a</span><span class="Other">:</span> <span class="Identifier">seq</span><span class="Other">[</span><a href="#SomeType"><span class="Identifier">SomeType</span></a><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">int</span> {.<span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
|
||||
<dd>
|
||||
|
||||
|
||||
|
||||
@@ -381,7 +381,7 @@
|
||||
<h1><a class="toc-backref" href="#7">Types</a></h1>
|
||||
<dl class="item">
|
||||
<div id="A">
|
||||
<dt><pre><a href="testproject.html#A"><span class="Identifier">A</span></a> {.<span class="Identifier">inject</span>.} <span class="Other">=</span> <span class="Keyword">enum</span>
|
||||
<dt><pre><a href="#A"><span class="Identifier">A</span></a> {.<span class="Identifier">inject</span>.} <span class="Other">=</span> <span class="Keyword">enum</span>
|
||||
<span class="Identifier">aA</span></pre></dt>
|
||||
<dd>
|
||||
|
||||
@@ -390,7 +390,7 @@
|
||||
</dd>
|
||||
</div>
|
||||
<div id="AnotherObject">
|
||||
<dt><pre><a href="testproject.html#AnotherObject"><span class="Identifier">AnotherObject</span></a> <span class="Other">=</span> <span class="Keyword">object</span>
|
||||
<dt><pre><a href="#AnotherObject"><span class="Identifier">AnotherObject</span></a> <span class="Other">=</span> <span class="Keyword">object</span>
|
||||
<span class="Keyword">case</span> <span class="Identifier">x</span><span class="Operator">*</span><span class="Other">:</span> <span class="Identifier">bool</span>
|
||||
<span class="Keyword">of</span> <span class="Identifier">true</span><span class="Other">:</span>
|
||||
<span class="Identifier">y</span><span class="Operator">*</span><span class="Other">:</span> <span class="Keyword">proc</span> <span class="Other">(</span><span class="Identifier">x</span><span class="Other">:</span> <span class="Identifier">string</span><span class="Other">)</span>
|
||||
@@ -402,7 +402,7 @@
|
||||
</dd>
|
||||
</div>
|
||||
<div id="B">
|
||||
<dt><pre><a href="testproject.html#B"><span class="Identifier">B</span></a> {.<span class="Identifier">inject</span>.} <span class="Other">=</span> <span class="Keyword">enum</span>
|
||||
<dt><pre><a href="#B"><span class="Identifier">B</span></a> {.<span class="Identifier">inject</span>.} <span class="Other">=</span> <span class="Keyword">enum</span>
|
||||
<span class="Identifier">bB</span></pre></dt>
|
||||
<dd>
|
||||
|
||||
@@ -411,7 +411,7 @@
|
||||
</dd>
|
||||
</div>
|
||||
<div id="Foo">
|
||||
<dt><pre><a href="testproject.html#Foo"><span class="Identifier">Foo</span></a> <span class="Other">=</span> <span class="Keyword">enum</span>
|
||||
<dt><pre><a href="#Foo"><span class="Identifier">Foo</span></a> <span class="Other">=</span> <span class="Keyword">enum</span>
|
||||
<span class="Identifier">enumValueA2</span></pre></dt>
|
||||
<dd>
|
||||
|
||||
@@ -420,7 +420,7 @@
|
||||
</dd>
|
||||
</div>
|
||||
<div id="FooBuzz">
|
||||
<dt><pre><a href="testproject.html#FooBuzz"><span class="Identifier">FooBuzz</span></a> {.<span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">deprecated</span><span class="Other">:</span> <span class="StringLit">"FooBuzz msg"</span></span>.} <span class="Other">=</span> <span class="Identifier">int</span></pre></dt>
|
||||
<dt><pre><a href="#FooBuzz"><span class="Identifier">FooBuzz</span></a> {.<span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">deprecated</span><span class="Other">:</span> <span class="StringLit">"FooBuzz msg"</span></span>.} <span class="Other">=</span> <span class="Identifier">int</span></pre></dt>
|
||||
<dd>
|
||||
<div class="deprecation-message">
|
||||
<b>Deprecated:</b> FooBuzz msg
|
||||
@@ -431,7 +431,7 @@
|
||||
</dd>
|
||||
</div>
|
||||
<div id="MyObject">
|
||||
<dt><pre><a href="testproject.html#MyObject"><span class="Identifier">MyObject</span></a> <span class="Other">=</span> <span class="Keyword">object</span>
|
||||
<dt><pre><a href="#MyObject"><span class="Identifier">MyObject</span></a> <span class="Other">=</span> <span class="Keyword">object</span>
|
||||
<span class="Identifier">someString</span><span class="Operator">*</span><span class="Other">:</span> <span class="Identifier">string</span> <span class="Comment">## This is a string</span>
|
||||
<span class="Identifier">annotated</span><span class="Operator">*</span> {.<span class="Identifier">somePragma</span>.}<span class="Other">:</span> <span class="Identifier">string</span> <span class="Comment">## This is an annotated string</span></pre></dt>
|
||||
<dd>
|
||||
@@ -441,7 +441,7 @@
|
||||
</dd>
|
||||
</div>
|
||||
<div id="Shapes">
|
||||
<dt><pre><a href="testproject.html#Shapes"><span class="Identifier">Shapes</span></a> <span class="Other">=</span> <span class="Keyword">enum</span>
|
||||
<dt><pre><a href="#Shapes"><span class="Identifier">Shapes</span></a> <span class="Other">=</span> <span class="Keyword">enum</span>
|
||||
<span class="Identifier">Circle</span><span class="Other">,</span> <span class="Comment">## A circle</span>
|
||||
<span class="Identifier">Triangle</span><span class="Other">,</span> <span class="Comment">## A three-sided shape</span>
|
||||
<span class="Identifier">Rectangle</span> <span class="Comment">## A four-sided shape</span></pre></dt>
|
||||
@@ -452,7 +452,7 @@
|
||||
</dd>
|
||||
</div>
|
||||
<div id="T19396">
|
||||
<dt><pre><a href="testproject.html#T19396"><span class="Identifier">T19396</span></a> <span class="Other">=</span> <span class="Keyword">object</span>
|
||||
<dt><pre><a href="#T19396"><span class="Identifier">T19396</span></a> <span class="Other">=</span> <span class="Keyword">object</span>
|
||||
<span class="Identifier">a</span><span class="Operator">*</span><span class="Other">:</span> <span class="Identifier">int</span></pre></dt>
|
||||
<dd>
|
||||
|
||||
@@ -461,7 +461,7 @@
|
||||
</dd>
|
||||
</div>
|
||||
<div id="Xxx">
|
||||
<dt><pre><a href="testproject.html#Xxx"><span class="Identifier">Xxx</span></a> <span class="Other">=</span> <span class="Keyword">object</span>
|
||||
<dt><pre><a href="#Xxx"><span class="Identifier">Xxx</span></a> <span class="Other">=</span> <span class="Keyword">object</span>
|
||||
<span class="Identifier">field</span><span class="Operator">*</span><span class="Other">:</span> <span class="Identifier">int</span>
|
||||
<span class="Identifier">field3</span><span class="Operator">*</span><span class="Other">:</span> <span class="Identifier">int</span> <span class="Comment">## Doc comment2</span></pre></dt>
|
||||
<dd>
|
||||
@@ -477,7 +477,7 @@
|
||||
<h1><a class="toc-backref" href="#8">Vars</a></h1>
|
||||
<dl class="item">
|
||||
<div id="aVariable">
|
||||
<dt><pre><a href="testproject.html#aVariable"><span class="Identifier">aVariable</span></a><span class="Other">:</span> <span class="Identifier">array</span><span class="Other">[</span><span class="DecNumber">1</span><span class="Other">,</span> <span class="Identifier">int</span><span class="Other">]</span></pre></dt>
|
||||
<dt><pre><a href="#aVariable"><span class="Identifier">aVariable</span></a><span class="Other">:</span> <span class="Identifier">array</span><span class="Other">[</span><span class="DecNumber">1</span><span class="Other">,</span> <span class="Identifier">int</span><span class="Other">]</span></pre></dt>
|
||||
<dd>
|
||||
|
||||
|
||||
@@ -485,7 +485,7 @@
|
||||
</dd>
|
||||
</div>
|
||||
<div id="someVariable">
|
||||
<dt><pre><a href="testproject.html#someVariable"><span class="Identifier">someVariable</span></a><span class="Other">:</span> <span class="Identifier">bool</span></pre></dt>
|
||||
<dt><pre><a href="#someVariable"><span class="Identifier">someVariable</span></a><span class="Other">:</span> <span class="Identifier">bool</span></pre></dt>
|
||||
<dd>
|
||||
|
||||
This should be visible.
|
||||
@@ -499,7 +499,7 @@
|
||||
<h1><a class="toc-backref" href="#10">Consts</a></h1>
|
||||
<dl class="item">
|
||||
<div id="C_A">
|
||||
<dt><pre><a href="testproject.html#C_A"><span class="Identifier">C_A</span></a> <span class="Other">=</span> <span class="FloatNumber">0x7FF0000000000000'f64</span></pre></dt>
|
||||
<dt><pre><a href="#C_A"><span class="Identifier">C_A</span></a> <span class="Other">=</span> <span class="FloatNumber">0x7FF0000000000000'f64</span></pre></dt>
|
||||
<dd>
|
||||
|
||||
|
||||
@@ -507,7 +507,7 @@
|
||||
</dd>
|
||||
</div>
|
||||
<div id="C_B">
|
||||
<dt><pre><a href="testproject.html#C_B"><span class="Identifier">C_B</span></a> <span class="Other">=</span> <span class="DecNumber">0o377'i8</span></pre></dt>
|
||||
<dt><pre><a href="#C_B"><span class="Identifier">C_B</span></a> <span class="Other">=</span> <span class="DecNumber">0o377'i8</span></pre></dt>
|
||||
<dd>
|
||||
|
||||
|
||||
@@ -515,7 +515,7 @@
|
||||
</dd>
|
||||
</div>
|
||||
<div id="C_C">
|
||||
<dt><pre><a href="testproject.html#C_C"><span class="Identifier">C_C</span></a> <span class="Other">=</span> <span class="DecNumber">0o277'i8</span></pre></dt>
|
||||
<dt><pre><a href="#C_C"><span class="Identifier">C_C</span></a> <span class="Other">=</span> <span class="DecNumber">0o277'i8</span></pre></dt>
|
||||
<dd>
|
||||
|
||||
|
||||
@@ -523,7 +523,7 @@
|
||||
</dd>
|
||||
</div>
|
||||
<div id="C_D">
|
||||
<dt><pre><a href="testproject.html#C_D"><span class="Identifier">C_D</span></a> <span class="Other">=</span> <span class="DecNumber">0o177777'i16</span></pre></dt>
|
||||
<dt><pre><a href="#C_D"><span class="Identifier">C_D</span></a> <span class="Other">=</span> <span class="DecNumber">0o177777'i16</span></pre></dt>
|
||||
<dd>
|
||||
|
||||
|
||||
@@ -611,7 +611,7 @@
|
||||
</div>
|
||||
<div id="bar-procs-all">
|
||||
<div id="bar">
|
||||
<dt><pre><span class="Keyword">proc</span> <a href="#bar"><span class="Identifier">bar</span></a><span class="Other">(</span><span class="Identifier">f</span><span class="Other">:</span> <a href="testproject.html#FooBuzz"><span class="Identifier">FooBuzz</span></a><span class="Other">)</span> {.<span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
|
||||
<dt><pre><span class="Keyword">proc</span> <a href="#bar"><span class="Identifier">bar</span></a><span class="Other">(</span><span class="Identifier">f</span><span class="Other">:</span> <a href="#FooBuzz"><span class="Identifier">FooBuzz</span></a><span class="Other">)</span> {.<span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
|
||||
<dd>
|
||||
|
||||
|
||||
@@ -835,7 +835,7 @@ at indent 0
|
||||
</div>
|
||||
<div id="z1-procs-all">
|
||||
<div id="z1">
|
||||
<dt><pre><span class="Keyword">proc</span> <a href="#z1"><span class="Identifier">z1</span></a><span class="Other">(</span><span class="Other">)</span><span class="Other">:</span> <a href="testproject.html#Foo"><span class="Identifier">Foo</span></a> {.<span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
|
||||
<dt><pre><span class="Keyword">proc</span> <a href="#z1"><span class="Identifier">z1</span></a><span class="Other">(</span><span class="Other">)</span><span class="Other">:</span> <a href="#Foo"><span class="Identifier">Foo</span></a> {.<span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
|
||||
<dd>
|
||||
|
||||
cz1
|
||||
|
||||
@@ -1018,7 +1018,7 @@ proc outlineNode(graph: ModuleGraph, n: PNode, endInfo: TLineInfo, infoPairs: Su
|
||||
if n.kind == nkSym and n.sym.checkSymbol(n.info):
|
||||
graph.suggestResult(n.sym, n.sym.info, ideOutline, endInfo.line, endInfo.col)
|
||||
return true
|
||||
elif n.kind == nkIdent:
|
||||
elif n.kind in {nkIdent, nkAccQuoted}:
|
||||
let symData = findByTLineInfo(n.info, infoPairs)
|
||||
if symData != nil and symData.sym.checkSymbol(symData.info):
|
||||
let sym = symData.sym
|
||||
@@ -1028,7 +1028,7 @@ proc outlineNode(graph: ModuleGraph, n: PNode, endInfo: TLineInfo, infoPairs: Su
|
||||
proc handleIdentOrSym(graph: ModuleGraph, n: PNode, endInfo: TLineInfo, infoPairs: SuggestFileSymbolDatabase): bool =
|
||||
result = false
|
||||
for child in n:
|
||||
if child.kind in {nkIdent, nkSym}:
|
||||
if child.kind in {nkIdent, nkAccQuoted, nkSym}:
|
||||
if graph.outlineNode(child, endInfo, infoPairs):
|
||||
return true
|
||||
elif child.kind == nkPostfix:
|
||||
|
||||
@@ -168,4 +168,3 @@ for q in 0..100:
|
||||
new topArr[i]
|
||||
topArr[i].m.di.b = q
|
||||
doAssert(cast[uint](addr topArr[i].m.di) mod uint(alignof(DeepInner)) == 0)
|
||||
|
||||
|
||||
10
tests/align/talign2.nim
Normal file
10
tests/align/talign2.nim
Normal file
@@ -0,0 +1,10 @@
|
||||
discard """
|
||||
matrix: "--mm:refc -d:useGcAssert -d:useSysAssert; --mm:orc"
|
||||
"""
|
||||
|
||||
block:
|
||||
type U = object
|
||||
d {.align: 16.}: int8
|
||||
var e: seq[ref U]
|
||||
for i in 0 ..< 10000: e.add(new U)
|
||||
doAssert getTotalMem() <= 1052672 * 2
|
||||
@@ -365,3 +365,28 @@ proc do2(x: int, e: ItemExt): seq[(string, ItemExt)] =
|
||||
do1(x).map(proc(v: (string, Item)): auto = (v[0], ItemExt(a: v[1], b: e.b)))
|
||||
|
||||
doAssert $do2(0, ItemExt(a: Item(kind: 1, c: "second"), b: "third")) == """@[("zero", (a: (kind: 1, c: "first"), b: "third"))]"""
|
||||
|
||||
block:
|
||||
type RangeCrash = object
|
||||
case x: range[0..7]
|
||||
of 0..2:
|
||||
a: string
|
||||
else:
|
||||
b: string
|
||||
|
||||
var rangeCrash = RangeCrash()
|
||||
{.cast(uncheckedAssign).}:
|
||||
rangeCrash.x = 5
|
||||
|
||||
block:
|
||||
type Discrim = distinct uint8
|
||||
type DistinctCrash = object
|
||||
case x: Discrim
|
||||
of Discrim(0)..Discrim(2):
|
||||
a: string
|
||||
else:
|
||||
b: string
|
||||
|
||||
var distinctCrash = DistinctCrash()
|
||||
{.cast(uncheckedAssign).}:
|
||||
distinctCrash.x = Discrim(5)
|
||||
|
||||
@@ -35,4 +35,5 @@ proc main() =
|
||||
|
||||
main()
|
||||
GC_fullCollect()
|
||||
echo getOccupiedMem() < 10 * 1024 * 1024, " peak memory: ", getMaxMem() < 10 * 1024 * 1024
|
||||
when not defined(useMalloc):
|
||||
echo getOccupiedMem() < 10 * 1024 * 1024, " peak memory: ", getMaxMem() < 10 * 1024 * 1024
|
||||
|
||||
@@ -605,3 +605,17 @@ block t18643:
|
||||
except IndexDefect:
|
||||
caught = true
|
||||
doAssert caught, "IndexDefect not caught!"
|
||||
|
||||
|
||||
# bug #25475
|
||||
block:
|
||||
type N = object
|
||||
b: seq[array[1'u, int]]
|
||||
doAssert N(b: @[[0]]) == N(b: @[[0]])
|
||||
|
||||
block:
|
||||
var x: array[5..6, int] = [0, 1]
|
||||
var y: array[1..2, int] = [0, 1]
|
||||
|
||||
doAssert x == y # compiles
|
||||
doAssert @[x] == @[y]
|
||||
|
||||
@@ -56,3 +56,22 @@ proc main = # bug #24677
|
||||
for NDEBUG in 0..2:
|
||||
doAssert NDEBUG == NDEBUG
|
||||
main()
|
||||
|
||||
block: # importc type inheritance
|
||||
type
|
||||
A {.inheritable, pure, bycopy, importc: "int".} = object
|
||||
B {.importc: "int", bycopy.} = object of A
|
||||
|
||||
{.emit: """
|
||||
int foo(int a) {
|
||||
return 123;
|
||||
}
|
||||
""".}
|
||||
|
||||
proc foo(a: A): B {.importc, nodecl.}
|
||||
|
||||
var a: A
|
||||
var b = foo(a)
|
||||
doAssert(cast[cint](b) == 123)
|
||||
var c = foo(b)
|
||||
doAssert(cast[cint](c) == 123)
|
||||
|
||||
@@ -1,35 +1,7 @@
|
||||
discard """
|
||||
targets: "c"
|
||||
matrix: "--debugger:native --mangle:nim"
|
||||
ccodecheck: "'testFunc__titaniummangle95nim_u1316'"
|
||||
ccodecheck: "'testFunc__titaniummangle95nim_u156'"
|
||||
ccodecheck: "'testFunc__titaniummangle95nim_u1305'"
|
||||
ccodecheck: "'testFunc__titaniummangle95nim_u241'"
|
||||
ccodecheck: "'testFunc__titaniummangle95nim_u1357'"
|
||||
ccodecheck: "'testFunc__titaniummangle95nim_u292'"
|
||||
ccodecheck: "'testFunc__titaniummangle95nim_u38'"
|
||||
ccodecheck: "'testFunc__titaniummangle95nim_u175'"
|
||||
ccodecheck: "'testFunc__titaniummangle95nim_u1302'"
|
||||
ccodecheck: "'testFunc__titaniummangle95nim_u1305'"
|
||||
ccodecheck: "'testFunc__titaniummangle95nim_u535'"
|
||||
ccodecheck: "'testFunc__titaniummangle95nim_u1294'"
|
||||
ccodecheck: "'testFunc__titaniummangle95nim_u336'"
|
||||
ccodecheck: "'testFunc__titaniummangle95nim_u425'"
|
||||
ccodecheck: "'testFunc__titaniummangle95nim_u308'"
|
||||
ccodecheck: "'testFunc__titaniummangle95nim_u129'"
|
||||
ccodecheck: "'testFunc__titaniummangle95nim_u320'"
|
||||
ccodecheck: "'testFunc__titaniummangle95nim_u223'"
|
||||
ccodecheck: "'testFunc__titaniummangle95nim_u545'"
|
||||
ccodecheck: "'testFunc__titaniummangle95nim_u543'"
|
||||
ccodecheck: "'testFunc__titaniummangle95nim_u895'"
|
||||
ccodecheck: "'testFunc__titaniummangle95nim_u1104'"
|
||||
ccodecheck: "'testFunc__titaniummangle95nim_u1155'"
|
||||
ccodecheck: "'testFunc__titaniummangle95nim_u636'"
|
||||
ccodecheck: "'testFunc__titaniummangle95nim_u705'"
|
||||
ccodecheck: "'testFunc__titaniummangle95nim_u800'"
|
||||
ccodecheck: "'new__titaniummangle95nim_u1320'"
|
||||
ccodecheck: "'xxx__titaniummangle95nim_u1391'"
|
||||
ccodecheck: "'xxx__titaniummangle95nim_u1394'"
|
||||
ccodecheck: "'testFunc__titaniummangle95nim_u'"
|
||||
"""
|
||||
|
||||
#When debugging this notice that if one check fails, it can be due to any of the above.
|
||||
@@ -48,7 +20,7 @@ type
|
||||
|
||||
Container[T] = object
|
||||
data: T
|
||||
|
||||
|
||||
Container2[T, T2] = object
|
||||
data: T
|
||||
data2: T2
|
||||
@@ -57,7 +29,7 @@ type
|
||||
|
||||
Coo = Foo
|
||||
|
||||
Doo = Boo | Foo
|
||||
Doo = Boo | Foo
|
||||
|
||||
TestProc = proc(a:string): string
|
||||
|
||||
@@ -67,87 +39,87 @@ type EnumSample = enum
|
||||
type EnumAnotherSample = enum
|
||||
a, b, c
|
||||
|
||||
proc testFunc(a: set[EnumSample]) =
|
||||
proc testFunc(a: set[EnumSample]) =
|
||||
echo $a
|
||||
|
||||
proc testFunc(a: typedesc) =
|
||||
proc testFunc(a: typedesc) =
|
||||
echo $a
|
||||
|
||||
proc testFunc(a: ptr Foo) =
|
||||
proc testFunc(a: ptr Foo) =
|
||||
echo repr a
|
||||
|
||||
proc testFunc(s: string, a: Coo) =
|
||||
proc testFunc(s: string, a: Coo) =
|
||||
echo repr a
|
||||
|
||||
proc testFunc(s: int, a: Comparable) =
|
||||
proc testFunc(s: int, a: Comparable) =
|
||||
echo repr a
|
||||
|
||||
proc testFunc(a: TestProc) =
|
||||
proc testFunc(a: TestProc) =
|
||||
let b = ""
|
||||
echo repr a("")
|
||||
|
||||
proc testFunc(a: ref Foo) =
|
||||
proc testFunc(a: ref Foo) =
|
||||
echo repr a
|
||||
|
||||
proc testFunc(b: Boo) =
|
||||
proc testFunc(b: Boo) =
|
||||
echo repr b
|
||||
|
||||
proc testFunc(a: ptr UncheckedArray[int]) =
|
||||
proc testFunc(a: ptr UncheckedArray[int]) =
|
||||
echo repr a
|
||||
|
||||
proc testFunc(a: ptr int) =
|
||||
proc testFunc(a: ptr int) =
|
||||
echo repr a
|
||||
|
||||
proc testFunc(a: ptr ptr int) =
|
||||
proc testFunc(a: ptr ptr int) =
|
||||
echo repr a
|
||||
|
||||
proc testFunc(e: FooTuple, str: cstring) =
|
||||
proc testFunc(e: FooTuple, str: cstring) =
|
||||
echo e
|
||||
|
||||
proc testFunc(e: (float, float)) =
|
||||
proc testFunc(e: (float, float)) =
|
||||
echo e
|
||||
|
||||
proc testFunc(e: EnumSample) =
|
||||
proc testFunc(e: EnumSample) =
|
||||
echo e
|
||||
|
||||
proc testFunc(e: var int) =
|
||||
proc testFunc(e: var int) =
|
||||
echo e
|
||||
|
||||
proc testFunc(e: var Foo, a, b: int32, refFoo: ref Foo) =
|
||||
proc testFunc(e: var Foo, a, b: int32, refFoo: ref Foo) =
|
||||
echo e
|
||||
|
||||
proc testFunc(xs: Container[int]) =
|
||||
proc testFunc(xs: Container[int]) =
|
||||
let a = 2
|
||||
echo xs
|
||||
|
||||
proc testFunc(xs: Container2[int32, int32]) =
|
||||
proc testFunc(xs: Container2[int32, int32]) =
|
||||
let a = 2
|
||||
echo xs
|
||||
|
||||
proc testFunc(xs: Container[Container2[int32, int32]]) =
|
||||
proc testFunc(xs: Container[Container2[int32, int32]]) =
|
||||
let a = 2
|
||||
echo xs
|
||||
|
||||
proc testFunc(xs: seq[int]) =
|
||||
proc testFunc(xs: seq[int]) =
|
||||
let a = 2
|
||||
echo xs
|
||||
|
||||
proc testFunc(xs: openArray[string]) =
|
||||
proc testFunc(xs: openArray[string]) =
|
||||
let a = 2
|
||||
echo xs
|
||||
|
||||
proc testFunc(xs: array[2, int]) =
|
||||
proc testFunc(xs: array[2, int]) =
|
||||
let a = 2
|
||||
echo xs
|
||||
|
||||
proc testFunc(e: EnumAnotherSample) =
|
||||
proc testFunc(e: EnumAnotherSample) =
|
||||
echo e
|
||||
|
||||
proc testFunc(a, b: int) =
|
||||
proc testFunc(a, b: int) =
|
||||
echo "hola"
|
||||
discard
|
||||
|
||||
proc testFunc(a: int, xs: varargs[string]) =
|
||||
proc testFunc(a: int, xs: varargs[string]) =
|
||||
let a = 10
|
||||
for x in xs:
|
||||
echo x
|
||||
@@ -155,7 +127,7 @@ proc testFunc(a: int, xs: varargs[string]) =
|
||||
proc xxx(v: static int) =
|
||||
echo v
|
||||
|
||||
proc testFunc() =
|
||||
proc testFunc() =
|
||||
var a = 2
|
||||
var aPtr = a.addr
|
||||
var foo = Foo()
|
||||
|
||||
@@ -40,7 +40,7 @@ proc `=destroy`*[T](x: var myseq[T]) =
|
||||
x.len = 0
|
||||
x.cap = 0
|
||||
|
||||
proc `=`*[T](a: var myseq[T]; b: myseq[T]) =
|
||||
proc `=copy`*[T](a: var myseq[T]; b: myseq[T]) =
|
||||
if a.data == b.data: return
|
||||
if a.data != nil:
|
||||
`=destroy`(a)
|
||||
@@ -66,6 +66,11 @@ proc `=sink`*[T](a: var myseq[T]; b: myseq[T]) =
|
||||
a.cap = b.cap
|
||||
a.data = b.data
|
||||
|
||||
proc `=wasMoved`*[T](a: var myseq[T]) =
|
||||
a.data = nil
|
||||
a.len = 0
|
||||
a.cap = 0
|
||||
|
||||
proc resize[T](s: var myseq[T]) =
|
||||
if s.cap == 0: s.cap = 8
|
||||
else: s.cap = (s.cap * 3) shr 1
|
||||
@@ -118,7 +123,7 @@ template `[]=`*[T](x: myseq[T]; i: Natural; y: T) =
|
||||
proc createSeq*[T](elems: varargs[T]): myseq[T] =
|
||||
result.cap = elems.len
|
||||
result.len = elems.len
|
||||
result.data = cast[type(result.data)](alloc(result.cap * sizeof(T)))
|
||||
result.data = cast[type(result.data)](alloc0(result.cap * sizeof(T)))
|
||||
inc allocCount
|
||||
when supportsCopyMem(T):
|
||||
copyMem(result.data, addr(elems[0]), result.cap * sizeof(T))
|
||||
|
||||
@@ -902,3 +902,15 @@ block: # issue #25494
|
||||
a, b, c
|
||||
|
||||
foo[MyEnum]()
|
||||
|
||||
block: # issue #25005
|
||||
type
|
||||
RpcResponse[T] = ref object
|
||||
result: T
|
||||
|
||||
func testit[T](p: var ref T) =
|
||||
p = new(T)
|
||||
|
||||
var v: RpcResponse[string]
|
||||
testit(v)
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ cmd: "nim check $options --hints:off $file"
|
||||
action: "reject"
|
||||
nimout:'''
|
||||
tpointerprocs.nim(22, 11) Error: 'foo' doesn't have a concrete type, due to unspecified generic parameters.
|
||||
tpointerprocs.nim(34, 14) Error: type mismatch: got <int>
|
||||
tpointerprocs.nim(34, 14) Error: type mismatch: got <typedesc[int]>
|
||||
but expected one of:
|
||||
proc foo(x: int | float; y: int or string): float
|
||||
first type mismatch at position: 2 in generic parameters
|
||||
|
||||
48
tests/generics/tself_type.nim
Normal file
48
tests/generics/tself_type.nim
Normal file
@@ -0,0 +1,48 @@
|
||||
# issue 16754
|
||||
|
||||
type
|
||||
Opt[T] = object
|
||||
when T is ref:
|
||||
val: T
|
||||
x: int
|
||||
else:
|
||||
val: T
|
||||
x: string
|
||||
|
||||
type
|
||||
Foo = ref object
|
||||
x: Opt[Foo]
|
||||
|
||||
Bar = object
|
||||
x: ref Opt[Bar]
|
||||
|
||||
var f = Foo()
|
||||
assert f.x.x is int
|
||||
var b = Bar()
|
||||
assert b.x.x is string
|
||||
|
||||
type
|
||||
BazG[T] = object
|
||||
x: int
|
||||
|
||||
BazGRef[T] = ref object
|
||||
x: T
|
||||
|
||||
Baz = object
|
||||
x: Opt[BazG[Baz]]
|
||||
y: Opt[BazGRef[Baz]]
|
||||
|
||||
var z = Baz()
|
||||
assert z.x.x is string
|
||||
assert z.y.x is int
|
||||
|
||||
import options
|
||||
|
||||
type
|
||||
Person = ref object
|
||||
parent: Option[Person]
|
||||
|
||||
proc newPerson(parent: Option[Person]): Person =
|
||||
Person(parent: parent)
|
||||
|
||||
var person = newPerson(none(Person))
|
||||
12
tests/iter/tclosureiter_objupconv_methodawait.nim
Normal file
12
tests/iter/tclosureiter_objupconv_methodawait.nim
Normal file
@@ -0,0 +1,12 @@
|
||||
discard """
|
||||
cmd: "nim c $file"
|
||||
"""
|
||||
|
||||
import std/asyncdispatch
|
||||
|
||||
type
|
||||
A {.inheritable.} = ref object
|
||||
B = ref object of A
|
||||
|
||||
method a(x: A): Future[A] {.async, base.} =
|
||||
B(await a(B()))
|
||||
@@ -559,17 +559,21 @@ block: # void iterator
|
||||
discard
|
||||
var a = it
|
||||
|
||||
block: # Locals present in only 1 state should be on the stack
|
||||
block:
|
||||
# Locals present in only 1 state should be on the stack
|
||||
proc checkOnStack(a: pointer, shouldBeOnStack: bool) =
|
||||
# Quick and dirty way to check if a points to stack
|
||||
var dummy = 0
|
||||
let dummyAddr = addr dummy
|
||||
let distance = abs(cast[int](dummyAddr) - cast[int](a))
|
||||
const requiredDistance = 300
|
||||
if shouldBeOnStack:
|
||||
doAssert(distance <= requiredDistance, "a is not on stack, but should")
|
||||
else:
|
||||
doAssert(distance > requiredDistance, "a is on stack, but should not")
|
||||
# bug #25596: the very fact we take the address prevents the local
|
||||
# from being on the stack
|
||||
when false:
|
||||
# Quick and dirty way to check if a points to stack
|
||||
var dummy = 0
|
||||
let dummyAddr = addr dummy
|
||||
let distance = abs(cast[int](dummyAddr) - cast[int](a))
|
||||
const requiredDistance = 300
|
||||
if shouldBeOnStack:
|
||||
doAssert(distance <= requiredDistance, "a is not on stack, but should")
|
||||
else:
|
||||
doAssert(distance > requiredDistance, "a is on stack, but should not")
|
||||
|
||||
iterator it(): int {.closure.} =
|
||||
var a = 1
|
||||
|
||||
39
tests/macros/tgettypeimpl_defaults.nim
Normal file
39
tests/macros/tgettypeimpl_defaults.nim
Normal file
@@ -0,0 +1,39 @@
|
||||
discard """
|
||||
nimout: '''
|
||||
ObjectTy
|
||||
Empty
|
||||
Empty
|
||||
RecList
|
||||
IdentDefs
|
||||
Sym "noDefault"
|
||||
Sym "int"
|
||||
Empty
|
||||
IdentDefs
|
||||
Sym "withDefault"
|
||||
Sym "string"
|
||||
StrLit "Hello World"
|
||||
ProcTy
|
||||
FormalParams
|
||||
Sym "bool"
|
||||
IdentDefs
|
||||
Sym "foo"
|
||||
Sym "string"
|
||||
StrLit "Proc default"
|
||||
Empty
|
||||
'''
|
||||
"""
|
||||
|
||||
import std/macros
|
||||
|
||||
type
|
||||
FooBar = object
|
||||
noDefault: int
|
||||
withDefault = "Hello World"
|
||||
|
||||
SomeProc = proc (foo = "Proc default"): bool
|
||||
|
||||
macro dumpBodies() =
|
||||
echo bindSym("FooBar").getTypeImpl().treeRepr
|
||||
echo bindSym("SomeProc").getTypeImpl().treeRepr
|
||||
|
||||
dumpBodies()
|
||||
508
tests/misc/tparseoptmodes.nim
Normal file
508
tests/misc/tparseoptmodes.nim
Normal file
@@ -0,0 +1,508 @@
|
||||
discard """
|
||||
action: run
|
||||
"""
|
||||
|
||||
import parseopt
|
||||
from std/sequtils import toSeq
|
||||
|
||||
type Opt = tuple[kind: CmdLineKind, key, val: string]
|
||||
proc `$`(opt: Opt): string = "(" & $opt[0] & ", \"" & opt[1] & "\", \"" & opt[2] & "\")"
|
||||
|
||||
proc collect(args: seq[string] | string;
|
||||
shortNoVal: set[char] = {};
|
||||
longNoVal: seq[string] = @[]): seq[(CliMode, seq[Opt])] =
|
||||
for mode in CliMode:
|
||||
var p = parseopt.initOptParser(args,
|
||||
shortNoVal = shortNoVal, longNoVal = longNoVal, mode = mode)
|
||||
let res = toSeq(parseopt.getopt(p))
|
||||
result.add (mode, res)
|
||||
|
||||
proc check(name: string;
|
||||
results: openArray[(CliMode, seq[Opt])];
|
||||
expected: proc(m: CliMode): seq[Opt]) =
|
||||
for (mode, res) in results:
|
||||
doAssert res == expected(mode), "[" & $mode & "]: " & name & ":\n" & $res
|
||||
|
||||
block:
|
||||
# pcShortValAllowNextArg: separate option-argument for mandatory opt-arg.
|
||||
let res = collect(@["-c", "4"], shortNoVal = {'a', 'b'})
|
||||
proc expected(m: CliMode): seq[Opt] =
|
||||
case m
|
||||
of LaxMode: @[(cmdShortOption, "c", "4")]
|
||||
of NimMode: @[(cmdShortOption, "c", ""), (cmdArgument, "4", "")]
|
||||
of GnuMode: @[(cmdShortOption, "c", "4")]
|
||||
check("short whitespace value", res, expected)
|
||||
|
||||
block:
|
||||
# No opt-arg knowledge: whitespace does not bind to short option.
|
||||
let res = collect(@["-c", "4"])
|
||||
proc expected(m: CliMode): seq[Opt] =
|
||||
@[(cmdShortOption, "c", ""), (cmdArgument, "4", "")]
|
||||
check("short no-val whitespace value", res, expected)
|
||||
|
||||
block:
|
||||
# pcShortBundle + pcShortValAllowNextArg: grouped shorts with one opt-arg.
|
||||
let res = collect(@["-abc", "4"], shortNoVal = {'a', 'b'})
|
||||
proc expected(m: CliMode): seq[Opt] =
|
||||
case m
|
||||
of LaxMode: @[(cmdShortOption, "a", ""),
|
||||
(cmdShortOption, "b", ""),
|
||||
(cmdShortOption, "c", "4")]
|
||||
|
||||
of NimMode: @[(cmdShortOption, "a", ""),
|
||||
(cmdShortOption, "b", ""),
|
||||
(cmdShortOption, "c", ""),
|
||||
(cmdArgument, "4", "")]
|
||||
|
||||
of GnuMode: @[(cmdShortOption, "a", ""),
|
||||
(cmdShortOption, "b", ""),
|
||||
(cmdShortOption, "c", "4")]
|
||||
check("short bundle with trailing value", res, expected)
|
||||
|
||||
block:
|
||||
# pcShortValAllowAdjacent: option+argument in same token (dash-led value).
|
||||
let res = collect(@["-c-x"], shortNoVal = {'a', 'b'})
|
||||
proc expected(m: CliMode): seq[Opt] =
|
||||
@[(cmdShortOption, "c", "-x")]
|
||||
check("short adjacent dash-led", res, expected)
|
||||
|
||||
block:
|
||||
# pcShortBundle + pcShortValAllowAdjacent (dash-led value).
|
||||
let res = collect(@["-abc-10"], shortNoVal = {'a', 'b'})
|
||||
proc expected(m: CliMode): seq[Opt] =
|
||||
@[(cmdShortOption, "a", ""),
|
||||
(cmdShortOption, "b", ""),
|
||||
(cmdShortOption, "c", "-10")]
|
||||
check("short bundle with adjacent negative", res, expected)
|
||||
|
||||
block:
|
||||
# pcShortValAllowNextArg: option and option-argument can be separate args.
|
||||
let res = collect(@["-c", ":"], shortNoVal = {'a', 'b'})
|
||||
proc expected(m: CliMode): seq[Opt] =
|
||||
case m
|
||||
of LaxMode: @[(cmdShortOption, "c", ":")]
|
||||
of NimMode: @[(cmdShortOption, "c", ""), (cmdArgument, ":", "")]
|
||||
of GnuMode: @[(cmdShortOption, "c", ":")]
|
||||
check("short whitespace colon value", res, expected)
|
||||
|
||||
block:
|
||||
# pcShortValAllowAdjacent: combined option+argument without blanks.
|
||||
let res = collect(@["-abc4"], shortNoVal = {'a', 'b'})
|
||||
proc expected(m: CliMode): seq[Opt] =
|
||||
@[(cmdShortOption, "a", ""),
|
||||
(cmdShortOption, "b", ""),
|
||||
(cmdShortOption, "c", "4")]
|
||||
check("short bundle adjacent value", res, expected)
|
||||
|
||||
block:
|
||||
# pcShortBundle: bundle of no-arg shorts should split into options.
|
||||
let res = collect(@["-ab"], shortNoVal = {'a', 'b'})
|
||||
proc expected(m: CliMode): seq[Opt] =
|
||||
@[(cmdShortOption, "a", ""), (cmdShortOption, "b", "")]
|
||||
check("short bundle no-arg", res, expected)
|
||||
|
||||
block:
|
||||
# pcShortBundle + pcShortValAllowNextArg: a no-arg short followed by one with arg.
|
||||
let res = collect(@["-ac", "4"], shortNoVal = {'a', 'b'})
|
||||
proc expected(m: CliMode): seq[Opt] =
|
||||
case m
|
||||
of LaxMode : @[(cmdShortOption, "a", ""),
|
||||
(cmdShortOption, "c", "4")]
|
||||
of NimMode: @[(cmdShortOption, "a", ""),
|
||||
(cmdShortOption, "c", ""),
|
||||
(cmdArgument, "4", "")]
|
||||
of GnuMode: @[(cmdShortOption, "a", ""),
|
||||
(cmdShortOption, "c", "4")]
|
||||
check("short bundle trailing value", res, expected)
|
||||
|
||||
block:
|
||||
# pcShortValAllowNextArg + cmdline parsing: whitespace-separated opt-arg.
|
||||
let res = collect("-c \"foo bar\"", shortNoVal = {'a', 'b'})
|
||||
proc expected(m: CliMode): seq[Opt] =
|
||||
case m
|
||||
of LaxMode: @[(cmdShortOption, "c", "foo bar")]
|
||||
of NimMode: @[(cmdShortOption, "c", ""), (cmdArgument, "foo bar", "")]
|
||||
of GnuMode: @[(cmdShortOption, "c", "foo bar")]
|
||||
check("short whitespace quoted value", res, expected)
|
||||
|
||||
block:
|
||||
# pcShortValAllowNextArg + pcShortValAllowDashLeading: negative numbers as opt-args.
|
||||
let res = collect(@["-n", "-10"], shortNoVal = {'a', 'b', 'c'})
|
||||
proc expected(m: CliMode): seq[Opt] =
|
||||
case m
|
||||
of LaxMode: @[(cmdShortOption, "n", "-10")]
|
||||
of NimMode: @[(cmdShortOption, "n", ""), (cmdShortOption, "1", "0")]
|
||||
of GnuMode: @[(cmdShortOption, "n", "-10")]
|
||||
check("short negative value, shortNoVal used", res, expected)
|
||||
|
||||
block:
|
||||
# pcShortValAllowNextArg + pcShortValAllowDashLeading: negative numbers as opt-args.
|
||||
let res = collect(@["-n", "-10"])
|
||||
proc expected(m: CliMode): seq[Opt] =
|
||||
case m
|
||||
of LaxMode: @[(cmdShortOption, "n", ""),
|
||||
(cmdShortOption, "1", ""),
|
||||
(cmdShortOption, "0", "")]
|
||||
of NimMode: @[(cmdShortOption, "n", ""),
|
||||
(cmdShortOption, "1", ""),
|
||||
(cmdShortOption, "0", "")]
|
||||
of GnuMode: @[(cmdShortOption, "n", ""),
|
||||
(cmdShortOption, "1", ""),
|
||||
(cmdShortOption, "0", "")]
|
||||
check("short negative value, shortNoVal empty", res, expected)
|
||||
|
||||
block:
|
||||
# pcShortValAllowNextArg: repeated option-argument pairs are interpreted in order.
|
||||
let res = collect(@["-c", "1", "-c", "2"], shortNoVal = {'a', 'b'})
|
||||
proc expected(m: CliMode): seq[Opt] =
|
||||
case m
|
||||
of LaxMode: @[(cmdShortOption, "c", "1"),
|
||||
(cmdShortOption, "c", "2")]
|
||||
of NimMode: @[(cmdShortOption, "c", ""),
|
||||
(cmdArgument, "1", ""),
|
||||
(cmdShortOption, "c", ""),
|
||||
(cmdArgument, "2", "")]
|
||||
of GnuMode: @[(cmdShortOption, "c", "1"),
|
||||
(cmdShortOption, "c", "2")]
|
||||
check("short repeat whitespace values", res, expected)
|
||||
|
||||
block:
|
||||
# pcShortValAllowAdjacent: adjacent opt-args preserve order for repeats.
|
||||
let res = collect(@["-c1", "-c2"], shortNoVal = {'a', 'b'})
|
||||
proc expected(m: CliMode): seq[Opt] =
|
||||
@[(cmdShortOption, "c", "1"), (cmdShortOption, "c", "2")]
|
||||
check("short repeat adjacent values", res, expected)
|
||||
|
||||
block:
|
||||
# pcShortValAllowDashLeading: value starting with '-' is consumed as opt-arg.
|
||||
# Divergence from POSIX Guideline 14 when enabled.
|
||||
let res = collect(@["-c", "-a"], shortNoVal = {'b'})
|
||||
proc expected(m: CliMode): seq[Opt] =
|
||||
case m
|
||||
of LaxMode: @[(cmdShortOption, "c", "-a")]
|
||||
of NimMode: @[(cmdShortOption, "c", ""), (cmdShortOption, "a", "")]
|
||||
of GnuMode: @[(cmdShortOption, "c", "-a")]
|
||||
check("short dash-led value", res, expected)
|
||||
|
||||
block:
|
||||
# Separator overrides shortNoVal
|
||||
let res = collect(@["-a=foo"], shortNoVal = {'a'})
|
||||
proc expected(m: CliMode): seq[Opt] =
|
||||
case m
|
||||
of LaxMode: @[(cmdShortOption, "a", "foo")]
|
||||
of NimMode: @[(cmdShortOption, "a", "foo")]
|
||||
of GnuMode: @[(cmdShortOption, "a", "=foo")]
|
||||
check("separator suppresses shortNoVal", res, expected)
|
||||
|
||||
block:
|
||||
let res = collect(@["-a=foo"], shortNoVal = {'v'})
|
||||
proc expected(m: CliMode): seq[Opt] =
|
||||
case m
|
||||
of LaxMode: @[(cmdShortOption, "a", "foo")]
|
||||
of NimMode: @[(cmdShortOption, "a", "foo")]
|
||||
of GnuMode: @[(cmdShortOption, "a", "=foo")]
|
||||
check("adjacent value-taking vs chort option bundling 1", res, expected)
|
||||
|
||||
block:
|
||||
# pcLongAllowSep, mixed long/short parsing.
|
||||
# Option-arguments may include ':'/'=' chars.
|
||||
let args = @[
|
||||
"foo bar",
|
||||
"--path:/i like space/projects",
|
||||
"--aa:bar=a",
|
||||
"--a=c:d",
|
||||
"--ab",
|
||||
"-c",
|
||||
"--a[baz]:doo"
|
||||
]
|
||||
let res = collect(args, shortNoVal = {'c'})
|
||||
proc expected(m: CliMode): seq[Opt] =
|
||||
case m
|
||||
of LaxMode: @[
|
||||
(cmdArgument, "foo bar", ""),
|
||||
(cmdLongOption, "path", "/i like space/projects"),
|
||||
(cmdLongOption, "aa", "bar=a"),
|
||||
(cmdLongOption, "a", "c:d"),
|
||||
(cmdLongOption, "ab", ""),
|
||||
(cmdShortOption, "c", ""),
|
||||
(cmdLongOption, "a[baz]", "doo")]
|
||||
of NimMode: @[
|
||||
(cmdArgument, "foo bar", ""),
|
||||
(cmdLongOption, "path", "/i like space/projects"),
|
||||
(cmdLongOption, "aa", "bar=a"),
|
||||
(cmdLongOption, "a", "c:d"),
|
||||
(cmdLongOption, "ab", ""),
|
||||
(cmdShortOption, "c", ""),
|
||||
(cmdLongOption, "a[baz]", "doo")]
|
||||
of GnuMode: @[
|
||||
(cmdArgument, "foo bar", ""),
|
||||
(cmdLongOption, "path:/i", ""), # longNoVal is empty so can't take arg here
|
||||
(cmdArgument, "like space/projects", ""),
|
||||
(cmdLongOption, "aa:bar", "a"),
|
||||
(cmdLongOption, "a", "c:d"),
|
||||
(cmdLongOption, "ab", ""),
|
||||
(cmdShortOption, "c", ""),
|
||||
(cmdLongOption, "a[baz]:doo", "")]
|
||||
check("mixed long/short argv tokens", res, expected)
|
||||
|
||||
|
||||
block:
|
||||
# pcLongAllowSep + separators: long option separator handling.
|
||||
let res = collect(@["--foo:bar"])
|
||||
proc expected(m: CliMode): seq[Opt] =
|
||||
case m
|
||||
of LaxMode: @[(cmdLongOption, "foo", "bar")]
|
||||
of NimMode: @[(cmdLongOption, "foo", "bar")]
|
||||
of GnuMode: @[(cmdLongOption, "foo:bar", "")]
|
||||
check("long option colon separator", res, expected)
|
||||
|
||||
block:
|
||||
# pcLongAllowSep + separators: long option separator handling.
|
||||
let res = collect(@["--foo= bar"])
|
||||
proc expected(m: CliMode): seq[Opt] =
|
||||
case m
|
||||
of LaxMode: @[(cmdLongOption, "foo", "bar")]
|
||||
of NimMode: @[(cmdLongOption, "foo", "bar")]
|
||||
of GnuMode: @[(cmdLongOption, "foo", " bar")]
|
||||
check("long option whitespace around separators", res, expected)
|
||||
|
||||
block:
|
||||
let res = collect(@["--foo =bar"])
|
||||
proc expected(m: CliMode): seq[Opt] =
|
||||
case m
|
||||
of LaxMode: @[(cmdLongOption, "foo", "bar")]
|
||||
of NimMode: @[(cmdLongOption, "foo", "bar")]
|
||||
of GnuMode: @[(cmdLongOption, "foo", ""), (cmdArgument, "=bar", "")]
|
||||
check("long option whitespace around separators", res, expected)
|
||||
|
||||
block:
|
||||
let res = collect("--foo =bar", longNoVal = @[""])
|
||||
proc expected(m: CliMode): seq[Opt] =
|
||||
@[(cmdLongOption, "foo", "=bar")]
|
||||
check("long option argument delimited with whitespace, val allowed", res, expected)
|
||||
|
||||
block:
|
||||
let res = collect("--foo =bar")
|
||||
proc expected(m: CliMode): seq[Opt] =
|
||||
@[(cmdLongOption, "foo", ""), (cmdArgument, "=bar", "")]
|
||||
check("long option argument delimited with whitespace, val not allowed", res, expected)
|
||||
|
||||
block:
|
||||
# pcLongAllowSep: '=' separator
|
||||
let res = collect(@["--foo=bar"])
|
||||
proc expected(m: CliMode): seq[Opt] =
|
||||
@[(cmdLongOption, "foo", "bar")]
|
||||
check("long option equals separator", res, expected)
|
||||
|
||||
block:
|
||||
# pcLongValAllowNextArg: long option value can be next argument.
|
||||
let res = collect(@["--foo", "bar"], longNoVal = @[""])
|
||||
proc expected(m: CliMode): seq[Opt] =
|
||||
@[(cmdLongOption, "foo", "bar")]
|
||||
check("long option next-arg value", res, expected)
|
||||
|
||||
block:
|
||||
# longNoVal disables next-arg value consumption.
|
||||
let res = collect(@["--foo", "bar"], longNoVal = @["foo"])
|
||||
proc expected(m: CliMode): seq[Opt] =
|
||||
@[(cmdLongOption, "foo", ""), (cmdArgument, "bar", "")]
|
||||
check("long option longNoVal disables argument taking", res, expected)
|
||||
|
||||
block:
|
||||
# "--" is parsed as a long option with an empty key.
|
||||
let res = collect(@["--", "rest"])
|
||||
proc expected(m: CliMode): seq[Opt] =
|
||||
@[(cmdLongOption, "", ""), (cmdArgument, "rest", "")]
|
||||
check("double-dash marker", res, expected)
|
||||
|
||||
block:
|
||||
# option values beginning with ':' - doubled up
|
||||
let res = collect(@["--foo::"])
|
||||
proc expected(m: CliMode): seq[Opt] =
|
||||
case m
|
||||
of LaxMode: @[(cmdLongOption, "foo", ":")]
|
||||
of NimMode: @[(cmdLongOption, "foo", ":")]
|
||||
of GnuMode: @[(cmdLongOption, "foo::", "")]
|
||||
check("long option value starting with colon (doubled)", res, expected)
|
||||
|
||||
block:
|
||||
# option values beginning with '=' - doubled up
|
||||
let res = collect(@["--foo=="])
|
||||
proc expected(m: CliMode): seq[Opt] =
|
||||
@[(cmdLongOption, "foo", "=")]
|
||||
check("long option value starting with equals (doubled)", res, expected)
|
||||
|
||||
block:
|
||||
# option values beginning with ':' - alternated with '='
|
||||
let res = collect(@["--foo=:"])
|
||||
proc expected(m: CliMode): seq[Opt] =
|
||||
@[(cmdLongOption, "foo", ":")]
|
||||
check("long option value starting with colon (alternated)", res, expected)
|
||||
|
||||
block:
|
||||
# option values beginning with '=' - alternated with ':'
|
||||
let res = collect(@["--foo:="])
|
||||
proc expected(m: CliMode): seq[Opt] =
|
||||
case m
|
||||
of LaxMode: @[(cmdLongOption, "foo", "=")]
|
||||
of NimMode: @[(cmdLongOption, "foo", "=")]
|
||||
of GnuMode: @[(cmdLongOption, "foo:", "")]
|
||||
check("long option value starting with equals (alternated)", res, expected)
|
||||
|
||||
block issue9619:
|
||||
let res = collect(@["--option=", "", "--anotherOption", "tree"])
|
||||
proc expected(m: CliMode): seq[Opt] =
|
||||
case m
|
||||
of LaxMode: @[(cmdLongOption, "option", ""),
|
||||
(cmdLongOption, "anotherOption", ""),
|
||||
(cmdArgument, "tree", "")]
|
||||
of NimMode: @[(cmdLongOption, "option", ""),
|
||||
(cmdLongOption, "anotherOption", ""),
|
||||
(cmdArgument, "tree", "")]
|
||||
of GnuMode: @[(cmdLongOption, "option", ""),
|
||||
(cmdArgument, "", ""),
|
||||
(cmdLongOption, "anotherOption", ""),
|
||||
(cmdArgument, "tree", "")]
|
||||
check("issue #9619, whitespace after separator", res, expected)
|
||||
|
||||
|
||||
block issue22736:
|
||||
let res = collect(@["--long", "", "-h", "--long:", "-h", "--long=", "-h", "arg"])
|
||||
proc expected(m: CliMode): seq[Opt] =
|
||||
case m
|
||||
of LaxMode: @[(cmdLongOption, "long", ""),
|
||||
(cmdArgument, "", ""),
|
||||
(cmdShortOption, "h", ""),
|
||||
(cmdLongOption, "long", "-h"),
|
||||
(cmdLongOption, "long", "-h"),
|
||||
(cmdArgument, "arg", "")]
|
||||
of NimMode: @[(cmdLongOption, "long", ""),
|
||||
(cmdArgument, "", ""),
|
||||
(cmdShortOption, "h", ""),
|
||||
(cmdLongOption, "long", "-h"),
|
||||
(cmdLongOption, "long", "-h"),
|
||||
(cmdArgument, "arg", "")]
|
||||
of GnuMode: @[(cmdLongOption, "long", ""),
|
||||
(cmdArgument, "", ""),
|
||||
(cmdShortOption, "h", ""),
|
||||
(cmdLongOption, "long:", ""),
|
||||
(cmdShortOption, "h", ""),
|
||||
(cmdLongOption, "long", ""),
|
||||
(cmdShortOption, "h", ""),
|
||||
(cmdArgument, "arg", "")]
|
||||
check("issue #22736, whitespace after separator, colon separator", res, expected)
|
||||
|
||||
# Numbers =====================================================================
|
||||
|
||||
block:
|
||||
# Positive integer adjacent to option
|
||||
let res = collect("-n42", shortNoVal = {'v'})
|
||||
proc expected(m: CliMode): seq[Opt] =
|
||||
@[(cmdShortOption, "n", "42")]
|
||||
check("numerical option: positive integer adjacent", res, expected)
|
||||
|
||||
block:
|
||||
# Positive integer adjacent to no-val option
|
||||
let res = collect("-n42x", shortNoVal = {'n'})
|
||||
proc expected(m: CliMode): seq[Opt] =
|
||||
case m
|
||||
of LaxMode: @[(cmdShortOption, "n", ""), (cmdShortOption, "4", "2x")]
|
||||
of NimMode: @[(cmdShortOption, "n", ""), (cmdShortOption, "4", "2x")]
|
||||
of GnuMode: @[(cmdShortOption, "n", ""), (cmdShortOption, "4", "2x")]
|
||||
check("numerical no-val option: positive integer adjacent", res, expected)
|
||||
|
||||
block:
|
||||
# Negative integer adjacent to option
|
||||
let res = collect("-n-42", shortNoVal = {'v'})
|
||||
proc expected(m: CliMode): seq[Opt] =
|
||||
@[(cmdShortOption, "n", "-42")]
|
||||
check("numerical option: negative integer adjacent", res, expected)
|
||||
|
||||
block:
|
||||
# Floating point number as value
|
||||
let res = collect(@["-n", "3.14"], shortNoVal = {'v'})
|
||||
proc expected(m: CliMode): seq[Opt] =
|
||||
case m
|
||||
of LaxMode: @[(cmdShortOption, "n", "3.14")]
|
||||
of NimMode: @[(cmdShortOption, "n", ""), (cmdArgument, "3.14", "")]
|
||||
of GnuMode: @[(cmdShortOption, "n", "3.14")]
|
||||
check("numerical option: floating point whitespace", res, expected)
|
||||
|
||||
block:
|
||||
# Floating point adjacent to option
|
||||
let res = collect(@["-n3.14"], shortNoVal = {'v'})
|
||||
proc expected(m: CliMode): seq[Opt] =
|
||||
@[(cmdShortOption, "n", "3.14")]
|
||||
check("numerical option: floating point adjacent", res, expected)
|
||||
|
||||
block:
|
||||
# Negative floating point
|
||||
let res = collect(@["-n", "-3.14"], shortNoVal = {'v'})
|
||||
proc expected(m: CliMode): seq[Opt] =
|
||||
case m
|
||||
of LaxMode: @[(cmdShortOption, "n", "-3.14")]
|
||||
of NimMode: @[(cmdShortOption, "n", ""), (cmdShortOption, "3", ".14")]
|
||||
of GnuMode: @[(cmdShortOption, "n", "-3.14")]
|
||||
check("numerical option: negative floating point whitespace", res, expected)
|
||||
|
||||
block:
|
||||
# Negative floating point adjacent
|
||||
let res = collect("-n-3.14", shortNoVal = {'v'})
|
||||
proc expected(m: CliMode): seq[Opt] =
|
||||
@[(cmdShortOption, "n", "-3.14")]
|
||||
check("numerical option: negative floating point adjacent", res, expected)
|
||||
|
||||
block:
|
||||
# Large number
|
||||
let res = collect(@["-n", "414"], shortNoVal = {'v'})
|
||||
proc expected(m: CliMode): seq[Opt] =
|
||||
case m
|
||||
of LaxMode: @[(cmdShortOption, "n", "414")]
|
||||
of NimMode: @[(cmdShortOption, "n", ""), (cmdArgument, "414", "")]
|
||||
of GnuMode: @[(cmdShortOption, "n", "414")]
|
||||
check("numerical option: large number", res, expected)
|
||||
|
||||
|
||||
block:
|
||||
# Multiple numerical options
|
||||
let res = collect("-n 10 -m20 -k= 30 -40", shortNoVal = {'v'})
|
||||
proc expected(m: CliMode): seq[Opt] =
|
||||
case m
|
||||
of LaxMode: @[(cmdShortOption, "n", "10"),
|
||||
(cmdShortOption, "m", "20"),
|
||||
(cmdShortOption, "k", ""), # buggy but preserved
|
||||
(cmdArgument, "30", ""),
|
||||
(cmdShortOption, "4", "0")]
|
||||
of NimMode: @[(cmdShortOption, "n", ""),
|
||||
(cmdArgument, "10", ""),
|
||||
(cmdShortOption, "m", "20"),
|
||||
(cmdShortOption, "k", ""), # buggy but preserved
|
||||
(cmdArgument, "30", ""),
|
||||
(cmdShortOption, "4", "0")]
|
||||
of GnuMode: @[(cmdShortOption, "n", "10"),
|
||||
(cmdShortOption, "m", "20"),
|
||||
(cmdShortOption, "k", "="),
|
||||
(cmdArgument, "30", ""),
|
||||
(cmdShortOption, "4", "0")]
|
||||
check("numerical option: multiple options", res, expected)
|
||||
|
||||
block:
|
||||
# Long option with numerical value
|
||||
let res = collect(@["--count=42"], longNoVal = @[])
|
||||
proc expected(m: CliMode): seq[Opt] =
|
||||
@[(cmdLongOption, "count", "42")]
|
||||
check("numerical option: long option with equals", res, expected)
|
||||
|
||||
block:
|
||||
# Long option with numerical value (whitespace)
|
||||
let res = collect(@["--count", "42"], longNoVal = @[""])
|
||||
proc expected(m: CliMode): seq[Opt] =
|
||||
@[(cmdLongOption, "count", "42")]
|
||||
check("numerical option: long option with whitespace", res, expected)
|
||||
|
||||
block:
|
||||
# Long option with negative numerical value
|
||||
let res = collect(@["--offset=-10"], longNoVal = @[])
|
||||
proc expected(m: CliMode): seq[Opt] =
|
||||
@[(cmdLongOption, "offset", "-10")]
|
||||
check("numerical option: long option negative", res, expected)
|
||||
@@ -70,4 +70,9 @@ var smallFloatRange: SmallFloat = SmallFloat(5.0)
|
||||
acceptWideFloat(smallFloatRange) # OK - SmallFloat (0.0..10.0) fits in WideFloatRange (0.0..100.0)
|
||||
|
||||
var wf: WideFloatRange
|
||||
wf = smallFloatRange # OK - SmallFloat range fits in WideFloatRange
|
||||
wf = smallFloatRange # OK - SmallFloat range fits in WideFloatRange
|
||||
|
||||
proc foo(x: Natural) =
|
||||
discard
|
||||
|
||||
foo(12)
|
||||
14
tests/range/timplicitrangedownsizing3.nim
Normal file
14
tests/range/timplicitrangedownsizing3.nim
Normal file
@@ -0,0 +1,14 @@
|
||||
discard """
|
||||
matrix: "--warning:systemRangeConversion --warningaserror:systemRangeConversion"
|
||||
"""
|
||||
|
||||
proc foo(x: range[0..100]) = discard
|
||||
|
||||
foo(12)
|
||||
|
||||
type
|
||||
Float = range[0.0..100.0]
|
||||
|
||||
proc bar(x: Float) = discard
|
||||
|
||||
bar(12.0)
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user