[SR-12953] Compiler Allows Redeclaration of Methods and Properties of Imported Types #55399
Labels
bug
A deviation from expected or documented behavior. Also: expected but undesirable behavior.
compiler
The Swift compiler in itself
documentation
Additional Detail from JIRA
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:
Obviously, this condition is known. Clearly, the error can be diagnosed at compile time. Why is no error omitted?
The text was updated successfully, but these errors were encountered: