分段错误:11-Xcode/Swift 5编译问题



编译时,我总是遇到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_ostream&) + 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 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>*) + 1687
9  swift                    0x000000010396d681 swift::SimpleRequest<swift::IRGenSourceFileRequest, swift::GeneratedModule (swift::IRGenDescriptor), (swift::RequestFlags)9>::evaluateRequest(swift::IRGenSourceFileRequest const&, swift::Evaluator&) + 97
10 swift                    0x0000000103948dec llvm::Expected<swift::IRGenSourceFileRequest::OutputType> swift::Evaluator::getResultUncached<swift::IRGenSourceFileRequest>(swift::IRGenSourceFileRequest const&) + 940
11 swift                    0x000000010394503d 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
12 swift                    0x0000000103590d3a 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
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')

命名枚举关联值是一个阴影区域。虽然它显然在某些条件下被接受,但在Swift官方语言指南中关于枚举的章节中,它没有被使用,甚至没有被提及。因此,在这种情况下,如果删除命名,则代码编译良好。

enum Enumeration {
case possibilityA(Protocol)
case possibilityB((Result<SomeStruct, Error>) -> ())
}
...
func method() {
let _ = Enumeration.possibilityB({ _ in
})

无论如何,崩溃的编译器从来都不是好的,无论你输入的代码有多糟糕,而且你有一个非常酷的最小测试,所以填写报告应该是你的职责和荣誉:(

最新更新