mirror of
https://github.com/nim-lang/Nim.git
synced 2026-09-17 18:44:53 +00:00
* fix #3505 wrong var {.global.} initialization, asign variable to it
* fix #5132 as well
* follow suggestions
* handle all call kinds
* Update tests/global/t3505.nim
* Update compiler/semstmts.nim
* Update compiler/semstmts.nim
* Update compiler/semstmts.nim
* follow suggestion
* Update compiler/semstmts.nim
Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
(cherry picked from commit 1410243d3b)
This commit is contained in:
@@ -36,6 +36,7 @@ const
|
||||
errRecursiveDependencyX = "recursive dependency: '$1'"
|
||||
errRecursiveDependencyIteratorX = "recursion is not supported in iterators: '$1'"
|
||||
errPragmaOnlyInHeaderOfProcX = "pragmas are only allowed in the header of a proc; redefinition of $1"
|
||||
errCannotAssignToGlobal = "cannot assign local to global variable"
|
||||
|
||||
proc semDiscard(c: PContext, n: PNode): PNode =
|
||||
result = n
|
||||
@@ -516,6 +517,24 @@ proc errorSymChoiceUseQualifier(c: PContext; n: PNode) =
|
||||
inc i
|
||||
localError(c.config, n.info, errGenerated, err)
|
||||
|
||||
template isLocalVarSym(n: PNode): bool =
|
||||
n.kind == nkSym and
|
||||
n.sym.kind in {skVar, skLet} and not
|
||||
({sfGlobal, sfPure} <= n.sym.flags or
|
||||
sfCompileTime in n.sym.flags)
|
||||
|
||||
proc usesLocalVar(n: PNode): bool =
|
||||
for z in 1 ..< n.len:
|
||||
if n[z].isLocalVarSym:
|
||||
return true
|
||||
elif n[z].kind in nkCallKinds:
|
||||
if usesLocalVar(n[z]):
|
||||
return true
|
||||
|
||||
proc globalVarInitCheck(c: PContext, n: PNode) =
|
||||
if n.isLocalVarSym or n.kind in nkCallKinds and usesLocalVar(n):
|
||||
localError(c.config, n.info, errCannotAssignToGlobal)
|
||||
|
||||
proc semVarOrLet(c: PContext, n: PNode, symkind: TSymKind): PNode =
|
||||
if n.len == 1:
|
||||
result = semLowerLetVarCustomPragma(c, n[0], n)
|
||||
@@ -668,7 +687,8 @@ proc semVarOrLet(c: PContext, n: PNode, symkind: TSymKind): PNode =
|
||||
vm.setupCompileTimeVar(c.module, c.idgen, c.graph, x)
|
||||
if v.flags * {sfGlobal, sfThread} == {sfGlobal}:
|
||||
message(c.config, v.info, hintGlobalVar)
|
||||
|
||||
if {sfGlobal, sfPure} <= v.flags:
|
||||
globalVarInitCheck(c, def)
|
||||
suggestSym(c.graph, v.info, v, c.graph.usageSym)
|
||||
|
||||
proc semConst(c: PContext, n: PNode): PNode =
|
||||
|
||||
Reference in New Issue
Block a user