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-5357] Type-constrained protocol cannot be used as a generic argument for similarly constrained generic parameter #47931

Closed
jarrodldavis opened this issue Jul 2, 2017 · 2 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@jarrodldavis
Copy link

Previous ID SR-5357
Radar rdar://problem/33095936
Original Reporter @jarrodldavis
Type Bug
Status Resolved
Resolution Duplicate
Environment

macOS Sierra 10.12.5 (16F73), Xcode 9.0 beta 2 (9M137d)

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

md5: 503457675ee4c46224ec7dac3e0dc725

duplicates:

  • SR-55 non-@objc protocol existentials do not conform to their own protocol type

Issue Description:

Given a generic type X<Y> where Y is constrained to some type Z and a protocol A that constrains Self to Z, a variable or constant declaration of X<A> should be considered valid. Currently, as of Xcode 9 beta 2, the compiler gives a warning similar to "'X' requires that 'A' inherit from 'Z'" even though that is already true.

// Imagine these sheet classes and the bed struct in a separate module somewhere

class Sheet {
    func pullOver() {
        print("sleep time")
    }
}

class ComfySheet : Sheet {
    public static let veryComfy: ComfySheet = {
        return ComfySheet()
    }()

    func comfort() {
        print("comfortable")
    }
}

class BouncySheet : Sheet {
    public static let veryBouncy: BouncySheet = {
        return BouncySheet()
    }()

    func bounce() {
        print("bounce")
    }
}

class AwfulSheet : Sheet {
    override func pullOver() {
        print("ew")
    }
}

struct Bed<SheetType> where SheetType : Sheet {
    public let size: Double
    public let sheets: SheetType
}

// This protocol and the corresponding extensions are editable

protocol EnjoymentMeasurable where Self : Sheet {
    func enjoy()
}

extension ComfySheet : EnjoymentMeasurable {
    func enjoy() {
        print("8/10")
    }
}

extension BouncySheet : EnjoymentMeasurable {
    func enjoy() {
        print("11/10")
    }
}

func doSomething() {
    let bed: Bed<EnjoymentMeasurable> // error here: "'Bed' requires that 'EnjoymentMeasurable' inherit from 'Sheet'"
    bed = Bed(size: 4.0, sheets: ComfySheet.veryComfy)
    bed.sheets.enjoy()
}
@belkadan
Copy link
Contributor

belkadan commented Jul 4, 2017

This is arguably correct because EnjoymentMeasurable could have static requirements, and then Bed wouldn't be able to call those methods because the protocol type doesn't implement them. But it is a little frustrating when the protocol does not have static requirements.

@slavapestov, @jckarter, how are we tracking these?

@jckarter
Copy link
Member

jckarter commented Jul 5, 2017

SR-55 is the canonical "protocol types should conform to their protocol" issue.

@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. compiler The Swift compiler in itself
Projects
None yet
Development

No branches or pull requests

3 participants