Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Constants of type Never used in switch statements do not trigger error for use of uninitialized constant, leading to runtime crash #74478

Open
schuelermine opened this issue Jun 17, 2024 · 3 comments
Labels
accepts invalid Bug: Accepts invalid bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler itself definite initialization Area → compiler → SIL: Definite initialization let & var Feature: constant and variable declarations Never Area → standard library: The `Never` type SILGen Area → compiler: The SIL generation stage statements Feature: statements swift 6.0 switch Feature → statements: 'switch' statements

Comments

@schuelermine
Copy link

schuelermine commented Jun 17, 2024

Description

When a constant is declared with type Never and subsequently used as the subject of a switch statement, the compiler fails to diagnose the use of an uninitialized constant, leading to a runtime crash.

Reproduction

let x: Never
switch x { }

I will provide the crash message shortly; at the moment, I am not at my computer.

Expected behavior

The compiler should produce an error.

Environment

I believe it is 5.10.1, but I am not sure as I am not at my computer. I downloaded the release about a week ago. I will report the version as soon as I am back at my computer.

I was running Swift in an Ubuntu 24.04 container. Swift was installed to /opt/swift.

Additional information

I was informed this is not intentional in this thread on the forum.

@schuelermine schuelermine added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Jun 17, 2024
@schuelermine schuelermine changed the title Variables of type Never used in switch statements do not trigger error for use of uninitialized variable, leading to runtime crash Constants of type Never used in switch statements do not trigger error for use of uninitialized variable, leading to runtime crash Jun 17, 2024
@schuelermine
Copy link
Author

In lieu of the full error message on my computer I have reproduced the error on Android (Termux):

$ swift run
Building for debugging...
[8/8] Linking swift-crash
Build complete! (3.77s)
fish: Job 1, 'swift run' terminated by signal SIGBUS (Misaligned address error)
$ swiftc -version
Swift version 5.10.1 (swift-5.10.1-RELEASE)
Target: aarch64-unknown-linux-android24

@schuelermine
Copy link
Author

The issue also occurs with a production build.

@hamishknight hamishknight added compiler The Swift compiler itself definite initialization Area → compiler → SIL: Definite initialization accepts invalid Bug: Accepts invalid SILGen Area → compiler: The SIL generation stage and removed triage needed This issue needs more specific labels definite initialization Area → compiler → SIL: Definite initialization labels Jun 17, 2024
@hamishknight
Copy link
Contributor

Looks like a SILGen issue, for this:

func foo() {
  let x: Never
  switch x {}
}

we emit:

sil hidden [ossa] @$s4main3fooyyF : $@convention(thin) () -> () {
bb0:
  %0 = alloc_stack [var_decl] $Never, let, name "x" // user: %1
  %1 = mark_uninitialized [var] %0 : $*Never
  unreachable                                     // id: %2
}

with no use of the variable, so definite initialization doesn't diagnose it.

@AnthonyLatsis AnthonyLatsis added standard library Area: Standard library umbrella Never Area → standard library: The `Never` type definite initialization Area → compiler → SIL: Definite initialization let & var Feature: constant and variable declarations swift 6.0 switch Feature → statements: 'switch' statements statements Feature: statements labels Jun 17, 2024
@Azoy Azoy removed the standard library Area: Standard library umbrella label Jun 20, 2024
@schuelermine schuelermine changed the title Constants of type Never used in switch statements do not trigger error for use of uninitialized variable, leading to runtime crash Constants of type Never used in switch statements do not trigger error for use of uninitialized constant, leading to runtime crash Jun 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepts invalid Bug: Accepts invalid bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler itself definite initialization Area → compiler → SIL: Definite initialization let & var Feature: constant and variable declarations Never Area → standard library: The `Never` type SILGen Area → compiler: The SIL generation stage statements Feature: statements swift 6.0 switch Feature → statements: 'switch' statements
Projects
None yet
Development

No branches or pull requests

4 participants