Skip to content

Commit

Permalink
fix #8485 ; make default public instead of redefined everywhere; us…
Browse files Browse the repository at this point in the history
…e better implementation
  • Loading branch information
timotheecour committed Aug 1, 2018
1 parent 8f4c5a8 commit cae1f84
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 27 deletions.
4 changes: 0 additions & 4 deletions lib/core/seqs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,6 @@ proc grow*[T](x: var seq[T]; newLen: int; value: T) =
x.data[i] = value
x.len = newLen

template default[T](t: typedesc[T]): T =
var v: T
v

proc setLen*[T](x: var seq[T]; newLen: int) {.deprecated.} =
if newlen < x.len: shrink(x, newLen)
else: grow(x, newLen, default(T))
Expand Down
4 changes: 0 additions & 4 deletions lib/pure/collections/queues.nim
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,6 @@ proc add*[T](q: var Queue[T], item: T) =
q.data[q.wr] = item
q.wr = (q.wr + 1) and q.mask

template default[T](t: typedesc[T]): T =
var v: T
v

proc pop*[T](q: var Queue[T]): T {.inline, discardable.} =
## Remove and returns the first (oldest) element of the queue `q`.
emptyCheck(q)
Expand Down
9 changes: 0 additions & 9 deletions lib/pure/collections/sets.nim
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,6 @@ type

{.deprecated: [TSet: HashSet].}

template default[T](t: typedesc[T]): T =
## Used by clear methods to get a default value.
var v: T
v

proc clear*[A](s: var HashSet[A]) =
## Clears the HashSet back to an empty state, without shrinking
## any of the existing storage. O(n) where n is the size of the hash bucket.
Expand Down Expand Up @@ -281,10 +276,6 @@ template doWhile(a, b) =
b
if not a: break

template default[T](t: typedesc[T]): T =
var v: T
v

proc exclImpl[A](s: var HashSet[A], key: A) : bool {. inline .} =
assert s.isValid, "The set needs to be initialized."
var hc: Hash
Expand Down
4 changes: 0 additions & 4 deletions lib/pure/collections/tableimpl.nim
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,6 @@ template hasKeyOrPutImpl(enlarge) {.dirty.} =
maybeRehashPutImpl(enlarge)
else: result = true

template default[T](t: typedesc[T]): T =
var v: T
v

template delImplIdx(t, i) =
let msk = maxHash(t)
if i >= 0:
Expand Down
16 changes: 16 additions & 0 deletions lib/system.nim
Original file line number Diff line number Diff line change
Expand Up @@ -4193,3 +4193,19 @@ when defined(genode):
componentConstructHook(env)
# Perform application initialization
# and return to thread entrypoint.

# needed pending https://github.com/nim-lang/Nim/issues/8486
proc defaultImpl[T]():T=
var a: T
result = a

template default*[T](t: typedesc[T]): T =
## returns default value for type ``T``, and can work at compile time; V2
runnableExamples:
const a = (1,2).type.default
doAssert a == (0, 0)
defaultImpl[T]()

when isMainModule:
# needed pending https://github.com/nim-lang/Nim/issues/7280
discard int.default
4 changes: 0 additions & 4 deletions tests/destructor/tcustomseqs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,6 @@ proc grow*[T](x: var myseq[T]; newLen: int; value: T) =
x.data[i] = value
x.len = newLen

template default[T](t: typedesc[T]): T =
var v: T
v

proc setLen*[T](x: var myseq[T]; newLen: int) {.deprecated.} =
if newlen < x.len: shrink(x, newLen)
else: grow(x, newLen, default(T))
Expand Down
18 changes: 18 additions & 0 deletions tests/system/tsystem_misc.nim
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,21 @@ proc foo(a: openArray[byte]) =

let str = "0123456789"
foo(toOpenArrayByte(str, 0, str.high))


# BUG:`block default2:` causes compile time error:
# attempting to call undeclared routine: 'default'
block default2:
doAssert int.default == 0

# test case from https://github.com/nim-lang/Nim/issues/8485
proc len(T: typedesc[tuple]):auto=
const t=default(T)
var n=0
for _ in t.fields:
inc n
return n

var a=(1,2,3)
const length = a.type.len
doAssert length == 3
4 changes: 2 additions & 2 deletions tests/vm/tvmmisc.nim
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ block:

# #4412
block:
proc default[T](t: typedesc[T]): T {.inline.} = discard
proc default2[T](t: typedesc[T]): T {.inline.} = discard

static:
var x = default(type(0))
var x = default2(type(0))

# #6379
import algorithm
Expand Down

0 comments on commit cae1f84

Please sign in to comment.