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-12953] Compiler Allows Redeclaration of Methods and Properties of Imported Types #55399

Open
swift-ci opened this issue Jun 8, 2020 · 1 comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself documentation

Comments

@swift-ci
Copy link
Collaborator

swift-ci commented Jun 8, 2020

Previous ID SR-12953
Radar None
Original Reporter Matt Rips (JIRA User)
Type Bug
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, Documentation
Assignee None
Priority Medium

md5: bfa13c56e1e89f9c4dfbd532e1babc02

Issue Description:

The Swift Programming Language states, "Extensions can add new functionality to a type, but they cannot override existing functionality." Normally, if one seeks to redeclare a method or property of a type, whether in the type's main declaration or in an extension, the compiler emits an error: "Invalid redeclaration of '[name of method or property]'".

However, if a type is imported from a module, including types provided by the Standard Library or any other module, the compiler allows the redeclaration of methods and computed properties via an extension of the type. It appears that the redeclared version of the method or property is the one that is called, but it is not clear whether that always is the case.

Allowing such code to compile and run is harmful. It gives the user the impression that it is valid to redeclare or "override" methods and properties via extensions. Code in the wild may depend upon this invalid behavior.

An example:

extension Array {
    /// A redeclared property
    var count: Int { -1 } // no error emitted
}

Obviously, this condition is known. Clearly, the error can be diagnosed at compile time. Why is no error omitted?

@swift-ci
Copy link
Collaborator Author

swift-ci commented Jun 9, 2020

Comment by Matthew Rips (JIRA)

Ok. I that this behavior is intended. BUT, it is not documented.

@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 documentation
Projects
None yet
Development

No branches or pull requests

1 participant