Fixed template const tuple unpacking (#18562)

This commit is contained in:
Jason Beetham 2021-07-23 00:46:13 -06:00 • committed by GitHub
commit 5386ae75ba
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 22 deletions

View file

@ -209,7 +209,7 @@ proc addLocalDecl(c: var TemplCtx, n: var PNode, k: TSymKind) =
let pragmaNode = n[1] let pragmaNode = n[1]
for i in 0..<pragmaNode.len: for i in 0..<pragmaNode.len:
openScope(c) openScope(c)
pragmaNode[i] = semTemplBody(c,pragmaNode[i]) pragmaNode[i] = semTemplBody(c, pragmaNode[i])
closeScope(c) closeScope(c)
let ident = getIdentNode(c, n) let ident = getIdentNode(c, n)
if not isTemplParam(c, ident): if not isTemplParam(c, ident):
@ -306,20 +306,24 @@ proc semRoutineInTemplBody(c: var TemplCtx, n: PNode, k: TSymKind): PNode =
# close scope for parameters # close scope for parameters
closeScope(c) closeScope(c)
proc semTemplSomeDecl(c: var TemplCtx, n: PNode, symKind: TSymKind; start=0) = proc semTemplSomeDecl(c: var TemplCtx, n: PNode, symKind: TSymKind; start = 0) =
for i in start..<n.len: for i in start..<n.len:
var a = n[i] var a = n[i]
if a.kind == nkCommentStmt: continue case a.kind:
if (a.kind != nkIdentDefs) and (a.kind != nkVarTuple): illFormedAst(a, c.c.config) of nkCommentStmt: continue
checkMinSonsLen(a, 3, c.c.config) of nkIdentDefs, nkVarTuple, nkConstDef:
when defined(nimsuggest): checkMinSonsLen(a, 3, c.c.config)
inc c.c.inTypeContext when defined(nimsuggest):
a[^2] = semTemplBody(c, a[^2]) inc c.c.inTypeContext
when defined(nimsuggest): a[^2] = semTemplBody(c, a[^2])
dec c.c.inTypeContext when defined(nimsuggest):
a[^1] = semTemplBody(c, a[^1]) dec c.c.inTypeContext
for j in 0..<a.len-2: a[^1] = semTemplBody(c, a[^1])
addLocalDecl(c, a[j], symKind) for j in 0..<a.len-2:
addLocalDecl(c, a[j], symKind)
else:
illFormedAst(a, c.c.config)
proc semPattern(c: PContext, n: PNode): PNode proc semPattern(c: PContext, n: PNode): PNode
@ -434,15 +438,7 @@ proc semTemplBody(c: var TemplCtx, n: PNode): PNode =
checkMinSonsLen(n, 1, c.c.config) checkMinSonsLen(n, 1, c.c.config)
semTemplSomeDecl(c, n, skParam, 1) semTemplSomeDecl(c, n, skParam, 1)
n[0] = semTemplBody(c, n[0]) n[0] = semTemplBody(c, n[0])
of nkConstSection: of nkConstSection: semTemplSomeDecl(c, n, skConst)
for i in 0..<n.len:
var a = n[i]
if a.kind == nkCommentStmt: continue
if (a.kind != nkConstDef): illFormedAst(a, c.c.config)
checkSonsLen(a, 3, c.c.config)
addLocalDecl(c, a[0], skConst)
a[1] = semTemplBody(c, a[1])
a[2] = semTemplBody(c, a[2])
of nkTypeSection: of nkTypeSection:
for i in 0..<n.len: for i in 0..<n.len:
var a = n[i] var a = n[i]

View file

@ -34,5 +34,15 @@ template main() =
doAssert ct in b, $(b, ct) doAssert ct in b, $(b, ct)
doAssert NimVersion in b doAssert NimVersion in b
block: # Test for fix on broken const unpacking
template mytemp() =
const
(x, increment) = (4, true)
a = 100
discard (x, increment, a)
mytemp()
static: main() static: main()
main() main()