Deprecated pragma is now supported on enum fields (#10113)

* {.deprecated.} pragma is now supported for enum fields
* Add tests
* Simplify code
This commit is contained in:
Neelesh Chandola 2018-12-30 14:13:59 +05:30 • committed by Andreas Rumpf
commit c5ad4c10cb
5 changed files with 63 additions and 12 deletions

View file

@ -1765,7 +1765,7 @@ proc parseSection(p: var TParser, kind: TNodeKind,
parMessage(p, errIdentifierExpected, p.tok) parMessage(p, errIdentifierExpected, p.tok)
proc parseEnum(p: var TParser): PNode = proc parseEnum(p: var TParser): PNode =
#| enum = 'enum' optInd (symbol optInd ('=' optInd expr COMMENT?)? comma?)+ #| enum = 'enum' optInd (symbol optPragmas optInd ('=' optInd expr COMMENT?)? comma?)+
result = newNodeP(nkEnumTy, p) result = newNodeP(nkEnumTy, p)
getTok(p) getTok(p)
addSon(result, p.emptyNode) addSon(result, p.emptyNode)
@ -1775,25 +1775,35 @@ proc parseEnum(p: var TParser): PNode =
while true: while true:
var a = parseSymbol(p) var a = parseSymbol(p)
if a.kind == nkEmpty: return if a.kind == nkEmpty: return
var symPragma = a
var pragma: PNode
if p.tok.tokType == tkCurlyDotLe:
pragma = optPragmas(p)
symPragma = newNodeP(nkPragmaExpr, p)
addSon(symPragma, a)
addSon(symPragma, pragma)
if p.tok.indent >= 0 and p.tok.indent <= p.currInd: if p.tok.indent >= 0 and p.tok.indent <= p.currInd:
add(result, a) add(result, symPragma)
break break
if p.tok.tokType == tkEquals and p.tok.indent < 0: if p.tok.tokType == tkEquals and p.tok.indent < 0:
getTok(p) getTok(p)
optInd(p, a) optInd(p, symPragma)
var b = a var b = symPragma
a = newNodeP(nkEnumFieldDef, p) symPragma = newNodeP(nkEnumFieldDef, p)
addSon(a, b) addSon(symPragma, b)
addSon(a, parseExpr(p)) addSon(symPragma, parseExpr(p))
if p.tok.indent < 0 or p.tok.indent >= p.currInd: if p.tok.indent < 0 or p.tok.indent >= p.currInd:
rawSkipComment(p, a) rawSkipComment(p, symPragma)
if p.tok.tokType == tkComma and p.tok.indent < 0: if p.tok.tokType == tkComma and p.tok.indent < 0:
getTok(p) getTok(p)
rawSkipComment(p, a) rawSkipComment(p, symPragma)
else: else:
if p.tok.indent < 0 or p.tok.indent >= p.currInd: if p.tok.indent < 0 or p.tok.indent >= p.currInd:
rawSkipComment(p, a) rawSkipComment(p, symPragma)
addSon(result, a) addSon(result, symPragma)
if p.tok.indent >= 0 and p.tok.indent <= p.currInd or if p.tok.indent >= 0 and p.tok.indent <= p.currInd or
p.tok.tokType == tkEof: p.tok.tokType == tkEof:
break break

View file

@ -73,6 +73,7 @@ const
wThread, wRaises, wLocks, wTags, wGcSafe} wThread, wRaises, wLocks, wTags, wGcSafe}
forVarPragmas* = {wInject, wGensym} forVarPragmas* = {wInject, wGensym}
allRoutinePragmas* = methodPragmas + iteratorPragmas + lambdaPragmas allRoutinePragmas* = methodPragmas + iteratorPragmas + lambdaPragmas
enumFieldPragmas* = {wDeprecated}
proc getPragmaVal*(procAst: PNode; name: TSpecialWord): PNode = proc getPragmaVal*(procAst: PNode; name: TSpecialWord): PNode =
let p = procAst[pragmasPos] let p = procAst[pragmasPos]

View file

@ -80,8 +80,13 @@ proc semEnum(c: PContext, n: PNode, prev: PType): PType =
if isPure: initStrTable(symbols) if isPure: initStrTable(symbols)
var hasNull = false var hasNull = false
for i in countup(1, sonsLen(n) - 1): for i in countup(1, sonsLen(n) - 1):
if n.sons[i].kind == nkEmpty: continue
case n.sons[i].kind case n.sons[i].kind
of nkEnumFieldDef: of nkEnumFieldDef:
if n.sons[i].sons[0].kind == nkPragmaExpr:
e = newSymS(skEnumField, n.sons[i].sons[0].sons[0], c)
pragma(c, e, n.sons[i].sons[0].sons[1], enumFieldPragmas)
else:
e = newSymS(skEnumField, n.sons[i].sons[0], c) e = newSymS(skEnumField, n.sons[i].sons[0], c)
var v = semConstExpr(c, n.sons[i].sons[1]) var v = semConstExpr(c, n.sons[i].sons[1])
var strVal: PNode = nil var strVal: PNode = nil
@ -115,6 +120,9 @@ proc semEnum(c: PContext, n: PNode, prev: PType): PType =
e = n.sons[i].sym e = n.sons[i].sym
of nkIdent, nkAccQuoted: of nkIdent, nkAccQuoted:
e = newSymS(skEnumField, n.sons[i], c) e = newSymS(skEnumField, n.sons[i], c)
of nkPragmaExpr:
e = newSymS(skEnumField, n.sons[i].sons[0], c)
pragma(c, e, n.sons[i].sons[1], enumFieldPragmas)
else: else:
illFormedAst(n[i], c.config) illFormedAst(n[i], c.config)
e.typ = result e.typ = result

View file

@ -0,0 +1,22 @@
discard """
nimout: '''tenumfieldpragma.nim(20, 10) Warning: d is deprecated [Deprecated]
tenumfieldpragma.nim(21, 10) Warning: e is deprecated [Deprecated]
tenumfieldpragma.nim(22, 10) Warning: f is deprecated [Deprecated]
'''
"""
type
A = enum
a
b = "abc"
c = (10, "def")
d {.deprecated.}
e {.deprecated.} = "ghi"
f {.deprecated.} = (20, "jkl")
var v1 = a
var v2 = b
var v3 = c
var v4 = d
var v5 = e
var v6 = f

View file

@ -0,0 +1,10 @@
discard """
errormsg: "annotation to deprecated not supported here"
line: 8
"""
type
A = enum
a {.deprecated: "njshd".}
var v1 = a