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
parent a6633b9658
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)
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)
getTok(p)
addSon(result, p.emptyNode)
@@ -1775,25 +1775,35 @@ proc parseEnum(p: var TParser): PNode =
while true:
var a = parseSymbol(p)
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:
add(result, a)
add(result, symPragma)
break
if p.tok.tokType == tkEquals and p.tok.indent < 0:
getTok(p)
optInd(p, a)
var b = a
a = newNodeP(nkEnumFieldDef, p)
addSon(a, b)
addSon(a, parseExpr(p))
optInd(p, symPragma)
var b = symPragma
symPragma = newNodeP(nkEnumFieldDef, p)
addSon(symPragma, b)
addSon(symPragma, parseExpr(p))
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:
getTok(p)
rawSkipComment(p, a)
rawSkipComment(p, symPragma)
else:
if p.tok.indent < 0 or p.tok.indent >= p.currInd:
rawSkipComment(p, a)
addSon(result, a)
rawSkipComment(p, symPragma)
addSon(result, symPragma)
if p.tok.indent >= 0 and p.tok.indent <= p.currInd or
p.tok.tokType == tkEof:
break

View File

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

View File

@@ -80,9 +80,14 @@ proc semEnum(c: PContext, n: PNode, prev: PType): PType =
if isPure: initStrTable(symbols)
var hasNull = false
for i in countup(1, sonsLen(n) - 1):
if n.sons[i].kind == nkEmpty: continue
case n.sons[i].kind
of nkEnumFieldDef:
e = newSymS(skEnumField, n.sons[i].sons[0], c)
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)
var v = semConstExpr(c, n.sons[i].sons[1])
var strVal: PNode = nil
case skipTypes(v.typ, abstractInst-{tyTypeDesc}).kind
@@ -115,6 +120,9 @@ proc semEnum(c: PContext, n: PNode, prev: PType): PType =
e = n.sons[i].sym
of nkIdent, nkAccQuoted:
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:
illFormedAst(n[i], c.config)
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