fixes #20811; Nested proc with inner being generic cannot access parameters of outer proc

This commit is contained in:
ringabout
2026-05-25 19:22:38 +08:00
parent 8771451701
commit 7585bec361
3 changed files with 37 additions and 1 deletions

View File

@@ -129,7 +129,14 @@ proc semGenericStmtSymbol(c: PContext, n: PNode, s: PSym,
result.typ = nil
onUse(n.info, s)
of skParam:
result = n
if s.owner == c.p.owner or true:
# Parameters of the routine currently being semchecked stay as local
# identifiers
result = n
else:
# Preserve captured outer parameters so nested generic procs can still
# see them after the generic pre-pass.
result = newSymNode(s, n.info)
onUse(n.info, s)
of skType:
if (s.typ != nil) and

16
tests/generics/t20811.nim Normal file
View File

@@ -0,0 +1,16 @@
discard """
output: '''42
42'''
"""
proc outer(j: int) =
proc genericInner[T](): int =
j
proc plainInner(): int =
j
echo genericInner[int]()
echo plainInner()
outer(42)

View File

@@ -135,6 +135,19 @@ block: # issue #22605 for templates, original complex example
doAssert g2(int) == "error"
block: # issue #20811
template injectError(body: untyped): untyped =
template error: untyped {.used, inject.} = "injected"
body
proc outerOpen(error: string): string =
injectError:
proc genericInner[T](): string =
error
genericInner[int]()
doAssert outerOpen("captured") == "injected"
block: # issue #23865 for templates
type Xxx = enum
error