fixes #25732; semStaticExpr and semStaticStmt to handle errors (#25742)

fix #25732
This commit is contained in:
ringabout
2026-04-17 16:00:00 +08:00
committed by GitHub
parent 2b2872928b
commit c22819ef17
3 changed files with 22 additions and 2 deletions

View File

@@ -963,12 +963,15 @@ proc evalAtCompileTime(c: PContext, n: PNode): PNode =
# echo "SUCCESS evaluated at compile time: ", call.renderTree
proc semStaticExpr(c: PContext, n: PNode; expectedType: PType = nil): PNode =
let oldErrorCount = c.config.errorCounter
inc c.inStaticContext
openScope(c)
let a = semExprWithType(c, n, expectedType = expectedType)
closeScope(c)
dec c.inStaticContext
if a.findUnresolvedStatic != nil: return a
if a.findUnresolvedStatic != nil or
c.config.errorCounter != oldErrorCount:
return a
result = evalStaticExpr(c.module, c.idgen, c.graph, a, c.p.owner)
if result.isNil:
localError(c.config, n.info, errCannotInterpretNodeX % renderTree(n))

View File

@@ -2919,13 +2919,15 @@ proc semPragmaBlock(c: PContext, n: PNode; expectedType: PType = nil): PNode =
proc semStaticStmt(c: PContext, n: PNode): PNode =
#echo "semStaticStmt"
#writeStackTrace()
let oldErrorCount = c.config.errorCounter
inc c.inStaticContext
openScope(c)
let a = semStmt(c, n[0], {})
closeScope(c)
dec c.inStaticContext
n[0] = a
evalStaticStmt(c.module, c.idgen, c.graph, a, c.p.owner)
if c.config.errorCounter == oldErrorCount:
evalStaticStmt(c.module, c.idgen, c.graph, a, c.p.owner)
when false:
# for incremental replays, keep the AST as required for replays:
result = n

15
tests/errmsgs/t25732.nim Normal file
View File

@@ -0,0 +1,15 @@
discard """
cmd: "nim check --hints:off $file"
action: "reject"
nimout: '''
t25732.nim(15, 32) Error: undeclared identifier: 'a'
t25732.nim(15, 32) Error: expression 'a' has no type (or is ambiguous)
t25732.nim(15, 33) Error: undeclared field: 'b'
t25732.nim(15, 33) Error: undeclared field: '.'
t25732.nim(15, 33) Error: undeclared field: '.'
'''
"""
static: (for f in [0]: discard a.b == f)