the .deprecated pragma for procs now supports a user-definable deprecation message

This commit is contained in:
Andreas Rumpf
2018-02-02 12:53:38 +01:00
parent 1b22a3b346
commit 212457f5e0
5 changed files with 27 additions and 4 deletions

View File

@@ -238,3 +238,12 @@ styledEcho "Red on Green.", resetStyle
- Type inference for generic type parameters involving numeric types is now symetric. See
[Generic type inference for numeric types](https://nim-lang.org/docs/manual.html#generics-generic-type-inference-fornumeric-types)
for more information.
- The ``deprecated`` pragma now supports a user-definable warning message for procs.
```nim
proc bar {.deprecated: "use foo instead".} =
return
bar()
```

View File

@@ -810,7 +810,10 @@ proc singlePragma(c: PContext, sym: PSym, n: PNode, i: int,
of wExplain:
sym.flags.incl sfExplain
of wDeprecated:
if it.kind in nkPragmaCallKinds: deprecatedStmt(c, it)
if sym != nil and sym.kind in routineKinds:
if it.kind in nkPragmaCallKinds: discard getStrLitNode(c, it)
incl(sym.flags, sfDeprecated)
elif it.kind in nkPragmaCallKinds: deprecatedStmt(c, it)
elif sym != nil: incl(sym.flags, sfDeprecated)
else: incl(c.module.flags, sfDeprecated)
of wVarargs:

View File

@@ -33,6 +33,7 @@
# included from sigmatch.nim
import algorithm, prefixmatches
from wordrecg import wDeprecated
when defined(nimsuggest):
import passes, tables # importer
@@ -479,12 +480,23 @@ proc suggestSym*(info: TLineInfo; s: PSym; usageSym: var PSym; isDecl=true) {.in
isDecl:
suggestResult(symToSuggest(s, isLocal=false, ideOutline, info, 100, PrefixMatch.None, false, 0))
proc warnAboutDeprecated(info: TLineInfo; s: PSym) =
if s.kind in routineKinds:
let n = s.ast[pragmasPos]
if n.kind != nkEmpty:
for it in n:
if whichPragma(it) == wDeprecated and it.safeLen == 2 and
it[1].kind in {nkStrLit..nkTripleStrLit}:
message(info, warnDeprecated, it[1].strVal & "; " & s.name.s)
return
message(info, warnDeprecated, s.name.s)
proc markUsed(info: TLineInfo; s: PSym; usageSym: var PSym) =
incl(s.flags, sfUsed)
if s.kind == skEnumField and s.owner != nil:
incl(s.owner.flags, sfUsed)
if {sfDeprecated, sfError} * s.flags != {}:
if sfDeprecated in s.flags: message(info, warnDeprecated, s.name.s)
if sfDeprecated in s.flags: warnAboutDeprecated(info, s)
if sfError in s.flags: localError(info, errWrongSymbolX, s.name.s)
when defined(nimsuggest):
suggestSym(info, s, usageSym, false)

View File

@@ -118,7 +118,7 @@ proc isRange*(n: PNode): bool {.inline.} =
result = true
proc whichPragma*(n: PNode): TSpecialWord =
let key = if n.kind == nkExprColonExpr: n.sons[0] else: n
let key = if n.kind in nkPragmaCallKinds and n.len > 0: n.sons[0] else: n
if key.kind == nkIdent: result = whichKeyword(key.ident)
proc unnestStmts(n, result: PNode) =

View File

@@ -3769,7 +3769,6 @@ proc failedAssertImpl*(msg: string) {.raises: [], tags: [].} =
# by ``assert``.
type Hide = proc (msg: string) {.noinline, raises: [], noSideEffect,
tags: [].}
{.deprecated: [THide: Hide].}
Hide(raiseAssert)(msg)
template assert*(cond: bool, msg = "") =