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-4375] Using Generic with Protocols causes Segmentation Fault #46954

Closed
swift-ci opened this issue Mar 27, 2017 · 5 comments
Closed

[SR-4375] Using Generic with Protocols causes Segmentation Fault #46954

swift-ci opened this issue Mar 27, 2017 · 5 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-4375
Radar None
Original Reporter arup193 (JIRA User)
Type Bug
Status Resolved
Resolution Duplicate
Environment

MacBook Pro
Operating System: OS X El Capitan (10.11.6)
RAM: 16GB
Xcode: 8.2.1
Swift Version: 3.0.2

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

md5: 6420fbb80f768d47f694f48e2932126c

duplicates:

  • SR-75 Referencing a protocol function crashes the compiler

Issue Description:

I will explain the issue with a sample example, that you can run on a playground project.

import Foundation
class IProtocol {
     func meth1(arg: Int){}
}
class IObserver: IProtocol {
    private var className: String
    init(name: String) {
        className = name
    }
    override func meth1(arg: Int) {
        print(className, ":", arg)
    }
}
class Notifier<T> {
    private var listOfObservers = NSMutableArray()
    func addObserver(observer: T) {
        listOfObservers.add(observer)
    }
    func callObservers<ARG, RET>(function: (T)->(ARG)->RET, arg: ARG) {
        for obj in listOfObservers {
            let observer = obj as! T
            _ = function(observer)(arg)
        }
    }
}
let a = IObserver(name: "First I Observer")
let b = IObserver(name: "Second I Observer")
let n = Notifier<IProtocol>()
n.addObserver(observer: a)
n.addObserver(observer: b)
n.callObservers(function: IProtocol.meth1, arg: Int(1))

OUTPUT:
First I Observer: 1
Second I Observer: 1

Explanation:
The above example fails and does not build giving Command failed due to signal: Segmentation Fault: 11, when I change IProtocol to protocol from class. I have different protocols and different observer classes confirming those protocols. I am trying to implement a Notifier class that notifies the observers by taking the method from the protocol and arguments to be passed for that method.

Note:
I remove the {} from meth1(arg: ) and also remove the override keyword from IObserver class, when I change IProtocol to a protocol

@belkadan
Copy link
Contributor

Reproduced on master:

