From 25a13ad0e5b6f79b547a034b6603ad27cd684e45 Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Fri, 23 May 2025 22:16:57 +0800 Subject: [PATCH] fixes #4594; disallow {.global.} uses local vars for basic expressions (#24961) fixes #4594 (cherry picked from commit a09da96c6592da3f231744b7032580777069c3a2) --- compiler/semstmts.nim | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/compiler/semstmts.nim b/compiler/semstmts.nim index 8e32589ea3..ec718e49fd 100644 --- a/compiler/semstmts.nim +++ b/compiler/semstmts.nim @@ -727,20 +727,31 @@ template isLocalSym(sym: PSym): bool = sym.kind in {skProc, skFunc, skIterator} and sfGlobal notin sym.flags -template isLocalVarSym(n: PNode): bool = - n.kind == nkSym and isLocalSym(n.sym) - proc usesLocalVar(n: PNode): bool = - result = false - for z in 1 ..< n.len: - if n[z].isLocalVarSym: - return true - elif n[z].kind in nkCallKinds: - if usesLocalVar(n[z]): + case n.kind + of nkSym: + result = isLocalSym(n.sym) + of nkCallKinds, nkObjConstr: + result = false + for i in 1 ..< n.len: + if usesLocalVar(n[i]): return true + of nkTupleConstr, nkPar, nkBracket, nkCurly: + result = false + for i in 0 ..< n.len: + if usesLocalVar(n[i]): + return true + of nkDotExpr, nkCheckedFieldExpr, + nkBracketExpr, nkAddr, nkHiddenAddr, + nkObjDownConv, nkObjUpConv: + result = usesLocalVar(n[0]) + of nkHiddenStdConv, nkHiddenSubConv, nkCast, nkExprColonExpr: + result = usesLocalVar(n[1]) + else: + result = false proc globalVarInitCheck(c: PContext, n: PNode) = - if n.isLocalVarSym or n.kind in nkCallKinds and usesLocalVar(n): + if usesLocalVar(n): localError(c.config, n.info, errCannotAssignToGlobal) const