#ios #swift #xcode
#iOS #swift #xcode
Вопрос:
Я всегда получаю Segmentation fault: 11
сообщение об ошибке при компиляции.
Я значительно сократил свой код, чтобы определить нюансы того, что вызывает проблему, и создал демонстрационный код ниже, который демонстрирует точно такую же проблему.
Может кто-нибудь предложить какой-либо способ решения этой проблемы (который отличается от того, что я уже выделил в комментариях к коду)?
PS: этот код Swift 5 и встречается как с Xcode 11.7, так и с Xcode 12
// If we make this a class instead, it compiles! (but I want it to be a struct)
struct SomeStruct {
// If we remove either of these properties or change them to an Int, it compiles! (but I obviously need 2 strings)
let propertyA: String
let propertyB: String
}
protocol Protocol { }
enum Enumeration {
// If we remove this case, it compiles! (but I need this case, and the protocol also)
case possibilityA(protocol: Protocol)
// If we add additional associated values to this case, it compiles! (but I need this case)
case possibilityB(completion: (Result<SomeStruct, Error>) -> ())
}
class SomeClass {
func method() {
// If we remove the use of `possibilityB`, it compiles! (but I need this)
_ = Enumeration.possibilityB(completion: { _ in
})
}
}
Вот дамп стека…
Stack dump:
0. Program arguments: /Applications/Xcode_12.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -frontend -c -primary-file /Users/x/Demo/Sources/Demo/Demo.swift -emit-module-path /Users/x/Library/Developer/Xcode/DerivedData/Demo-hjukrlyoxxzhqwasgqzluieckmux/Build/Intermediates.noindex/Demo.build/Debug-iphoneos/Demo.build/Objects-normal/arm64/Demo~partial.swiftmodule -emit-module-doc-path /Users/x/Library/Developer/Xcode/DerivedData/Demo-hjukrlyoxxzhqwasgqzluieckmux/Build/Intermediates.noindex/Demo.build/Debug-iphoneos/Demo.build/Objects-normal/arm64/Demo~partial.swiftdoc -emit-module-source-info-path /Users/x/Library/Developer/Xcode/DerivedData/Demo-hjukrlyoxxzhqwasgqzluieckmux/Build/Intermediates.noindex/Demo.build/Debug-iphoneos/Demo.build/Objects-normal/arm64/Demo~partial.swiftsourceinfo -serialize-diagnostics-path /Users/x/Library/Developer/Xcode/DerivedData/Demo-hjukrlyoxxzhqwasgqzluieckmux/Build/Intermediates.noindex/Demo.build/Debug-iphoneos/Demo.build/Objects-normal/arm64/Demo.dia -emit-dependencies-path /Users/x/Library/Developer/Xcode/DerivedData/Demo-hjukrlyoxxzhqwasgqzluieckmux/Build/Intermediates.noindex/Demo.build/Debug-iphoneos/Demo.build/Objects-normal/arm64/Demo.d -emit-reference-dependencies-path /Users/x/Library/Developer/Xcode/DerivedData/Demo-hjukrlyoxxzhqwasgqzluieckmux/Build/Intermediates.noindex/Demo.build/Debug-iphoneos/Demo.build/Objects-normal/arm64/Demo.swiftdeps -target arm64-apple-ios9.0 -Xllvm -aarch64-use-tbi -enable-objc-interop -sdk /Applications/Xcode_12.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS14.0.sdk -I /Users/x/Library/Developer/Xcode/DerivedData/Demo-hjukrlyoxxzhqwasgqzluieckmux/Build/Products/Debug-iphoneos -I /Applications/Xcode_12.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/lib -F /Users/x/Library/Developer/Xcode/DerivedData/Demo-hjukrlyoxxzhqwasgqzluieckmux/Build/Products/Debug-iphoneos -F /Applications/Xcode_12.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Frameworks -F /Applications/Xcode_12.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS14.0.sdk/Developer/Library/Frameworks -enable-testing -g -module-cache-path /Users/x/Library/Developer/Xcode/DerivedData/ModuleCache.noindex -swift-version 5 -enforce-exclusivity=checked -Onone -D SWIFT_PACKAGE -D DEBUG -D Xcode -serialize-debugging-options -Xcc -working-directory -Xcc /Users/x/Demo -enable-anonymous-context-mangled-names -Xcc -I/Users/x/Library/Developer/Xcode/DerivedData/Demo-hjukrlyoxxzhqwasgqzluieckmux/Build/Intermediates.noindex/Demo.build/Debug-iphoneos/Demo.build/swift-overrides.hmap -Xcc -I/Users/x/Library/Developer/Xcode/DerivedData/Demo-hjukrlyoxxzhqwasgqzluieckmux/Build/Products/Debug-iphoneos/include -Xcc -I/Users/x/Library/Developer/Xcode/DerivedData/Demo-hjukrlyoxxzhqwasgqzluieckmux/Build/Intermediates.noindex/Demo.build/Debug-iphoneos/Demo.build/DerivedSources-normal/arm64 -Xcc -I/Users/x/Library/Developer/Xcode/DerivedData/Demo-hjukrlyoxxzhqwasgqzluieckmux/Build/Intermediates.noindex/Demo.build/Debug-iphoneos/Demo.build/DerivedSources/arm64 -Xcc -I/Users/x/Library/Developer/Xcode/DerivedData/Demo-hjukrlyoxxzhqwasgqzluieckmux/Build/Intermediates.noindex/Demo.build/Debug-iphoneos/Demo.build/DerivedSources -Xcc -DSWIFT_PACKAGE -Xcc -DDEBUG=1 -target-sdk-version 14.0 -parse-as-library -module-name Demo -o /Users/x/Library/Developer/Xcode/DerivedData/Demo-hjukrlyoxxzhqwasgqzluieckmux/Build/Intermediates.noindex/Demo.build/Debug-iphoneos/Demo.build/Objects-normal/arm64/Demo.o -embed-bitcode-marker -index-store-path /Users/x/Library/Developer/Xcode/DerivedData/Demo-hjukrlyoxxzhqwasgqzluieckmux/Index/DataStore -index-system-modules
1. Apple Swift version 5.3 (swiftlang-1200.0.29.2 clang-1200.0.30.1)
2. While evaluating request IRGenSourceFileRequest(IR Generation for file "/Users/x/Demo/Sources/Demo/Demo.swift")
3. While emitting IR SIL function "@$s8Demo9SomeClassC6methodyyF".
for 'method()' (at /Users/x/Demo/Sources/Demo/Demo.swift:21:5)
4. While converting type '(@callee_guaranteed (@in_guaranteed Result<SomeStruct, Error>) -> ())'
0 swift 0x0000000107a19865 llvm::sys::PrintStackTrace(llvm::raw_ostreamamp;) 37
1 swift 0x0000000107a18865 llvm::sys::RunSignalHandlers() 85
2 swift 0x0000000107a19e1f SignalHandler(int) 111
3 libsystem_platform.dylib 0x00007fff68acc5fd _sigtramp 29
4 libsystem_platform.dylib 0x0000000000000004 _sigtramp 18446603338760010276
5 swift 0x00000001039794d8 swift::SILInstructionVisitor<(anonymous namespace)::IRGenSILFunction, void>::visit(swift::SILInstruction*) 21400
6 swift 0x000000010396fc26 swift::irgen::IRGenModule::emitSILFunction(swift::SILFunction*) 8982
7 swift 0x0000000103809dc7 swift::irgen::IRGenerator::emitGlobalTopLevel(llvm::StringSet<llvm::MallocAllocator>*) 1607
8 swift 0x00000001039449d7 performIRGeneration(swift::IRGenOptions constamp;, swift::ModuleDecl*, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule> >, llvm::StringRef, swift::PrimarySpecificPaths constamp;, llvm::StringRef, swift::SourceFile*, llvm::GlobalVariable**, llvm::StringSet<llvm::MallocAllocator>*) 1687
9 swift 0x000000010396d681 swift::SimpleRequest<swift::IRGenSourceFileRequest, swift::GeneratedModule (swift::IRGenDescriptor), (swift::RequestFlags)9>::evaluateRequest(swift::IRGenSourceFileRequest constamp;, swift::Evaluatoramp;) 97
10 swift 0x0000000103948dec llvm::Expected<swift::IRGenSourceFileRequest::OutputType> swift::Evaluator::getResultUncached<swift::IRGenSourceFileRequest>(swift::IRGenSourceFileRequest constamp;) 940
11 swift 0x000000010394503d swift::performIRGeneration(swift::IRGenOptions constamp;, swift::SourceFileamp;, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule> >, llvm::StringRef, swift::PrimarySpecificPaths constamp;, llvm::StringRef, llvm::GlobalVariable**, llvm::StringSet<llvm::MallocAllocator>*) 269
12 swift 0x0000000103590d3a performCompileStepsPostSILGen(swift::CompilerInstanceamp;, swift::CompilerInvocation constamp;, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule> >, llvm::PointerUnion<swift::ModuleDecl*, swift::SourceFile*>, swift::PrimarySpecificPaths constamp;, intamp;, swift::FrontendObserver*) 3002
13 swift 0x0000000103580928 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) 20712
14 swift 0x0000000103501c07 main 1255
15 libdyld.dylib 0x00007fff688d3cc9 start 1
16 libdyld.dylib 0x000000000000004f start 18446603338762077063
error: Segmentation fault: 11 (in target 'Demo' from project 'Demo')
Ответ №1:
Присвоение имен значениям, связанным с перечислением, является теневой зоной. Хотя он, по-видимому, принимается при определенных условиях, он не используется и даже не упоминается в официальной главе руководства по языку Swift, посвященной перечислениям. Таким образом, в этом случае ваш код компилируется нормально, если вы удалите именование.
enum Enumeration {
case possibilityA(Protocol)
case possibilityB((Result<SomeStruct, Error>) -> ())
}
...
func method() {
let _ = Enumeration.possibilityB({ _ in
})
В любом случае, сбой компилятора никогда не бывает приятным, независимо от того, насколько плохой код вы его вводите, и, кроме того, вы получили довольно крутой минимальный тест, поэтому заполнение отчета должно быть вашей обязанностью и честью 🙂