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-7046] Autocomplete suggests methods that can't be called on certain instances #49594

Closed
AnthonyLatsis opened this issue Feb 20, 2018 · 8 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. code completion Area → source tooling: code completion source tooling Area: IDE support, SourceKit, and other source tooling

Comments

@AnthonyLatsis
Copy link
Collaborator

Previous ID SR-7046
Radar rdar://problem/45340583
Original Reporter @AnthonyLatsis
Type Bug
Status Resolved
Resolution Done

Attachment: Download

Environment

Xcode Version 9.2 (9C40b)

Additional Detail from JIRA
Votes 3
Component/s CodeCompletion
Labels Bug
Assignee @rintaro
Priority Medium

md5: 2666fe154a7afbbb66b70fdd218be9a2

relates to:

  • SR-9027 SourceKit suggests autocomplete for unavailable constrained methods
  • SR-9396 Nonsensical error message related to constrained extensions

Issue Description:

On the screenshot, `foo` can't be called on b, and the compiler will rightly show an error if I hit enter on the Autocomplete suggestion below.

![](Screen Shot 2018-02-21 at 1.41.38 AM.png)

I am not sure if Autocomplete is currently meant to be this smart, but if by any chance it is, here you go.

Another example:

extension Array { 
   func foo(...) where Element == Int { ... }
 // This method will also always be visible in code completion, regardless of what type Element is
} 
@swift-ci
Copy link
Collaborator

swift-ci commented Aug 2, 2018

Comment by Will Lisac (JIRA)

Using a protocol in the where clause is a possible workaround for this code completion issue.

class A<T> { }

extension A where T == String {
    // This method will always be visible in code completion
    func fooString() { }
}

extension A where T: StringProtocol {
    // This method will only be visible when T conforms to StringProtocol
    func fooStringProtocol() { }
}

let example = A<Bool>()
example.foo

@jepers
Copy link

jepers commented Dec 2, 2018

The protocol-workaround only works when all code is in the same swift file, if you put the class and extensions and the calling code in separate files, it will list all extensions again.

Another very similar attempted workaround detailed example here will show the same behavior, ie working only if all code is in the same file.

@jepers
Copy link

jepers commented Dec 3, 2018

I guess the following might be another manifestation of this issue:

let floatRange = Float(0) ..< Float(1)
floatRange.cou // ERROR: Value of type 'Range<Float>' has no member 'cou'; did you mean 'count'?
floatRange.count // ERROR: Type 'Float' does not conform to protocol 'SignedInteger'

I mean the supposedly helpful "did you mean `count`?" and the fact that `.count` is in the list of code completions, even though `Range<Float>` does not have a `.count` member.

@rintaro
Copy link
Mannequin

rintaro mannequin commented Apr 1, 2019

This has been fixed in #23065

@rintaro
Copy link
Mannequin

rintaro mannequin commented Apr 1, 2019

let floatRange = Float(0) ..< Float(1)
floatRange.cou // ERROR: Value of type 'Range<Float>' has no member 'cou'; did you mean 'count'?

This has been fixed in #23332

@AnthonyLatsis
Copy link
Collaborator Author

Thanks, Rintaro!

@jepers
Copy link

jepers commented Apr 19, 2019

Is this supposed to be fixed in the default toolchain of Xcode 10.2.1?

I see Rintaro saying above that it has been fixed but the issue as described here (in SR-7046) and in the linked SR-9396 remains as of default toolchain of Xcode 10.2.1. That is, for example the following is still what happens in Xcode 10.2.1:

let floatRange = Float(0) ..< Float(1)
floatRange.cou // ERROR: Value of type 'Range<Float>' has no member 'cou'; did you mean 'count'?

even though Range<Float> has no property called count.

@rintaro
Copy link
Mannequin

rintaro mannequin commented Apr 19, 2019

Not for Xcode 10.2.1 (Swift 5.0.1). Please wait for Swift 5.1.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@AnthonyLatsis AnthonyLatsis added the source tooling Area: IDE support, SourceKit, and other source tooling label Feb 6, 2023
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. code completion Area → source tooling: code completion source tooling Area: IDE support, SourceKit, and other source tooling
Projects
None yet
Development

No branches or pull requests

3 participants