Static member of super class is available without qualification in inner classes?

This code is illegal:

class A {
    static var superA: String {
        return "a"
    }
}

class B : A {
    func foo() {
         let myA: String = superA
     }
}

We need to make explicit that we’re referring to a static member by writing A.superA.

Surprisingly, this compiles (Swift 4.0):

class B : A {
    class Innner {
        func foo() {
            let myA: String = superA
        }
    }
}

Is this bug or feature?

That looks like a bug to me.

2 Likes

Should I report it, or do we need additional confirmation?

It seems this can be simplified to

class A {
    static let superA = "a"
    
    func foo() {
        // Doesn't compile
        _ = superA
        // Compiles
        _ = A.superA
    }
    
    class Inner {
        func foo() {
            // Compiles
            _ = superA
        }
    }
}

I’m quite used to the non-compiling behaviour, as func foo works at instance-scope, and superA is defined at type scope, so that’s not surprising.
I don’t know if it’s something that we want to revise (allowing instance scopes to access type scopes implicitly) as it might be confusing when shadowing is in place.

Though I don’t understand why the instance scope of Inner should be able to access superA

Please report it at bugs.swift.org. This way it’ll get more visibility.

Done: https://bugs.swift.org/browse/SR-7092

1 Like