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-7048] Layout constraint warning or compilation error #49596

Closed
swift-ci opened this issue Feb 21, 2018 · 6 comments
Closed

[SR-7048] Layout constraint warning or compilation error #49596

swift-ci opened this issue Feb 21, 2018 · 6 comments
Assignees
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-7048
Radar rdar://problem/34812895
Original Reporter tkanzakic (JIRA User)
Type Bug
Status Closed
Resolution Duplicate
Environment

Xcode 9.2 (9C40b)

Swift 4.0.3 (swiftlang-900.0.74.1 clang-900.0.39.2)

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

md5: a48f0f6b52d1f7e64830730271baf2ab

duplicates:

  • SR-6816 Let protocols inherit from class types

relates to:

  • SR-6265 Remove "redundant layout constraint" warning

Issue Description:

I am defining a protocol that has a requirement to be implemented only by a specific Objective-C class. If I define the protocol like this the code compile but I get a warning:

// Warning: Layout constraint constraint 'Self' : 'AnyObject' implied here
protocol A: class where Self: NSObject {
    var p: NSObject { get set }
}

class B {
    func doSomethingWith(a: A) {
        a.p = NSObject()
    }
}

If I remove the redundant definition then I get a value semantic error:

protocol A where Self: NSObject {
    var p: NSObject { get set }
}

class B {
    func doSomethingWith(a: A) {
        // Compiler error: Cannot assign to property: 'a' is a 'let' constant
        a.p = NSObject()
    }
}

More on this:

Instead of using a property I have defined a setter method (see bellow with a close to the reality example) instead of property, in the same way if I declare the class restriction I get the warning but the code works.

import UIKit

struct ViewModel {}

// Warning: Layout constraint constraint 'Self' : 'AnyObject' implied here
protocol ViewControllerProtocol: class where Self: UIViewController {
    func setViewModel(_ viewModel: ViewModel)
}

class Presenter {
    func configure(viewController: ViewControllerProtocol) {
        viewController.setViewModel(ViewModel())
    }
}

class ViewController: UIViewController {
    var viewModel: ViewModel?
}

extension ViewController: ViewControllerProtocol {
    func setViewModel(_ viewModel: ViewModel) {
        self.viewModel = viewModel
    }
}

let viewController = ViewController()
let presenter = Presenter()
presenter.configure(viewController: viewController) 

If I remove the class restriction in the protocol definition I get a crash:

error: Playground execution aborted: error: Execution was interrupted, reason: EXC_BAD_ACCESS (code=1, address=0x0).
The process has been left at the point where it was interrupted, use "thread return -x" to return to the state before expression evaluation.
@belkadan
Copy link
Contributor

Oops, I guess it's not strictly a duplicate. It definitely should be unnecessary, though.

@belkadan
Copy link
Contributor

@swift-ci create

@swift-ci
Copy link
Collaborator Author

swift-ci commented Apr 9, 2018

Comment by Takeichi Kanzaki Cabrera (JIRA)

reproduced in:

  • Xcode 9.3 (9E145)

  • Apple Swift version 4.1 (swiftlang-902.0.48 clang-902.0.37.1). Target: x86_64-apple-darwin17.5.0

@slavapestov
Copy link
Member

The problem here is this is not supported yet: "protocol A where Self: NSObject"

I'm collecting dupes of this, will consolidate later.

@AnnaZaks
Copy link
Mannequin

AnnaZaks mannequin commented Jul 23, 2019

tkanzakic (JIRA User), Could you verify if the problem is fixed and if so move the JIRA to "Closed"?

Thanks!
Anna

@swift-ci
Copy link
Collaborator Author

Comment by Takeichi Kanzaki Cabrera (JIRA)

Tested in Xcode 10.2.1 (10E1001) with Swift 5.0 and working as a charm. Thanks

@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