nim-lang / Nim

Nim is a statically typed compiled systems programming language. It combines successful concepts from mature languages like Python, Ada and Modula. Its design focuses on efficiency, expressiveness, and elegance (in that order of priority).
https://nim-lang.org
Other
16.57k stars 1.47k forks source link

Nim doesn't catch wrong var {.global.} initialization #3505

Closed Wh1teDuke closed 1 year ago

Wh1teDuke commented 9 years ago

Test

proc foo =
  let a = 0
  var b {.global.} = a # Nim should complain about this
foo()

Result:

>nim c test
[...]\nimcache\compiler_test.c:100:12: error: 'a' undeclared (first use in this f
unction)
  b_97006 = a;
            ^
Error: execution of an external program failed: [...]

>nim -v
Nim Compiler Version 0.12.0 (2015-10-28) [Windows: i386]
Copyright (c) 2006-2015 by Andreas Rumpf

git hash: 22a702868bfd242ff76bf86ea00a278f979a112e
active boot switches: -d:release
stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. If you think it is still a valid issue, write a comment below; otherwise it will be closed. Thank you for your contributions.

bung87 commented 2 years ago

this works in current devel 3b973f3fc8a87a42b2b3855a56d87f6646fe76ac, and similar issues are closed, guess it's just reopened by adding a label.

ringabout commented 2 years ago

this works in current devel https://github.com/nim-lang/Nim/commit/3b973f3fc8a87a42b2b3855a56d87f6646fe76ac, and similar issues are closed, guess it's just reopened by adding a label.

It doesn't work if the proc gets called

proc foo =
  let a = 0
  var b {.global.} = a # Nim should complain about this

foo()
C:\Users\blue\Documents\GitHub\Nim\test3.nim(3, 7) Hint: 'b' is declared but not used [XDeclaredButNotUsed]
CC: ../../../.choosenim/toolchains/nim-#devel/lib/system/exceptions.nim
CC: ../../../.choosenim/toolchains/nim-#devel/lib/system.nim
CC: test3.nim
C:\Users\blue\nimcache\test3_d\@mtest3.nim.c: In function 'NimMainModule':
C:\Users\blue\nimcache\test3_d\@mtest3.nim.c:229:23: error: 'a' undeclared (first use in this function)
  229 |         b__test51_3 = a;
      |                       ^
bung87 commented 2 years ago

hmm, seems like we need transform the lvalue to global too.