swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.59k stars 10.37k forks source link

[SR-3479] Segmentation fault and other error for closure with inout parameter #46067

Closed swift-ci closed 7 years ago

swift-ci commented 7 years ago
Previous ID SR-3479
Radar None
Original Reporter ingoem (JIRA User)
Type Bug
Status Resolved
Resolution Done
Environment swiftc 3.0.2
Additional Detail from JIRA | | | |------------------|-----------------| |Votes | 0 | |Component/s | Compiler | |Labels | Bug, CompilerCrash | |Assignee | ingoem (JIRA) | |Priority | Medium | md5: 9ecb7420a0cb4bea9dccbd3411386b03

is duplicated by:

relates to:

Issue Description:

The following gives a segmentation fault in swiftc:

func f() {
  let _ = unfold((0, 0)) { s in 0 }
}

func unfold<A, B>(_ a0: A, next: (inout A)->B) {}

It compiles when I write an explicit type for the closure or if I pass 0 instead of (0,0) for a0. Now, if I change unfold to use a single type parameter:

func f() {
  let _ = unfold((0,0)) { s in 0 }  // incompatible types
}

func unfold<A>(_ a0: A, next: (inout A)->A) {}

instead of giving a type error swiftc just prints

<unknown>:0: error: parameters may not have the 'var' specifier

If I change the closure to return (0,0) instead of 0, it segfaults again.

0  swift                    0x0000000105f6b3ad PrintStackTraceSignalHandler(void*) + 45
1  swift                    0x0000000105f6ab56 SignalHandler(int) + 790
2  libsystem_platform.dylib 0x00007fff8783e52a _sigtramp + 26
3  libsystem_platform.dylib 0x0000000106dda000 _sigtramp + 2136586992
4  swift                    0x00000001031e4250 (anonymous namespace)::TranslateArguments::translate(swift::Lowering::AbstractionPattern, swift::CanType, swift::Lowering::AbstractionPattern, swift::CanType) + 1472
5  swift                    0x00000001031e3028 createThunk(swift::Lowering::SILGenFunction&, swift::SILLocation, swift::Lowering::ManagedValue, swift::Lowering::AbstractionPattern, swift::CanTypeWrapper<swift::AnyFunctionType>, swift::Lowering::AbstractionPattern, swift::CanTypeWrapper<swift::AnyFunctionType>, swift::Lowering::TypeLowering const&) + 1784
6  swift                    0x00000001031e0515 (anonymous namespace)::Transform::transform(swift::Lowering::ManagedValue, swift::Lowering::AbstractionPattern, swift::CanType, swift::Lowering::AbstractionPattern, swift::CanType, swift::Lowering::SGFContext) + 1909
7  swift                    0x00000001031a5c19 swift::ASTVisitor<(anonymous namespace)::RValueEmitter, swift::Lowering::RValue, void, void, void, void, void, swift::Lowering::SGFContext>::visit(swift::Expr*, swift::Lowering::SGFContext) + 58313
8  swift                    0x000000010314c264 (anonymous namespace)::ArgEmitter::emit(swift::Lowering::ArgumentSource&&, swift::Lowering::AbstractionPattern) + 1332
9  swift                    0x000000010314e557 (anonymous namespace)::ArgEmitter::emitExpanded(swift::Lowering::ArgumentSource&&, swift::Lowering::AbstractionPattern) + 263
10 swift                    0x000000010314be29 (anonymous namespace)::ArgEmitter::emit(swift::Lowering::ArgumentSource&&, swift::Lowering::AbstractionPattern) + 249
11 swift                    0x0000000103150d1d (anonymous namespace)::ArgEmitter::emitShuffle(swift::Expr*, swift::Expr*, llvm::ArrayRef<swift::TupleTypeElt>, swift::ConcreteDeclRef, llvm::ArrayRef<swift::Expr*>, llvm::ArrayRef<int>, llvm::ArrayRef<unsigned int>, swift::Type, swift::Lowering::AbstractionPattern) + 3885
12 swift                    0x000000010314eca3 (anonymous namespace)::ArgEmitter::emitExpanded(swift::Lowering::ArgumentSource&&, swift::Lowering::AbstractionPattern) + 2131
13 swift                    0x000000010314be29 (anonymous namespace)::ArgEmitter::emit(swift::Lowering::ArgumentSource&&, swift::Lowering::AbstractionPattern) + 249
14 swift                    0x000000010314b407 (anonymous namespace)::CallSite::emit(swift::Lowering::SILGenFunction&, swift::Lowering::AbstractionPattern, (anonymous namespace)::ParamLowering&, llvm::SmallVectorImpl<swift::Lowering::ManagedValue>&, llvm::SmallVectorImpl<std::__1::pair<swift::Lowering::LValue, swift::SILLocation> >&, llvm::Optional<swift::ForeignErrorConvention> const&, swift::ImportAsMemberStatus const&) && + 519
15 swift                    0x000000010314809b (anonymous namespace)::CallEmission::apply(swift::Lowering::SGFContext) + 5339
16 swift                    0x00000001031978c5 swift::ASTVisitor<(anonymous namespace)::RValueEmitter, swift::Lowering::RValue, void, void, void, void, void, swift::Lowering::SGFContext>::visit(swift::Expr*, swift::Lowering::SGFContext) + 117
17 swift                    0x00000001031976e3 swift::Lowering::SILGenFunction::emitExprInto(swift::Expr*, swift::Lowering::Initialization*) + 195
18 swift                    0x0000000103184dd3 swift::Lowering::SILGenFunction::emitPatternBinding(swift::PatternBindingDecl*, unsigned int) + 195
19 swift                    0x000000010313c09d swift::ASTVisitor<swift::Lowering::SILGenFunction, void, void, void, void, void, void>::visit(swift::Decl*) + 125
20 swift                    0x00000001031f5769 swift::ASTVisitor<(anonymous namespace)::StmtEmitter, void, void, void, void, void, void>::visit(swift::Stmt*) + 4169
21 swift                    0x00000001031b5e1a swift::Lowering::SILGenFunction::emitFunction(swift::FuncDecl*) + 314
22 swift                    0x0000000103132055 swift::Lowering::SILGenModule::emitFunction(swift::FuncDecl*)::$_1::operator()(swift::SILFunction*) const + 1877
23 swift                    0x0000000103130c02 swift::Lowering::SILGenModule::emitFunction(swift::FuncDecl*) + 626
24 swift                    0x000000010313db03 swift::Lowering::SILGenModule::emitSourceFile(swift::SourceFile*, unsigned int) + 2195
25 swift                    0x000000010313f62d swift::SILModule::constructSIL(swift::ModuleDecl*, swift::SILOptions&, swift::FileUnit*, llvm::Optional<unsigned int>, bool, bool) + 1629
26 swift                    0x0000000102f77e86 performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*) + 19510
27 swift                    0x0000000102f712b3 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 17859
28 swift                    0x0000000102f2d5cf main + 8239
29 libdyld.dylib            0x00007fff892545ad start + 1
Stack dump:
0.  Program arguments: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -frontend -c -primary-file crash.swift -target x86_64-apple-macosx10.9 -enable-objc-interop -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk -color-diagnostics -module-name crash -o /var/folders/.../crash-9f8735.o 
1.  While emitting SIL for 'f' at crash.swift:1:1
2.  While emitting reabstraction thunk in SIL function @_TTRXFo_dSidSi_dSidSi_XFo_lTSiSi__dSidSi_<unknown>:0: error: unable to execute command: Segmentation fault: 11
belkadan commented 7 years ago
   Unhandled conversion to exploded tuple
UNREACHABLE executed at /Volumes/Data/swift-public/swift/lib/SILGen/SILGenPoly.cpp:962!

1.  While emitting SIL for 'f' at <stdin>:1:1
2.  While emitting reabstraction thunk in SIL function @_TTRXFo_dSidSi_dSi_XFo_lTSiSi__dSi_

@slavapestov, you've been looking at things like this...

slavapestov commented 7 years ago

I believe in this case it's actually Sema producing invalid AST, which blows up SILGen.

belkadan commented 7 years ago

Oh, yeah, sorry, I didn't mean it was SILGen's fault, just general things around tuples in closures.

slavapestov commented 7 years ago

Tentatively assigning to @xedin since he's looking at some related issues. Feel free to re-assign πŸ™‚

xedin commented 7 years ago

@slavapestov Thanks! πŸ™‚

rudkx commented 7 years ago

Fixed on master with this merge: https://github.com/apple/swift/commit/c04c6c9c61d149b3a46311df770eb70cfaa67bff

Please confirm with a build that has this fix in it and close the issue if you agree the issue is fixed.