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.55k stars 1.47k forks source link

generics break global init order #17087

Open arnetheduck opened 3 years ago

arnetheduck commented 3 years ago

testit2:

proc inTestit2*[T](): T =
  var x {.global.} = inTestit[T]()
  echo "inTestit2: ", x
  x

testit:

import testit2

proc inTestit[T: int](): T =
  3 + inTestit2[int]()

echo inTestit2[int]()
[arnetheduck@tempus tmp]$ nim c -r --hints:off testit
inTestit2: 0
inTestit2: 3
3
[arnetheduck@tempus tmp]$ nim --version
Nim Compiler Version 1.4.2 [Linux: amd64]
Compiled at 2020-11-30
Copyright (c) 2006-2020 by Andreas Rumpf

git hash: 3fb5157ab1b666a5a5c34efde0f357a82d433d04
active boot switches: -d:release

notice how the first time around x has not had its initializer run yet. Of course, these kinds of circular inits are not sound, but the compiler should be able to detect them and ideally prevent them.

arnetheduck commented 3 years ago

also, shouldn't mixin be required for inTestit in intTestit2?