Less warnings in macros (#10799)

This commit is contained in:
Arne Döring 2019-03-11 19:21:39 +01:00 • committed by Andreas Rumpf
commit 35aab357e2
3 changed files with 34 additions and 24 deletions

View file

@ -540,10 +540,6 @@ proc markUsed(conf: ConfigRef; info: TLineInfo; s: PSym; usageSym: var PSym) =
when defined(nimsuggest): when defined(nimsuggest):
suggestSym(conf, info, s, usageSym, false) suggestSym(conf, info, s, usageSym, false)
proc useSym*(conf: ConfigRef; sym: PSym; usageSym: var PSym): PNode =
result = newSymNode(sym)
markUsed(conf, result.info, sym, usageSym)
proc safeSemExpr*(c: PContext, n: PNode): PNode = proc safeSemExpr*(c: PContext, n: PNode): PNode =
# use only for idetools support! # use only for idetools support!
try: try:

View file

@ -8,7 +8,6 @@
# #
include "system/inclrtl" include "system/inclrtl"
include "system/helpers"
## This module contains the interface to the compiler's abstract syntax ## This module contains the interface to the compiler's abstract syntax
## tree (`AST`:idx:). Macros operate on this tree. ## tree (`AST`:idx:). Macros operate on this tree.
@ -121,7 +120,7 @@ type
TNimSymKinds* {.deprecated.} = set[NimSymKind] TNimSymKinds* {.deprecated.} = set[NimSymKind]
type type
NimIdent* = object of RootObj NimIdent* {.deprecated.} = object of RootObj
## represents a Nim identifier in the AST. **Note**: This is only ## represents a Nim identifier in the AST. **Note**: This is only
## rarely useful, for identifier construction from a string ## rarely useful, for identifier construction from a string
## use ``ident"abc"``. ## use ``ident"abc"``.
@ -138,6 +137,8 @@ const
nnkCallStrLit} nnkCallStrLit}
nnkPragmaCallKinds = {nnkExprColonExpr, nnkCall, nnkCallStrLit} nnkPragmaCallKinds = {nnkExprColonExpr, nnkCall, nnkCallStrLit}
{.push warnings: off.}
proc `!`*(s: string): NimIdent {.magic: "StrToIdent", noSideEffect, deprecated.} proc `!`*(s: string): NimIdent {.magic: "StrToIdent", noSideEffect, deprecated.}
## constructs an identifier from the string `s` ## constructs an identifier from the string `s`
## **Deprecated since version 0.18.0**: Use ``ident`` or ``newIdentNode`` instead. ## **Deprecated since version 0.18.0**: Use ``ident`` or ``newIdentNode`` instead.
@ -157,6 +158,7 @@ proc `==`*(a, b: NimSym): bool {.magic: "EqNimrodNode", noSideEffect, deprecated
## compares two Nim symbols ## compares two Nim symbols
## **Deprecated since version 0.18.1**; Use ``==(NimNode, NimNode)`` instead. ## **Deprecated since version 0.18.1**; Use ``==(NimNode, NimNode)`` instead.
{.pop.}
proc sameType*(a, b: NimNode): bool {.magic: "SameNodeType", noSideEffect.} = proc sameType*(a, b: NimNode): bool {.magic: "SameNodeType", noSideEffect.} =
## compares two Nim nodes' types. Return true if the types are the same, ## compares two Nim nodes' types. Return true if the types are the same,
@ -227,6 +229,8 @@ proc intVal*(n: NimNode): BiggestInt {.magic: "NIntVal", noSideEffect.}
proc floatVal*(n: NimNode): BiggestFloat {.magic: "NFloatVal", noSideEffect.} proc floatVal*(n: NimNode): BiggestFloat {.magic: "NFloatVal", noSideEffect.}
{.push warnings: off.}
proc ident*(n: NimNode): NimIdent {.magic: "NIdent", noSideEffect, deprecated.} = proc ident*(n: NimNode): NimIdent {.magic: "NIdent", noSideEffect, deprecated.} =
## **Deprecated since version 0.18.1**; All functionality is defined on ``NimNode``. ## **Deprecated since version 0.18.1**; All functionality is defined on ``NimNode``.
@ -268,6 +272,8 @@ else: # bootstrapping substitute
else: else:
n.strValOld n.strValOld
{.pop.}
when defined(nimSymImplTransform): when defined(nimSymImplTransform):
proc getImplTransformed*(symbol: NimNode): NimNode {.magic: "GetImplTransf", noSideEffect.} proc getImplTransformed*(symbol: NimNode): NimNode {.magic: "GetImplTransf", noSideEffect.}
## for a typed proc returns the AST after transformation pass ## for a typed proc returns the AST after transformation pass
@ -360,12 +366,16 @@ proc getTypeImpl*(n: typedesc): NimNode {.magic: "NGetType", noSideEffect.}
proc `intVal=`*(n: NimNode, val: BiggestInt) {.magic: "NSetIntVal", noSideEffect.} proc `intVal=`*(n: NimNode, val: BiggestInt) {.magic: "NSetIntVal", noSideEffect.}
proc `floatVal=`*(n: NimNode, val: BiggestFloat) {.magic: "NSetFloatVal", noSideEffect.} proc `floatVal=`*(n: NimNode, val: BiggestFloat) {.magic: "NSetFloatVal", noSideEffect.}
{.push warnings: off.}
proc `symbol=`*(n: NimNode, val: NimSym) {.magic: "NSetSymbol", noSideEffect, deprecated.} proc `symbol=`*(n: NimNode, val: NimSym) {.magic: "NSetSymbol", noSideEffect, deprecated.}
## **Deprecated since version 0.18.1**; Generate a new ``NimNode`` with ``genSym`` instead. ## **Deprecated since version 0.18.1**; Generate a new ``NimNode`` with ``genSym`` instead.
proc `ident=`*(n: NimNode, val: NimIdent) {.magic: "NSetIdent", noSideEffect, deprecated.} proc `ident=`*(n: NimNode, val: NimIdent) {.magic: "NSetIdent", noSideEffect, deprecated.}
## **Deprecated since version 0.18.1**; Generate a new ``NimNode`` with ``ident(string)`` instead. ## **Deprecated since version 0.18.1**; Generate a new ``NimNode`` with ``ident(string)`` instead.
{.pop.}
#proc `typ=`*(n: NimNode, typ: typedesc) {.magic: "NSetType".} #proc `typ=`*(n: NimNode, typ: typedesc) {.magic: "NSetType".}
# this is not sound! Unfortunately forbidding 'typ=' is not enough, as you # this is not sound! Unfortunately forbidding 'typ=' is not enough, as you
# can easily do: # can easily do:
@ -418,15 +428,20 @@ proc newFloatLitNode*(f: BiggestFloat): NimNode {.compileTime.} =
result = newNimNode(nnkFloatLit) result = newNimNode(nnkFloatLit)
result.floatVal = f result.floatVal = f
proc newIdentNode*(i: NimIdent): NimNode {.compileTime.} = {.push warnings: off.}
proc newIdentNode*(i: NimIdent): NimNode {.compileTime, deprecated.} =
## creates an identifier node from `i` ## creates an identifier node from `i`
result = newNimNode(nnkIdent) result = newNimNode(nnkIdent)
result.ident = i result.ident = i
{.pop.}
proc newIdentNode*(i: string): NimNode {.magic: "StrToIdent", noSideEffect.} proc newIdentNode*(i: string): NimNode {.magic: "StrToIdent", noSideEffect.}
## creates an identifier node from `i`. It is simply an alias for ## creates an identifier node from `i`. It is simply an alias for
## ``ident(string)``. Use that, it's shorter. ## ``ident(string)``. Use that, it's shorter.
type type
BindSymRule* = enum ## specifies how ``bindSym`` behaves BindSymRule* = enum ## specifies how ``bindSym`` behaves
brClosed, ## only the symbols in current scope are bound brClosed, ## only the symbols in current scope are bound
@ -478,7 +493,7 @@ type
proc `$`*(arg: Lineinfo): string = proc `$`*(arg: Lineinfo): string =
# BUG: without `result = `, gives compile error # BUG: without `result = `, gives compile error
result = lineInfoToString(arg.filename, arg.line, arg.column) result = arg.filename & "(" & $arg.line & ", " & $arg.column & ")"
#proc lineinfo*(n: NimNode): LineInfo {.magic: "NLineInfo", noSideEffect.} #proc lineinfo*(n: NimNode): LineInfo {.magic: "NLineInfo", noSideEffect.}
## returns the position the node appears in the original source file ## returns the position the node appears in the original source file
@ -603,6 +618,8 @@ proc newCall*(theProc: NimNode,
result.add(theProc) result.add(theProc)
result.add(args) result.add(args)
{.push warnings: off.}
proc newCall*(theProc: NimIdent, proc newCall*(theProc: NimIdent,
args: varargs[NimNode]): NimNode {.compileTime, deprecated.} = args: varargs[NimNode]): NimNode {.compileTime, deprecated.} =
## produces a new call node. `theProc` is the proc that is called with ## produces a new call node. `theProc` is the proc that is called with
@ -613,6 +630,8 @@ proc newCall*(theProc: NimIdent,
result.add(newIdentNode(theProc)) result.add(newIdentNode(theProc))
result.add(args) result.add(args)
{.pop.}
proc newCall*(theProc: string, proc newCall*(theProc: string,
args: varargs[NimNode]): NimNode {.compileTime.} = args: varargs[NimNode]): NimNode {.compileTime.} =
## produces a new call node. `theProc` is the proc that is called with ## produces a new call node. `theProc` is the proc that is called with
@ -770,12 +789,13 @@ proc nestList*(op: NimNode; pack: NimNode; init: NimNode): NimNode {.compileTime
for i in countdown(pack.len - 1, 0): for i in countdown(pack.len - 1, 0):
result = newCall(op, pack[i], result) result = newCall(op, pack[i], result)
{.push warnings: off.}
proc nestList*(theProc: NimIdent, x: NimNode): NimNode {.compileTime, deprecated.} = proc nestList*(theProc: NimIdent, x: NimNode): NimNode {.compileTime, deprecated.} =
## **Deprecated since version 0.18.1**; Use one of ``nestList(NimNode, ...)`` instead. ## **Deprecated since version 0.18.1**; Use one of ``nestList(NimNode, ...)`` instead.
var L = x.len nestList(newIdentNode(theProc), x)
result = newCall(theProc, x[L-2], x[L-1])
for i in countdown(L-3, 0): {.pop.}
result = newCall(theProc, x[i], result)
proc treeTraverse(n: NimNode; res: var string; level = 0; isLisp = false, indented = false) {.benign.} = proc treeTraverse(n: NimNode; res: var string; level = 0; isLisp = false, indented = false) {.benign.} =
if level > 0: if level > 0:
@ -1276,8 +1296,10 @@ proc basename*(a: NimNode): NimNode =
proc `basename=`*(a: NimNode; val: string) {.compileTime.}= proc `basename=`*(a: NimNode; val: string) {.compileTime.}=
case a.kind case a.kind
of nnkIdent: macros.`ident=`(a, toNimIdent val) of nnkIdent:
of nnkPostfix, nnkPrefix: a[1] = ident(val) a.strVal = val
of nnkPostfix, nnkPrefix:
a[1] = ident(val)
else: else:
quit "Do not know how to get basename of (" & treeRepr(a) & ")\n" & repr(a) quit "Do not know how to get basename of (" & treeRepr(a) & ")\n" & repr(a)

View file

@ -1,17 +1,9 @@
# helpers used system.nim and other modules, avoids code duplication while # TODO: remove this file. It causes "declared but not used" warnings everywhere.
# also minimizing symbols exposed in system.nim
#
# TODO: move other things here that should not be exposed in system.nim
proc lineInfoToString(file: string, line, column: int): string =
file & "(" & $line & ", " & $column & ")"
type InstantiationInfo = tuple[filename: string, line: int, column: int] type InstantiationInfo = tuple[filename: string, line: int, column: int]
proc `$`(info: InstantiationInfo): string = proc `$`(info: InstantiationInfo): string =
# The +1 is needed here info.fileName & "(" & $info.line & ", " & $(info.column+1) & ")"
# instead of overriding `$` (and changing its meaning), consider explicit name.
lineInfoToString(info.fileName, info.line, info.column+1)
proc isNamedTuple(T: type): bool = proc isNamedTuple(T: type): bool =
## return true for named tuples, false for any other type. ## return true for named tuples, false for any other type.