Skip to content

Instantly share code, notes, and snippets.

@ole
Created September 8, 2016 13:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ole/f33d64c06ca4517261c3fdab67830b4e to your computer and use it in GitHub Desktop.
Save ole/f33d64c06ca4517261c3fdab67830b4e to your computer and use it in GitHub Desktop.
Compiler segmentation fault in Xcode 8 GM using ExpressibleByArrayLiteral
// Save this to stack.swift, then run
//
// $ xcrun swift stack.swift
//
// Does it compile or does the compiler segfault?
struct Stack<Element> {
var elements: [Element] = []
}
extension Stack: ExpressibleByArrayLiteral {
init(arrayLiteral elements: Element...) {
self.elements = elements
}
}
@ole
Copy link
Author

ole commented Sep 8, 2016

I am seeing this with Xcode 8 GM seed:

 $ xcrun swift --version
Apple Swift version 3.0 (swiftlang-800.0.46.2 clang-800.0.38)
Target: x86_64-apple-macosx10.9
$ xcrun swift stack.swift
0  swift                    0x000000010558fb6d PrintStackTraceSignalHandler(void*) + 45
1  swift                    0x000000010558f5b6 SignalHandler(int) + 470
2  libsystem_platform.dylib 0x00007fff8e5a952a _sigtramp + 26
3  libsystem_platform.dylib 0x00007fff5d2ca6d0 _sigtramp + 3469873600
4  swift                    0x0000000102a6d8b9 swift::irgen::IRGenFunction::emitTypeMetadataRef(swift::CanType) + 73
5  swift                    0x0000000102aab07d void llvm::function_ref<void (swift::irgen::GenericRequirement)>::callback_fn<(anonymous namespace)::EmitPolymorphicArguments::emit(swift::CanTypeWrapper<swift::SILFunctionType>, llvm::ArrayRef<swift::Substitution>, swift::irgen::WitnessMetadata*, swift::irgen::Explosion&)::$_14>(long, swift::irgen::GenericRequirement) + 861
6  swift                    0x0000000102aa09e1 (anonymous namespace)::PolymorphicConvention::enumerateRequirements(llvm::function_ref<void (swift::irgen::GenericRequirement)> const&) + 129
7  swift                    0x0000000102aaabdb swift::irgen::emitPolymorphicArguments(swift::irgen::IRGenFunction&, swift::CanTypeWrapper<swift::SILFunctionType>, swift::CanTypeWrapper<swift::SILFunctionType>, llvm::ArrayRef<swift::Substitution>, swift::irgen::WitnessMetadata*, swift::irgen::Explosion&) + 459
8  swift                    0x0000000102b074f5 (anonymous namespace)::IRGenSILFunction::visitFullApplySite(swift::FullApplySite) + 2997
9  swift                    0x0000000102af2268 swift::irgen::IRGenModule::emitSILFunction(swift::SILFunction*) + 9080
10 swift                    0x0000000102a184d0 swift::irgen::IRGenerator::emitLazyDefinitions() + 5216
11 swift                    0x0000000102ad819b performIRGeneration(swift::IRGenOptions&, swift::ModuleDecl*, swift::SILModule*, llvm::StringRef, llvm::LLVMContext&, swift::SourceFile*, unsigned int) + 1723
12 swift                    0x0000000102ad6507 swift::performIRGeneration(swift::IRGenOptions&, swift::ModuleDecl*, swift::SILModule*, llvm::StringRef, llvm::LLVMContext&) + 1527
13 swift                    0x00000001029baa7b swift::RunImmediately(swift::CompilerInstance&, std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&, swift::IRGenOptions&, swift::SILOptions const&) + 187
14 swift                    0x00000001029a607e performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*) + 23358
15 swift                    0x000000010299e265 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 17029
16 swift                    0x000000010295b82d main + 8685
17 libdyld.dylib            0x00007fff91eef5ad start + 1
18 libdyld.dylib            0x000000000000000c start + 1846610528
Stack dump:
0.      Program arguments: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -frontend -interpret stack.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 stack
1.      While emitting IR SIL function @_TFV5stack5StackCft12arrayLiteralGSax__GS0_x_ for 'init' at stack.swift:6:5
[1]    38858 segmentation fault  xcrun swift stack.swift
$

Using DEVELOPMENT-SNAPSHOT-2016-09-07-a, it compiles fine:

