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-11108] Conformance checking doesn't consider 'where' clause for vars #53503

Closed
swift-ci opened this issue Jul 11, 2019 · 8 comments
Closed
Assignees
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

@swift-ci
Copy link
Collaborator

Previous ID SR-11108
Radar rdar://problem/52950463
Original Reporter minuscorp (JIRA User)
Type Bug
Status Resolved
Resolution Duplicate

Attachment: Download

Environment

macOS 10.15 beta 3

Xcode 11 beta 3 && Xcode 10.2

Swift 5.0, Swift 5.1 (master branch, 5.1 branch, Xcode built-in)

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, CompilerCrash
Assignee @slavapestov
Priority Medium

md5: 95afe6f0a7d84a0afa7fd3aa02cc1f82

duplicates:

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

Issue Description:

Executing the following code crashes the compiler on Xcode 10.2 using Swift 5.0 and on Xcode 11 beta 3, using three different snapshots (master branch, 5.1, Xcode's one):

protocol Example {
    associatedtype Signed: SignedInteger
    associatedtype SP: StringProtocol
    var string: String { get }
}
extension Example {
    var string: String {
        return "Foo"
    }
}
class MyClass<T: SignedInteger, S: StringProtocol>: Example {
    typealias Signed = T
    typealias SP = S
}
extension MyClass where T == Int, S == String {
    var string: String {
        return "Bar"
    }
}

let myclass = MyClass<Int, String>()

print(myclass.string)
1.  While emitting IR SIL function "@$s15example_package7MyClassCyqd__qd_0_GAA7ExampleA2aEP6stringSSvgTW".
 for getter for string (at /Users/minuscorp/Documents/example-package/Sources/example-package/example_package.swift:10:9)
0  swift                    0x00000001081caee3 PrintStackTraceSignalHandler(void*) + 51
1  swift                    0x00000001081ca6bc SignalHandler(int) + 348
2  libsystem_platform.dylib 0x00007fff733c5b1d _sigtramp + 29
3  swift                    0x00000001055f7e71 swift::AccessLevelRequest::OutputType swift::evaluateOrDefault<swift::AccessLevelRequest>(swift::Evaluator&, swift::AccessLevelRequest, swift::AccessLevelRequest::OutputType) + 721
4  swift                    0x0000000104078031 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                    0x0000000104077fa2 swift::irgen::FulfillmentMap::searchWitnessTable(swift::irgen::IRGenModule&, swift::CanType, swift::ProtocolDecl*, unsigned int, swift::irgen::MetadataPath&&, swift::irgen::FulfillmentMap::InterestingKeysCallback const&) + 450
6  swift                    0x0000000104077da8 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                    0x00000001041c88cf swift::irgen::GenericTypeRequirements::enumerateFulfillments(swift::irgen::IRGenModule&, swift::SubstitutionMap, llvm::function_ref<void (unsigned int, swift::CanType, llvm::Optional<swift::ProtocolConformanceRef>)>) + 335
8  swift                    0x0000000104076f5a swift::irgen::FulfillmentMap::searchNominalTypeMetadata(swift::irgen::IRGenModule&, swift::CanType, swift::MetadataState, unsigned int, swift::irgen::MetadataPath&&, swift::irgen::FulfillmentMap::InterestingKeysCallback const&) + 490
9  swift                    0x0000000104076d38 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                    0x00000001042754fa swift::irgen::IRGenFunction::bindLocalTypeDataFromTypeMetadata(swift::CanType, swift::irgen::IsExact_t, llvm::Value*, swift::MetadataState) + 314
11 swift                    0x00000001041c6264 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                    0x000000010422d311 swift::irgen::IRGenModule::emitSILFunction(swift::SILFunction*) + 8049
13 swift                    0x00000001040ec4c9 swift::irgen::IRGenerator::emitLazyDefinitions() + 1353
14 swift                    0x000000010420d4b0 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                    0x000000010402f319 performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*, swift::UnifiedStatsReporter*) + 49433
16 swift                    0x000000010401f92e swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 6862
17 swift                    0x0000000103fbd9ce main + 1246
18 libdyld.dylib            0x00007fff731c4c49 start + 1
error: Segmentation fault: 11
@belkadan
Copy link
Contributor

With assertions:

Assertion failed: (is<T>() && "Invalid accessor called"), function get, file /Volumes/Data/swift-public/llvm/include/llvm/ADT/PointerUnion.h, line 136.
Stack dump:
0.  Program arguments: /Volumes/Data/swift-public/build/ninja/swift-macosx-x86_64/bin/swift -frontend -interpret - -enable-objc-interop -sdk /Volumes/Data/Applications/Xcode11.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.Internal.sdk -color-diagnostics -module-name main 
1.  Swift version 5.1-dev (LLVM af1f73e9e9, Swift 827465a077)
2.  While generating protocol witness thunk SIL function "@$s4main7MyClassCyqd__qd_0_GAA7ExampleA2aEP6stringSSvgTW".
 for getter for string (at <stdin>:7:9)

7  swift                    0x00000001101a9f03 swift::Lowering::SILGenModule::useConformance(swift::ProtocolConformanceRef) (.cold.1) + 35
8  swift                    0x000000010cb27bd7 swift::Lowering::SILGenModule::useConformance(swift::ProtocolConformanceRef) + 535
9  swift                    0x000000010cb27a6c swift::Lowering::SILGenModule::useConformance(swift::ProtocolConformanceRef) + 172
10 swift                    0x000000010cb281c6 swift::SILInstructionVisitor<LazyConformanceEmitter, void>::visit(swift::SILInstruction*) + 1062
11 swift                    0x000000010cb27d53 swift::Lowering::SILGenModule::emitLazyConformancesForFunction(swift::SILFunction*) + 99
12 swift                    0x000000010cb7640f swift::Lowering::SILGenModule::emitProtocolWitness(swift::ProtocolConformanceRef, swift::SILLinkage, swift::IsSerialized_t, swift::SILDeclRef, swift::SILDeclRef, swift::Lowering::IsFreeFunctionWitness_t, swift::Witness) + 1951
13 swift                    0x000000010cb7863b (anonymous namespace)::SILGenWitnessTable<(anonymous namespace)::SILGenConformance>::addMethodImplementation(swift::SILDeclRef, swift::SILDeclRef, swift::Witness) + 283
14 swift                    0x000000010cb784eb (anonymous namespace)::SILGenWitnessTable<(anonymous namespace)::SILGenConformance>::addMethod(swift::SILDeclRef) + 379
15 swift                    0x000000010cb78358 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                    0x000000010d488c36 swift::AbstractStorageDecl::visitExpectedOpaqueAccessors(llvm::function_ref<void (swift::AccessorKind)>) const + 86
17 swift                    0x000000010d488cfc swift::AbstractStorageDecl::visitOpaqueAccessors(llvm::function_ref<void (swift::AccessorDecl*)>) const + 44
18 swift                    0x000000010cb78100 swift::SILWitnessVisitor<(anonymous namespace)::SILGenConformance>::visitProtocolDecl(swift::ProtocolDecl*) + 1568

Just to be clear, that overload won't be used to satisfy the requirement, because requirements are satisfied once at the time the conformance is declared. But the compiler shouldn't crash, of course.

@swift-ci create

@swift-ci
Copy link
Collaborator Author

Comment by Jorge Revuelta Herrero (JIRA)

So that means that `string` should be declared on MyClass?

@belkadan
Copy link
Contributor

If you want it to be used when calling through the protocol, yes.

@swift-ci
Copy link
Collaborator Author

Comment by Jorge Revuelta Herrero (JIRA)

I want to make use of the specializations when initializing MyClass when the concrete extension was available, but it seems that is not possible?

protocol Example {
    associatedtype Signed: SignedInteger
    associatedtype SP: StringProtocol
    var string: String { get }
}
extension Example {
    var string: String {
        return "Foo"
    }
}
class MyClass<T: SignedInteger, S: StringProtocol>: Example {
    typealias Signed = T
    typealias SP = S
    var _string: String!

    init() {
        _string = string
    }
}
extension MyClass where T == Int, S == String {
    var string: String {
        return "Bar"
    }
}

let myclass = MyClass<Int, String>()

print(myclass.string) // should be "Bar"
print(myclass._string) // would like to be "Bar" but it'll be "Foo"

@belkadan
Copy link
Contributor

Swift only has three ways to get dynamic dispatch:

  • calling through a protocol requirement

  • calling a method on a class that might be overridden

  • calling a closure explicitly (which of course has to be dynamic)

Everything else is chosen by the compiler through overload resolution, including "which implementation satisfies this protocol requirement". And since Swift, unlike C++, does not make copies of the code for different generic arguments (except as an optimization), MyClass can only conform to Example in one way.

@slavapestov
Copy link
Member

@swift-ci
Copy link
Collaborator Author

Comment by Jorge Revuelta Herrero (JIRA)

Which PR solved this? 🙂

@slavapestov
Copy link
Member

Sorry, I did the wrong thing in JIRA. I meant to dupe it to an existing bug (still unresolved).

@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

4 participants