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-2673] @NSManaged property can't satisfy protocol requirement #45278

Closed
swift-ci opened this issue Sep 17, 2016 · 11 comments
Closed

[SR-2673] @NSManaged property can't satisfy protocol requirement #45278

swift-ci opened this issue Sep 17, 2016 · 11 comments

Comments

@swift-ci
Copy link
Collaborator

@swift-ci swift-ci commented Sep 17, 2016

Previous ID SR-2673
Radar rdar://problem/28632818
Original Reporter charlieMonroe (JIRA User)
Type Bug
Status Resolved
Resolution Done
Environment

macOS 10.12 GM, Xcode 8 (final)

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, 3.0Regression
Assignee @belkadan
Priority Medium

md5: a342417346a61fd647db73f0c7929e52

cloned to:

  • SR-2874 Protocol checking synthesizes accessors incorrectly

relates to:

  • SR-2916 Compiler crash when using a lazy var to implement property in @objc protocol

Issue Description:

I don't know if this should be a radar issue, or an issue here - please let me know if I should refile this as a radar. Consider the following code:

@objc public protocol XUObjectProtocol: NSObjectProtocol { }

@objc protocol IdentifiableEntity: XUObjectProtocol {
    var entityID: String { get set }
}

class XUObject: NSManagedObject, IdentifiableEntity {
    @NSManaged var entityID: String
}

This produces an error that "Stored property 'entityID' requires an initial value or should be @NSManaged". Adding a default value, of course produces an error that "@NSManaged property cannot have an initial value".

@belkadan
Copy link
Contributor

@belkadan belkadan commented Sep 20, 2016

@slavapestov, possibly related to some of the eager synthesis of accessors for protocols?

@ChristopherRogers
Copy link
Contributor

@ChristopherRogers ChristopherRogers commented Oct 5, 2016

Relatedly, starting with Swift 3.0.1 (Xcode 8.1 beta 2) having the managed properties declared in an extension rather than in the class itself (similar to how Xcode generates subclasses) produces the error, "Extensions may not contain stored properties", along with a segfault.

0  swift                    0x00000001105972ed PrintStackTraceSignalHandler(void*) + 45
1  swift                    0x0000000110596d36 SignalHandler(int) + 470
2  libsystem_platform.dylib 0x00007fffbdaebbba _sigtramp + 26
3  libsystem_platform.dylib 0xb5510da74faacc08 _sigtramp + 2449215592
4  swift                    0x000000010d9fcc42 swift::irgen::projectPhysicalClassMemberAddress(swift::irgen::IRGenFunction&, llvm::Value*, swift::SILType, swift::SILType, swift::VarDecl*) + 1250
5  swift                    0x000000010daf9ab0 swift::SILVisitor<(anonymous namespace)::IRGenSILFunction, void>::visit(swift::ValueBase*) + 27440
6  swift                    0x000000010daf0518 swift::irgen::IRGenModule::emitSILFunction(swift::SILFunction*) + 9080
7  swift                    0x000000010da139c1 swift::irgen::IRGenerator::emitGlobalTopLevel() + 1329
8  swift                    0x000000010dad622b performIRGeneration(swift::IRGenOptions&, swift::ModuleDecl*, swift::SILModule*, llvm::StringRef, llvm::LLVMContext&, swift::SourceFile*, unsigned int) + 1259
9  swift                    0x000000010d9a30d7 performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*) + 23687
10 swift                    0x000000010d99b175 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 17029
11 swift                    0x000000010d9583bd main + 8685
12 libdyld.dylib            0x00007fffbd8df255 start + 1
13 libdyld.dylib            0x00000000000000ed start + 1114771097

@belkadan
Copy link
Contributor

@belkadan belkadan commented Oct 5, 2016

@ChristopherRogers, do you have a self-contained example of that? (Rather than guess at what options to use in Xcode to match your situation.)

@belkadan
Copy link
Contributor

@belkadan belkadan commented Oct 5, 2016

I don't see the behavior Christopher is describing on master with this extension, either in the same source file as the class or in a separate one.

extension MyEntity {
  @NSManaged public var thingy: String?
  @NSManaged public var relationship: MyEntity?
}

@belkadan
Copy link
Contributor

@belkadan belkadan commented Oct 5, 2016

…unless like Charlie's case this only shows up when you have an @objc protocol.

@swift-ci
Copy link
Collaborator Author

@swift-ci swift-ci commented Oct 5, 2016

Comment by Charlie Monroe (JIRA)

BTW @objc is necessary since otherwise the protocol conformity doesn't work (radar://24031727):

@objc(XUObject)
public class XUObject: NSManagedObject {}

protocol XUMyProtocol {
    func sayHello()
}

extension XUObject: XUMyProtocol {
    func sayHello() {
        print("Hello")
    }
}

let moc = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
let model = NSManagedObjectModel.mergedModel(from: [Bundle.main])!
moc.persistentStoreCoordinator = NSPersistentStoreCoordinator(managedObjectModel: model)
let obj = XUObject(entity: NSEntityDescription.entity(forEntityName: "XUObject", in: moc)!, insertInto: moc)
let foo = (obj as Any) as? XUMyProtocol // nil!

@belkadan
Copy link
Contributor

@belkadan belkadan commented Oct 5, 2016

Ah, yeah, that's a longstanding issue. :-(

@belkadan
Copy link
Contributor

@belkadan belkadan commented Oct 6, 2016

@ChristopherRogers
Copy link
Contributor

@ChristopherRogers ChristopherRogers commented Oct 6, 2016

It seems you already figured it out while I was busy sleeping, but yes, it seems that the error only shows up when it's a @objc protocol.

import CoreData

class MyEntity: NSManagedObject { }

extension MyEntity {
    @NSManaged public var thingy: String?
    @NSManaged public var relationship: MyEntity?
}

@objc
protocol ThingyProvider {
    var thingy: String? { get }
}

extension MyEntity: ThingyProvider { }

@swift-ci
Copy link
Collaborator Author

@swift-ci swift-ci commented Oct 6, 2016

Comment by Charlie Monroe (JIRA)

@belkadan - actually I've tested now again and it seems to be working with Xcode 8 (the non-@objc conformity). I just need to test whether this is something fixed in Swift (and thus applicable for previous versions of the OS), or in Siera's CoreData (and thus only applicable for 10.12+)...

@ChristopherRogers
Copy link
Contributor

@ChristopherRogers ChristopherRogers commented Oct 11, 2016

Just wanted to let you know that this still isn't working in the latest Xcode beta (8.1 beta (8T47)). You can test it with the sample code I posted in the comment above.

@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
Projects
None yet
Development

No branches or pull requests

3 participants