$ swift --version                                15:13:28
Apple Swift version 3.0-dev (LLVM f54fa77021, Clang 0e6c928036, Swift 016bc24998)
Target: x86_64-apple-macosx10.9
$ swift ~/code/stack.swift
$

It also compiles fine with older snapshots, such as DEVELOPMENT-SNAPSHOT-2016-08-07-a, which is the oldest one I still have installed.

@okket
Copy link

okket commented Sep 8, 2016

Same here:

→ xcrun swift stack.swift
0  swift                    0x0000000109cb8b6d PrintStackTraceSignalHandler(void*) + 45
1  swift                    0x0000000109cb85b6 SignalHandler(int) + 470
2  libsystem_platform.dylib 0x00007fffa3b15bba _sigtramp + 26
3  libsystem_platform.dylib 0x00007fff58ba1320 _sigtramp + 3037247360
4  swift                    0x00000001071968b9 swift::irgen::IRGenFunction::emitTypeMetadataRef(swift::CanType) + 73
5  swift                    0x00000001071d407d void llvm::function_ref<void (swift::irgen::GenericRequirement)>::callback_fn<(anonymous namespace)::EmitPolymorphicArguments::emit(swift::CanTypeWrapper<swift::SILFunctionType>, llvm::ArrayRef<swift::Substitution>, swift::irgen::WitnessMetadata*, swift::irgen::Explosion&)::$_14>(long, swift::irgen::GenericRequirement) + 861
6  swift                    0x00000001071c99e1 (anonymous namespace)::PolymorphicConvention::enumerateRequirements(llvm::function_ref<void (swift::irgen::GenericRequirement)> const&) + 129
7  swift                    0x00000001071d3bdb swift::irgen::emitPolymorphicArguments(swift::irgen::IRGenFunction&, swift::CanTypeWrapper<swift::SILFunctionType>, swift::CanTypeWrapper<swift::SILFunctionType>, llvm::ArrayRef<swift::Substitution>, swift::irgen::WitnessMetadata*, swift::irgen::Explosion&) + 459
8  swift                    0x00000001072304f5 (anonymous namespace)::IRGenSILFunction::visitFullApplySite(swift::FullApplySite) + 2997
9  swift                    0x000000010721b268 swift::irgen::IRGenModule::emitSILFunction(swift::SILFunction*) + 9080
10 swift                    0x00000001071414d0 swift::irgen::IRGenerator::emitLazyDefinitions() + 5216
11 swift                    0x000000010720119b performIRGeneration(swift::IRGenOptions&, swift::ModuleDecl*, swift::SILModule*, llvm::StringRef, llvm::LLVMContext&, swift::SourceFile*, unsigned int) + 1723
12 swift                    0x00000001071ff507 swift::performIRGeneration(swift::IRGenOptions&, swift::ModuleDecl*, swift::SILModule*, llvm::StringRef, llvm::LLVMContext&) + 1527
13 swift                    0x00000001070e3a7b swift::RunImmediately(swift::CompilerInstance&, std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&, swift::IRGenOptions&, swift::SILOptions const&) + 187
14 swift                    0x00000001070cf07e performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*) + 23358
15 swift                    0x00000001070c7265 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 17029
16 swift                    0x000000010708482d main + 8685
17 libdyld.dylib            0x00007fffa3909255 start + 1
Stack dump:
0.  Program arguments: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -frontend -interpret stack.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 stack 
1.  While emitting IR SIL function @_TFV5stack5StackCft12arrayLiteralGSax__GS0_x_ for 'init' at stack.swift:12:7
Segmentation fault: 11
→ swift --version
Apple Swift version 3.0 (swiftlang-800.0.46.2 clang-800.0.38)
Target: x86_64-apple-macosx10.9

@chrisfsampaio
Copy link

If you need a workaround for the time being, this seems to compile fine:

struct Stack<Element> {
    var elements: [Element]
    init(elements: [Element]) {
        self.elements = elements
    }
}

extension Stack: ExpressibleByArrayLiteral {
    init(arrayLiteral elements: Element...) {
        self.elements = elements
    }
}

The problem seems to be related to the inline instantiation of the elements array.

@ole
Copy link
Author

ole commented Sep 8, 2016

@chrisfsampaio Ah, that's interesting, thanks. Another workaround is to explicitly call self.init from the extension:

struct Stack<Element> {
    var elements: [Element] = []
}

extension Stack: ExpressibleByArrayLiteral {
    init(arrayLiteral elements: Element...) {
        self.init(elements: elements)
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment