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-12352] Segmentation Fault With Generic Protocol Extensions #54786

Closed
swift-ci opened this issue Mar 12, 2020 · 1 comment
Closed

[SR-12352] Segmentation Fault With Generic Protocol Extensions #54786

swift-ci opened this issue Mar 12, 2020 · 1 comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior.

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-12352
Radar rdar://problem/60832801
Original Reporter adamhitt (JIRA User)
Type Bug
Status Resolved
Resolution Duplicate

Attachment: Download

Environment

Tools

Xcode 11.3.1

Swift 5.1.3

System

macOS 10.15.2

iOS 13.2 SDK

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

md5: 59efa67ae8051b28e1474cd71a947d21

duplicates:

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

Issue Description:

Description

A protocol with an associated type requirement that has a function declared that does not utilize this requirement will cause a segmentation fault during compilation if there exists an extension that provides an implementation of this method.

Note: This behavior seems to be observed only when adopting the protocol in a generic class, and the class does not itself provide an implementation of said function.

TL/DR Workarounds: Using type aliasing to conform to the protocol instead of declaring the class as generic solves the issue, as does providing a default implementation in the generic class.

Playground for experimentation and reproduction attached!

Feedback ID: FB7624146

Reproduction Setup

  1. Create protocol that has an associated type requirement

  2. Declare two function signatures in protocol

    1. Function signature one with a parameter that has the associated type

    2. Function signature two as a void function (more specifically any function signature omitting that associated type)

  3. Declare an extension to the protocol

    1. Provide default implementation for both methods
  4. Declare a generic class that conforms to the protocol and declares associated type in its declaration (not using a type alias)

    1. Empty class, no methods or protocol overrides

Results

You will receive a segmentation fault.

Example of Failure

protocol AssociatedType {}


protocol GenericProtocol {
    associatedtype R: AssociatedType
    func works(t: R)
    func fails()
}


extension GenericProtocol {
    func works(t: R) {}
    func fails() {}
}


class Class<R: AssociatedType>: GenericProtocol {
}

Workaround 1: Generic class provides implementation

protocol AssociatedType {}


protocol GenericProtocol {
    associatedtype R: AssociatedType
    func works(t: R)
    func fails()
}


extension GenericProtocol {
    func works(t: R) {}
    func fails() {}
}


class Class<R: AssociatedType>: GenericProtocol {
    func fails() {}
}

Workaround 2: Declare type alias in basic class instead of declaring a generic class

protocol AssociatedType {}
class ConcreteAsociatedType: AssociatedType {}


protocol GenericProtocol {
    associatedtype R: AssociatedType
    func works(t: R)
    func fails()
}


extension GenericProtocol {
    func works(t: R) {}
    func fails() {}
}


class Class: GenericProtocol {
    typealias R = ConcreteAsociatedType
}
@beccadax
Copy link
Contributor

@swift-ci create

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 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.
Projects
None yet
Development

No branches or pull requests

2 participants