Skip to content

Circular imports break global init order #17088

Open
@arnetheduck

Description

@arnetheduck

testit:

proc init(): string =
  echo "init a"
  "a"

proc f*() =
  echo "A"
  var a {.global.} = init()
  echo a

import testit2

f()

testit2:

import testit

proc init(): string =
  testit.f()
  echo "init b"
  "b"

proc f*() =
  echo "B"
  var a {.global.} = init()
  echo a

f()
A

init b
B
b
init a
A
a

notice how nothing is printed after the first A. This issue is the non-generic version of #17087 and of course is logically unsound due to the circular dependency - global inits are not sound to use in this context - as such, it would be good for the language to limit their capabilities - imports like this happen in the wild and are incredibly hard to find.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions