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-1480] Subclasses do not inherit protocol conformance from superclasses as of Xcode 7.3.1 #44089

Closed
swift-ci opened this issue May 11, 2016 · 25 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-1480
Radar rdar://problem/26223408
Original Reporter irace (JIRA User)
Type Bug
Status Resolved
Resolution Done

Attachment: Download

Environment

Xcode Version 7.3.1 (7D1014)

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug
Assignee irace (JIRA)
Priority Medium

md5: 327a189ed01c04fc9cc3cc6c213709b3

Issue Description:

The following code worked in Xcode 7.3 and still works in a 7.3.1 playground, but no longer works in a 7.3.1 project file:

protocol Foo {
}

extension Foo {
    func bar() -> Self { return self }
}

extension NSObject: Foo {}

let view = UIView().bar() // Value of type `UIView` has no member `bar`
@swift-ci
Copy link
Collaborator Author

Comment by Bryan Irace (JIRA)

Seems similar to this: https://bugs.swift.org/browse/SR-1158

@jckarter
Copy link
Member

irace (JIRA User) Do you have a project that reproduces the bug? I tried pasting the code into a new file into a fresh iOS single-view application project, and it builds successfully. I was also successfully able to compile a standalone file with `swiftc`:

import UIKit


protocol Foo {
}

extension Foo {
    func bar() -> Self { return self }
}

extension NSObject: Foo {}

let view = UIView().bar() // Value of type `UIView` has no member `bar`

@swift-ci
Copy link
Collaborator Author

Comment by Bryan Irace (JIRA)

Hmmm... it also is not happening for me in a fresh iOS single-view application project, but it does happen in (both of) my “real” projects. I can't share either of those though...

Is there anything in particular I should be looking for, when comparing e.g. build settings between my actual projects and this dummy one that works?

@jckarter
Copy link
Member

irace (JIRA User) A couple possible influencers come to mind:

  • Whole module optimization being enabled or not,

  • Whether the extension and use site appear in different files, and potentially the relative order of the files in the build command.

@jckarter
Copy link
Member

On the off chance you have multiple Xcodes installed, or multiple Swift toolchains installed, you might double-check that you have the right Xcode xcode-select-ed, and the 7.3.1 toolchain enabled within Xcode.

@swift-ci
Copy link
Collaborator Author

Comment by Nick DiStefano (JIRA)

irace (JIRA User) @jckarter Adding an extension on UIView brings this error to the playground for me.

@swift-ci
Copy link
Collaborator Author

Comment by Ray Migneco (JIRA)

I was able to reproduce the issue in my project using Xcode 7.3.1

The "solution" was re-ordering the source files for the compiler by editing the PBX. In this specific case, there was an extension on my subclass that I placed last in the PBX file. By doing this, I was able to compile my project without build errors.

For what it's worth, I also tried module optimization and this did not fix the issue.

@jckarter
Copy link
Member

rmigneco (JIRA User) Would you be able to share a project that shows the problem?

@jckarter
Copy link
Member

ndis1 (JIRA User) Thanks! The extension UIView seems to be what triggers it.

@swift-ci
Copy link
Collaborator Author

Comment by Ray Migneco (JIRA)

@jckarter I can't share my employer's project code and having some issues reproducing on a simplified scale. In essence, the issue is very similar to what ndis1 (JIRA User) shared with his playground example

@jckarter
Copy link
Member

Thanks rmigneco (JIRA User). I can look into ndis1 (JIRA User)'s case, but I'm not sure your or irace (JIRA User)'s problem is exactly the same, since it involves multiple files. Let me know if you do manage to isolate it.

@swift-ci
Copy link
Collaborator Author

Comment by Nick DiStefano (JIRA)

TestExtensionError.zip

@jckarter Attached is a version of this error in a project. If the file `DummyExtension.swift` is the first file in Compile Sources, the build will fail. If `DummyExtension.swift` is moved down so that it is after `ViewController.swift` the project will build

@jckarter
Copy link
Member

ndis1 (JIRA User) Thanks! This sounds more like the situation Bryan and Ray are in.

@jckarter
Copy link
Member

Playing around a bit, it appears that the single-file case only affects imported Objective-C classes. I tried a single-file pure Swift test case, and it worked:

class Object {}
class Responder: Object {}
class View: Responder {}

extension View {}

protocol Foo {}

extension Foo {
  func bar() -> Self { return self }
}

extension Object: Foo {}

_ = Responder().bar()
_ = View().bar()

I also tried breaking Object, Responder and View out into a separate module, as well as splitting Object and Responder/View into two modules to simulate the Foundation/AppKit split, and those cases worked as well.

Once I move the class hierarchy into a bridging header, the bug repros:

// bridging.h
__attribute__((objc_root_class))
@interface Object
+alloc;
-init;
@end

@interface Responder: Object
@end

@interface View: Responder
@end
// test.swift
extension View {}

protocol Foo {}

extension Foo {
  func bar() -> Self { return self }
}

extension Object: Foo {}

_ = Responder().bar() // Works
_ = View().bar() // Fails

ndis1 (JIRA User) rmigneco (JIRA User) irace (JIRA User) is this consistent with the problems you're seeing? Do you ever see this also happen with native Swift class hierarchies?

@swift-ci
Copy link
Collaborator Author

Comment by Bryan Irace (JIRA)

My issue is specifically with an extension on NSObject.

@swift-ci
Copy link
Collaborator Author

Comment by Nick DiStefano (JIRA)

It is consistent with what I have seen. I have not seen it with native Swift class hierarchies

@jckarter
Copy link
Member

Also appears to only affect grandchild classes of the conforming type. If the dummy extension and method invocation are performed on `Responder` instead of `View`, the test works.

@swift-ci
Copy link
Collaborator Author

Comment by Ray Migneco (JIRA)

@jckarter Thanks for following up.

My set-up does involve objective C:

  • Class A is defined in an external framework, written in Objective C and conforms to a protocol

  • Class B inherits from Class A, is written in Objective C and defined within my project

  • Class B has one extension written in Swift on it, within the project.

By ensuring that the file containing the extension on Class B is compiled last, there is no compile time error.

Otherwise, I receive the error. A work-around is to define a redundant extension on Class B that conforms to the protocol but this seems unnecessary in the long-term.

@jckarter
Copy link
Member

rmigneco (JIRA User) Does class B directly inherit class A, or are there superclasses in between them?

@swift-ci
Copy link
Collaborator Author

Comment by Ray Migneco (JIRA)

@jckarter B directly inherits from A

@jckarter
Copy link
Member

Interesting, in my test case, the bug doesn't repro for Responder (directly inherited from NSObject), only for View (indirectly inheriting from NSObject via Responder).

@swift-ci
Copy link
Collaborator Author

Comment by Ray Migneco (JIRA)

@jckarter

Sorry, I have to update my earlier description. My class B actually is a grandchild class. Class A, which I mentioned previously, actually inherits from another class which conforms to the protocol.

@jckarter
Copy link
Member

Thanks for double-checking!

@jckarter
Copy link
Member

Trying out a fix: #2565

@jckarter
Copy link
Member

Fix merged as c88720e.

@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

2 participants