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-11688] Swift should consider functions with default args ambiguous when there is another function with an identical signature #54097

Open
swift-ci opened this issue Oct 31, 2019 · 1 comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself feature A feature request or implementation

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-11688
Radar None
Original Reporter benpious (JIRA User)
Type Bug
Environment

Swift 5.1.

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

md5: 5d92be961dfad3959beb2f10b93a6c76

relates to:

  • SR-11095 Warn on pointless default argument

Issue Description:

The Swift compiler accepts the following code:

struct S {
    let i: Int
    init(_ i: Int = 5) {
        self.i = i
    }
    
    init() {
        i = 6
    }
}
print(S().i) // prints 6

This is dangerous, because if S.init() were deleted, the program would still compile; however, S().i would now evaluate to 5.

Conversely, if init() didn't already exist, and was added later, especially in another module as a convenience init, there might be difficult to debug issues in modules it was exposed to, as which init was called might be inconsistent depending on the location of the new init() function and which modules depended on it (I haven't checked this, but it seems likely).

For these reasons, I believe that this program should be rejected by the compiler.

@belkadan
Copy link
Contributor

belkadan commented Nov 1, 2019

This would be source-breaking, so it would have to go through the Swift Evolution Process. SR-11095 talks about a warning on the default argument only, but that wouldn't handle the extension-in-another-module case brought up here.

@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 feature A feature request or implementation
Projects
None yet
Development

No branches or pull requests

2 participants