Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-10128] Compiler crashes when generic type constraint is the associated type of another protocol which the class also conforms to which and has a default implementation #52530

Closed
glbrntt opened this issue Mar 18, 2019 · 9 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself crash Bug: A crash, i.e., an abnormal termination of software

Comments

@glbrntt
Copy link

glbrntt commented Mar 18, 2019

Previous ID SR-10128
Radar rdar://44736411
Original Reporter @glbrntt
Type Bug
Status Resolved
Resolution Duplicate
Environment

Xcode 10.2 beta 4

Additional Detail from JIRA
Votes 1
Component/s Compiler
Labels Bug, CompilerCrash
Assignee None
Priority Medium

md5: f46d160b1e3f4181dc3a360dd5fd62d6

duplicates:

  • SR-8814 GenericSignatureBuilder should re-introduce conformance requirements when adding a superclass requirement

is duplicated by:

  • SR-10595 Complex Generic Statements Cause Compiler Segfault

relates to:

  • SR-10182 Segmentation Fault after updating to Xcode 10.2

Issue Description:

The compiler crashes with the following code:

protocol ProtoA { }

protocol ProtoB {
    associatedtype T: ProtoA
    var stuff: Int { get }
}

extension ProtoB {
    var stuff: Int {
        return 0
    }
}

class Foo<T: ProtoA>: ProtoB { }

Stacktrace:

0  swift                    0x000000010ebd2eb3 PrintStackTraceSignalHandler(void*) + 51
1  swift                    0x000000010ebd268c SignalHandler(int) + 348
2  libsystem_platform.dylib 0x00007fff7e69eb5d _sigtramp + 29
3  swift                    0x000000010bffff61 swift::AccessLevelRequest::OutputType swift::evaluateOrDefault<swift::AccessLevelRequest>(swift::Evaluator&, swift::AccessLevelRequest, swift::AccessLevelRequest::OutputType) + 721
4  swift                    0x000000010aa80391 swift::irgen::FulfillmentMap::searchWitnessTable(swift::irgen::IRGenModule&, swift::CanType, swift::ProtocolDecl*, unsigned int, swift::irgen::MetadataPath&&, swift::irgen::FulfillmentMap::InterestingKeysCallback const&, llvm::SmallPtrSetImpl<swift::ProtocolDecl*>*) + 65
5  swift                    0x000000010aa80302 swift::irgen::FulfillmentMap::searchWitnessTable(swift::irgen::IRGenModule&, swift::CanType, swift::ProtocolDecl*, unsigned int, swift::irgen::MetadataPath&&, swift::irgen::FulfillmentMap::InterestingKeysCallback const&) + 450
6  swift                    0x000000010aa80108 void llvm::function_ref<void (unsigned int, swift::CanType, llvm::Optional<swift::ProtocolConformanceRef>)>::callback_fn<swift::irgen::FulfillmentMap::searchNominalTypeMetadata(swift::irgen::IRGenModule&, swift::CanType, swift::MetadataState, unsigned int, swift::irgen::MetadataPath&&, swift::irgen::FulfillmentMap::InterestingKeysCallback const&)::$_1>(long, unsigned int, swift::CanType, llvm::Optional<swift::ProtocolConformanceRef>) + 1656
7  swift                    0x000000010abd0b8f swift::irgen::GenericTypeRequirements::enumerateFulfillments(swift::irgen::IRGenModule&, swift::SubstitutionMap, llvm::function_ref<void (unsigned int, swift::CanType, llvm::Optional<swift::ProtocolConformanceRef>)>) + 335
8  swift                    0x000000010aa7f2ba swift::irgen::FulfillmentMap::searchNominalTypeMetadata(swift::irgen::IRGenModule&, swift::CanType, swift::MetadataState, unsigned int, swift::irgen::MetadataPath&&, swift::irgen::FulfillmentMap::InterestingKeysCallback const&) + 490
9  swift                    0x000000010aa7f098 swift::irgen::FulfillmentMap::searchTypeMetadata(swift::irgen::IRGenModule&, swift::CanType, swift::irgen::IsExact_t, swift::MetadataState, unsigned int, swift::irgen::MetadataPath&&, swift::irgen::FulfillmentMap::InterestingKeysCallback const&) + 952
10 swift                    0x000000010ac7d7fa swift::irgen::IRGenFunction::bindLocalTypeDataFromTypeMetadata(swift::CanType, swift::irgen::IsExact_t, llvm::Value*, swift::MetadataState) + 314
11 swift                    0x000000010abce524 swift::irgen::emitPolymorphicParameters(swift::irgen::IRGenFunction&, swift::SILFunction&, swift::irgen::Explosion&, swift::irgen::WitnessMetadata*, llvm::function_ref<llvm::Value* (unsigned int)> const&) + 580
12 swift                    0x000000010ac35611 swift::irgen::IRGenModule::emitSILFunction(swift::SILFunction*) + 8049
13 swift                    0x000000010aaf4829 swift::irgen::IRGenerator::emitLazyDefinitions() + 1353
14 swift                    0x000000010ac158f0 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**) + 1232
15 swift                    0x000000010aa37679 performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*, swift::UnifiedStatsReporter*) + 49417
16 swift                    0x000000010aa27c9e swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 6862
17 swift                    0x000000010a9c5dae main + 1246
18 libdyld.dylib            0x00007fff7e4b33d5 start + 1
19 libdyld.dylib            0x0000000000000042 start + 2176109678
error: Segmentation fault: 11
@theblixguy
Copy link
Collaborator

I cannot reproduce this on 5.1, so I assume it's fixed.

@belkadan
Copy link
Contributor

I'm seeing this with a near-master with asserts:

Assertion failed: (!isInvalid()), function getRequirement, file /Volumes/Data/swift-public/swift/lib/AST/ProtocolConformance.cpp, line 77.
Stack dump:
0.  Program arguments: /Volumes/Data/swift-public/build/ninja/swift-macosx-x86_64/bin/swift -frontend -c -primary-file - -target x86_64-apple-darwin18.5.0 -enable-objc-interop -sdk /Volumes/Data/Applications/Xcode10.2.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk -color-diagnostics -module-name main -o /var/folders/_d/dmrgv26d3bs6lkrks9z825_w0000gn/T/--332dc4.o 
1.  While emitting IR SIL function "@$s4main3FooCyqd__GAA6ProtoBA2aEP5stuffSivgTW".
 for getter for stuff (at <stdin>:9:9)
0  swift                    0x000000010a467a85 llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 37
1  swift                    0x000000010a466d45 llvm::sys::RunSignalHandlers() + 85
2  swift                    0x000000010a468068 SignalHandler(int) + 264
3  libsystem_platform.dylib 0x00007fff64468b5d _sigtramp + 29
4  libsystem_platform.dylib 0x00007ffee9238310 _sigtramp + 2229073872
5  libsystem_c.dylib        0x00007fff643286a6 abort + 127
6  libsystem_c.dylib        0x00007fff642f120d basename_r + 0
7  swift                    0x00000001077ffc0f swift::ProtocolConformanceRef::getRequirement() const + 63
8  swift                    0x0000000106a76366 void llvm::function_ref<void (unsigned int, swift::CanType, llvm::Optional<swift::ProtocolConformanceRef>)>::callback_fn<swift::irgen::FulfillmentMap::searchNominalTypeMetadata(swift::irgen::IRGenModule&, swift::CanType, swift::MetadataState, unsigned int, swift::irgen::MetadataPath&&, swift::irgen::FulfillmentMap::InterestingKeysCallback const&)::$_1>(long, unsigned int, swift::CanType, llvm::Optional<swift::ProtocolConformanceRef>) + 1142
9  swift                    0x0000000106b5142f swift::irgen::GenericTypeRequirements::enumerateFulfillments(swift::irgen::IRGenModule&, swift::SubstitutionMap, llvm::function_ref<void (unsigned int, swift::CanType, llvm::Optional<swift::ProtocolConformanceRef>)>) + 239
10 swift                    0x0000000106a75465 swift::irgen::FulfillmentMap::searchNominalTypeMetadata(swift::irgen::IRGenModule&, swift::CanType, swift::MetadataState, unsigned int, swift::irgen::MetadataPath&&, swift::irgen::FulfillmentMap::InterestingKeysCallback const&) + 245
11 swift                    0x0000000106a751b8 swift::irgen::FulfillmentMap::searchTypeMetadata(swift::irgen::IRGenModule&, swift::CanType, swift::irgen::IsExact_t, swift::MetadataState, unsigned int, swift::irgen::MetadataPath&&, swift::irgen::FulfillmentMap::InterestingKeysCallback const&) + 840
12 swift                    0x0000000106be7462 swift::irgen::LocalTypeDataCache::addAbstractForTypeMetadata(swift::irgen::IRGenFunction&, swift::CanType, swift::irgen::IsExact_t, swift::irgen::MetadataResponse) + 114
13 swift                    0x0000000106be73b0 swift::irgen::IRGenFunction::bindLocalTypeDataFromTypeMetadata(swift::CanType, swift::irgen::IsExact_t, llvm::Value*, swift::MetadataState) + 272
14 swift                    0x0000000106b4fb94 swift::irgen::emitPolymorphicParameters(swift::irgen::IRGenFunction&, swift::SILFunction&, swift::irgen::Explosion&, swift::irgen::WitnessMetadata*, llvm::function_ref<llvm::Value* (unsigned int)> const&) + 820
15 swift                    0x0000000106bb0680 swift::irgen::IRGenModule::emitSILFunction(swift::SILFunction*) + 6032
16 swift                    0x0000000106abcaeb swift::irgen::IRGenerator::emitLazyDefinitions() + 683
17 swift                    0x0000000106b8b8c5 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**) + 1349
18 swift                    0x0000000106b8bd46 swift::performIRGeneration(swift::IRGenOptions&, swift::SourceFile&, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule> >, llvm::StringRef, swift::PrimarySpecificPaths const&, llvm::LLVMContext&, llvm::GlobalVariable**) + 86
19 swift                    0x0000000106a21d91 performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*, swift::UnifiedStatsReporter*) + 14497
20 swift                    0x0000000106a1d492 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 2978
21 swift                    0x00000001069c43a8 main + 696
22 libdyld.dylib            0x00007fff642833d5 start + 1
23 libdyld.dylib            0x000000000000000f start + 2614611003

@rjmccall, does this look familiar?

@omochi
Copy link
Collaborator

omochi commented Mar 26, 2019

I tried this.
#23567

@CodaFi
Copy link
Member

CodaFi commented Nov 22, 2019

The crash has moved higher up the stack to SIL. This is definitely a bug in the GSB

0.  Program arguments: ./bin/swift -frontend -emit-ir -sdk /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk - 
1.  Swift version 5.1.1-dev (Swift 4043d43d43)
2.  While generating SIL witness table protocol conformance to 'ProtoB' (at <stdin>:3:1) for type 'Foo<T>' (declared at [<stdin>:14:1 - line:14:32] RangeText="class Foo<T: ProtoA>: ProtoB { ")
3.  While generating protocol witness thunk SIL function "@$s4main3FooCyqd__GAA6ProtoBA2aEP5stuffSivgTW".
 for getter for stuff (at <stdin>:9:9)
0  swift                    0x00000001059c5c28 llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 40
1  swift                    0x00000001059c4b68 llvm::sys::RunSignalHandlers() + 248
2  swift                    0x00000001059c6247 SignalHandler(int) + 295
3  libsystem_platform.dylib 0x00007fff67f6ab1d _sigtramp + 29
4  libdyld.dylib            0x00007fff67d5a065 dyldGlobalLockRelease() + 0
5  libsystem_c.dylib        0x00007fff67e40a1c abort + 120
6  libsystem_c.dylib        0x00007fff67e3fcd6 err + 0
7  swift                    0x0000000105ad6893 swift::Lowering::SILGenModule::useConformance(swift::ProtocolConformanceRef) (.cold.1) + 35
8  swift                    0x0000000101daba57 swift::Lowering::SILGenModule::useConformance(swift::ProtocolConformanceRef) + 535
9  swift                    0x0000000101dab8ec swift::Lowering::SILGenModule::useConformance(swift::ProtocolConformanceRef) + 172
10 swift                    0x0000000101dac044 swift::SILInstructionVisitor<LazyConformanceEmitter, void>::visit(swift::SILInstruction*) + 1060
11 swift                    0x0000000101dabbf8 swift::Lowering::SILGenModule::emitLazyConformancesForFunction(swift::SILFunction*) + 136
12 swift                    0x0000000101dfaa8f swift::Lowering::SILGenModule::emitProtocolWitness(swift::ProtocolConformanceRef, swift::SILLinkage, swift::IsSerialized_t, swift::SILDeclRef, swift::SILDeclRef, swift::Lowering::IsFreeFunctionWitness_t, swift::Witness) + 2111
13 swift                    0x0000000101dfc87b (anonymous namespace)::SILGenWitnessTable<(anonymous namespace)::SILGenConformance>::addMethodImplementation(swift::SILDeclRef, swift::SILDeclRef, swift::Witness) + 283
14 swift                    0x0000000101dfc72a (anonymous namespace)::SILGenWitnessTable<(anonymous namespace)::SILGenConformance>::addMethod(swift::SILDeclRef) + 362
15 swift                    0x0000000101dfc5a8 void llvm::function_ref<void (swift::AccessorDecl*)>::callback_fn<swift::SILWitnessVisitor<(anonymous namespace)::SILGenConformance>::visitAbstractStorageDecl(swift::AbstractStorageDecl*)::'lambda'(swift::AccessorDecl*)>(long, swift::AccessorDecl*) + 72
16 swift                    0x00000001027f8876 swift::AbstractStorageDecl::visitExpectedOpaqueAccessors(llvm::function_ref<void (swift::AccessorKind)>) const + 166
17 swift                    0x00000001027f87bc swift::AbstractStorageDecl::visitOpaqueAccessors(llvm::function_ref<void (swift::AccessorDecl*)>) const + 44
18 swift                    0x0000000101dfc33a swift::SILWitnessVisitor<(anonymous namespace)::SILGenConformance>::visitProtocolDecl(swift::ProtocolDecl*) + 1530
19 swift                    0x0000000101dfa0e4 swift::Lowering::SILGenModule::getWitnessTable(swift::NormalProtocolConformance*) + 356
20 swift                    0x0000000101dfbb85 (anonymous namespace)::SILGenType::emitType() + 1317
21 swift                    0x0000000101dfb659 swift::Lowering::SILGenModule::visitNominalTypeDecl(swift::NominalTypeDecl*) + 25
22 swift                    0x0000000101d26e13 swift::Lowering::SILGenModule::emitSourceFile(swift::SourceFile*) + 915
23 swift                    0x0000000101d27d52 swift::SILModule::constructSIL(swift::ModuleDecl*, swift::Lowering::TypeConverter&, swift::SILOptions&, swift::FileUnit*) + 322
24 swift                    0x0000000101d28131 swift::performSILGeneration(swift::ModuleDecl*, swift::Lowering::TypeConverter&, swift::SILOptions&) + 17
25 swift                    0x0000000101a2ae3d performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*, swift::UnifiedStatsReporter*) + 8205
26 swift                    0x0000000101a27ec8 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 3080
27 swift                    0x00000001019cd0be main + 734
28 libdyld.dylib            0x00007fff67d692e5 start + 1
29 libdyld.dylib            0x0000000000000006 start + 2552851746

@CodaFi
Copy link
Member

CodaFi commented Nov 22, 2019

Pulling this back since it hasn't been resolved.

@swift-ci
Copy link
Collaborator

swift-ci commented Jun 3, 2020

Comment by Frank Langel (JIRA)

@glbrntt Hi George, could you advise on the best way of fixing this issue.

  • Who can fix it ?

  • What is the timeline ?

  • What would be the best design approach to fix it ?

Any advise to get this fix quickly would be much appreciated

Kind regards

Frank

@glbrntt
Copy link
Author

glbrntt commented Jun 5, 2020

fll (JIRA User) I only reported the bug so can't help you there I'm afraid!

@swift-ci
Copy link
Collaborator

swift-ci commented Jun 5, 2020

Comment by Frank Langel (JIRA)

@glbrntt

Hi George, thank you for the response. Who would be a good contact/person to talk to drive this forward?

Thanks a lot

Frank

@CodaFi
Copy link
Member

CodaFi commented Jun 5, 2020

fll (JIRA User) We're aware of this as a structural problem with the GSB. It has the propensity to drop certain kinds of requirements if they are not referenced in a certain structural way. In fact, a much smaller reproducer is

protocol Requirement {}
extension C : P {}

class C<T> where T : Requirement {}

protocol P {
  associatedtype T : Requirement
  func f()
}

extension P {
  func f() {}
}

This is going to sound very strange, but the workaround is to force a data dependency on the requirement that isn't otherwise referenced by the protocol. In my example above, that means the function "f" needs to use "T" in some way.

protocol Requirement {}
extension C : P {}

class C<T> where T : Requirement {}

protocol P {
  associatedtype T : Requirement
  func f(_ : T)
}

extension P {
  func f(_ : T) {}
}

We are in the process of re-evaluating the algorithms underpinning the GSB and hope we can arrive at a resolution to this issue that will leave us with not just a more correct facility, but a faster one too!

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@AnthonyLatsis AnthonyLatsis added the crash Bug: A crash, i.e., an abnormal termination of software label Dec 12, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself crash Bug: A crash, i.e., an abnormal termination of software
Projects
None yet
Development

No branches or pull requests

7 participants