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

Compiler Hangs on Swift 5.10 and Crashes with Latest Swift 6.0 Snapshot When Using lazy var with try #73073

Closed
CrazyFanFan opened this issue Apr 17, 2024 · 2 comments · Fixed by #74591
Labels
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 error handling lazy var Feature: Lazy variables throw Feature → error handling: Throw statements type checker Area → compiler: Semantic analysis

Comments

@CrazyFanFan
Copy link
Contributor

Description

I've encountered an issue where the compiler hangs when building the following code with Swift 5.10. When switching to the latest daily build of Swift 6.0,(swift-6.0-DEVELOPMENT-SNAPSHOT-2024-04-15-a) the compiler crashes instead.

1.	Apple Swift version 6.0-dev (LLVM c5ac128e678cf31, Swift 59748d9cf60064b)
2.	Compiling with effective version 5.10
3.	While evaluating request ASTLoweringRequest(Lowering AST to SIL for file "/Users/kkk/repo/lark/lazytry/Sources/main.swift")
4.	While silgen emitFunction SIL function "@$s7lazytry4foo2yyKF".
 for 'foo2()' (at /Users/kkk/repo/lark/lazytry/Sources/main.swift:6:1)
5.	While silgen emitFunction SIL function "@$s7lazytry4foo2yyKF1xL_Sivg".
 for getter for x (at /Users/kkk/repo/lark/lazytry/Sources/main.swift:7:14)
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           0x0000000107d96e08 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x0000000107d955d4 llvm::sys::RunSignalHandlers() + 112
2  swift-frontend           0x0000000107d97450 SignalHandler(int) + 304
3  libsystem_platform.dylib 0x0000000180593584 _sigtramp + 56
4  libsystem_pthread.dylib  0x0000000180562c20 pthread_kill + 288
5  libsystem_c.dylib        0x000000018046fa30 abort + 180
6  libsystem_c.dylib        0x000000018046ed20 err + 0
7  swift-frontend           0x0000000107ed6230 swift::Lowering::SILGenFunction::emitThrow(swift::SILLocation, swift::Lowering::ManagedValue, bool) (.cold.2) + 0
8  swift-frontend           0x0000000103044cd4 swift::Lowering::SILGenFunction::emitThrow(swift::SILLocation, swift::Lowering::ManagedValue, bool) + 2228
9  swift-frontend           0x00000001030443d4 swift::Lowering::SILGenFunction::getTryApplyErrorDest(swift::SILLocation, swift::CanTypeWrapper<swift::SILFunctionType>, swift::Lowering::ExecutorBreadcrumb, swift::SILResultInfo, swift::SILValue, bool) + 548
10 swift-frontend           0x0000000102f61300 emitRawApply(swift::Lowering::SILGenFunction&, swift::SILLocation, swift::Lowering::ManagedValue, swift::SubstitutionMap, llvm::ArrayRef<swift::Lowering::ManagedValue>, swift::CanTypeWrapper<swift::SILFunctionType>, swift::optionset::OptionSet<swift::ApplyFlags, unsigned char>, llvm::ArrayRef<swift::SILValue>, swift::SILValue, llvm::SmallVectorImpl<swift::SILValue>&, swift::Lowering::ExecutorBreadcrumb) + 2760
11 swift-frontend           0x0000000102f61c80 swift::Lowering::SILGenFunction::emitApply(std::__1::unique_ptr<swift::Lowering::ResultPlan, std::__1::default_delete<swift::Lowering::ResultPlan>>&&, swift::Lowering::ArgumentScope&&, swift::SILLocation, swift::Lowering::ManagedValue, swift::SubstitutionMap, llvm::ArrayRef<swift::Lowering::ManagedValue>, swift::Lowering::CalleeTypeInfo const&, swift::optionset::OptionSet<swift::ApplyFlags, unsigned char>, swift::Lowering::SGFContext, std::__1::optional<swift::ActorIsolation>) + 1340
12 swift-frontend           0x0000000102f671b8 (anonymous namespace)::CallEmission::apply(swift::Lowering::SGFContext) + 2028
13 swift-frontend           0x0000000102f65768 swift::Lowering::SILGenFunction::emitApplyExpr(swift::ApplyExpr*, swift::Lowering::SGFContext) + 2028
14 swift-frontend           0x0000000102fbcaa0 swift::Lowering::SILGenFunction::emitExprInto(swift::Expr*, swift::Lowering::Initialization*, std::__1::optional<swift::SILLocation>) + 156
15 swift-frontend           0x0000000102faabf8 swift::Lowering::SILGenFunction::emitPatternBinding(swift::PatternBindingDecl*, unsigned int, bool) + 1308
16 swift-frontend           0x0000000102fb0ce0 swift::ASTVisitor<swift::Lowering::SILGenFunction, void, void, void, void, void, void>::visit(swift::Decl*) + 180
17 swift-frontend           0x000000010304188c swift::ASTVisitor<(anonymous namespace)::StmtEmitter, void, void, void, void, void, void>::visit(swift::Stmt*) + 4196
18 swift-frontend           0x000000010304081c swift::Lowering::SILGenFunction::emitStmt(swift::Stmt*) + 24
19 swift-frontend           0x0000000102fe1770 swift::Lowering::SILGenFunction::emitFunction(swift::FuncDecl*) + 432
20 swift-frontend           0x0000000102f53aec swift::Lowering::SILGenModule::emitFunctionDefinition(swift::SILDeclRef, swift::SILFunction*) + 6324
21 swift-frontend           0x0000000102f54920 swift::Lowering::SILGenModule::emitOrDelayFunction(swift::SILDeclRef) + 252
22 swift-frontend           0x0000000102f52200 swift::Lowering::SILGenModule::emitFunction(swift::FuncDecl*) + 196
23 swift-frontend           0x0000000103fe12e0 swift::AbstractStorageDecl::visitExpectedOpaqueAccessors(llvm::function_ref<void (swift::AccessorKind)>) const + 68
24 swift-frontend           0x0000000103fe1284 swift::AbstractStorageDecl::visitEmittedAccessors(llvm::function_ref<void (swift::AccessorDecl*)>) const + 160
25 swift-frontend           0x0000000102f569c0 swift::Lowering::SILGenModule::visitEmittedAccessors(swift::AbstractStorageDecl*, llvm::function_ref<void (swift::AccessorDecl*)>) + 48
26 swift-frontend           0x0000000102faaed0 swift::Lowering::SILGenFunction::visitVarDecl(swift::VarDecl*) + 148
27 swift-frontend           0x000000010304188c swift::ASTVisitor<(anonymous namespace)::StmtEmitter, void, void, void, void, void, void>::visit(swift::Stmt*) + 4196
28 swift-frontend           0x000000010304081c swift::Lowering::SILGenFunction::emitStmt(swift::Stmt*) + 24
29 swift-frontend           0x0000000102fe1770 swift::Lowering::SILGenFunction::emitFunction(swift::FuncDecl*) + 432
30 swift-frontend           0x0000000102f53aec swift::Lowering::SILGenModule::emitFunctionDefinition(swift::SILDeclRef, swift::SILFunction*) + 6324
31 swift-frontend           0x0000000102f54920 swift::Lowering::SILGenModule::emitOrDelayFunction(swift::SILDeclRef) + 252
32 swift-frontend           0x0000000102f52200 swift::Lowering::SILGenModule::emitFunction(swift::FuncDecl*) + 196
33 swift-frontend           0x0000000102f57260 swift::ASTLoweringRequest::evaluate(swift::Evaluator&, swift::ASTLoweringDescriptor) const + 880
34 swift-frontend           0x0000000103040274 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&) + 148
35 swift-frontend           0x0000000102f5cb54 swift::ASTLoweringRequest::OutputType swift::Evaluator::getResultUncached<swift::ASTLoweringRequest, swift::ASTLoweringRequest::OutputType swift::evaluateOrFatal<swift::ASTLoweringRequest>(swift::Evaluator&, swift::ASTLoweringRequest)::'lambda'()>(swift::ASTLoweringRequest const&, swift::ASTLoweringRequest::OutputType swift::evaluateOrFatal<swift::ASTLoweringRequest>(swift::Evaluator&, swift::ASTLoweringRequest)::'lambda'()) + 340
36 swift-frontend           0x0000000102f57b64 swift::performASTLowering(swift::FileUnit&, swift::Lowering::TypeConverter&, swift::SILOptions const&, swift::IRGenOptions const*) + 96
37 swift-frontend           0x0000000102945b94 swift::performCompileStepsPostSema(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 568
38 swift-frontend           0x0000000102956db8 withSemanticAnalysis(swift::CompilerInstance&, swift::FrontendObserver*, llvm::function_ref<bool (swift::CompilerInstance&)>, bool) + 160
39 swift-frontend           0x00000001029481e0 performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 708
40 swift-frontend           0x000000010294714c swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 2380
41 swift-frontend           0x0000000102761a78 swift::mainEntry(int, char const**) + 3096
42 dyld                     0x00000001801da0e0 start + 2360

Reproduction

func foo() throws -> Int { 0 }

func foo2() throws {
    lazy var x = try foo()
}

Expected behavior

The compiler should handle the lazy var initialization with a try without hanging or crashing, and either compile the code or produce a meaningful compilation error if the usage is incorrect.

Environment

swift

swift-driver version: 1.90.11.1 Apple Swift version 5.10 (swiftlang-5.10.0.13 clang-1500.3.9.4)
Target: arm64-apple-macosx14.0

MacOS

macOS Sonoma 14.4

Additional information

No response

@CrazyFanFan CrazyFanFan added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Apr 17, 2024
@hamishknight hamishknight added compiler The Swift compiler itself type checker Area → compiler: Semantic analysis error handling throw Feature → error handling: Throw statements and removed triage needed This issue needs more specific labels labels Apr 18, 2024
@CrazyFanFan
Copy link
Contributor Author

In Swift 6, this issue has been fixed, and now exceptions can be correctly thrown: Call can throw, but errors cannot be thrown out of a lazy variable initializer.

@hamishknight
Copy link
Contributor

Yup this was fixed by #74591

@AnthonyLatsis AnthonyLatsis added crash Bug: A crash, i.e., an abnormal termination of software lazy var Feature: Lazy variables labels Jul 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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 error handling lazy var Feature: Lazy variables throw Feature → error handling: Throw statements type checker Area → compiler: Semantic analysis
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants