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

[SR-7055] Crash when referencing closure variable inside the closure #49603

Open
nevil opened this issue Feb 22, 2018 · 5 comments
Open

[SR-7055] Crash when referencing closure variable inside the closure #49603

nevil opened this issue Feb 22, 2018 · 5 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself crash Bug: A crash, i.e., an abnormal termination of software

Comments

@nevil
Copy link

nevil commented Feb 22, 2018

Previous ID SR-7055
Radar rdar://problem/37790416
Original Reporter @nevil
Type Bug
Environment

macOS
swift-DEVELOPMENT-SNAPSHOT-2018-02-21-a.xctoolchain
swift-4.1-DEVELOPMENT-SNAPSHOT-2018-02-21-a.xctoolchain
Apple Swift version 4.0.3 (swiftlang-900.0.74.1 clang-900.0.39.2) (Xcode 9.2)

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, CompilerCrash
Assignee @slavapestov
Priority Medium

md5: ce65ae04a96a7b4e71fd20ad8664715d

Issue Description:

func afunc() {
    let nestedA: (_: (@escaping (Error?) -> Void)) -> Void = { (_) in
    }

    let nestedB: (Error?) -> Void = { (error) in
        guard error == nil else {
            nestedA(nestedB)
            return
        }
    }

    nestedA(nestedB)
}

The code above causes a compiler crash.
It has been reproduced on:
swift-DEVELOPMENT-SNAPSHOT-2018-02-21-a.xctoolchain
swift-4.1-DEVELOPMENT-SNAPSHOT-2018-02-21-a.xctoolchain
Apple Swift version 4.0.3 (swiftlang-900.0.74.1 clang-900.0.39.2) (Xcode 9.2)

Invocation:
swiftc -swift-version 4 Crasher.swift or swiftc -swift-version 3 Crasher.swift

Note:
If the nestedA(nestedB) is moved outside of the guard we instead get an error:

error: variable used within its own initial value
            nestedA(nestedB)

Callstack

:> /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2018-02-21-a.xctoolchain/usr/bin/swiftc -v -swift-version 4 Crasher.swift
Apple Swift version 4.1-dev (LLVM c4ec2ab808, Clang d8b11579e8, Swift c8e37242fe)
Target: x86_64-apple-darwin17.4.0
/Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2018-02-21-a.xctoolchain/usr/bin/swift -frontend -c -primary-file Crasher.swift -target x86_64-apple-darwin17.4.0 -enable-objc-interop -swift-version 4 -color-diagnostics -module-name Crasher -o /var/folders/0l/kr96lmsd1wgbnq21nftllcwh0000gp/T/Crasher-ccdd6b.o
Assertion failed: (found != VarLocs.end()), function emitCaptures, file /Users/buildnode/jenkins/workspace/oss-swift-package-osx/swift/lib/SILGen/SILGenFunction.cpp, line 199.
0  swift                    0x000000010d82a7e8 llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 40
1  swift                    0x000000010d82aef6 SignalHandler(int) + 694
2  libsystem_platform.dylib 0x00007fff77f92f5a _sigtramp + 26
3  libsystem_platform.dylib 0x00000001105f6568 _sigtramp + 2556835368
4  libsystem_c.dylib        0x00007fff77dbd312 abort + 127
5  libsystem_c.dylib        0x00007fff77d85368 basename_r + 0
6  swift                    0x000000010a9b8245 swift::Lowering::SILGenFunction::emitCaptures(swift::SILLocation, swift::AnyFunctionRef, swift::Lowering::CaptureEmission, llvm::SmallVectorImpl<swift::Lowering::ManagedValue>&) + 3845
7  swift                    0x000000010a9b8617 swift::Lowering::SILGenFunction::emitClosureValue(swift::SILLocation, swift::SILDeclRef, swift::CanType, llvm::ArrayRef<swift::Substitution>) + 839
8  swift                    0x000000010a9a9f01 (anonymous namespace)::RValueEmitter::visitAbstractClosureExpr(swift::AbstractClosureExpr*, swift::Lowering::SGFContext) + 241
9  swift                    0x000000010a99c8f3 swift::ASTVisitor<(anonymous namespace)::RValueEmitter, swift::Lowering::RValue, void, void, void, void, void, swift::Lowering::SGFContext>::visit(swift::Expr*, swift::Lowering::SGFContext) + 115
10 swift                    0x000000010a991bef swift::Lowering::SILGenFunction::emitExprInto(swift::Expr*, swift::Lowering::Initialization*, llvm::Optional<swift::SILLocation>) + 335
11 swift                    0x000000010a981054 swift::Lowering::SILGenFunction::emitPatternBinding(swift::PatternBindingDecl*, unsigned int) + 292
12 swift                    0x000000010a9812ad swift::Lowering::SILGenFunction::visitPatternBindingDecl(swift::PatternBindingDecl*) + 45
13 swift                    0x000000010aa02677 swift::ASTVisitor<(anonymous namespace)::StmtEmitter, void, void, void, void, void, void>::visit(swift::Stmt*) + 8711
14 swift                    0x000000010aa00465 swift::Lowering::SILGenFunction::emitStmt(swift::Stmt*) + 21
15 swift                    0x000000010a9b8c9d swift::Lowering::SILGenFunction::emitFunction(swift::FuncDecl*) + 509
16 swift                    0x000000010a939327 swift::Lowering::SILGenModule::emitFunction(swift::FuncDecl*)::$_1::operator()(swift::SILFunction*) const + 311
17 swift                    0x000000010a93169b swift::Lowering::SILGenModule::emitFunction(swift::FuncDecl*) + 875
18 swift                    0x000000010a93612b swift::Lowering::SILGenModule::emitSourceFile(swift::SourceFile*, unsigned int) + 811
19 swift                    0x000000010a936ee0 swift::SILModule::constructSIL(swift::ModuleDecl*, swift::SILOptions&, swift::FileUnit*, llvm::Optional<unsigned int>, bool) + 352
20 swift                    0x000000010a9374ea swift::performSILGeneration(swift::FileUnit&, swift::SILOptions&, llvm::Optional<unsigned int>) + 122
21 swift                    0x000000010a2712c5 performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*, swift::UnifiedStatsReporter*) + 10197
22 swift                    0x000000010a26db65 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 3365
23 swift                    0x000000010a224a62 main + 3042
24 libdyld.dylib            0x00007fff77d11115 start + 1
Stack dump:
0.  Program arguments: /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2018-02-21-a.xctoolchain/usr/bin/swift -frontend -c -primary-file Crasher.swift -target x86_64-apple-darwin17.4.0 -enable-objc-interop -swift-version 4 -color-diagnostics -module-name Crasher -o /var/folders/0l/kr96lmsd1wgbnq21nftllcwh0000gp/T/Crasher-ccdd6b.o
1.  While emitting SIL for 'afunc()' at Crasher.swift:1:1
2.  While silgen emitFunction SIL function "@$S7Crasher5afuncyyF".
 for 'afunc()' at Crasher.swift:1:1
<unknown>:0: error: unable to execute command: Abort trap: 6
<unknown>:0: error: compile command failed due to signal 6 (use -v to see invocation)
@belkadan
Copy link
Contributor

@swift-ci create

@slavapestov
Copy link
Member

No longer crashes but error message is wrong:

spestov@Slavas-iMac-Pro ~/s/swift> xcrun ../build/Ninja-RelWithDebInfoAssert/swift-macosx-x86_64/bin/swiftc e.swift 
e.swift:7:21: error: variable declared in 'guard' condition is not usable in its body
            nestedA(nestedB)
                    ^

@nevil
Copy link
Author

nevil commented Oct 29, 2019

In case it is useful, the error message above is still same.

Apple Swift version 5.1 (swiftlang-1100.0.270.13 clang-1100.0.33.7)
Target: x86_64-apple-darwin18.7.0
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -frontend -c -primary-file SR-7055.swift -target x86_64-apple-darwin18.7.0 -enable-objc-interop -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -color-diagnostics -module-name main -o SR-7055.o
SR-7055.swift:7:21: error: variable declared in 'guard' condition is not usable in its body
            nestedA(nestedB)

@nevil
Copy link
Author

nevil commented Sep 23, 2020

The error is still same with the latest release.

Apple Swift version 5.3 (swiftlang-1200.0.29.2 clang-1200.0.30.1)
Target: x86_64-apple-darwin19.6.0
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -frontend -c -primary-file SR-7055.swift -target x86_64-apple-darwin19.6.0 -enable-objc-interop -stack-check -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -color-diagnostics -target-sdk-version 10.15.6 -module-name main -o SR-7055.o
SR-7055.swift:7:21: error: variable declared in 'guard' condition is not usable in its body
            nestedA(nestedB)
                    ^

@nevil
Copy link
Author

nevil commented Jun 10, 2021

I think the error message is fixed in 5.4.

Apple Swift version 5.4 (swiftlang-1205.0.26.9 clang-1205.0.19.55)

SR-7055.swift:5:37: error: closure captures 'nestedB' before it is declared
    let nestedB: (Error?) -> Void = { (error) in
                                    ^
SR-7055.swift:5:9: note: captured value declared here
    let nestedB: (Error?) -> Void = { (error) in
        ^
SR-7055.swift:7:21: note: captured here
            nestedA(nestedB)

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@AnthonyLatsis AnthonyLatsis added the crash Bug: A crash, i.e., an abnormal termination of software label Dec 12, 2022
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 in itself crash Bug: A crash, i.e., an abnormal termination of software
Projects
None yet
Development

No branches or pull requests

4 participants