Skip to content

Commit

Permalink
--nilseqs is now a deprecated noop (#17211)
Browse files Browse the repository at this point in the history
* --nilseqs is now a deprecated noop

* fix tests; fix: future => sugar
  • Loading branch information
timotheecour authored Mar 1, 2021
1 parent bb0c19f commit 6c5872c
Show file tree
Hide file tree
Showing 15 changed files with 37 additions and 53 deletions.
2 changes: 2 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,8 @@ provided by the operating system.

- Deprecated `TaintedString` and `--taintmode`.

- Deprecated `--nilseqs` which is now a noop.

- Source+Edit links now appear on top of every docgen'd page when
`nim doc --git.url:url ...` is given.

Expand Down
16 changes: 6 additions & 10 deletions compiler/ccgexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,9 @@ proc genLiteral(p: BProc, n: PNode, ty: PType): Rope =
of tyNil:
result = genNilStringLiteral(p.module, n.info)
of tyString:
# with the new semantics for 'nil' strings, we can map "" to nil and
# with the new semantics for not 'nil' strings, we can map "" to nil and
# save tons of allocations:
if n.strVal.len == 0 and optNilSeqs notin p.options and
optSeqDestructors notin p.config.globalOptions:
if n.strVal.len == 0 and optSeqDestructors notin p.config.globalOptions:
result = genNilStringLiteral(p.module, n.info)
else:
result = genStringLiteral(p.module, n)
Expand Down Expand Up @@ -176,8 +175,7 @@ proc canMove(p: BProc, n: PNode; dest: TLoc): bool =
if not isDeepConstExpr(n) or n.len == 0:
if skipTypes(n.typ, abstractVarRange).kind == tySequence:
return true
elif optNilSeqs notin p.options and
n.kind in nkStrKinds and n.strVal.len == 0:
elif n.kind in nkStrKinds and n.strVal.len == 0:
# Empty strings are codegen'd as NIM_NIL so it's just a pointer copy
return true
result = n.kind in nkCallKinds
Expand Down Expand Up @@ -1371,8 +1369,7 @@ proc genNewSeq(p: BProc, e: PNode) =
getTypeDesc(p.module, seqtype.lastSon),
getSeqPayloadType(p.module, seqtype)])
else:
let lenIsZero = optNilSeqs notin p.options and
e[2].kind == nkIntLit and e[2].intVal == 0
let lenIsZero = e[2].kind == nkIntLit and e[2].intVal == 0
genNewSeqAux(p, a, b.rdLoc, lenIsZero)
gcUsage(p.config, e)

Expand Down Expand Up @@ -1497,8 +1494,7 @@ proc genSeqConstr(p: BProc, n: PNode, d: var TLoc) =
getSeqPayloadType(p.module, seqtype)])
else:
# generate call to newSeq before adding the elements per hand:
genNewSeqAux(p, dest[], l,
optNilSeqs notin p.options and n.len == 0)
genNewSeqAux(p, dest[], l, n.len == 0)
for i in 0..<n.len:
initLoc(arr, locExpr, n[i], OnHeap)
arr.r = ropecg(p.module, "$1$3[$2]", [rdLoc(dest[]), intLiteral(i), dataField(p)])
Expand Down Expand Up @@ -1527,7 +1523,7 @@ proc genArrToSeq(p: BProc, n: PNode, d: var TLoc) =
[rdLoc d, L, getTypeDesc(p.module, seqtype.lastSon),
getSeqPayloadType(p.module, seqtype)])
else:
genNewSeqAux(p, d, intLiteral(L), optNilSeqs notin p.options and L == 0)
genNewSeqAux(p, d, intLiteral(L), L == 0)
initLocExpr(p, n[1], a)
# bug #5007; do not produce excessive C source code:
if L < 10:
Expand Down
6 changes: 4 additions & 2 deletions compiler/commands.nim
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,9 @@ proc testCompileOption*(conf: ConfigRef; switch: string, info: TLineInfo): bool
of "implicitstatic": result = contains(conf.options, optImplicitStatic)
of "patterns", "trmacros": result = contains(conf.options, optTrMacros)
of "excessivestacktrace": result = contains(conf.globalOptions, optExcessiveStackTrace)
of "nilseqs": result = contains(conf.options, optNilSeqs)
of "nilseqs":
warningDeprecated(conf, info, "nilseqs is a deprecated noop")
result = false
else: invalidCmdLineOption(conf, passCmd1, switch, info)

