Skip to content

Assigning to self in a Never initializer using a switch expression crashes the compiler #66490

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

Closed
hamishknight opened this issue Jun 9, 2023 · 4 comments · Fixed by #66551
Closed
Assignees
Labels
assertion failure Bug → crash: An assertion failure bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler itself crash Bug: A crash, i.e., an abnormal termination of software expressions Feature: expressions SILGen Area → compiler: The SIL generation stage swift 5.9 switch Feature → statements: 'switch' statements

Comments

@hamishknight
Copy link
Contributor

hamishknight commented Jun 9, 2023

This crashes in SILGen:

extension Never {
  init(value: Self) {
    self = switch value {
    case let v: v
    }
  }
}
Assertion failed: (hasValidInsertionPoint()), function insertImpl, file SILBuilder.h, line 2953.
Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the crash backtrace.
Stack dump:
0.	Program arguments: ./swift -frontend -emit-ir /Users/hamish/src/swift-test-arena/swift-test-arena/main.swift -sdk /Users/hamish/src/MacOSX.sdk/ -debug-diagnostic-names
1.	Swift version 5.9-dev (LLVM ca18bb83c79530c, Swift 40396d681d52ebd)
2.	Compiling with the current language version
3.	While evaluating request ASTLoweringRequest(Lowering AST to SIL for module main)
4.	While silgen emitValueConstructor SIL function "@$ss5NeverO4mainE5valueA2B_tcfC".
 for 'init(value:)' (at /Users/hamish/src/swift-test-arena/swift-test-arena/main.swift:2324:3)
