Nested classes or structs cannot use protocols in methods

class Test {
    func test() {
        struct NestedTest: Comparable {
            static func < (lhs: NestedTest, rhs: NestedTest) -> Bool {
                return false;
            }
            static func == (lhs: NestedTest, rhs: NestedTest) -> Bool {
                return false;
            }
        }
    }
}

error: Type 'NestedTest' does not conform to protocol 'Comparable'

Is it a bug or does not support this?

The fourth time this is asked about this week; apparently a recently introduced issue. Here's the SR-8696

2 Likes

Thank you for your answer. I want to try the sample you provided, but can't compile?How to get the output you gave?

why wasn’t this caught in the source compatibility suite?

Types nested in functions that explicitly conform to protocols with an operator function requirement is quite the specific case, I'm not surprised there isn't a compiler test for that. Regarding the source compatibility suite, it could be that the changes in the PR that introduced the bug didn't require testing source compatibility. But I can't be sure there is an appropriate test in the source compatibility suite either; that might also be the case, assuming there is high probability that the source compatibility tests were run at least once after introducing the bug.

I'm pretty sure there's no test for it because it has never worked. I'm not sure why it's getting so much attention right now.