14

The following Swift code generates a compile error at build time:

import Foundation

class Wrapper<T> : NSObject {
    let obj : T

    init(x : T) {
        self.obj = x
    }
}

Am I doing something wrong or is this a compiler bug?

If so, what could I do to work around it?

The error log:

CompileSwift normal i386 com.apple.xcode.tools.swift.compiler
    cd /Users/hermespique/workspace/HanekeSwift
    export PATH="/Applications/Xcode6-Beta.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin:/Applications/Xcode6-Beta.app/Contents/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin"
    /Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -target i386-apple-ios8.0 -module-name HanekeTests -O0 -sdk /Applications/Xcode6-Beta.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator8.0.sdk -g -module-cache-path /Users/hermespique/Library/Developer/Xcode/DerivedData/ModuleCache -I /Users/hermespique/Library/Developer/Xcode/DerivedData/Haneke-dsgqpxenngbaqpctbkhhwtqbiorj/Build/Products/Debug-iphonesimulator -F /Users/hermespique/Library/Developer/Xcode/DerivedData/Haneke-dsgqpxenngbaqpctbkhhwtqbiorj/Build/Products/Debug-iphonesimulator -F /Applications/Xcode6-Beta.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator8.0.sdk/Developer/Library/Frameworks -F /Applications/Xcode6-Beta.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/Library/Frameworks -F /Applications/Xcode6-Beta.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator8.0.sdk/Developer/Library/Frameworks -c -j4 /Users/hermespique/workspace/HanekeSwift/HanekeTests/XCTestCase+Assert.swift /Users/hermespique/workspace/HanekeSwift/HanekeTests/Cache.swift /Users/hermespique/workspace/HanekeSwift/HanekeTests/Wrapper.swift -output-file-map /Users/hermespique/Library/Developer/Xcode/DerivedData/Haneke-dsgqpxenngbaqpctbkhhwtqbiorj/Build/Intermediates/Haneke.build/Debug-iphonesimulator/HanekeTests.build/Objects-normal/i386/HanekeTests-OutputFileMap.json -serialize-diagnostics -emit-dependencies -emit-module -emit-module-path /Users/hermespique/Library/Developer/Xcode/DerivedData/Haneke-dsgqpxenngbaqpctbkhhwtqbiorj/Build/Intermediates/Haneke.build/Debug-iphonesimulator/HanekeTests.build/Objects-normal/i386/HanekeTests.swiftmodule -Xcc -iquote -Xcc /Users/hermespique/Library/Developer/Xcode/DerivedData/Haneke-dsgqpxenngbaqpctbkhhwtqbiorj/Build/Intermediates/Haneke.build/Debug-iphonesimulator/HanekeTests.build/HanekeTests-generated-files.hmap -Xcc -I/Users/hermespique/Library/Developer/Xcode/DerivedData/Haneke-dsgqpxenngbaqpctbkhhwtqbiorj/Build/Intermediates/Haneke.build/Debug-iphonesimulator/HanekeTests.build/HanekeTests-own-target-headers.hmap -Xcc -ivfsoverlay -Xcc /Users/hermespique/Library/Developer/Xcode/DerivedData/Haneke-dsgqpxenngbaqpctbkhhwtqbiorj/Build/Intermediates/Haneke.build/all-product-headers.yaml -Xcc -iquote -Xcc /Users/hermespique/Library/Developer/Xcode/DerivedData/Haneke-dsgqpxenngbaqpctbkhhwtqbiorj/Build/Intermediates/Haneke.build/Debug-iphonesimulator/HanekeTests.build/HanekeTests-project-headers.hmap -Xcc -I/Users/hermespique/Library/Developer/Xcode/DerivedData/Haneke-dsgqpxenngbaqpctbkhhwtqbiorj/Build/Products/Debug-iphonesimulator/include -Xcc -I/Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include -Xcc -I/Users/hermespique/Library/Developer/Xcode/DerivedData/Haneke-dsgqpxenngbaqpctbkhhwtqbiorj/Build/Intermediates/Haneke.build/Debug-iphonesimulator/HanekeTests.build/DerivedSources/i386 -Xcc -I/Users/hermespique/Library/Developer/Xcode/DerivedData/Haneke-dsgqpxenngbaqpctbkhhwtqbiorj/Build/Intermediates/Haneke.build/Debug-iphonesimulator/HanekeTests.build/DerivedSources -Xcc -DDEBUG=1 -Xcc -DDEBUG=1 -emit-objc-header -emit-objc-header-path /Users/hermespique/Library/Developer/Xcode/DerivedData/Haneke-dsgqpxenngbaqpctbkhhwtqbiorj/Build/Intermediates/Haneke.build/Debug-iphonesimulator/HanekeTests.build/Objects-normal/i386/HanekeTests-Swift.h

0  swift                    0x000000010a889608 llvm::sys::PrintStackTrace(__sFILE*) + 40
1  swift                    0x000000010a889af4 SignalHandler(int) + 452
2  libsystem_platform.dylib 0x00007fff88e185aa _sigtramp + 26
3  libsystem_platform.dylib 0x00007fff55fb0334 _sigtramp + 3440999844
4  swift                    0x0000000109c87395 swift::irgen::IRGenFunction::emitTypeMetadataRef(swift::CanType) + 21
5  swift                    0x0000000109cbcfb8 (anonymous namespace)::OpaqueArchetypeTypeInfo::destroy(swift::irgen::IRGenFunction&, swift::irgen::Address, swift::CanType) const + 24
6  swift                    0x0000000109cf467b swift::SILVisitor<(anonymous namespace)::IRGenSILFunction, void>::visit(swift::ValueBase*) + 23579
7  swift                    0x0000000109cee266 swift::irgen::IRGenModule::emitSILFunction(swift::SILFunction*) + 8678
8  swift                    0x0000000109c6f6f8 swift::irgen::IRGenModule::emitGlobalTopLevel() + 184
9  swift                    0x0000000109cdb6e3 performIRGeneration(swift::IRGenOptions&, swift::Module*, swift::SILModule*, llvm::StringRef, llvm::LLVMContext&, swift::SourceFile*, unsigned int) + 1859
10 swift                    0x0000000109cdc033 swift::performIRGeneration(swift::IRGenOptions&, swift::SourceFile&, swift::SILModule*, llvm::StringRef, llvm::LLVMContext&, unsigned int) + 51
11 swift                    0x0000000109c4e65a frontend_main(llvm::ArrayRef<char const*>, char const*, void*) + 4842
12 swift                    0x0000000109c4d35d main + 1533
13 libdyld.dylib            0x00007fff8ddbb5fd start + 1
14 libdyld.dylib            0x0000000000000040 start + 1914980932
Stack dump:
0.  Program arguments: /Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -frontend -c /Users/hermespique/workspace/HanekeSwift/HanekeTests/XCTestCase+Assert.swift /Users/hermespique/workspace/HanekeSwift/HanekeTests/Cache.swift -primary-file /Users/hermespique/workspace/HanekeSwift/HanekeTests/Wrapper.swift -enable-objc-attr-requires-objc-module -target i386-apple-ios8.0 -module-name HanekeTests -sdk /Applications/Xcode6-Beta.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator8.0.sdk -I /Users/hermespique/Library/Developer/Xcode/DerivedData/Haneke-dsgqpxenngbaqpctbkhhwtqbiorj/Build/Products/Debug-iphonesimulator -F /Users/hermespique/Library/Developer/Xcode/DerivedData/Haneke-dsgqpxenngbaqpctbkhhwtqbiorj/Build/Products/Debug-iphonesimulator -F /Applications/Xcode6-Beta.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator8.0.sdk/Developer/Library/Frameworks -F /Applications/Xcode6-Beta.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/Library/Frameworks -F /Applications/Xcode6-Beta.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator8.0.sdk/Developer/Library/Frameworks -g -module-cache-path /Users/hermespique/Library/Developer/Xcode/DerivedData/ModuleCache -Xcc -iquote -Xcc /Users/hermespique/Library/Developer/Xcode/DerivedData/Haneke-dsgqpxenngbaqpctbkhhwtqbiorj/Build/Intermediates/Haneke.build/Debug-iphonesimulator/HanekeTests.build/HanekeTests-generated-files.hmap -Xcc -I/Users/hermespique/Library/Developer/Xcode/DerivedData/Haneke-dsgqpxenngbaqpctbkhhwtqbiorj/Build/Intermediates/Haneke.build/Debug-iphonesimulator/HanekeTests.build/HanekeTests-own-target-headers.hmap -Xcc -ivfsoverlay -Xcc /Users/hermespique/Library/Developer/Xcode/DerivedData/Haneke-dsgqpxenngbaqpctbkhhwtqbiorj/Build/Intermediates/Haneke.build/all-product-headers.yaml -Xcc -iquote -Xcc /Users/hermespique/Library/Developer/Xcode/DerivedData/Haneke-dsgqpxenngbaqpctbkhhwtqbiorj/Build/Intermediates/Haneke.build/Debug-iphonesimulator/HanekeTests.build/HanekeTests-project-headers.hmap -Xcc -I/Users/hermespique/Library/Developer/Xcode/DerivedData/Haneke-dsgqpxenngbaqpctbkhhwtqbiorj/Build/Products/Debug-iphonesimulator/include -Xcc -I/Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include -Xcc -I/Users/hermespique/Library/Developer/Xcode/DerivedData/Haneke-dsgqpxenngbaqpctbkhhwtqbiorj/Build/Intermediates/Haneke.build/Debug-iphonesimulator/HanekeTests.build/DerivedSources/i386 -Xcc -I/Users/hermespique/Library/Developer/Xcode/DerivedData/Haneke-dsgqpxenngbaqpctbkhhwtqbiorj/Build/Intermediates/Haneke.build/Debug-iphonesimulator/HanekeTests.build/DerivedSources -Xcc -DDEBUG=1 -Xcc -DDEBUG=1 -emit-module-doc-path /Users/hermespique/Library/Developer/Xcode/DerivedData/Haneke-dsgqpxenngbaqpctbkhhwtqbiorj/Build/Intermediates/Haneke.build/Debug-iphonesimulator/HanekeTests.build/Objects-normal/i386/Wrapper~partial.swiftdoc -O0 -emit-module-path /Users/hermespique/Library/Developer/Xcode/DerivedData/Haneke-dsgqpxenngbaqpctbkhhwtqbiorj/Build/Intermediates/Haneke.build/Debug-iphonesimulator/HanekeTests.build/Objects-normal/i386/Wrapper~partial.swiftmodule -serialize-diagnostics-path /Users/hermespique/Library/Developer/Xcode/DerivedData/Haneke-dsgqpxenngbaqpctbkhhwtqbiorj/Build/Intermediates/Haneke.build/Debug-iphonesimulator/HanekeTests.build/Objects-normal/i386/Wrapper.dia -emit-dependencies-path /Users/hermespique/Library/Developer/Xcode/DerivedData/Haneke-dsgqpxenngbaqpctbkhhwtqbiorj/Build/Intermediates/Haneke.build/Debug-iphonesimulator/HanekeTests.build/Objects-normal/i386/Wrapper.d -o /Users/hermespique/Library/Developer/Xcode/DerivedData/Haneke-dsgqpxenngbaqpctbkhhwtqbiorj/Build/Intermediates/Haneke.build/Debug-iphonesimulator/HanekeTests.build/Objects-normal/i386/Wrapper.o 
1.  While emitting IR SIL function @_TToFC11HanekeTests7WrapperE for 'deinit' at /Users/hermespique/workspace/HanekeSwift/HanekeTests/Wrapper.swift:11:7
<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
hpique
  • 119,096
  • 131
  • 338
  • 476
  • 1
    this looks like a bad compiler error... time file a bug report. it is very to crash compiler when play round generic code – Bryan Chen Jun 11 '14 at 11:22
  • Try deleting the entire init function and the isEqual function and see if it compiles, then add them back in separately to see which one is breaking it if it compiled with none of them. I noticed that subclassing some Apple classes would not compile with the generated init so it is most likely a bug that will be fixed. – Kris Gellci Jun 11 '14 at 12:51
  • @KrisGellci The problem appears to be a combination of subclassing NSObject, using generics, and having variables. Not sure if there's a workaround. – hpique Jun 11 '14 at 13:56
  • here's a more simplified version of the problem: `class Wrapper : NSObject { let obj : T init(x : T) { self.obj = x } }` – newacct Jun 11 '14 at 19:29
  • Thanks @newacct. I edited the question accordingly. – hpique Jun 11 '14 at 22:33
  • I think I already file a bug with this kind before... [rdar://17213742](http://openradar.appspot.com/radar?id=4951382314450944) – Bryan Chen Jun 11 '14 at 22:37
  • @BryanChen I believe this is a different bug, but they might be related. – hpique Jun 11 '14 at 22:40
  • the code is almost the same, except my one is not subclass from `NSObject`. and both of them are related to `swift::irgen`. but you can still file a bug report to increase its priority – Bryan Chen Jun 11 '14 at 22:44
  • 1
    I've noted a similar issue here without NSObject being involved. Noted here: http://stackoverflow.com/questions/24193957/error-when-using-generic-as-property-type-in-swift – michaelavila Jun 13 '14 at 03:27

6 Answers6

6

I found a workaround to this problem

class Wrapper<T> {
    let _val: T[]

    init(_ v: T) {
        _val = [v]
    }

    var val : T {
        get {
            return _val[0]
        }
        set {
            _val[0] = newValue
        }
    }
}

Since Array is a reference type (even though it is struct), the size of Wrapper class is now fixed and compiler can handle it.

Bryan Chen
  • 45,816
  • 18
  • 112
  • 143
3

I think its an X-code bug with generics and NSObject

class Foo<T>:NSObject{
    let _foo:T?

    init(foo:T){
        _foo  = foo
    }
}

gives swiftc failed with exit code 254

Removing the NSObject Works OK. ie:

class Foo<T>{
    let _foo:T?

    init(foo:T){
        _foo  = foo
    }
}

Or adding a constraint to the generic seems to work. ie:

class Foo<T:AnyObject>:NSObject{
    let _foo:T?

    init(foo:T){
        _foo  = foo
    }
}
Friggles
  • 634
  • 5
  • 7
0

I fiddled with your interesting (+1) snippet of code, and here's the closest code I could get while avoiding the compiler error:

struct Wrapper<T>  {
    var obj : T

    init(_ x : T) {
        obj = x
    }
}

So, if you don't really need Wrapper to be a class, then you'll be able to move on... until next release of Xcode ;)

Jean Le Moignan
  • 22,158
  • 3
  • 31
  • 37
0

As I see it, generics do not like NSObjects types, only AnyObject ones..

NSObject does not conform to AnyObject (they are different by nature and functionality) .. meaning you cannot use Array instead of NSArray if you need to create a generic method. I think this is the reason they left NSArray in swift .. because they still have NS.. things left behind .. The most important one is CoreData. I was trying to make a generic NSManagedObject class to derive from, that would use this powerful feature (generics).. I couldn't do it.

Stefan
  • 762
  • 1
  • 8
  • 11
0

This little code snippet (another +1 from me) still causes a compiler crash in Xcode 7 beta 1, I guess it is intended that the code is not a valid class definition. A polite error message instead of a crash would be appreciated if anyone at Cupertino listens in to this thread.

I resolved it by constraining the generic parameter to implement NSObjectProtocol

class Wrapper<T:NSObjectProtocol> : NSObject
{
   let obj : T
   init(x : T) { self.obj = x }
}

This was useful for me when constructing a function that matches Wrapper<T> whenever T is a subclass of NSManagedObject.

Lev Landau
  • 788
  • 3
  • 16
0

For Swift 2.0. This issue is no longer valid.

You can have a class like this and the compiler will not complain.

class Wrapper<T> : NSObject {
    let obj : T

    init(x : T) {
        self.obj = x
    }
}
mohamede1945
  • 7,092
  • 6
  • 47
  • 61