Stack dump without symbol names (ensure you have llvm-symbolizer in your PATH or set the environment var `LLVM_SYMBOLIZER_PATH` to point to it):
0  swift-frontend           0x0000000106d1c767 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 39
1  swift-frontend           0x0000000106d1b6b8 llvm::sys::RunSignalHandlers() + 248
2  swift-frontend           0x0000000106d1cdb0 SignalHandler(int) + 272
3  libsystem_platform.dylib 0x00007ff808ba65ed _sigtramp + 29
4  libsystem_platform.dylib 0x00007ff1b316d290 _sigtramp + 18446744046502964416
5  libsystem_c.dylib        0x00007ff808a9fb45 abort + 123
6  libsystem_c.dylib        0x00007ff808a9ee5e err + 0
7  swift-frontend           0x0000000106d22b83 swift::SILBuilder::insertImpl(swift::SILInstruction*) (.cold.1) + 35
8  swift-frontend           0x00000001011b3f7d swift::SILBuilder::insertImpl(swift::SILInstruction*) + 141
9  swift-frontend           0x0000000101cfde93 swift::SILBuilder::createLoad(swift::SILLocation, swift::SILValue, swift::LoadOwnershipQualifier) + 227
10 swift-frontend           0x000000010198196a swift::Lowering::SILGenBuilder::createLoadBorrow(swift::SILLocation, swift::Lowering::ManagedValue) + 74
11 swift-frontend           0x0000000101938859 (anonymous namespace)::ExplodeTupleValue::visitType(swift::CanType, swift::Lowering::ManagedValue) + 121
12 swift-frontend           0x00000001019365b7 swift::Lowering::RValue::RValue(swift::Lowering::SILGenFunction&, swift::Expr*, swift::Lowering::ManagedValue) + 183
13 swift-frontend           0x00000001019cf1cf (anonymous namespace)::RValueEmitter::visitSingleValueStmtExpr(swift::SingleValueStmtExpr*, swift::Lowering::SGFContext) + 671
14 swift-frontend           0x00000001019be05f swift::ASTVisitor<(anonymous namespace)::RValueEmitter, swift::Lowering::RValue, void, void, void, void, void, swift::Lowering::SGFContext>::visit(swift::Expr*, swift::Lowering::SGFContext) + 1231
15 swift-frontend           0x00000001019b1838 swift::Lowering::SILGenFunction::emitRValue(swift::Expr*, swift::Lowering::SGFContext) + 40
16 swift-frontend           0x000000010192a683 swift::Lowering::ArgumentSource::getAsRValue(swift::Lowering::SILGenFunction&, swift::Lowering::SGFContext) && + 115
17 swift-frontend           0x00000001019f5d67 swift::Lowering::SILGenFunction::emitAssignToLValue(swift::SILLocation, swift::Lowering::ArgumentSource&&, swift::Lowering::LValue&&) + 359
18 swift-frontend           0x00000001019cdcd2 (anonymous namespace)::RValueEmitter::visitAssignExpr(swift::AssignExpr*, swift::Lowering::SGFContext) + 1250
19 swift-frontend           0x00000001019be02e swift::ASTVisitor<(anonymous namespace)::RValueEmitter, swift::Lowering::RValue, void, void, void, void, void, swift::Lowering::SGFContext>::visit(swift::Expr*, swift::Lowering::SGFContext) + 1182
20 swift-frontend           0x00000001019b475c swift::Lowering::SILGenFunction::emitIgnoredExpr(swift::Expr*) + 1116
21 swift-frontend           0x0000000101a3a209 swift::ASTVisitor<(anonymous namespace)::StmtEmitter, void, void, void, void, void, void>::visit(swift::Stmt*) + 5001
22 swift-frontend           0x0000000101a38e75 swift::Lowering::SILGenFunction::emitStmt(swift::Stmt*) + 21
23 swift-frontend           0x000000010198f705 swift::Lowering::SILGenFunction::emitValueConstructor(swift::ConstructorDecl*) + 8661
24 swift-frontend           0x0000000101943cf9 swift::Lowering::SILGenModule::emitFunctionDefinition(swift::SILDeclRef, swift::SILFunction*) + 6473
25 swift-frontend           0x0000000101945bd7 emitOrDelayFunction(swift::Lowering::SILGenModule&, swift::SILDeclRef) + 231
26 swift-frontend           0x0000000101945f12 swift::Lowering::SILGenModule::emitConstructor(swift::ConstructorDecl*) + 338
27 swift-frontend           0x0000000101a4b389 swift::ASTVisitor<SILGenExtension, void, void, void, void, void, void>::visit(swift::Decl*) + 169
28 swift-frontend           0x0000000101a47325 SILGenExtension::emitExtension(swift::ExtensionDecl*) + 101
29 swift-frontend           0x0000000101a472b5 swift::Lowering::SILGenModule::visitExtensionDecl(swift::ExtensionDecl*) + 21
30 swift-frontend           0x00000001019483f5 swift::ASTLoweringRequest::evaluate(swift::Evaluator&, swift::ASTLoweringDescriptor) const + 2341
31 swift-frontend           0x0000000101a38968 swift::SimpleRequest<swift::ASTLoweringRequest, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule>> (swift::ASTLoweringDescriptor), (swift::RequestFlags)9>::evaluateRequest(swift::ASTLoweringRequest const&, swift::Evaluator&) + 120
32 swift-frontend           0x000000010194bff2 llvm::Expected<swift::ASTLoweringRequest::OutputType> swift::Evaluator::getResultUncached<swift::ASTLoweringRequest>(swift::ASTLoweringRequest const&) + 386
33 swift-frontend           0x00000001019498a1 swift::performASTLowering(swift::ModuleDecl*, swift::Lowering::TypeConverter&, swift::SILOptions const&, swift::IRGenOptions const*) + 161
34 swift-frontend           0x00000001013811c2 swift::performCompileStepsPostSema(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 2290
35 swift-frontend           0x00000001013921d0 withSemanticAnalysis(swift::CompilerInstance&, swift::FrontendObserver*, llvm::function_ref<bool (swift::CompilerInstance&)>, bool) + 144
36 swift-frontend           0x00000001013836cd performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 861
37 swift-frontend           0x0000000101382578 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 2872
38 swift-frontend           0x000000010119dd75 swift::mainEntry(int, char const**) + 2309
39 dyld                     0x00007ff80881f41f start + 1903

rdar://110636210

@hamishknight hamishknight added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler itself SILGen Area → compiler: The SIL generation stage crash Bug: A crash, i.e., an abnormal termination of software labels Jun 9, 2023
@hamishknight hamishknight self-assigned this Jun 9, 2023
@mickeyl
Copy link

mickeyl commented Jun 9, 2023

I can't confirm this. Which version are you using?

With Swift 5.8:

foo.swift:3:12: error: expected expression in assignment
    self = switch value {
           ^
foo.swift:4:14: warning: constant 'v' inferred to have type 'Never', which is an enum with no cases
    case let v: v
             ^
foo.swift:4:14: note: add an explicit type annotation to silence this warning
    case let v: v
             ^
              : Never

With Swift 5.9:

foo.swift:4:17: error: cannot find 'v' in scope
    case let v: v
                ^

@hamishknight
Copy link
Contributor Author

This is on main @mickeyl, the 5.9 error is a bug that has been fixed (#66412)

@AnthonyLatsis
Copy link
Collaborator

Which version are you using?

@mickeyl At the risk of stating the obvious, Swift compiler stack dumps like the one above include the compiler version (and revision if it is a *-dev compiler).

@AnthonyLatsis AnthonyLatsis added assertion failure Bug → crash: An assertion failure switch Feature → statements: 'switch' statements expressions Feature: expressions assignments Feature → expressions: assignments labels Jun 9, 2023
@hamishknight
Copy link
Contributor Author

Even simpler reduction:

func foo() {
  let i = switch fatalError() {}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
assertion failure Bug → crash: An assertion failure bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler itself crash Bug: A crash, i.e., an abnormal termination of software expressions Feature: expressions SILGen Area → compiler: The SIL generation stage swift 5.9 switch Feature → statements: 'switch' statements
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants