From 6e267d28b3459aa447cae07ba61209438977cd5c Mon Sep 17 00:00:00 2001 From: Timothee Cour Date: Fri, 29 Jan 2021 05:30:24 -0800 Subject: remove conditionals on nimHasUserErrors, nimNoNilSeqs2, nimNoNilSeqs (#16861) * cleanup docs for type(nil) | type(nil); simplify nimHasUserErrors * simplify nimNoNilSeqs2 * simplify nimNoNilSeqs * fixup --- compiler/ast.nim | 37 +++++-------------------------------- compiler/condsyms.nim | 6 +++--- compiler/docgen.nim | 3 +-- compiler/magicsys.nim | 2 -- compiler/parser.nim | 2 -- compiler/patterns.nim | 7 ++----- compiler/semstmts.nim | 5 +---- compiler/types.nim | 2 -- compiler/vm.nim | 4 ---- lib/pure/streams.nim | 10 ++-------- lib/std/packedsets.nim | 12 +++--------- lib/system.nim | 30 ++++++++++++------------------ tools/niminst/niminst.nim | 2 -- 13 files changed, 29 insertions(+), 93 deletions(-) diff --git a/compiler/ast.nim b/compiler/ast.nim index 31d47a6fd..63db11d54 100644 --- a/compiler/ast.nim +++ b/compiler/ast.nim @@ -1127,11 +1127,7 @@ proc discardSons*(father: PNode) type Indexable = PNode | PType proc len*(n: Indexable): int {.inline.} = - when defined(nimNoNilSeqs): - result = n.sons.len - else: - if isNil(n.sons): result = 0 - else: result = n.sons.len + result = n.sons.len proc safeLen*(n: PNode): int {.inline.} = ## works even for leaves. @@ -1146,8 +1142,6 @@ proc safeArrLen*(n: PNode): int {.inline.} = proc add*(father, son: Indexable) = assert son != nil - when not defined(nimNoNilSeqs): - if isNil(father.sons): father.sons = @[] father.sons.add(son) proc addAllowNil*(father, son: Indexable) {.inline.} = @@ -1308,10 +1302,7 @@ proc copyObjectSet*(dest: var TObjectSet, src: TObjectSet) = for i in 0..high(src.data): dest.data[i] = src.data[i] proc discardSons*(father: PNode) = - when defined(nimNoNilSeqs): - father.sons = @[] - else: - father.sons = nil + father.sons = @[] proc withInfo*(n: PNode, info: TLineInfo): PNode = n.info = info @@ -1436,13 +1427,7 @@ proc mergeLoc(a: var TLoc, b: TLoc) = if a.r == nil: a.r = b.r proc newSons*(father: Indexable, length: int) = - when defined(nimNoNilSeqs): - setLen(father.sons, length) - else: - if isNil(father.sons): - newSeq(father.sons, length) - else: - setLen(father.sons, length) + setLen(father.sons, length) proc assignType*(dest, src: PType) = dest.kind = src.kind @@ -1576,26 +1561,17 @@ proc propagateToOwner*(owner, elem: PType; propagateHasAsgn = true) = owner.flags.incl tfHasGCedMem proc rawAddSon*(father, son: PType; propagateHasAsgn = true) = - when not defined(nimNoNilSeqs): - if isNil(father.sons): father.sons = @[] father.sons.add(son) if not son.isNil: propagateToOwner(father, son, propagateHasAsgn) proc rawAddSonNoPropagationOfTypeFlags*(father, son: PType) = - when not defined(nimNoNilSeqs): - if isNil(father.sons): father.sons = @[] father.sons.add(son) proc addSonNilAllowed*(father, son: PNode) = - when not defined(nimNoNilSeqs): - if isNil(father.sons): father.sons = @[] father.sons.add(son) proc delSon*(father: PNode, idx: int) = - when defined(nimNoNilSeqs): - if father.len == 0: return - else: - if isNil(father.sons): return + if father.len == 0: return for i in idx.. p.tok.commentOffsetA: node.comment.add fileSection(p.lex.config, p.lex.fileIdx, p.tok.commentOffsetA, p.tok.commentOffsetB) diff --git a/compiler/patterns.nim b/compiler/patterns.nim index f24d25c5c..1ef903161 100644 --- a/compiler/patterns.nim +++ b/compiler/patterns.nim @@ -210,11 +210,8 @@ proc matchStmtList(c: PPatternContext, p, n: PNode): PNode = for j in 0.. 0 and c.s.contains((a.id, b.id)) if not result: - when not defined(nimNoNilSeqs): - if isNil(c.s): c.s = @[] c.s.add((a.id, b.id)) proc sameTypeAux(x, y: PType, c: var TSameTypeClosure): bool diff --git a/compiler/vm.nim b/compiler/vm.nim index 00f0aca7c..0c9c0ec33 100644 --- a/compiler/vm.nim +++ b/compiler/vm.nim @@ -271,8 +271,6 @@ template getstr(a: untyped): untyped = (if a.kind == rkNode: a.node.strVal else: $chr(int(a.intVal))) proc pushSafePoint(f: PStackFrame; pc: int) = - when not defined(nimNoNilSeqs): - if f.safePoints.isNil: f.safePoints = @[] f.safePoints.add(pc) proc popSafePoint(f: PStackFrame) = @@ -2092,8 +2090,6 @@ proc rawExecute(c: PCtx, start: int, tos: PStackFrame): TFullReg = inc pc let typ = c.types[c.code[pc].regBx - wordExcess] createStrKeepNode(regs[ra]) - when not defined(nimNoNilSeqs): - if regs[ra].node.strVal.isNil: regs[ra].node.strVal = newStringOfCap(1000) storeAny(regs[ra].node.strVal, typ, regs[rb].regToNode, c.config) c.profiler.leave(c) diff --git a/lib/pure/streams.nim b/lib/pure/streams.nim index 55a4e1b85..f9d0ffc5f 100644 --- a/lib/pure/streams.nim +++ b/lib/pure/streams.nim @@ -1150,10 +1150,7 @@ when (NimMajor, NimMinor) < (1, 3) and defined(js): proc ssClose(s: Stream) {.compileTime.} = var s = StringStream(s) - when defined(nimNoNilSeqs): - s.data = "" - else: - s.data = nil + s.data = "" proc newStringStream*(s: string = ""): owned StringStream {.compileTime.} = new(result) @@ -1253,10 +1250,7 @@ else: # after 1.3 or JS not defined proc ssClose(s: Stream) = var s = StringStream(s) - when defined(nimNoNilSeqs): - s.data = "" - else: - s.data = nil + s.data = "" proc newStringStream*(s: string = ""): owned StringStream = ## Creates a new stream from the string `s`. diff --git a/lib/std/packedsets.nim b/lib/std/packedsets.nim index 42814547c..a5101ec37 100644 --- a/lib/std/packedsets.nim +++ b/lib/std/packedsets.nim @@ -180,7 +180,7 @@ proc initPackedSet*[A]: PackedSet[A] = counter: 0, max: 0, head: nil, - data: when defined(nimNoNilSeqs): @[] else: nil) + data: @[]) # a: array[0..33, int] # profiling shows that 34 elements are enough proc contains*[A](s: PackedSet[A], key: A): bool = @@ -392,10 +392,7 @@ proc clear*[A](result: var PackedSet[A]) = # setLen(result.data, InitIntSetSize) # for i in 0..InitIntSetSize - 1: result.data[i] = nil # result.max = InitIntSetSize - 1 - when defined(nimNoNilSeqs): - result.data = @[] - else: - result.data = nil + result.data = @[] result.max = 0 result.counter = 0 result.head = nil @@ -426,10 +423,7 @@ proc assign*[A](dest: var PackedSet[A], src: PackedSet[A]) = assert len(a) == 2 if src.elems <= src.a.len: - when defined(nimNoNilSeqs): - dest.data = @[] - else: - dest.data = nil + dest.data = @[] dest.max = 0 dest.counter = src.counter dest.head = nil diff --git a/lib/system.nim b/lib/system.nim index e5ef54fa4..a2e6675d9 100644 --- a/lib/system.nim +++ b/lib/system.nim @@ -1559,11 +1559,8 @@ proc len*[U: Ordinal; V: Ordinal](x: HSlice[U, V]): int {.noSideEffect, inline.} ## assert((5..2).len == 0) result = max(0, ord(x.b) - ord(x.a) + 1) -when defined(nimNoNilSeqs2): - when not compileOption("nilseqs"): - {.pragma: nilError, error.} - else: - {.pragma: nilError.} +when not compileOption("nilseqs"): + {.pragma: nilError, error.} else: {.pragma: nilError.} @@ -2942,19 +2939,16 @@ proc `==`*(x, y: cstring): bool {.magic: "EqCString", noSideEffect, elif x.isNil or y.isNil: result = false else: result = strcmp(x, y) == 0 -when defined(nimNoNilSeqs2) and not compileOption("nilseqs"): - when defined(nimHasUserErrors): - # bug #9149; ensure that 'type(nil)' does not match *too* well by using 'type(nil) | type(nil)'. - # Eventually (in 0.20?) we will be able to remove this hack completely. - proc `==`*(x: string; y: type(nil) | type(nil)): bool {. - error: "'nil' is now invalid for 'string'; compile with --nilseqs:on for a migration period".} = - discard - proc `==`*(x: type(nil) | type(nil); y: string): bool {. - error: "'nil' is now invalid for 'string'; compile with --nilseqs:on for a migration period".} = - discard - else: - proc `==`*(x: string; y: type(nil) | type(nil)): bool {.error.} = discard - proc `==`*(x: type(nil) | type(nil); y: string): bool {.error.} = discard +when not compileOption("nilseqs"): + # bug #9149; ensure that 'type(nil)' does not match *too* well by using 'type(nil) | type(nil)', + # especially for converters, see tests/overload/tconverter_to_string.nim + # Eventually we will be able to remove this hack completely. + proc `==`*(x: string; y: type(nil) | type(nil)): bool {. + error: "'nil' is now invalid for 'string'; compile with --nilseqs:on for a migration period".} = + discard + proc `==`*(x: type(nil) | type(nil); y: string): bool {. + error: "'nil' is now invalid for 'string'; compile with --nilseqs:on for a migration period".} = + discard template closureScope*(body: untyped): untyped = ## Useful when creating a closure in a loop to capture local loop variables by diff --git a/tools/niminst/niminst.nim b/tools/niminst/niminst.nim index 47137f8f3..d81b98be9 100644 --- a/tools/niminst/niminst.nim +++ b/tools/niminst/niminst.nim @@ -477,8 +477,6 @@ proc deduplicateFiles(c: var ConfigData) = let build = getOutputDir(c) for osA in countup(1, c.oses.len): for cpuA in countup(1, c.cpus.len): - when not defined(nimNoNilSeqs): - if c.cfiles[osA][cpuA].isNil: c.cfiles[osA][cpuA] = @[] if c.explicitPlatforms and not c.platforms[osA][cpuA]: continue for dup in mitems(c.cfiles[osA][cpuA]): let key = $secureHashFile(build / dup) -- cgit 1.4.1-2-gfad0