proc processPath(conf: ConfigRef; path: string, info: TLineInfo,
Expand Down Expand Up @@ -650,7 +652,7 @@ proc processSwitch*(switch, arg: string, pass: TCmdLinePass, info: TLineInfo;
else:
undefSymbol(conf.symbols, "hotcodereloading")
undefSymbol(conf.symbols, "useNimRtl")
of "nilseqs": processOnOffSwitch(conf, {optNilSeqs}, arg, pass, info)
of "nilseqs": warningDeprecated(conf, info, "nilseqs is a deprecated noop")
of "checks", "x": processOnOffSwitch(conf, ChecksOptions, arg, pass, info)
of "floatchecks":
processOnOffSwitch(conf, {optNaNCheck, optInfCheck}, arg, pass, info)
Expand Down
3 changes: 3 additions & 0 deletions compiler/msgs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,9 @@ template localError*(conf: ConfigRef; info: TLineInfo, format: string, params: o
template message*(conf: ConfigRef; info: TLineInfo, msg: TMsgKind, arg = "") =
liMessage(conf, info, msg, arg, doNothing, instLoc())

proc warningDeprecated*(conf: ConfigRef, info: TLineInfo = gCmdLineInfo, msg = "") {.inline.} =
message(conf, info, warnDeprecated, msg)

proc internalErrorImpl(conf: ConfigRef; info: TLineInfo, errMsg: string, info2: InstantiationInfo) =
if conf.cmd == cmdIdeTools and conf.structuredErrorHook.isNil: return
writeContext(conf, info)
Expand Down
1 change: 0 additions & 1 deletion compiler/options.nim
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ type # please make sure we have under 32 options
# evaluation
optTrMacros, # en/disable pattern matching
optMemTracker,
optNilSeqs,
optSinkInference # 'sink T' inference
optCursorInference

Expand Down
10 changes: 2 additions & 8 deletions compiler/sigmatch.nim
Original file line number Diff line number Diff line change
Expand Up @@ -556,12 +556,6 @@ proc recordRel(c: var TCandidate, f, a: PType): TTypeRelation =
proc allowsNil(f: PType): TTypeRelation {.inline.} =
result = if tfNotNil notin f.flags: isSubtype else: isNone

proc allowsNilDeprecated(c: TCandidate, f: PType): TTypeRelation =
if optNilSeqs in c.c.config.options:
result = allowsNil(f)
else:
result = isNone

proc inconsistentVarTypes(f, a: PType): bool {.inline.} =
result = f.kind != a.kind and
(f.kind in {tyVar, tyLent, tySink} or a.kind in {tyVar, tyLent, tySink})
Expand Down Expand Up @@ -1300,7 +1294,7 @@ proc typeRel(c: var TCandidate, f, aOrig: PType,
result = isNone
elif tfNotNil in f.flags and tfNotNil notin a.flags:
result = isNilConversion
of tyNil: result = allowsNilDeprecated(c, f)
of tyNil: result = isNone
else: discard
of tyOrdinal:
if isOrdinalType(a, allowEnumWithHoles = optNimV1Emulation in c.c.config.globalOptions):
Expand Down Expand Up @@ -1396,7 +1390,7 @@ proc typeRel(c: var TCandidate, f, aOrig: PType,
result = isNilConversion
else:
result = isEqual
of tyNil: result = allowsNilDeprecated(c, f)
of tyNil: result = isNone
else: discard
of tyCString:
# conversion from string to cstring is automatic:
Expand Down
2 changes: 0 additions & 2 deletions doc/advopt.txt
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,6 @@ Advanced options:
--excessiveStackTrace:on|off
stack traces use full file paths
--stackTraceMsgs:on|off enable user defined stack frame msgs via `setFrameMsg`
--nilseqs:on|off allow 'nil' for strings/seqs for
backwards compatibility
--seqsv2:on|off use the new string/seq implementation based on
destructors
--skipCfg:on|off do not read the nim installation's configuration file
Expand Down
33 changes: 13 additions & 20 deletions lib/system.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1600,26 +1600,19 @@ 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 not compileOption("nilseqs"):
{.pragma: nilError, error.}
else:
{.pragma: nilError.}
when true: # PRTEMP: remove?
proc isNil*[T](x: seq[T]): bool {.noSideEffect, magic: "IsNil", error.}
## Seqs are no longer nil by default, but set and empty.
## Check for zero length instead.
##
## See also:
## * `isNil(string) <#isNil,string>`_

proc isNil*[T](x: seq[T]): bool {.noSideEffect, magic: "IsNil", nilError.}
## Requires `--nilseqs:on` since 0.19.
##
## Seqs are no longer nil by default, but set and empty.
## Check for zero length instead.
##
## See also:
## * `isNil(string) <#isNil,string>`_
proc isNil*(x: string): bool {.noSideEffect, magic: "IsNil", error.}
## See also:
## * `isNil(seq[T]) <#isNil,seq[T]>`_

proc isNil*[T](x: ref T): bool {.noSideEffect, magic: "IsNil".}
proc isNil*(x: string): bool {.noSideEffect, magic: "IsNil", nilError.}
## Requires `--nilseqs:on`.
##
## See also:
## * `isNil(seq[T]) <#isNil,seq[T]>`_

proc isNil*[T](x: ptr T): bool {.noSideEffect, magic: "IsNil".}
proc isNil*(x: pointer): bool {.noSideEffect, magic: "IsNil".}
Expand Down Expand Up @@ -2985,15 +2978,15 @@ proc `==`*(x, y: cstring): bool {.magic: "EqCString", noSideEffect,
elif x.isNil or y.isNil: result = false
else: result = strcmp(x, y) == 0

when not compileOption("nilseqs"):
when true: # xxx PRTEMP remove
# bug #9149; ensure that 'typeof(nil)' does not match *too* well by using 'typeof(nil) | typeof(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: typeof(nil) | typeof(nil)): bool {.
error: "'nil' is now invalid for 'string'; compile with --nilseqs:on for a migration period".} =
error: "'nil' is now invalid for 'string'".} =
discard
proc `==`*(x: typeof(nil) | typeof(nil); y: string): bool {.
error: "'nil' is now invalid for 'string'; compile with --nilseqs:on for a migration period".} =
error: "'nil' is now invalid for 'string'".} =
discard

template closureScope*(body: untyped): untyped =
Expand Down
2 changes: 0 additions & 2 deletions testament/categories.nim
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,6 @@ proc jsTests(r: var TResults, cat: Category, options: string) =
# ------------------------- nim in action -----------

proc testNimInAction(r: var TResults, cat: Category, options: string) =
let options = options & " --nilseqs:on"

template test(filename: untyped) =
testSpec r, makeTest(filename, options, cat)

Expand Down
2 changes: 1 addition & 1 deletion tests/ccgbugs/tassign_nil_strings.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
discard """
cmd: "nim $target --nilseqs:off $options $file"
cmd: "nim $target $options $file"
output: "Hello"
ccodecheck: "\\i@'a = ((NimStringDesc*) NIM_NIL)'"
"""
Expand Down
2 changes: 1 addition & 1 deletion tests/cpp/tasync_cpp.nim
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
discard """
targets: "cpp"
output: "hello"
cmd: "nim cpp --nilseqs:on --clearNimblePath --nimblePath:build/deps/pkgs $file"
cmd: "nim cpp --clearNimblePath --nimblePath:build/deps/pkgs $file"
"""

# bug #3299
Expand Down
1 change: 0 additions & 1 deletion tests/manyloc/keineschweine/keineschweine.nim.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,3 @@ path = "dependencies/genpacket"
path = "enet_server"
debugger = off
warning[SmallLshouldNotBeUsed] = off
nilseqs = on
2 changes: 1 addition & 1 deletion tests/niminaction/Chapter1/various1.nim
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ block: # Block added due to clash.
let dog = Dog()
dog.bark() #<2>

import sequtils, future, strutils
import sequtils, sugar, strutils
let list = @["Dominik Picheta", "Andreas Rumpf", "Desmond Hume"]
list.map(
(x: string) -> (string, string) => (x.split[0], x.split[1])
Expand Down
2 changes: 1 addition & 1 deletion tests/niminaction/Chapter2/resultaccept.nim
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ proc resultVar2: string =
result.add("returned")

doAssert implicit() == "I will be returned"
doAssert discarded() == nil
doAssert discarded().len == 0
doAssert explicit() == "I will be returned"
doAssert resultVar() == "I will be returned"
doAssert resultVar2() == "I will be returned"
6 changes: 3 additions & 3 deletions tests/niminaction/Chapter2/various2.nim
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ let numbers = @[1, 2, 3, 4, 5, 6]
let odd = filter(numbers, proc (x: int): bool = x mod 2 != 0)
doAssert odd == @[1, 3, 5]

import sequtils, future
import sequtils, sugar
let numbers1 = @[1, 2, 3, 4, 5, 6]
let odd1 = filter(numbers1, (x: int) -> bool => x mod 2 != 0)
assert odd1 == @[1, 3, 5]
Expand All @@ -149,7 +149,7 @@ proc isValid(x: int, validator: proc (x: int): bool) =
if validator(x): echo(x, " is valid")
else: echo(x, " is NOT valid")

import future
import sugar
proc isValid2(x: int, validator: (x: int) -> bool) =
if validator(x): echo(x, " is valid")
else: echo(x, " is NOT valid")
Expand Down Expand Up @@ -193,7 +193,7 @@ doAssertRaises(IndexDefect):

block:
var list = newSeq[string](3)
assert list[0] == nil
assert list[0].len == 0
list[0] = "Foo"
list[1] = "Bar"
list[2] = "Baz"
Expand Down

0 comments on commit 6c5872c

Please sign in to comment.