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-1772] File-level function with the same name as instance function not picked up by compiler #44381

Open
swift-ci opened this issue Jun 15, 2016 · 4 comments
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-1772
Radar rdar://problem/17368791
Original Reporter charlieMonroe (JIRA User)
Type Bug

Attachment: Download

Environment

OS X 10.11.5, Xcode 8b1

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

md5: 8b397a0255bb33a4d7a23649ea2b6c11

is duplicated by:

  • SR-3051 Extra argument in call--min(by:) seems to have shadowed the global min(::)
  • SR-4660 Methods on self should not block unambiguous calls to free functions
  • SR-7986 Error when using similarly named local function when initializing a static property
  • SR-8602 Self implicitness too strong

relates to:

  • SR-6214 compiler cannot disambiguate variable named type from the function type(of:)
  • SR-9105 You can't name a nested function the same as a closure parameter.

Issue Description:

I'm attaching a playground file that demonstrates the issue:

Have a function at the file-level:

private func _validateAccountName(name: String, existingAccounts: [Account]) -> Bool {
    // Logic goes here...
}

which includes a name-checking logic which you want to be shared among several components in the file.

Then create class that has the following method:

class MyController {
    private func _validateAccountName(name: String) -> Bool {
        let accounts = ...
        return _validateAccountName(name, existingAccounts: accounts)
    }
}

The compiler complains that there's an extra argument existingAccounts within the call. There is hence no way to address the function outside of the controller class without renaming it. The compiler should consider it as an overload candidate as well. Since self. is not explicitely mentioned, the compiler should use the file-level function.

@belkadan
Copy link
Contributor

Normally you use the name of the module to disambiguate: MyApp._validateAccountName. Playgrounds don't have a standard module name, though, so that's still a problem.

@swift-ci
Copy link
Collaborator Author

Comment by Charlie Monroe (JIRA)

IMHO there should be a chain of precedence for shadowing where globally (file-level) declared symbols take precedence over references to instance members without explicit self as one would expect. In this example:

private func _validateAccountName(name: String) -> Bool {
    // Logic goes here...
}

class MyController {
    private func _validateAccountName(name: String) -> Bool {
        _validateAccountName("123") // Calls file-level function, even if it has the same signature as the instance method on MyController
        self._validateAccountName(name) // Calls instance method
    }
}

Nevertheless, in my original example, the file-level function has different signature (contains two arguments), unlike the instance method on MyController which only contains one argument, so it shouldn't be ambiguous at all.

@belkadan
Copy link
Contributor

There is a chain of precedence, but it's always inner scope to outer scope, and only checking base names. That's a deliberate design (whether or not it's a good one), so any changes would need to go through the Swift Evolution Process.

@swift-ci
Copy link
Collaborator Author

Comment by Charlie Monroe (JIRA)

Ok, thanks for the comment, I've started a discussion thread in swift-evolution mail list.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
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

2 participants