I am using Ray Wenderlich's Storyboard tutorial part 1 to learn how to use storyboards, and I'm doing it in Swift. So I'm transferring the following code, which is located in the AppDelegate.swift file, from
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
UINavigationController *navigationController = [tabBarController viewControllers][0];
PlayersViewController *playersViewController = [navigationController viewControllers][0];
playersViewController.players = _players;
to
let tabBarController : UITabBarController = self.window?.rootViewController as UITabBarController
let navBarController = tabBarController.viewControllers[0] as UINavigationController
let playersController = navBarController.viewControllers[0]
playersController.players = self.players
However, the latter section of code throws a compiler error because when I declare playersController I'm not downcasting the AnyObject that is returned by navBarController.viewControllers[0] to my custom PlayersTableViewController class. So I do that with the following code
let playersController = navBarController.viewControllers[0] as PlayersTableViewController
But this causes the SourceKitService Xcode 6 bug to be appear, and I lose all syntax highlighting.
So I'm left with either continuing with no syntax highlighting, or not continue at all because playersController needs to be recognized as a PlayersTableViewController in order to access the players property. I've narrowed it down to that downcast operation that causes Xcode to bug out, does anyone have any solutions to this problem?
EDIT: I tried to go ahead without syntax coloring, write the necessary code, and then compile it, but it threw the following messy compiler error.
0 swift 0x0000000109fed688 llvm::sys::PrintStackTrace(__sFILE*) + 40
1 swift 0x0000000109fedb74 SignalHandler(int) + 452
2 libsystem_platform.dylib 0x00007fff8f55b5aa _sigtramp + 26
3 libsystem_platform.dylib 000000000000000000 _sigtramp + 1890208368
4 swift 0x00000001094b8dab (anonymous namespace)::RValueEmitter::emitForceValue(swift::SILLocation, swift::Expr*, unsigned int, swift::Lowering::SGFContext) + 1291
5 swift 0x00000001094ace5f swift::ASTVisitor<(anonymous namespace)::RValueEmitter, swift::Lowering::RValue, void, void, void, void, void, swift::Lowering::SGFContext>::visit(swift::Expr*, swift::Lowering::SGFContext) + 4111
6 swift 0x00000001094b436d swift::Lowering::SILGenFunction::emitRValueAsSingleValue(swift::Expr*, swift::Lowering::SGFContext) + 45
7 swift 0x00000001094cb903 swift::Lowering::SILGenFunction::emitRValueAsOrig(swift::Expr*, swift::Lowering::AbstractionPattern, swift::Lowering::TypeLowering const&, swift::Lowering::SGFContext) + 179
8 swift 0x000000010949f496 (anonymous namespace)::CheckedCastEmitter::emitOperand(swift::Expr*) + 150
9 swift 0x00000001094b6350 emitUnconditionalCheckedCast(swift::Lowering::SILGenFunction&, swift::SILLocation, swift::Expr*, swift::Type, swift::CheckedCastKind, swift::Lowering::SGFContext) + 288
10 swift 0x00000001094ae40d swift::ASTVisitor<(anonymous namespace)::RValueEmitter, swift::Lowering::RValue, void, void, void, void, void, swift::Lowering::SGFContext>::visit(swift::Expr*, swift::Lowering::SGFContext) + 9661
11 swift 0x000000010949caeb swift::Lowering::SILGenFunction::emitExprInto(swift::Expr*, swift::Lowering::Initialization*) + 219
12 swift 0x000000010949397e swift::Lowering::SILGenFunction::visitPatternBindingDecl(swift::PatternBindingDecl*) + 190
13 swift 0x00000001094cfd4c swift::Lowering::SILGenFunction::visitBraceStmt(swift::BraceStmt*) + 332
14 swift 0x00000001094d2c54 swift::ASTVisitor<swift::Lowering::SILGenFunction, void, void, void, void, void, void>::visit(swift::Stmt*) + 148
15 swift 0x00000001094a0f93 swift::Lowering::SILGenFunction::emitFunction(swift::FuncDecl*) + 243
16 swift 0x0000000109482096 swift::Lowering::SILGenModule::emitFunction(swift::FuncDecl*) + 230
17 swift 0x000000010949bd83 swift::ASTVisitor<SILGenType, void, void, void, void, void, void>::visit(swift::Decl*) + 355
18 swift 0x000000010949a70b SILGenType::emitType() + 203
19 swift 0x00000001094956de swift::Lowering::SILGenModule::visitNominalTypeDecl(swift::NominalTypeDecl*) + 30
20 swift 0x000000010948400b swift::Lowering::SILGenModule::emitSourceFile(swift::SourceFile*, unsigned int) + 411
21 swift 0x000000010948428c swift::SILModule::constructSIL(swift::Module*, swift::SourceFile*, unsigned int) + 268
22 swift 0x0000000109484390 swift::performSILGeneration(swift::SourceFile&, unsigned int) + 32
23 swift 0x00000001093751a7 frontend_main(llvm::ArrayRef<char const*>, char const*, void*) + 3255
24 swift 0x000000010937448d main + 1645
25 libdyld.dylib 0x00007fff9a2fd5fd start + 1
26 libdyld.dylib 0x0000000000000039 start + 1708141117
Stack dump:
0. Program arguments: /Applications/Xcode6-Beta4.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -frontend -c /Users/alex/Desktop/CodingFolder/iOS/Ratings/Ratings/Player.swift /Users/alex/Desktop/CodingFolder/iOS/Ratings/Ratings/ViewController.swift -primary-file /Users/alex/Desktop/CodingFolder/iOS/Ratings/Ratings/AppDelegate.swift /Users/alex/Desktop/CodingFolder/iOS/Ratings/Ratings/PlayersTableViewController.swift -enable-objc-attr-requires-objc-module -target i386-apple-ios7.1 -module-name Ratings -sdk /Applications/Xcode6-Beta4.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator8.0.sdk -I /Users/alex/Library/Developer/Xcode/DerivedData/Ratings-cylggjacqbkxbeeotoghnkluxwly/Build/Products/Debug-iphonesimulator -F /Users/alex/Library/Developer/Xcode/DerivedData/Ratings-cylggjacqbkxbeeotoghnkluxwly/Build/Products/Debug-iphonesimulator -g -module-cache-path /Users/alex/Library/Developer/Xcode/DerivedData/ModuleCache -Xcc -I/Users/alex/Library/Developer/Xcode/DerivedData/Ratings-cylggjacqbkxbeeotoghnkluxwly/Build/Intermediates/Ratings.build/Debug-iphonesimulator/Ratings.build/swift-overrides.hmap -Xcc -iquote -Xcc /Users/alex/Library/Developer/Xcode/DerivedData/Ratings-cylggjacqbkxbeeotoghnkluxwly/Build/Intermediates/Ratings.build/Debug-iphonesimulator/Ratings.build/Ratings-generated-files.hmap -Xcc -I/Users/alex/Library/Developer/Xcode/DerivedData/Ratings-cylggjacqbkxbeeotoghnkluxwly/Build/Intermediates/Ratings.build/Debug-iphonesimulator/Ratings.build/Ratings-own-target-headers.hmap -Xcc -I/Users/alex/Library/Developer/Xcode/DerivedData/Ratings-cylggjacqbkxbeeotoghnkluxwly/Build/Intermediates/Ratings.build/Debug-iphonesimulator/Ratings.build/Ratings-all-target-headers.hmap -Xcc -iquote -Xcc /Users/alex/Library/Developer/Xcode/DerivedData/Ratings-cylggjacqbkxbeeotoghnkluxwly/Build/Intermediates/Ratings.build/Debug-iphonesimulator/Ratings.build/Ratings-project-headers.hmap -Xcc -I/Users/alex/Library/Developer/Xcode/DerivedData/Ratings-cylggjacqbkxbeeotoghnkluxwly/Build/Products/Debug-iphonesimulator/include -Xcc -I/Applications/Xcode6-Beta4.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include -Xcc -I/Users/alex/Library/Developer/Xcode/DerivedData/Ratings-cylggjacqbkxbeeotoghnkluxwly/Build/Intermediates/Ratings.build/Debug-iphonesimulator/Ratings.build/DerivedSources/i386 -Xcc -I/Users/alex/Library/Developer/Xcode/DerivedData/Ratings-cylggjacqbkxbeeotoghnkluxwly/Build/Intermediates/Ratings.build/Debug-iphonesimulator/Ratings.build/DerivedSources -Xcc -DDEBUG=1 -emit-module-doc-path /Users/alex/Library/Developer/Xcode/DerivedData/Ratings-cylggjacqbkxbeeotoghnkluxwly/Build/Intermediates/Ratings.build/Debug-iphonesimulator/Ratings.build/Objects-normal/i386/AppDelegate~partial.swiftdoc -O0 -emit-module-path /Users/alex/Library/Developer/Xcode/DerivedData/Ratings-cylggjacqbkxbeeotoghnkluxwly/Build/Intermediates/Ratings.build/Debug-iphonesimulator/Ratings.build/Objects-normal/i386/AppDelegate~partial.swiftmodule -serialize-diagnostics-path /Users/alex/Library/Developer/Xcode/DerivedData/Ratings-cylggjacqbkxbeeotoghnkluxwly/Build/Intermediates/Ratings.build/Debug-iphonesimulator/Ratings.build/Objects-normal/i386/AppDelegate.dia -emit-dependencies-path /Users/alex/Library/Developer/Xcode/DerivedData/Ratings-cylggjacqbkxbeeotoghnkluxwly/Build/Intermediates/Ratings.build/Debug-iphonesimulator/Ratings.build/Objects-normal/i386/AppDelegate.d -o /Users/alex/Library/Developer/Xcode/DerivedData/Ratings-cylggjacqbkxbeeotoghnkluxwly/Build/Intermediates/Ratings.build/Debug-iphonesimulator/Ratings.build/Objects-normal/i386/AppDelegate.o
1. While emitting SIL for 'application' at /Users/alex/Desktop/CodingFolder/iOS/Ratings/Ratings/AppDelegate.swift:18: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-Beta4.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift failed with exit code 254
Related
I was trying to create a generic collection, PowerCollection, such that e.g. PowerCollection([0,1], 2) would contain [0, 0], [0, 1], [1, 0], and [1, 1], but I ran into trouble. Even with all method implementations stubbed out, the compiler is segfaulting and I don't know why.
Here is a minimal example that causes the segfault. Am I doing something wrong here?
I am using: Xcode 12.3 (17715) (Build 12C33) on macOS Version 10.15.7 (Build 19H2)
struct PowerCollection<C : Collection> : Collection {
typealias Index = [C.Index]
typealias Element = [C.Element]
var startIndex, endIndex: Index
subscript(position: Index) -> [C.Element] {
return []
}
func index(after i: Index) -> Index {
return i
}
}
extension Array : Comparable where Element : Comparable {
public static func < (lhs: [Element], rhs: [Element]) -> Bool {
return false
}
}
Trying to compile the above gave me a compiler segfault with the following stack dump
0. Program arguments: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -frontend -c -primary-file /Users/justinh/Desktop/CrashInvestigation/CrashInvestigation/main.swift -emit-module-path /Users/justinh/Library/Developer/Xcode/DerivedData/CrashInvestigation-hgzhbvtdchhupjellxarahpklfll/Build/Intermediates.noindex/CrashInvestigation.build/Debug/CrashInvestigation.build/Objects-normal/x86_64/main~partial.swiftmodule -emit-module-doc-path /Users/justinh/Library/Developer/Xcode/DerivedData/CrashInvestigation-hgzhbvtdchhupjellxarahpklfll/Build/Intermediates.noindex/CrashInvestigation.build/Debug/CrashInvestigation.build/Objects-normal/x86_64/main~partial.swiftdoc -emit-module-source-info-path /Users/justinh/Library/Developer/Xcode/DerivedData/CrashInvestigation-hgzhbvtdchhupjellxarahpklfll/Build/Intermediates.noindex/CrashInvestigation.build/Debug/CrashInvestigation.build/Objects-normal/x86_64/main~partial.swiftsourceinfo -serialize-diagnostics-path /Users/justinh/Library/Developer/Xcode/DerivedData/CrashInvestigation-hgzhbvtdchhupjellxarahpklfll/Build/Intermediates.noindex/CrashInvestigation.build/Debug/CrashInvestigation.build/Objects-normal/x86_64/main.dia -emit-dependencies-path /Users/justinh/Library/Developer/Xcode/DerivedData/CrashInvestigation-hgzhbvtdchhupjellxarahpklfll/Build/Intermediates.noindex/CrashInvestigation.build/Debug/CrashInvestigation.build/Objects-normal/x86_64/main.d -emit-reference-dependencies-path /Users/justinh/Library/Developer/Xcode/DerivedData/CrashInvestigation-hgzhbvtdchhupjellxarahpklfll/Build/Intermediates.noindex/CrashInvestigation.build/Debug/CrashInvestigation.build/Objects-normal/x86_64/main.swiftdeps -target x86_64-apple-macos11.0 -enable-objc-interop -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk -I /Users/justinh/Library/Developer/Xcode/DerivedData/CrashInvestigation-hgzhbvtdchhupjellxarahpklfll/Build/Products/Debug -F /Users/justinh/Library/Developer/Xcode/DerivedData/CrashInvestigation-hgzhbvtdchhupjellxarahpklfll/Build/Products/Debug -enable-testing -g -module-cache-path /Users/justinh/Library/Developer/Xcode/DerivedData/ModuleCache.noindex -swift-version 5 -enforce-exclusivity=checked -Onone -D DEBUG -serialize-debugging-options -Xcc -working-directory -Xcc /Users/justinh/Desktop/CrashInvestigation -enable-anonymous-context-mangled-names -Xcc -I/Users/justinh/Library/Developer/Xcode/DerivedData/CrashInvestigation-hgzhbvtdchhupjellxarahpklfll/Build/Intermediates.noindex/CrashInvestigation.build/Debug/CrashInvestigation.build/swift-overrides.hmap -Xcc -iquote -Xcc /Users/justinh/Library/Developer/Xcode/DerivedData/CrashInvestigation-hgzhbvtdchhupjellxarahpklfll/Build/Intermediates.noindex/CrashInvestigation.build/Debug/CrashInvestigation.build/CrashInvestigation-generated-files.hmap -Xcc -I/Users/justinh/Library/Developer/Xcode/DerivedData/CrashInvestigation-hgzhbvtdchhupjellxarahpklfll/Build/Intermediates.noindex/CrashInvestigation.build/Debug/CrashInvestigation.build/CrashInvestigation-own-target-headers.hmap -Xcc -I/Users/justinh/Library/Developer/Xcode/DerivedData/CrashInvestigation-hgzhbvtdchhupjellxarahpklfll/Build/Intermediates.noindex/CrashInvestigation.build/Debug/CrashInvestigation.build/CrashInvestigation-all-target-headers.hmap -Xcc -iquote -Xcc /Users/justinh/Library/Developer/Xcode/DerivedData/CrashInvestigation-hgzhbvtdchhupjellxarahpklfll/Build/Intermediates.noindex/CrashInvestigation.build/Debug/CrashInvestigation.build/CrashInvestigation-project-headers.hmap -Xcc -I/Users/justinh/Library/Developer/Xcode/DerivedData/CrashInvestigation-hgzhbvtdchhupjellxarahpklfll/Build/Products/Debug/include -Xcc -I/Users/justinh/Library/Developer/Xcode/DerivedData/CrashInvestigation-hgzhbvtdchhupjellxarahpklfll/Build/Intermediates.noindex/CrashInvestigation.build/Debug/CrashInvestigation.build/DerivedSources-normal/x86_64 -Xcc -I/Users/justinh/Library/Developer/Xcode/DerivedData/CrashInvestigation-hgzhbvtdchhupjellxarahpklfll/Build/Intermediates.noindex/CrashInvestigation.build/Debug/CrashInvestigation.build/DerivedSources/x86_64 -Xcc -I/Users/justinh/Library/Developer/Xcode/DerivedData/CrashInvestigation-hgzhbvtdchhupjellxarahpklfll/Build/Intermediates.noindex/CrashInvestigation.build/Debug/CrashInvestigation.build/DerivedSources -Xcc -DDEBUG=1 -target-sdk-version 11.1 -module-name CrashInvestigation -o /Users/justinh/Library/Developer/Xcode/DerivedData/CrashInvestigation-hgzhbvtdchhupjellxarahpklfll/Build/Intermediates.noindex/CrashInvestigation.build/Debug/CrashInvestigation.build/Objects-normal/x86_64/main.o -index-store-path /Users/justinh/Library/Developer/Xcode/DerivedData/CrashInvestigation-hgzhbvtdchhupjellxarahpklfll/Index/DataStore -index-system-modules
1. Apple Swift version 5.3.2 (swiftlang-1200.0.45 clang-1200.0.32.28)
2. While evaluating request IRGenSourceFileRequest(IR Generation for file "/Users/justinh/Desktop/CrashInvestigation/CrashInvestigation/main.swift")
3. While emitting IR SIL function "#$s18CrashInvestigation15PowerCollectionVyxGSlAASl20_failEarlyRangeCheck_6boundsySny5IndexQzG_AItFTW".
for '_failEarlyRangeCheck(_:bounds:)' (in module 'Swift')
4. While mangling type for debugger type 'Range<Array<τ_0_0.Index>>'
0 swift 0x000000011288f615 llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 37
1 swift 0x000000011288e615 llvm::sys::RunSignalHandlers() + 85
2 swift 0x000000011288fbcf SignalHandler(int) + 111
3 libsystem_platform.dylib 0x00007fff6fddd5fd _sigtramp + 29
4 libsystem_platform.dylib 0x00007faafe8397c0 _sigtramp + 18446743711030559200
5 swift 0x000000010f69e03f swift::GenericSignatureImpl::getConformanceAccessPath(swift::Type, swift::ProtocolDecl*) + 31
6 swift 0x000000010f598069 swift::Mangle::ASTMangler::appendAnyProtocolConformance(swift::CanGenericSignature, swift::CanType, swift::ProtocolConformanceRef) + 217
7 swift 0x000000010f596386 swift::Mangle::ASTMangler::appendConcreteProtocolConformance(swift::ProtocolConformance const*) + 2070
8 swift 0x000000010f594aa3 swift::Mangle::ASTMangler::appendRetroactiveConformances(swift::SubstitutionMap, swift::ModuleDecl*) + 179
9 swift 0x000000010f58de5e swift::Mangle::ASTMangler::appendType(swift::Type, swift::ValueDecl const*) + 3454
10 swift 0x000000010f591af8 swift::Mangle::ASTMangler::mangleTypeForDebugger(swift::Type, swift::DeclContext const*) + 632
11 swift 0x000000010e7c9d21 (anonymous namespace)::IRGenDebugInfoImpl::getOrCreateType(swift::irgen::DebugTypeInfo) + 1505
12 swift 0x000000010e7cb8e5 (anonymous namespace)::IRGenDebugInfoImpl::createParameterTypes(swift::CanTypeWrapper<swift::SILFunctionType>) + 1461
13 swift 0x000000010e7c2af4 (anonymous namespace)::IRGenDebugInfoImpl::emitFunction(swift::SILDebugScope const*, llvm::Function*, swift::SILFunctionTypeRepresentation, swift::SILType, swift::DeclContext*) + 2868
14 swift 0x000000010e7e245f swift::irgen::IRGenModule::emitSILFunction(swift::SILFunction*) + 2639
15 swift 0x000000010e67d3a7 swift::irgen::IRGenerator::emitLazyDefinitions() + 1207
16 swift 0x000000010e7b8d20 performIRGeneration(swift::IRGenOptions const&, swift::ModuleDecl*, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule> >, llvm::StringRef, swift::PrimarySpecificPaths const&, llvm::StringRef, swift::SourceFile*, llvm::GlobalVariable**, llvm::StringSet<llvm::MallocAllocator>*) + 1920
17 swift 0x000000010e7e1781 swift::SimpleRequest<swift::IRGenSourceFileRequest, swift::GeneratedModule (swift::IRGenDescriptor), (swift::RequestFlags)9>::evaluateRequest(swift::IRGenSourceFileRequest const&, swift::Evaluator&) + 97
18 swift 0x000000010e7bd04c llvm::Expected<swift::IRGenSourceFileRequest::OutputType> swift::Evaluator::getResultUncached<swift::IRGenSourceFileRequest>(swift::IRGenSourceFileRequest const&) + 940
19 swift 0x000000010e7b929d swift::performIRGeneration(swift::IRGenOptions const&, swift::SourceFile&, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule> >, llvm::StringRef, swift::PrimarySpecificPaths const&, llvm::StringRef, llvm::GlobalVariable**, llvm::StringSet<llvm::MallocAllocator>*) + 269
20 swift 0x000000010e4031ba performCompileStepsPostSILGen(swift::CompilerInstance&, swift::CompilerInvocation const&, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule> >, llvm::PointerUnion<swift::ModuleDecl*, swift::SourceFile*>, swift::PrimarySpecificPaths const&, int&, swift::FrontendObserver*) + 3002
21 swift 0x000000010e3f2d97 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 20695
22 swift 0x000000010e373c27 main + 1255
23 libdyld.dylib 0x00007fff6fbe4cc9 start + 1
24 libdyld.dylib 0x000000000000004b start + 18446603338641421187
error: Segmentation fault: 11 (in target 'CrashInvestigation' from project 'CrashInvestigation')
I have a small swift script
#!/usr/bin/swift
import Foundation
let url = URL(fileURLWithPath: ".", isDirectory: true)
print(url.absoluteString)
If I run it from command line I am getting a Segmentation fault
swift-test $ ./url-test.swift
Stack dump:
0. Program arguments: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -frontend -interpret ./url-test.swift -enable-objc-interop -stack-check -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk -color-diagnostics -module-name main
1. Apple Swift version 5.2.2 (swiftlang-1103.0.32.6 clang-1103.0.32.51)
2. While running user code "./url-test.swift"
0 swift 0x0000000113fa74ea PrintStackTraceSignalHandler(void*) + 42
1 swift 0x0000000113fa6cc0 SignalHandler(int) + 352
2 libsystem_platform.dylib 0x00007fff724cf5fd _sigtramp + 29
3 libsystem_malloc.dylib 0x00007fff7248e0c6 szone_malloc_should_clear + 66
4 swift 0x000000010fd2a48a llvm::MCJIT::runFunction(llvm::Function*, llvm::ArrayRef<llvm::GenericValue>) + 458
5 swift 0x000000010fd314fb llvm::ExecutionEngine::runFunctionAsMain(llvm::Function*, 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&, char const* const*) + 2011
6 swift 0x000000010fd065ba 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*) + 14362
7 swift 0x000000010fcfaf75 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 55813
8 swift 0x000000010fc70f53 main + 1283
9 libdyld.dylib 0x00007fff722d6cc9 start + 1
10 libdyld.dylib 0x000000000000000b start + 2379387715
Segmentation fault: 11
If I run it inside swift interpreter everything is fine
swift-test $ swift
Welcome to Apple Swift version 5.2.2 (swiftlang-1103.0.32.6 clang-1103.0.32.51).
Type :help for assistance.
1> import Foundation
2. let url = URL(fileURLWithPath: ".", isDirectory: true)
3. print(url.absoluteString)
file:///Users/user/projects/swift-test/
url: URL = {}
4>
Any thoughts how to fix segmentation fault?
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
I have a very simple script written in swift and running this with swift file.swift
import Foundation
let url = URL(string: "www.google.com")
and this gives me a segfault error with this message
Stack dump:
0. Program arguments: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -frontend -interpret file.swift -enable-objc-interop -stack-check -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk -color-diagnostics -module-name file
1. Apple Swift version 5.2 (swiftlang-1103.0.32.1 clang-1103.0.32.29)
2. While running user code "file.swift"
0 swift 0x00000001101354ea PrintStackTraceSignalHandler(void*) + 42
1 swift 0x0000000110134cc0 SignalHandler(int) + 352
2 libsystem_platform.dylib 0x00007fff6c0aa5fd _sigtramp + 29
3 libsystem_platform.dylib 0x00007ffee3e196a8 _sigtramp + 2010575048
4 swift 0x000000010beb89ba llvm::MCJIT::runFunction(llvm::Function*, llvm::ArrayRef<llvm::GenericValue>) + 458
5 swift 0x000000010bebfa2b llvm::ExecutionEngine::runFunctionAsMain(llvm::Function*, 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&, char const* const*) + 2011
6 swift 0x000000010be94aea 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*) + 14362
7 swift 0x000000010be894a5 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 55813
8 swift 0x000000010bdff4d3 main + 1283
9 libdyld.dylib 0x00007fff6beb1cc9 start + 1
zsh: segmentation fault swift file.swift
any ideas as to where the problem is? Thanks!
EDIT: I have tried putting the code to Xcode and it works as it should.
I had a script which was working and suddenly stopped. Exactly on the line where I have created an URL. It was working on Xcode but not from terminal.
Looks like, it is bug.
If you first compile by using swiftc then run it, it is working.
I have found the answer in the following page:
https://blog.kulman.sk/workaround-for-swift-scripts-crash/
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)")
}
}