Swift fails to link Array extension defined in framework

Originator:jlieske
Number:rdar://17500497 Date Originated:29-Jun-2014 04:55 PM
Status:Open Resolved:
Product:Developer Tools Product Version:Xcode6 beta: Version 6.0 (6A216f)
Classification:Other Bug Reproducible:Always
 
The Swift language supports extending standard library types with additional methods.  Such extensions should be exportable from modules/frameworks.  However, the Beta2 compiler either crashes or has a linkage error when attempting to use such an extension.  

For example, this is an extension on Array:

extension Array {
    var lastObject: T? { get {
        let n = self.count
        if n > 0 {
            return self[n-1]
        }
        else {
            return nil
        }
    }}
}

1. If that extension is defined in a framework module, code in the main application compiles but does not link:

Undefined symbols for architecture x86_64:
  "__TFSag10lastObjectGSqQ_", referenced from:
      __TFC12PlaySwiftApp11AppDelegate11applicationfS0_FTCSo13UIApplication29didFinishLaunchingWithOptionsGSqCSo12NSDictionary__Sb in AppDelegate.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

2. If that extension is defined in a framework module, code in the accompanying test module causes a compiler error:

0  swift                    0x000000010fa84e08 llvm::sys::PrintStackTrace(__sFILE*) + 40
1  swift                    0x000000010fa852f4 SignalHandler(int) + 452
2  libsystem_platform.dylib 0x00007fff8c67b5aa _sigtramp + 26
3  libsystem_platform.dylib 0x00007fff00000002 _sigtramp + 1939360370
4  swift                    0x000000010ee93cc7 emitNominalMetadataRef(swift::irgen::IRGenFunction&, swift::NominalTypeDecl*, swift::CanType) + 599
5  swift                    0x000000010ee829fc llvm::Value* swift::CanTypeVisitor<(anonymous namespace)::EmitTypeMetadataRef, llvm::Value*>::visit<>(swift::CanType) + 124
6  swift                    0x000000010ee82975 swift::irgen::IRGenFunction::emitTypeMetadataRef(swift::CanType) + 21
7  swift                    0x000000010eea59f9 swift::irgen::WitnessSizedTypeInfo<(anonymous namespace)::NonFixedEnumTypeInfo>::allocateStack(swift::irgen::IRGenFunction&, swift::CanType, llvm::Twine const&) const + 89
8  swift                    0x000000010eef2782 swift::SILVisitor<(anonymous namespace)::IRGenSILFunction, void>::visit(swift::ValueBase*) + 34626
9  swift                    0x000000010eee9846 swift::irgen::IRGenModule::emitSILFunction(swift::SILFunction*) + 8678
10 swift                    0x000000010ee6acd8 swift::irgen::IRGenModule::emitGlobalTopLevel() + 184
11 swift                    0x000000010eed6cc3 performIRGeneration(swift::IRGenOptions&, swift::Module*, swift::SILModule*, llvm::StringRef, llvm::LLVMContext&, swift::SourceFile*, unsigned int) + 1859
12 swift                    0x000000010eed7613 swift::performIRGeneration(swift::IRGenOptions&, swift::SourceFile&, swift::SILModule*, llvm::StringRef, llvm::LLVMContext&, unsigned int) + 51
13 swift                    0x000000010ee4995a frontend_main(llvm::ArrayRef<char const*>, char const*, void*) + 4842
14 swift                    0x000000010ee4865d main + 1533
15 libdyld.dylib            0x00007fff947d05fd start + 1
Stack dump:

1.	While emitting IR SIL function @_TFC14PlaySwiftTests13JaySwiftTests19testArrayLastObjectfS0_FT_T_ for 'testArrayLastObject' at JaySwiftTests.swift:14:5
<unknown>:0: error: unable to execute command: Segmentation fault: 11
<unknown>:0: error: swift frontend command failed due to signal (use -v to see invocation)
Command /Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift failed with exit code 254

Comments

The problem still exists Xcode 6.3, though the error message changed after the betas. Declaring an extension like the following:

public extension Array { public var myLastObject: T? { get { let n = self.count if n > 0 { return self[n-1] } else { return nil } } } }

Xcode 6.2 and 6.3 beta 2 report the following error:

/Users/jay/Coding/Swift/TrySwift/TryFramework/TryArrayExtension.swift:12:16: Extension of generic type 'Array' from a different module cannot provide public declarations


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!