-
Notifications
You must be signed in to change notification settings - Fork 10.4k
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-7573] Overridden class function with generic constraint causes unsafe behavior #50115
Labels
accepts invalid
Bug: Accepts invalid
bug
A deviation from expected or documented behavior. Also: expected but undesirable behavior.
compiler
The Swift compiler itself
Comments
Yikes! Thanks, Ben. @swift-ci create |
cc @huonw |
With compiler/runtime assertions on this gives:
Definitely looks like something is getting confused. |
Also, slightly reduced:
|
This issue was closed.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
accepts invalid
Bug: Accepts invalid
bug
A deviation from expected or documented behavior. Also: expected but undesirable behavior.
compiler
The Swift compiler itself
Environment
Xcode 9.3, tested in a playground.
Additional Detail from JIRA
md5: c5c2fa906d0f758365411aed2ecf1e4d
duplicates:
Issue Description:
Class
C
defines a functionf
. ClassD
overrides it, but has a different generic constraint. Callingf
from an instance ofC
usingtype(of:)
type-checks as thoughC.f
will be called, but at runtimeD.f
is called.Because
D.f
tries to access a member ofE
, the program reads random memory, leading to undefined behavior.As an aside, the ability to tighten type constraints on overrides is actually very useful in the context of the code I was working on when I discovered this. I'd like to be able to write things like
var functionToRunWhenSomethingChanges: (Self, Context) -> ()
, which creates similar issues to this when used in the context of classes. So my solution was to makevar functionToRunWhenSomethingChanges: (Any, Context) -> ()
, and have asetFunctionToRunWhenSomethingChanges<T>(on subject: T, _ function: (T, Context) -> ())
, which ensures that things are still type safe. So It's useful to be able to add generic constraints that require that a particular class func only works for subclasses of the type that it's implemented for. Hopefully when/if this is fixed it's still possible to do that.The text was updated successfully, but these errors were encountered: