Swift compiler error in for loop - swift

When I try to list the available fonts in the system with the code below, I get the error message attached. Is anything wrong with my code or is this a compiler bug?
for fontFamilyName: String! in UIFont.familyNames() {
for fontName: String! in UIFont.fontNamesForFamilyName(fontFamilyName) {
println("font: \(fontName)")
}
}
Error message:
0 swift 0x0000000101678e08 llvm::sys::PrintStackTrace(__sFILE*) + 40
1 swift 0x00000001016792f4 SignalHandler(int) + 452
2 libsystem_platform.dylib 0x00007fff8968a5aa _sigtramp + 26
3 libsystem_platform.dylib 0x0000000102a79a00 _sigtramp + 2034168944
4 swift 0x00000001015759b7 llvm::StoreInst::StoreInst(llvm::Value*, llvm::Value*, bool, llvm::Instruction*) + 55
5 swift 0x0000000100aca412 swift::irgen::IRBuilder::CreateStore(llvm::Value*, llvm::Value*, swift::irgen::Alignment) + 66
6 swift 0x0000000100a9737c (anonymous namespace)::PayloadEnumImplStrategyBase::initialize(swift::irgen::IRGenFunction&, swift::irgen::Explosion&, swift::irgen::Address) const + 124
7 swift 0x0000000100abd070 swift::irgen::SequentialTypeInfo<(anonymous namespace)::LoadableStructTypeInfo, swift::irgen::LoadableTypeInfo, (anonymous namespace)::StructFieldInfo, true>::initialize(swift::irgen::IRGenFunction&, swift::irgen::Explosion&, swift::irgen::Address) const + 224
8 swift 0x0000000100abd070 swift::irgen::SequentialTypeInfo<(anonymous namespace)::LoadableStructTypeInfo, swift::irgen::LoadableTypeInfo, (anonymous namespace)::StructFieldInfo, true>::initialize(swift::irgen::IRGenFunction&, swift::irgen::Explosion&, swift::irgen::Address) const + 224
9 swift 0x0000000100ae5a48 swift::SILVisitor<(anonymous namespace)::IRGenSILFunction, void>::visit(swift::ValueBase*) + 31240
10 swift 0x0000000100add846 swift::irgen::IRGenModule::emitSILFunction(swift::SILFunction*) + 8678
11 swift 0x0000000100a5ecd8 swift::irgen::IRGenModule::emitGlobalTopLevel() + 184
12 swift 0x0000000100acacc3 performIRGeneration(swift::IRGenOptions&, swift::Module*, swift::SILModule*, llvm::StringRef, llvm::LLVMContext&, swift::SourceFile*, unsigned int) + 1859
13 swift 0x0000000100acb613 swift::performIRGeneration(swift::IRGenOptions&, swift::SourceFile&, swift::SILModule*, llvm::StringRef, llvm::LLVMContext&, unsigned int) + 51
14 swift 0x0000000100a3d95a frontend_main(llvm::ArrayRef<char const*>, char const*, void*) + 4842
15 swift 0x0000000100a3c65d main + 1533
16 libdyld.dylib 0x00007fff889d05fd start + 1
1. While emitting IR SIL function #_TFC10SwiftTests11AppDelegate11applicationfS0_FTCSo13UIApplication29didFinishLaunchingWithOptionsGSqCSo12NSDictionary__Sb for 'application' at /Users/hb/Developer/martsys/SwiftTests/SwiftTests/AppDelegate.swift:15:3
<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)

You will be better off by casting the arrays returned from your class methods, like so:
for fontFamilyName in UIFont.familyNames() as Array<String!> {
for fontName in UIFont.fontNamesForFamilyName(fontFamilyName) as Array<String!> {
println("font: \(fontName)")
}
}
// prints fontFamilyName: Marion,
// font: Marion-Italic
// font: Marion-Bold
// font: Marion-Regular
// fontFamilyName: Copperplate
// font: Copperplate-Light
// etc.

Both, I think. Your code is wrong in such a way that it exposes a compiler bug. It appears to be blowing up on the type assertion String!. I think that this is because the return type of UIFont.familyNames() is AnyObject[]!. The ! here means it is forcibly unwrapped, so you expect to get back an AnyObject[] not a Optional<AnyObject>. So String! isn't Optional, which should be an error with a nice message, not something that crashes Xcode.
What you need to do instead is tell the compiler that the AnyObject[] it is getting back is of type String[]:
for fontFamilyName in UIFont.familyNames() as String[] {
for fontName in UIFont.fontNamesForFamilyName(fontFamilyName) as String[] {
println("font: \(fontName)")
}
}

Related

Swift 5.2 crash compiler, protocol method reference

The following code snippet crashes the Swift 5.2 compiler with a segmentation fault.
protocol Something {
func add(_ n: Int)
}
let function = Something.add
What is the problem here?
Here is the stacktrace
1. Apple Swift version 5.2.2 (swiftlang-1103.0.32.6 clang-1103.0.32.51)
0 swift 0x000000011369f4ea PrintStackTraceSignalHandler(void*) + 42
1 swift 0x000000011369ecc0 SignalHandler(int) + 352
2 libsystem_platform.dylib 0x00007fff6db7d5fd _sigtramp + 29
3 libsystem_platform.dylib 0x00007ffee08b1f48 _sigtramp + 1926449512
4 swift 0x000000010f8885fa swift::Lowering::SILGenFunction::emitOpenExistentialExprImpl(swift::OpenExistentialExpr*, llvm::function_ref<void (swift::Expr*)>) + 666
5 swift 0x000000010f87979a swift::ASTVisitor<(anonymous namespace)::RValueEmitter, swift::Lowering::RValue, void, void, void, void, void, swift::Lowering::SGFContext>::visit(swift::Expr*, swift::Lowering::SGFContext) + 38938
6 swift 0x000000010f86fd93 swift::Lowering::SILGenFunction::emitExprInto(swift::Expr*, swift::Lowering::Initialization*, llvm::Optional<swift::SILLocation>) + 131
7 swift 0x000000010f863c18 swift::Lowering::SILGenFunction::emitPatternBinding(swift::PatternBindingDecl*, unsigned int) + 280
8 swift 0x000000010f80ec5d swift::ASTVisitor<swift::Lowering::SILGenFunction, void, void, void, void, void, void>::visit(swift::Decl*) + 109
9 swift 0x000000010f8105f1 swift::ASTVisitor<swift::Lowering::SILGenModule, void, void, void, void, void, void>::visit(swift::Decl*) + 1265
10 swift 0x000000010f80f31c swift::Lowering::SILGenModule::emitSourceFile(swift::SourceFile*) + 1356
11 swift 0x000000010f81139e swift::SILModule::constructSIL(swift::ModuleDecl*, swift::Lowering::TypeConverter&, swift::SILOptions&, swift::FileUnit*) + 1438
12 swift 0x000000010f3f1131 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 48065
13 swift 0x000000010f368f53 main + 1283
14 libdyld.dylib 0x00007fff6d984cc9 start + 1
15 libdyld.dylib 0x0000000000000045 start + 2456269693
error: Segmentation fault: 11
Looks like this is a know bug in the Swift 5.2 compiler. It should be fixed in Swift 5.3 with support for this pattern. See here https://bugs.swift.org/browse/SR-75

Swift compiler crash - anyone seen this?

I have an Obj-C class extending NSObject. One of its properties is an NS_ENUM; let's call it direction.
Then I have a Swift class with an optional property of this Obj-C class; let's call that message.
The following example code is fine:
switch message?.direction
{
case .Inbound:
...
case .OutBound:
...
case nil:
...
}
However, if I then introduce:
guard let message = self.message else { return }
and remove the ? in the code above, the compiler crashes; see below.
It seems to get confused by case nil:, because the crash disappears if I delete it; it's of course no longer needed.
Just curious, has anyone seen this Swift compiler crash?
1. Apple Swift version 5.2 (swiftlang-1103.0.32.1 clang-1103.0.32.29)
2. While emitting IR SIL function "#$s7SomeFunction...".
for 'update()' (at /Users/.../SomeFile.swift:83:11)
0 swift 0x00000001133554ea PrintStackTraceSignalHandler(void*) + 42
1 swift 0x0000000113354cc0 SignalHandler(int) + 352
2 libsystem_platform.dylib 0x00007fff6b8d75fd _sigtramp + 29
3 swift 0x000000010f1aaa4c (anonymous namespace)::SinglePayloadEnumImplStrategy::consume(swift::irgen::IRGenFunction&, swift::irgen::Explosion&, swift::irgen::Atomicity) const + 1244
4 swift 0x00000001131d082c llvm::IRBuilderBase::CreateLifetimeEnd(llvm::Value*, llvm::ConstantInt*) + 28
5 swift 0x000000010f2105a1 swift::irgen::FixedTypeInfo::deallocateStack(swift::irgen::IRGenFunction&, swift::irgen::StackAddress, swift::SILType) const + 81
6 swift 0x000000010f2e8244 swift::SILInstructionVisitor<(anonymous namespace)::IRGenSILFunction, void>::visit(swift::SILInstruction*) + 33828
7 swift 0x000000010f2dc8e3 swift::irgen::IRGenModule::emitSILFunction(swift::SILFunction*) + 8835
8 swift 0x000000010f190032 swift::irgen::IRGenerator::emitGlobalTopLevel() + 1410
9 swift 0x000000010f2baed9 performIRGeneration(swift::IRGenOptions&, swift::ModuleDecl*, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule> >, llvm::StringRef, swift::PrimarySpecificPaths const&, llvm::LLVMContext&, swift::SourceFile*, llvm::GlobalVariable**) + 1097
10 swift 0x000000010f0b236f performCompileStepsPostSILGen(swift::CompilerInstance&, swift::CompilerInvocation&, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule> >, bool, llvm::PointerUnion<swift::ModuleDecl*, swift::SourceFile*>, swift::PrimarySpecificPaths const&, bool, int&, swift::FrontendObserver*, swift::UnifiedStatsReporter*) + 4255
11 swift 0x000000010f0a77c0 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 48416
12 swift 0x000000010f01f4d3 main + 1283
13 libdyld.dylib 0x00007fff6b6decc9 start + 1
error: Segmentation fault: 11 (in target 'SomeApp' from project 'SomeApp')

When I archive a iOS project, there has a error: Segmentation fault: 11

MacOS: 10.14.6 - Xcode: Version 11.3.1 (11C504)
I create a demo project in github. if anyone can help me, you can check it
https://github.com/philCc/URLNavigatorExt.git branch:Segmentation_fault_11
1. While running pass #153921 SILFunctionTransform "DCE" on SILFunction "#$s23URLNavigatorExt_Example6RouterO9PRWebviewV8instance2byAESgSDyS2SG_tFZ".
for 'instance(by:)' (at /Users/phil/Documents/WorkSpace/ky_tech/URLNavigatorExt/Example/URLNavigatorExt/generated/Router.generated.swift:989:20)
0 swift 0x000000010e1cca63 PrintStackTraceSignalHandler(void*) + 51
1 swift 0x000000010e1cc236 SignalHandler(int) + 358
2 libsystem_platform.dylib 0x00007fff5b7bfb5d _sigtramp + 29
3 libsystem_platform.dylib 0x000000010f830600 _sigtramp + 3020360384
4 swift 0x000000010a53a370 (anonymous namespace)::DCE::markControllingTerminatorsLive(swift::SILBasicBlock*) + 800
5 swift 0x000000010a539ca2 (anonymous namespace)::DCE::markValueLive(swift::SILNode*) + 466
6 swift 0x000000010a5375e8 (anonymous namespace)::DCE::run() + 2728
7 swift 0x000000010a4bef3e swift::SILPassManager::execute() + 4606
8 swift 0x000000010a11d96b swift::CompilerInstance::performSILProcessing(swift::SILModule*, swift::UnifiedStatsReporter*) + 6379
9 swift 0x0000000109e15ec5 performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*, swift::UnifiedStatsReporter*) + 33925
10 swift 0x0000000109e0a234 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 6820
11 swift 0x0000000109d97733 main + 1219
12 libdyld.dylib 0x00007fff5b5d43d5 start + 1
13 libdyld.dylib 0x0000000000000056 start + 2762128514
error: Segmentation fault: 11 (in target 'URLNavigatorExt_Example' from project 'URLNavigatorExt')
if change the line: 993
then archive will be successful
var _url: String?
// to
var _url: String? = ""
// or
var _url: String? = dict["url"]

How to fix Swift compile issue trap6

The issue happen in Swift3 with XCode10.That maybe be a Swift bug。 Actually,clean and rebuild can solve the issue,but that wast too much time。Any body know a effect way to solve or avoid the issue。Thank you
** DESERIALIZATION FAILURE (please include this section in any bug report) ***
could not deserialize type for '_': could not deserialize type for 'operImageView': top-level value not found
Cross-reference to module 'LKImageKit'
... LKImageView
0 swift 0x000000010460f59a PrintStackTraceSignalHandler(void*) + 42
1 swift 0x000000010460ed4e SignalHandler(int) + 302
2 libsystem_platform.dylib 0x00007fff5e91df5a _sigtramp + 26
3 libsystem_platform.dylib 0x00007fe96482b200 _sigtramp + 99668672
4 libsystem_c.dylib 0x00007fff5e6bb1ae abort + 127
5 swift 0x0000000101a74feb swift::ModuleFile::fatal(llvm::Error) + 1915
6 swift 0x0000000101aac643 getSILDeclRef(swift::ModuleFile*, llvm::ArrayRef<unsigned long long>, unsigned int&) + 307
7 swift 0x0000000101aae984 swift::SILDeserializer::readVTable(llvm::PointerEmbeddedInt<unsigned int, 31>) + 740
8 swift 0x0000000101b5576b swift::SerializedSILLoader::getAllForModule(swift::Identifier, swift::FileUnit*) + 331
9 swift 0x000000010089d92e performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*, swift::UnifiedStatsReporter*) + 34558
10 swift 0x0000000100891d35 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 7717
11 swift 0x0000000100837965 main + 1349
12 libdyld.dylib 0x00007fff5e60f015 start + 1
WSRecommendHeaderCollectionReusableView~partial.swiftmodule
/Users/levinyang/Library/Developer/Xcode/DerivedData/myProject-hgotlvyrysfhcyeakqaatptobuxm/Build/Intermediates.noindex/myProject.build/Debug-iphoneos/myProject.build/Objects-normal/arm64/WSActionSheetRecommendFlowHandler~partial.swiftmodule
---
2. While deserializing SIL vtable for 'WSNotificationOperCell' in module 'myProject'
** DESERIALIZATION FAILURE (please include this section in any bug report) ***
could not deserialize type for '_': could not deserialize type for 'operImageView': top-level value not found
Cross-reference to module 'LKImageKit'
... LKImageView
0 swift 0x000000010460f59a PrintStackTraceSignalHandler(void*) + 42
1 swift 0x000000010460ed4e SignalHandler(int) + 302
2 libsystem_platform.dylib 0x00007fff5e91df5a _sigtramp + 26
3 libsystem_platform.dylib 0x00007fe96482b200 _sigtramp + 99668672
4 libsystem_c.dylib 0x00007fff5e6bb1ae abort + 127
5 swift 0x0000000101a74feb swift::ModuleFile::fatal(llvm::Error) + 1915
6 swift 0x0000000101aac643 getSILDeclRef(swift::ModuleFile*, llvm::ArrayRef<unsigned long long>, unsigned int&) + 307
7 swift 0x0000000101aae984 swift::SILDeserializer::readVTable(llvm::PointerEmbeddedInt<unsigned int, 31>) + 740
8 swift 0x0000000101b5576b swift::SerializedSILLoader::getAllForModule(swift::Identifier, swift::FileUnit*) + 331
9 swift 0x000000010089d92e performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*, swift::UnifiedStatsReporter*) + 34558
10 swift 0x0000000100891d35 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 7717
11 swift 0x0000000100837965 main + 1349
12 libdyld.dylib 0x00007fff5e60f015 start + 1
WSRecommendHeaderCollectionReusableView~partial.swiftmodule
/Users/levinyang/Library/Developer/Xcode/DerivedData/myProject-hgotlvyrysfhcyeakqaatptobuxm/Build/Intermediates.noindex/myProject.build/Debug-iphoneos/myProject.build/Objects-normal/arm64/WSActionSheetRecommendFlowHandler~partial.swiftmodule
---
2. While deserializing SIL vtable for 'WSNotificationOperCell' in module 'myProject'
error: Abort trap: 6
This is a Swift compiler bug. It's possible that you're triggering it by linking modules that were compiled by different versions of the compiler, or some other unsupported behavior, but it probably still shouldn't crash.
However, you say it's a Swift 3 problem. The Swift developers no longer support Swift 3. They are working on Swift 5. If you can reproduce the problem in Swift 5, you can file a bug report at https://bugs.swift.org.

Cast to a class with a protocol as its generic type in Swift

I have several classes all conforming to a protocol called DataObject. I have another class that receives a generic type that also conforms to DataObject, e.g.:
protocol DataObject {
func foo()
//...
}
class A : DataObject {
//...
}
class B : DataObject {
//...
}
class C : DataObject {
//...
}
class SomeClass<T: DataObject> {
//...
}
Later in my code I have an object of type AnyObject that I need to cast to SomeClass. I don't know the specific type of the object inside SomeClass (and don't care). However, whenever I do:
var someClass = obj as? SomeClass<DataObject>
The compiler gives an error of what seems to be a crash in the compiling process - "Command failed due to signal: Segmentation Fault: 11".
Somewhere in the middle of the error details there's a call stack of the crash:
0 swift 0x000000010fecb2b8 llvm::sys::PrintStackTrace(__sFILE*) + 40
1 swift 0x000000010fecb794 SignalHandler(int) + 452
2 libsystem_platform.dylib 0x00007fff87563f1a _sigtramp + 26
3 swift 0x00000001112b0637 llvm::DenseMapAPIntKeyInfo::getHashValue(llvm::DenseMapAPIntKeyInfo::KeyTy const&) + 151
4 swift 0x000000010f84db39 swift::irgen::emitWitnessTableRefs(swift::irgen::IRGenFunction&, swift::Substitution const&, llvm::SmallVectorImpl<llvm::Value*>&) + 329
5 swift 0x000000010f8280be emitNominalMetadataRef(swift::irgen::IRGenFunction&, swift::NominalTypeDecl*, swift::CanType) + 1310
6 swift 0x000000010f81a3d4 getTypeMetadataAccessFunction(swift::irgen::IRGenModule&, swift::CanType, swift::ForDefinition_t) + 580
7 swift 0x000000010f813526 swift::irgen::IRGenFunction::emitTypeMetadataRef(swift::CanType) + 54
8 swift 0x000000010f7dca9f swift::irgen::emitClassDowncast(swift::irgen::IRGenFunction&, llvm::Value*, swift::SILType, swift::irgen::CheckedCastMode) + 143
9 swift 0x000000010f89ac89 emitValueCheckedCast((anonymous namespace)::IRGenSILFunction&, swift::SILValue, swift::SILType, swift::irgen::CheckedCastMode, swift::irgen::Explosion&) + 1065
10 swift 0x000000010f894bfa swift::SILVisitor<(anonymous namespace)::IRGenSILFunction, void>::visit(swift::ValueBase*) + 23626
11 swift 0x000000010f88e39d swift::irgen::IRGenModule::emitSILFunction(swift::SILFunction*) + 9901
12 swift 0x000000010f7f8f4f swift::irgen::IRGenModule::emitGlobalTopLevel() + 159
13 swift 0x000000010f878c59 performIRGeneration(swift::IRGenOptions&, swift::Module*, swift::SILModule*, llvm::StringRef, llvm::LLVMContext&, swift::SourceFile*, unsigned int) + 2121
14 swift 0x000000010f879693 swift::performIRGeneration(swift::IRGenOptions&, swift::SourceFile&, swift::SILModule*, llvm::StringRef, llvm::LLVMContext&, unsigned int) + 51
15 swift 0x000000010f7b5087 frontend_main(llvm::ArrayRef<char const*>, char const*, void*) + 6647
16 swift 0x000000010f7b34e6 main + 1814
17 libdyld.dylib 0x00007fff8db4d5c9 start + 1
I'm really stuck as I cannot use the object the way I should... Any explanations to why this happens, or better yet, any workarounds?