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-13404] Swift 5 Compiler Fails to Distinguish Variable from Function #55845

Closed
swift-ci opened this issue Aug 15, 2020 · 2 comments
Closed
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself type checker Area → compiler: Semantic analysis

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-13404
Radar None
Original Reporter bdkjones (JIRA User)
Type Bug
Status Resolved
Resolution Duplicate

Attachment: Download

Environment

Xcode 12.0 beta 4 (12A8179i)
macOS Catalina 10.15.6

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

md5: a3858008ce047d7795f759316684c5f2

duplicates:

  • SR-1846 "Variable used in its own initial value" error when using a function (not a variable) of the same name

Issue Description:

Consider this code snippet:

import Foundation
import Cocoa

class OutlineView: NSOutlineView
{
    override func validateProposedFirstResponder(_ responder: NSResponder, for event: NSEvent?) -> Bool
    {
        let location: CGPoint = convert(event!.locationInWindow, from: nil)
        
        // Compiler produces error: "Variable used within its own initial value"
        // Shouldn't the compiler know the difference between "row" as a variable and "row(at:)" as a method of NSOutlineView?
        let row: Int = row(at: location)
    }
}

Using a playground in Xcode 12.0 beta 4 (12A8179i), the above code snippet produces the error: "Variable used within its own initial value". But that's not right. On the left side, "row" is indeed a variable. But on the right side, "row(at🙂" is a method of NSOutlineView.

This line of code should be perfectly legal, should it not? It seems that the compiler is misidentifying the second instance of "row" in the line.

@swift-ci
Copy link
Collaborator Author

Comment by Jessy Catterwaul (JIRA)

Swift has always required

let row = self.row(at: location)

for this. I can't say if it can be overcome, but it's a simple fix as long as you can grab from an outer scope.

@swift-ci
Copy link
Collaborator Author

Comment by Bryan Jones (JIRA)

Sure, the workaround is easy. And this behavior may be a byproduct of some limitation, but it’s still an invalid error, no?

The compiler is identifying the second “row” as something it is not: a variable. The error it produces is objectively incorrect; the second “row” is not a use of the first “row” we declared on the left side of the equals sign.

Given this, it seems like the compiler could be improved cases like this (rare though they are).

@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 type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

1 participant