fixes #9498, typeof is for everybody

This commit is contained in:
Araq
2018-10-25 13:04:51 +02:00
parent 8fbdac544b
commit c844a9169c
2 changed files with 11 additions and 6 deletions

View File

@@ -201,7 +201,7 @@ proc semConv(c: PContext, n: PNode): PNode =
if targetType.kind == tyTypeDesc:
internalAssert c.config, targetType.len > 0
if targetType.base.kind == tyNone:
return semTypeOf(c, n[1])
return semTypeOf(c, n)
else:
targetType = targetType.base
elif targetType.kind == tyStatic:
@@ -1963,8 +1963,7 @@ proc semMagic(c: PContext, n: PNode, s: PSym, flags: TExprFlags): PNode =
checkSonsLen(n, 2, c.config)
result = semAddr(c, n.sons[1], s.name.s == "unsafeAddr")
of mTypeOf:
checkSonsLen(n, 2, c.config)
result = semTypeOf(c, n.sons[1])
result = semTypeOf(c, n)
#of mArrGet: result = semArrGet(c, n, flags)
#of mArrPut: result = semArrPut(c, n, flags)
#of mAsgn: result = semAsgnOpr(c, n)

View File

@@ -21,8 +21,15 @@ proc semAddr(c: PContext; n: PNode; isUnsafeAddr=false): PNode =
result.typ = makePtrType(c, x.typ)
proc semTypeOf(c: PContext; n: PNode): PNode =
var m = BiggestInt 1 # typeOfIter
if n.len == 3:
let mode = semConstExpr(c, n[2])
if mode.kind != nkIntLit:
localError(c.config, n.info, "typeof: cannot evaluate 'mode' parameter at compile-time")
else:
m = mode.intVal
result = newNodeI(nkTypeOfExpr, n.info)
let typExpr = semExprWithType(c, n, {efInTypeof})
let typExpr = semExprWithType(c, n[1], if m == 1: {efInTypeof} else: {})
result.add typExpr
result.typ = makeTypeDesc(c, typExpr.typ)
@@ -320,8 +327,7 @@ proc magicsAfterOverloadResolution(c: PContext, n: PNode,
checkSonsLen(n, 2, c.config)
result = semAddr(c, n.sons[1], n[0].sym.name.s == "unsafeAddr")
of mTypeOf:
checkSonsLen(n, 2, c.config)
result = semTypeOf(c, n.sons[1])
result = semTypeOf(c, n)
of mSizeOf:
# TODO there is no proper way to find out if a type cannot be queried for the size.
let size = getSize(c.config, n[1].typ)