Skip to content

Module functions can access uninitialized main.swift globals #69535

Open
@tristanlabelle

Description

@tristanlabelle

Description
Global variables used in main.swift are visible in functions of the module, which allows their access while uninitialized

Steps to reproduce

class Foo { let message: String = "oh no" }
func getFoo() -> Foo { foo }
let foo = getFoo()
print(foo.message)
image

This happens as well if getFoo() is moved to a different file in the same module.

Expected behavior
The code should fail to compile because getFoo() accesses an uninitialized foo. More generally, variables in main.swift should probably be treated like local variables in a function rather than like global variables.

Environment

  • Swift compiler version info:
compnerd.org Swift version 5.11-dev (LLVM ee13eccb18b0dc4, Swift a94d22e017bd6a4)
Target: x86_64-unknown-windows-msvc
(built from main on 2023-10-27)
  • Xcode version info: N/A
  • Deployment target: Windows 11

Metadata

Metadata

Assignees

Labels

SILaccepts invalidBug: Accepts invalidbugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfdefinite initializationArea → compiler → SIL: Definite initializationglobal let & varFeature: global constants and variablesmemory safetyFeature: memory safetyswift 6.0

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions