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-9275] Crash when calling init on a protocol's metatype where the protocol is constrained to a type #51749

Closed
swift-ci opened this issue Nov 16, 2018 · 3 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

@swift-ci
Copy link
Collaborator

Previous ID SR-9275
Radar None
Original Reporter mlex (JIRA User)
Type Bug
Status Resolved
Resolution Duplicate
Environment

macOS High Sierra (Version 10.13.6)

Xcode 10.0 (10A255)

Apple Swift version 4.2 (swiftlang-1000.11.37.1 clang-1000.11.45.1)
Target: x86_64-apple-darwin17.7.0

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

md5: 7e265f036ada5e12b2d3f4f9ba4dce7f

duplicates:

  • SR-5581 Proper support for protocol 'where' clause with constraints on 'Self'

Issue Description:

Created a protocol that's constrained to a class type. Created an instance of the metatype of that protocol, then attempted to call init on that instance, which crashes the compiler.

If instead, I create a typealias combining the bare protocol (without constraints) and the type constraint, then I can call init on an instance of that typealias' metatype.

Example code:

import Foundation

// MARK: - Crashes

protocol ProtocolWithObjectA where Self: NSObject {
    init(string: String)
}

final class ImplementationA: NSObject, ProtocolWithObjectA {
    init(string: String) { super.init() }
}

let typeA: ProtocolWithObjectA.Type = ImplementationA.self
typeA.init(string: "") // Crash here

// MARK: - Works

protocol ProtocolB {
    init(string: String)
}

typealias ProtocolWithObjectB = (ProtocolB & NSObject)

final class ImplementationB: NSObject, ProtocolB {
    init(string: String) { super.init() }
}

let typeB: ProtocolWithObjectB.Type = ImplementationB.self
typeB.init(string: "")

Stack trace:

0  swift                    0x000000011207c64a PrintStackTraceSignalHandler(void*) + 42
1  swift                    0x000000011207bdfe SignalHandler(int) + 302
2  libsystem_platform.dylib 0x00007fff78290f5a _sigtramp + 26
3  libsystem_platform.dylib 0x00007f90620dcad0 _sigtramp + 3924081552
4  swift                    0x000000010ec70590 swift::Lowering::ManagedValue::materialize(swift::Lowering::SILGenFunction&, swift::SILLocation) const + 352
5  swift                    0x000000010ed4fa9b (anonymous namespace)::Transform::transform(swift::Lowering::ManagedValue, swift::Lowering::AbstractionPattern, swift::CanType, swift::Lowering::AbstractionPattern, swift::CanType, swift::Lowering::SGFContext) + 7835
6  swift                    0x000000010ecd1830 swift::Lowering::Conversion::emit(swift::Lowering::SILGenFunction&, swift::SILLocation, swift::Lowering::ManagedValue, swift::Lowering::SGFContext) const + 1136
7  swift                    0x000000010ec7156b (anonymous namespace)::ScalarResultPlan::finish(swift::Lowering::SILGenFunction&, swift::SILLocation, swift::CanType, llvm::ArrayRef<swift::Lowering::ManagedValue>&) + 907
8  swift                    0x000000010ec8c5e7 swift::Lowering::SILGenFunction::emitApply(std::__1::unique_ptr<swift::Lowering::ResultPlan, std::__1::default_delete<swift::Lowering::ResultPlan> >&&, swift::Lowering::ArgumentScope&&, swift::SILLocation, swift::Lowering::ManagedValue, llvm::ArrayRef<swift::Substitution>, llvm::ArrayRef<swift::Lowering::ManagedValue>, swift::Lowering::CalleeTypeInfo const&, swift::Lowering::ApplyOptions, swift::Lowering::SGFContext) + 6935
9  swift                    0x000000010ec92c54 (anonymous namespace)::CallEmission::apply(swift::Lowering::SGFContext) + 3556
10 swift                    0x000000010ec8f359 swift::Lowering::SILGenFunction::emitApplyExpr(swift::Expr*, swift::Lowering::SGFContext) + 1513
11 swift                    0x000000010ed5b131 swift::Lowering::SILGenFunction::emitRValueAsOrig(swift::Expr*, swift::Lowering::AbstractionPattern, swift::Lowering::TypeLowering const&, swift::Lowering::SGFContext) + 529
12 swift                    0x000000010ed03785 swift::Lowering::ManagedValue llvm::function_ref<swift::Lowering::ManagedValue (swift::Lowering::SGFContext)>::callback_fn<(anonymous namespace)::RValueEmitter::visitErasureExpr(swift::ErasureExpr*, swift::Lowering::SGFContext)::$_8>(long, swift::Lowering::SGFContext) + 101
13 swift                    0x000000010ecd8ede std::__1::__function::__func<swift::Lowering::SILGenFunction::emitExistentialErasure(swift::SILLocation, swift::CanType, swift::Lowering::TypeLowering const&, swift::Lowering::TypeLowering const&, llvm::ArrayRef<swift::ProtocolConformanceRef>, swift::Lowering::SGFContext, llvm::function_ref<swift::Lowering::ManagedValue (swift::Lowering::SGFContext)>, bool)::$_5, std::__1::allocator<swift::Lowering::SILGenFunction::emitExistentialErasure(swift::SILLocation, swift::CanType, swift::Lowering::TypeLowering const&, swift::Lowering::TypeLowering const&, llvm::ArrayRef<swift::ProtocolConformanceRef>, swift::Lowering::SGFContext, llvm::function_ref<swift::Lowering::ManagedValue (swift::Lowering::SGFContext)>, bool)::$_5>, void (swift::SILValue)>::operator()(swift::SILValue&&) + 526
14 swift                    0x000000010ecbc323 swift::Lowering::SILGenBuilder::bufferForExpr(swift::SILLocation, swift::SILType, swift::Lowering::TypeLowering const&, swift::Lowering::SGFContext, std::__1::function<void (swift::SILValue)>) + 195
15 swift                    0x000000010ecd770c swift::Lowering::SILGenFunction::emitExistentialErasure(swift::SILLocation, swift::CanType, swift::Lowering::TypeLowering const&, swift::Lowering::TypeLowering const&, llvm::ArrayRef<swift::ProtocolConformanceRef>, swift::Lowering::SGFContext, llvm::function_ref<swift::Lowering::ManagedValue (swift::Lowering::SGFContext)>, bool) + 3516
16 swift                    0x000000010ecf375a swift::ASTVisitor<(anonymous namespace)::RValueEmitter, swift::Lowering::RValue, void, void, void, void, void, swift::Lowering::SGFContext>::visit(swift::Expr*, swift::Lowering::SGFContext) + 29530
17 swift                    0x000000010ed01719 void llvm::function_ref<void (swift::Expr*)>::callback_fn<swift::Lowering::RValue swift::Lowering::SILGenFunction::emitOpenExistentialExpr<swift::Lowering::RValue, (anonymous namespace)::RValueEmitter::visitOpenExistentialExpr(swift::OpenExistentialExpr*, swift::Lowering::SGFContext)::$_16>(swift::OpenExistentialExpr*, (anonymous namespace)::RValueEmitter::visitOpenExistentialExpr(swift::OpenExistentialExpr*, swift::Lowering::SGFContext)::$_16)::'lambda'(swift::Expr*)>(long, swift::Expr*) + 41
18 swift                    0x000000010ed01b7c swift::Lowering::SILGenFunction::emitOpenExistentialExprImpl(swift::OpenExistentialExpr*, llvm::function_ref<void (swift::Expr*)>) + 1004
19 swift                    0x000000010ecee187 swift::ASTVisitor<(anonymous namespace)::RValueEmitter, swift::Lowering::RValue, void, void, void, void, void, swift::Lowering::SGFContext>::visit(swift::Expr*, swift::Lowering::SGFContext) + 7559
20 swift                    0x000000010ed04363 swift::Lowering::SILGenFunction::emitIgnoredExpr(swift::Expr*) + 819
21 swift                    0x000000010ec89142 swift::ASTVisitor<swift::Lowering::SILGenModule, void, void, void, void, void, void>::visit(swift::Decl*) + 402
22 swift                    0x000000010ec8801b swift::Lowering::SILGenModule::emitSourceFile(swift::SourceFile*, unsigned int) + 939
23 swift                    0x000000010ec89bd5 swift::SILModule::constructSIL(swift::ModuleDecl*, swift::SILOptions&, swift::FileUnit*, llvm::Optional<unsigned int>, bool) + 1333
24 swift                    0x000000010e30b3fe performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*, swift::UnifiedStatsReporter*) + 28990
25 swift                    0x000000010e300dc5 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 7717
26 swift                    0x000000010e2a6a35 main + 1349
27 libdyld.dylib            0x00007fff77f82015 start + 1
@belkadan
Copy link
Contributor

Looks like this is fixed in swift-5.0-branch. @slavapestov, do you know what might have fixed it, or should we just add a test case and call it a day?

@slavapestov
Copy link
Member

@belkadan its your favorite bug: "Created a protocol that's constrained to a class type." No need to add a test case, it's pretty well covered now.

@belkadan
Copy link
Contributor

Oh, of course.

@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