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-7235] "nearly matches optional requirement" warning for correct function signature from protocol UICollectionViewDelegate #49783

Closed
nevil opened this issue Mar 19, 2018 · 3 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself regression swift 4.2

Comments

@nevil
Copy link

nevil commented Mar 19, 2018

Previous ID SR-7235
Radar rdar://problem/38641997
Original Reporter @nevil
Type Bug
Status Closed
Resolution Invalid
Environment

swift-DEVELOPMENT-SNAPSHOT-2018-03-17-a.xctoolchain
iOS SDK from Xcode 9.2:
Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, 4.2Regression
Assignee None
Priority Medium

md5: 422f3bbc92fab223281f16c055b6d033

relates to:

  • SR-7229 Unexpected ambiguity error

Issue Description:

The below code generates an unexpected warning as the function has a correct signature from UICollectionViewDelegate:

bug.swift:12:10: warning: instance method 'collectionView(_:cellForItemAt:)' nearly matches optional requirement 'collectionView(_:canFocusItemAt:)' of protocol 'UICollectionViewDelegate'
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
//

import UIKit

class ACollectionViewDelegate: NSObject, UICollectionViewDelegate {
    var collectionView: UICollectionView! { return UICollectionView() }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 0
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        return UICollectionViewCell()
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    }
}

Full invocation and output:

Apple Swift version 4.2-dev (LLVM cef3fe6d75, Clang bdc970d89c, Swift 931d06d1cc)
Target: arm64-apple-ios10.0
/Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2018-03-17-a.xctoolchain/usr/bin/swift -frontend -c -primary-file SR-7235.swift -target arm64-apple-ios10.0 -Xllvm -aarch64-use-tbi -enable-objc-interop -sdk /Applications/Xcode-beta.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk -color-diagnostics -module-name main -o SR-7235.o
SR-7235.swift:12:10: warning: instance method 'collectionView(_:cellForItemAt:)' nearly matches optional requirement 'collectionView(_:canFocusItemAt:)' of protocol 'UICollectionViewDelegate'
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
         ^
SR-7235.swift:12:10: note: candidate has non-matching type '(UICollectionView, IndexPath) -> UICollectionViewCell'
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
         ^
SR-7235.swift:12:10: note: move 'collectionView(_:cellForItemAt:)' to an extension to silence this warning
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
         ^
SR-7235.swift:12:10: note: make 'collectionView(_:cellForItemAt:)' private to silence this warning
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
         ^
    private
UIKit.UICollectionViewDelegate:63:26: note: requirement 'collectionView(_:canFocusItemAt:)' declared here
    optional public func collectionView(_ collectionView: UICollectionView, canFocusItemAt indexPath: IndexPath) -> Bool
                         ^
@belkadan
Copy link
Contributor

@swift-ci create

@nevil
Copy link
Author

nevil commented Oct 29, 2019

Still occurring:

Apple Swift version 5.1 (swiftlang-1100.0.270.13 clang-1100.0.33.7)
Target: arm64-apple-ios10.0
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -frontend -c -primary-file SR-7235.swift -target arm64-apple-ios10.0 -Xllvm -aarch64-use-tbi -enable-objc-interop -sdk /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk -color-diagnostics -module-name main -o SR-7235.o
SR-7235.swift:12:10: warning: instance method 'collectionView(_:cellForItemAt:)' nearly matches optional requirement 'collectionView(_:canFocusItemAt:)' of protocol 'UICollectionViewDelegate'
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
         ^
SR-7235.swift:12:10: note: move 'collectionView(_:cellForItemAt:)' to an extension to silence this warning
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
         ^
SR-7235.swift:12:10: note: make 'collectionView(_:cellForItemAt:)' private to silence this warning
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
         ^
    private
UIKit.UICollectionViewDelegate:66:19: note: requirement 'collectionView(_:canFocusItemAt:)' declared here
    optional func collectionView(_ collectionView: UICollectionView, canFocusItemAt indexPath: IndexPath) -> Bool
                  ^

@nevil
Copy link
Author

nevil commented Oct 29, 2019

Revisiting this I realized that this is my issue.
cellForItemAt delegate is from UICollectionViewDataSource, not UICollectionViewDelegate.

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

No branches or pull requests

3 participants