And If so, what should it print?
test.swift:
func f() { g() }
f()
print(x)
var x = 100
func g() { x += 1 }
The current behavior is that it compiles fine and prints 1 (why not eg 101?):
$ swiftc --version
Apple Swift version 5.2 (swiftlang-1103.0.32.1 clang-1103.0.32.29)
Target: x86_64-apple-darwin19.4.0
$ swiftc test.swift && ./test
1
I think it looks like an accepts invalid bug because (among other things) …
… look what happens if `x` is an `Array` instead of an `Int`.
func f() { g() }
f()
print(x)
var x = [1, 2, 3]
func g() { x.append(4) }
$ swiftc test.swift
$ ./test
Segmentation fault: 11
That is, it compiles fine, but crashes at runtime.
Possibly related threads and bugs.
Can anyone please explain this behavior? - #8 by jrose
Compiler bug or feature? - #2 by Jens
Calling global function before declaration
SR-11534 (Note that this one is open, yet it seems to have been "fixed" in a way that makes my examples above "work" ...)