not existential
UNREACHABLE executed at /Volumes/Data/swift-public/swift/lib/SILGen/SILGenConvert.cpp:857!
0  swift                    0x000000010e70a1b8 llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 40
1  swift                    0x000000010e7093d6 llvm::sys::RunSignalHandlers() + 86
2  swift                    0x000000010e70a809 SignalHandler(int) + 361
3  libsystem_platform.dylib 0x00007fffd6ec1fba _sigtramp + 26
4  libsystem_platform.dylib 0x0000000112cba75c _sigtramp + 1004505020
5  libsystem_c.dylib        0x00007fffd6d20f83 abort + 127
6  swift                    0x000000010e6a9ca7 llvm::llvm_unreachable_internal(char const*, char const*, unsigned int) + 471
7  swift                    0x000000010bd03a82 swift::Lowering::SILGenFunction::emitOpenExistential(swift::SILLocation, swift::Lowering::ManagedValue, swift::ArchetypeType*, swift::SILType, swift::AccessKind) + 1330
8  swift                    0x000000010bd1f4fa swift::Lowering::SILGenFunction::emitOpenExistentialExprImpl(swift::OpenExistentialExpr*, llvm::function_ref<void (swift::Expr*)>) + 554
9  swift                    0x000000010bd2c531 swift::Lowering::RValue swift::Lowering::SILGenFunction::emitOpenExistentialExpr<swift::Lowering::RValue, (anonymous namespace)::RValueEmitter::visitOpenExistentialExpr(swift::OpenExistentialExpr*, swift::Lowering::SGFContext)::$_4>(swift::OpenExistentialExpr*, (anonymous namespace)::RValueEmitter::visitOpenExistentialExpr(swift::OpenExistentialExpr*, swift::Lowering::SGFContext)::$_4) + 65
10 swift                    0x000000010bd2214c swift::ASTVisitor<(anonymous namespace)::RValueEmitter, swift::Lowering::RValue, void, void, void, void, void, swift::Lowering::SGFContext>::visit(swift::Expr*, swift::Lowering::SGFContext) + 7452
11 swift                    0x000000010bd1eb89 swift::Lowering::SILGenFunction::emitRValueAsSingleValue(swift::Expr*, swift::Lowering::SGFContext) + 57
12 swift                    0x000000010bd26a70 swift::ASTVisitor<(anonymous namespace)::RValueEmitter, swift::Lowering::RValue, void, void, void, void, void, swift::Lowering::SGFContext>::visit(swift::Expr*, swift::Lowering::SGFContext) + 26176
13 swift                    0x000000010bd19f93 swift::Lowering::SILGenFunction::emitRValue(swift::Expr*, swift::Lowering::SGFContext) + 35
14 swift                    0x000000010bce44f0 (anonymous namespace)::ArgEmitter::emitArgumentFromSource(swift::Lowering::ArgumentSource&&, swift::SILType, swift::Lowering::AbstractionPattern, swift::SILParameterInfo) + 288
15 swift                    0x000000010bce0217 (anonymous namespace)::ArgEmitter::emit(swift::Lowering::ArgumentSource&&, swift::Lowering::AbstractionPattern) + 3735
16 swift                    0x000000010bce13fa (anonymous namespace)::ArgEmitter::emitExpanded(swift::Lowering::ArgumentSource&&, swift::Lowering::AbstractionPattern) + 234
17 swift                    0x000000010bcdf4b0 (anonymous namespace)::ArgEmitter::emit(swift::Lowering::ArgumentSource&&, swift::Lowering::AbstractionPattern) + 304
18 swift                    0x000000010bcdeb04 (anonymous namespace)::CallSite::emit(swift::Lowering::SILGenFunction&, swift::Lowering::AbstractionPattern, (anonymous namespace)::ParamLowering&, llvm::SmallVectorImpl<swift::Lowering::ManagedValue>&, llvm::SmallVectorImpl<std::__1::pair<swift::Lowering::LValue, swift::SILLocation> >&, llvm::Optional<swift::ForeignErrorConvention> const&, swift::ImportAsMemberStatus const&) && + 452
19 swift                    0x000000010bcde2e8 (anonymous namespace)::CallEmission::emitArgumentsForNormalApply(swift::CanTypeWrapper<swift::FunctionType>&, swift::Lowering::AbstractionPattern&, swift::CanTypeWrapper<swift::SILFunctionType>&, llvm::Optional<swift::ForeignErrorConvention>&, swift::ImportAsMemberStatus&, swift::Lowering::ApplyOptions&, llvm::SmallVectorImpl<swift::Lowering::ManagedValue>&, llvm::Optional<swift::SILLocation>&, swift::CanTypeWrapper<swift::FunctionType>&) + 2232
20 swift                    0x000000010bccc1af (anonymous namespace)::CallEmission::apply(swift::Lowering::SGFContext) + 3311
21 swift                    0x000000010bccaf73 swift::Lowering::SILGenFunction::emitApplyExpr(swift::Expr*, swift::Lowering::SGFContext) + 51
22 swift                    0x000000010bd20484 swift::ASTVisitor<(anonymous namespace)::RValueEmitter, swift::Lowering::RValue, void, void, void, void, void, swift::Lowering::SGFContext>::visit(swift::Expr*, swift::Lowering::SGFContext) + 84
23 swift                    0x000000010bd1ee53 swift::Lowering::SILGenFunction::emitIgnoredExpr(swift::Expr*) + 467
24 swift                    0x000000010bcc1ad7 swift::Lowering::SILGenModule::visitTopLevelCodeDecl(swift::TopLevelCodeDecl*) + 375

@belkadan
Copy link
Contributor

Looks like we still don't handle direct references to protocol methods. Reduced:

protocol IProtocol {
  func meth1()
}
struct IObserver: IProtocol {
  func meth1() { print("success") }
}
let x = IProtocol.meth1
x(IObserver())()

@jckarter, what's the bug for this?

arup193 (JIRA User), your workaround is to pass an explicit closure instead, something like

n.callObservers(function: { obj in { arg in obj.meth1(arg: arg) } }, arg: 1)

@jckarter
Copy link
Member

I think @slavapestov has that one.

@slavapestov
Copy link
Member

@swift-ci
Copy link
Collaborator Author

Comment by Arup Dandapat (JIRA)

Jordan Rose, thanks for the work around.

@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

5 participants