don't warn/error symbols in semGenericStmt/templates (#24907)

fixes #24905
fixes #24903
fixes https://github.com/nim-lang/Nim/issues/11805
fixes https://github.com/nim-lang/Nim/issues/15650

In the first phase of generic checking, we cannot warn/error symbols
because they can belong a false branch of `when` or there is a
`push/pop` options using open symbols. So we cannot decide whether to
warn/error or not

(cherry picked from commit 0506d5b973)
This commit is contained in:
ringabout
2025-04-29 17:08:10 +08:00
committed by narimiran
parent b67f7fab64
commit 0bdc4434e0
5 changed files with 47 additions and 10 deletions

View File

@@ -113,6 +113,8 @@ proc semExprNoDeref(c: PContext, n: PNode, flags: TExprFlags = {}): PNode =
proc semSymGenericInstantiation(c: PContext, n: PNode, s: PSym): PNode =
result = symChoice(c, n, s, scClosed)
if result.kind == nkSym:
markUsed(c, n.info, s)
proc semSym(c: PContext, n: PNode, sym: PSym, flags: TExprFlags): PNode
@@ -3288,6 +3290,7 @@ proc semExpr(c: PContext, n: PNode, flags: TExprFlags = {}, expectedType: PType
#performProcvarCheck(c, n, s)
result = symChoice(c, n, s, scClosed)
if result.kind == nkSym:
markUsed(c, n.info, s)
markIndirect(c, result.sym)
# if isGenericRoutine(result.sym):
# localError(c.config, n.info, errInstantiateXExplicitly, s.name.s)

View File

@@ -274,7 +274,8 @@ proc semGenericStmt(c: PContext, n: PNode,
result = lookup(c, n, flags, ctx)
if result != nil and result.kind == nkSym:
assert result.sym != nil
markUsed(c, n.info, result.sym)
incl result.sym.flags, sfUsed
markOwnerModuleAsUsed(c, result.sym)
of nkDotExpr:
#let luf = if withinMixin notin flags: {checkUndeclared} else: {}
#var s = qualifiedLookUp(c, n, luf)

View File

@@ -67,12 +67,9 @@ proc symChoice(c: PContext, n: PNode, s: PSym, r: TSymChoiceRule;
# for instance 'nextTry' is both in tables.nim and astalgo.nim ...
if not isField or sfGenSym notin s.flags:
result = newSymNode(s, info)
if isField:
# possibly not final field sym
incl(s.flags, sfUsed)
markOwnerModuleAsUsed(c, s)
else:
markUsed(c, info, s)
# possibly not final field sym
incl(s.flags, sfUsed)
markOwnerModuleAsUsed(c, s)
onUse(info, s)
else:
result = n

View File

@@ -6,9 +6,6 @@ discard """
$nimsuggest --tester $file
>highlight $1
highlight;;skProc;;1;;7;;4
highlight;;skProc;;1;;7;;4
highlight;;skTemplate;;2;;7;;4
highlight;;skTemplate;;2;;7;;4
highlight;;skTemplate;;2;;7;;4
highlight;;skFunc;;3;;8;;1
"""

View File

@@ -0,0 +1,39 @@
discard """
matrix: "--warningAsError:Deprecated"
"""
block: # bug #24905
proc y() {.deprecated.} = discard
proc v(_: int | int) =
{.push warning[Deprecated]: off.}
y()
{.pop.}
v(1)
block: # bug #24903
block:
proc y() {.deprecated.} = discard
proc m(_: int | int) =
when false: y()
block:
proc y() {.error.} = discard
proc m(_: int | int) =
when false: y()
block:
proc y() {.error.} = discard
proc m(_: int | int) =
when true: y()
block: # bug #15650
proc bar() {.deprecated.} = discard
template foo() =
when false:
bar()
else:
discard
foo()