Skip to content

Allows top-level variable to be used in its own initial value #70380

@brow

Description

@brow

Description

In Swift 5.4 and later, a variable can in some cases be used in the initial value of its own declaration. Doing so produces a crash at runtime rather than a compiler error.

Reproduction

let x: Any = x

Run in a Swift Playground in Xcode 15.0.1. Crashes at runtime with error:

error: Execution was interrupted, reason: EXC_BAD_ACCESS (code=1, address=0xfffffffffffffff8).

Expected behavior

Compiler error such as use of variable 'x' before its declaration or variable used within its own initial value.

The former is the compiler error given for do { let x: Any = x }.

The latter is the error given by Swift 5.3.

Environment

The runtime error was produced in a Swift Playground in Xcode 15.0.1.

The lack of compiler error was reproduced in swiftc 5.9 and 5.4, but not in 5.3, using swift.godbolt.org.

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    SILaccepts invalidBug: Accepts invalidbugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfcrashBug: A crash, i.e., an abnormal termination of softwaredefinite initializationArea → compiler → SIL: Definite initializationmemory safetyFeature: memory safetyscript modeFlag: Specific to script mode compilationswift 6.0

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions