Skip to content

Commit

Permalink
use typeof instead type (#16962)
Browse files Browse the repository at this point in the history
  • Loading branch information
ringabout authored Feb 8, 2021
1 parent 4fac8af commit d447c0f
Show file tree
Hide file tree
Showing 31 changed files with 253 additions and 253 deletions.
2 changes: 1 addition & 1 deletion compiler/sem.nim
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ proc commonType*(c: PContext; x, y: PType): PType =
# range[0..4]. But then why is (range[0..4], 6) not range[0..6]?
# But then why is (2,4) not range[2..4]? But I think this would break
# too much code. So ... it's the same range or the base type. This means
# type(if b: 0 else 1) == int and not range[0..1]. For now. In the long
# typeof(if b: 0 else 1) == int and not range[0..1]. For now. In the long
# run people expect ranges to work properly within a tuple.
if not sameType(a, b):
result = skipTypes(a, {tyRange}).skipIntLit(c.idgen)
Expand Down
2 changes: 1 addition & 1 deletion compiler/semexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -842,7 +842,7 @@ proc semOverloadedCallAnalyseEffects(c: PContext, n: PNode, nOrig: PNode,
# consider: 'for x in pReturningArray()' --> we don't want the restriction
# to 'skIterator' anymore; skIterator is preferred in sigmatch already
# for typeof support.
# for ``type(countup(1,3))``, see ``tests/ttoseq``.
# for ``typeof(countup(1,3))``, see ``tests/ttoseq``.
result = semOverloadedCall(c, n, nOrig,
{skProc, skFunc, skMethod, skConverter, skMacro, skTemplate, skIterator}, flags)
else:
Expand Down
4 changes: 2 additions & 2 deletions compiler/seminst.nim
Original file line number Diff line number Diff line change
Expand Up @@ -232,11 +232,11 @@ proc instantiateProcType(c: PContext, pt: TIdTable,
# time adding the instantiated proc params into the current scope.
# This is necessary, because the instantiation process may refer to
# these params in situations like this:
# proc foo[Container](a: Container, b: a.type.Item): type(b.x)
# proc foo[Container](a: Container, b: a.type.Item): typeof(b.x)
#
# Alas, doing this here is probably not enough, because another
# proc signature could appear in the params:
# proc foo[T](a: proc (x: T, b: type(x.y))
# proc foo[T](a: proc (x: T, b: typeof(x.y))
#
# The solution would be to move this logic into semtypinst, but
# at this point semtypinst have to become part of sem, because it
Expand Down
4 changes: 2 additions & 2 deletions compiler/semtypes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1346,7 +1346,7 @@ proc semProcTypeNode(c: PContext, n, genericParams: PNode,
r = skipIntLit(r, c.idgen)
if kind == skIterator:
# see tchainediterators
# in cases like iterator foo(it: iterator): type(it)
# in cases like iterator foo(it: iterator): typeof(it)
# we don't need to change the return type to iter[T]
result.flags.incl tfIterator
# XXX Would be nice if we could get rid of this
Expand Down Expand Up @@ -1704,7 +1704,7 @@ proc semTypeNode(c: PContext, n: PNode, prev: PType): PType =
case n.kind
of nkEmpty: result = n.typ
of nkTypeOfExpr:
# for ``type(countup(1,3))``, see ``tests/ttoseq``.
# for ``typeof(countup(1,3))``, see ``tests/ttoseq``.
checkSonsLen(n, 1, c.config)
result = semTypeof(c, n[0], prev)
if result.kind == tyTypeDesc: result.flags.incl tfExplicit
Expand Down
6 changes: 3 additions & 3 deletions lib/core/macros.nim
Original file line number Diff line number Diff line change
Expand Up @@ -791,7 +791,7 @@ when declared(float128):

proc newLit*(arg: enum): NimNode {.compileTime.} =
result = newCall(
arg.type.getTypeInst[1],
arg.typeof.getTypeInst[1],
newLit(int(arg))
)

Expand All @@ -801,13 +801,13 @@ proc newLit*[T](s: set[T]): NimNode {.compileTime.}
proc newLit*[T: tuple](arg: T): NimNode {.compileTime.}

proc newLit*(arg: object): NimNode {.compileTime.} =
result = nnkObjConstr.newTree(arg.type.getTypeInst[1])
result = nnkObjConstr.newTree(arg.typeof.getTypeInst[1])
for a, b in arg.fieldPairs:
result.add nnkExprColonExpr.newTree( newIdentNode(a), newLit(b) )

proc newLit*(arg: ref object): NimNode {.compileTime.} =
## produces a new ref type literal node.
result = nnkObjConstr.newTree(arg.type.getTypeInst[1])
result = nnkObjConstr.newTree(arg.typeof.getTypeInst[1])
for a, b in fieldPairs(arg[]):
result.add nnkExprColonExpr.newTree(newIdentNode(a), newLit(b))

Expand Down
2 changes: 1 addition & 1 deletion lib/pure/collections/setimpl.nim
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ proc exclImpl[A](s: var HashSet[A], key: A): bool {.inline.} =
var j = i # The correctness of this depends on (h+1) in nextTry,
var r = j # though may be adaptable to other simple sequences.
s.data[i].hcode = 0 # mark current EMPTY
s.data[i].key = default(type(s.data[i].key))
s.data[i].key = default(typeof(s.data[i].key))
doWhile((i >= r and r > j) or (r > j and j > i) or (j > i and i >= r)):
i = (i + 1) and msk # increment mod table size
if isEmpty(s.data[i].hcode): # end of collision cluster; So all done
Expand Down
4 changes: 2 additions & 2 deletions lib/pure/collections/sets.nim
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ proc clear*[A](s: var HashSet[A]) =
s.counter = 0
for i in 0 ..< s.data.len:
s.data[i].hcode = 0
s.data[i].key = default(type(s.data[i].key))
s.data[i].key = default(typeof(s.data[i].key))

proc len*[A](s: HashSet[A]): int =
## Returns the number of elements in `s`.
Expand Down Expand Up @@ -812,7 +812,7 @@ proc clear*[A](s: var OrderedSet[A]) =
for i in 0 ..< s.data.len:
s.data[i].hcode = 0
s.data[i].next = 0
s.data[i].key = default(type(s.data[i].key))
s.data[i].key = default(typeof(s.data[i].key))

proc len*[A](s: OrderedSet[A]): int {.inline.} =
## Returns the number of elements in `s`.
Expand Down
10 changes: 5 additions & 5 deletions lib/pure/collections/tableimpl.nim
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ template delImplIdx(t, i, makeEmpty, cellEmpty, cellHash) =
var j = i # The correctness of this depends on (h+1) in nextTry
var r = j # though may be adaptable to other simple sequences.
makeEmpty(i) # mark current EMPTY
t.data[i].key = default(type(t.data[i].key))
t.data[i].val = default(type(t.data[i].val))
t.data[i].key = default(typeof(t.data[i].key))
t.data[i].val = default(typeof(t.data[i].val))
while true:
i = (i + 1) and msk # increment mod table size
if cellEmpty(i): # end of collision cluster; So all done
Expand Down Expand Up @@ -149,8 +149,8 @@ template clearImpl() {.dirty.} =
for i in 0 ..< t.dataLen:
when compiles(t.data[i].hcode): # CountTable records don't contain a hcode
t.data[i].hcode = 0
t.data[i].key = default(type(t.data[i].key))
t.data[i].val = default(type(t.data[i].val))
t.data[i].key = default(typeof(t.data[i].key))
t.data[i].val = default(typeof(t.data[i].val))
t.counter = 0

template ctAnd(a, b): bool =
Expand All @@ -161,7 +161,7 @@ template ctAnd(a, b): bool =

template initImpl(result: typed, size: int) =
let correctSize = slotsNeeded(size)
when ctAnd(declared(SharedTable), type(result) is SharedTable):
when ctAnd(declared(SharedTable), typeof(result) is SharedTable):
init(result, correctSize)
else:
result.counter = 0
Expand Down
2 changes: 1 addition & 1 deletion lib/pure/concurrency/atomics.nim
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ else:
{.pop.}

proc load*[T: Trivial](location: var Atomic[T]; order: MemoryOrder = moSequentiallyConsistent): T {.inline.} =
cast[T](atomic_load_explicit[nonAtomicType(T), type(location.value)](addr(location.value), order))
cast[T](atomic_load_explicit[nonAtomicType(T), typeof(location.value)](addr(location.value), order))
proc store*[T: Trivial](location: var Atomic[T]; desired: T; order: MemoryOrder = moSequentiallyConsistent) {.inline.} =
atomic_store_explicit(addr(location.value), cast[nonAtomicType(T)](desired), order)
proc exchange*[T: Trivial](location: var Atomic[T]; desired: T; order: MemoryOrder = moSequentiallyConsistent): T {.inline.} =
Expand Down
4 changes: 2 additions & 2 deletions lib/pure/net.nim
Original file line number Diff line number Diff line change
Expand Up @@ -446,14 +446,14 @@ proc toSockAddr*(address: IpAddress, port: Port, sa: var Sockaddr_storage,
of IpAddressFamily.IPv4:
sl = sizeof(Sockaddr_in).SockLen
let s = cast[ptr Sockaddr_in](addr sa)
s.sin_family = type(s.sin_family)(toInt(AF_INET))
s.sin_family = typeof(s.sin_family)(toInt(AF_INET))
s.sin_port = port
copyMem(addr s.sin_addr, unsafeAddr address.address_v4[0],
sizeof(s.sin_addr))
of IpAddressFamily.IPv6:
sl = sizeof(Sockaddr_in6).SockLen
let s = cast[ptr Sockaddr_in6](addr sa)
s.sin6_family = type(s.sin6_family)(toInt(AF_INET6))
s.sin6_family = typeof(s.sin6_family)(toInt(AF_INET6))
s.sin6_port = port
copyMem(addr s.sin6_addr, unsafeAddr address.address_v6[0],
sizeof(s.sin6_addr))
Expand Down
4 changes: 2 additions & 2 deletions lib/pure/options.nim
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ proc some*[T](val: T): Option[T] {.inline.} =
var
a = some("abc")
b = some(42)
assert $type(a) == "Option[system.string]"
assert $typeof(a) == "Option[system.string]"
assert b.isSome
assert a.get == "abc"
assert $b == "Some(42)"
Expand All @@ -122,7 +122,7 @@ proc none*(T: typedesc): Option[T] {.inline.} =
runnableExamples:
var a = none(int)
assert a.isNone
assert $type(a) == "Option[system.int]"
assert $typeof(a) == "Option[system.int]"

# the default is the none type
discard
Expand Down
2 changes: 1 addition & 1 deletion lib/pure/reservedmem.nim
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ when defined(windows):

template check(expr) =
let r = expr
if r == cast[type(r)](0):
if r == cast[typeof(r)](0):
raiseOSError(osLastError())

else:
Expand Down
6 changes: 3 additions & 3 deletions lib/pure/strformat.nim
Original file line number Diff line number Diff line change
Expand Up @@ -379,9 +379,9 @@ proc formatInt(n: SomeNumber; radix: int;
result = "0"
else:
result = ""
while v > type(v)(0):
let d = v mod type(v)(radix)
v = v div type(v)(radix)
while v > typeof(v)(0):
let d = v mod typeof(v)(radix)
v = v div typeof(v)(radix)
result.add(mkDigit(d.int, spec.typ))
for idx in 0..<(result.len div 2):
swap result[idx], result[result.len - idx - 1]
Expand Down
6 changes: 3 additions & 3 deletions lib/pure/typetraits.nim
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ since (1, 1):
doAssert 12.MyInt.distinctBase == 12
doAssert 12.distinctBase == 12
when T is distinct:
distinctBase(type(a))(a)
distinctBase(typeof(a))(a)
else: # avoids hint ConvFromXtoItselfNotNeeded
a

Expand All @@ -124,15 +124,15 @@ since (1, 1):
runnableExamples:
doAssert tupleLen((1, 2)) == 2

tupleLen(type(t))
tupleLen(typeof(t))

template get*(T: typedesc[tuple], i: static int): untyped =
## Returns the `i`-th element of `T`.
# Note: `[]` currently gives: `Error: no generic parameters allowed for ...`
runnableExamples:
doAssert get((int, int, float, string), 2) is float

type(default(T)[i])
typeof(default(T)[i])

type StaticParam*[value: static type] = object
## Used to wrap a static value in `genericParams <#genericParams.t,typedesc>`_.
Expand Down
4 changes: 2 additions & 2 deletions lib/pure/volatile.nim
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ template volatileLoad*[T](src: ptr T): T =
src[]
else:
var res: T
{.emit: [res, " = (*(", type(src[]), " volatile*)", src, ");"].}
{.emit: [res, " = (*(", typeof(src[]), " volatile*)", src, ");"].}
res

template volatileStore*[T](dest: ptr T, val: T) =
Expand All @@ -33,4 +33,4 @@ template volatileStore*[T](dest: ptr T, val: T) =
when defined(js):
dest[] = val
else:
{.emit: ["*((", type(dest[]), " volatile*)(", dest, ")) = ", val, ";"].}
{.emit: ["*((", typeof(dest[]), " volatile*)(", dest, ")) = ", val, ";"].}
4 changes: 2 additions & 2 deletions lib/std/jsonutils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ runnableExamples:
z1: int8
let a = (1.5'f32, (b: "b2", a: "a2"), 'x', @[Foo(t: true, z1: -3), nil], [{"name": "John"}.newStringTable])
let j = a.toJson
doAssert j.jsonTo(type(a)).toJson == j
doAssert j.jsonTo(typeof(a)).toJson == j

import std/[json,strutils,tables,sets,strtabs,options]

Expand Down Expand Up @@ -42,7 +42,7 @@ type

proc isNamedTuple(T: typedesc): bool {.magic: "TypeTrait".}
proc distinctBase(T: typedesc): typedesc {.magic: "TypeTrait".}
template distinctBase[T](a: T): untyped = distinctBase(type(a))(a)
template distinctBase[T](a: T): untyped = distinctBase(typeof(a))(a)

macro getDiscriminants(a: typedesc): seq[string] =
## return the discriminant keys
Expand Down
8 changes: 4 additions & 4 deletions lib/std/wrapnils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ template `.`*(a: Wrapnil, b): untyped =
## See top-level example.
let a1 = a # to avoid double evaluations
let a2 = a1.valueImpl
type T = Wrapnil[type(a2.b)]
type T = Wrapnil[typeof(a2.b)]
if a1.validImpl:
when type(a2) is ref|ptr:
when typeof(a2) is ref|ptr:
if a2 == nil:
default(T)
else:
Expand All @@ -72,13 +72,13 @@ template `[]`*[I](a: Wrapnil, i: I): untyped =
# correctly will raise IndexDefect if a is valid but wraps an empty container
wrapnil(a1.valueImpl[i])
else:
default(Wrapnil[type(a1.valueImpl[i])])
default(Wrapnil[typeof(a1.valueImpl[i])])

template `[]`*(a: Wrapnil): untyped =
## See top-level example.
let a1 = a # to avoid double evaluations
let a2 = a1.valueImpl
type T = Wrapnil[type(a2[])]
type T = Wrapnil[typeof(a2[])]
if a1.validImpl:
if a2 == nil:
default(T)
Expand Down
Loading

0 comments on commit d447c0f

Please sign in to comment.