-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Sema: Diagnose unbound method references on 'super.' #30097
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
Conversation
@swift-ci Please smoke test |
test/NameBinding/name_lookup.swift
Outdated
super.baseFunc1(42) // expected-error {{instance member 'baseFunc1' cannot be used on type 'ThisBase1'}} | ||
super.baseFunc1(ThisBase1())(42) | ||
super.baseFunc1(ThisBase1())(42) // expected-error {{'super' instance member 'baseFunc1' cannot be used on type 'ThisBase1'}} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, I don't know why it mentions baseFunc0
here when this expression has nothing to do with it...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I meant compared to the line above, super.baseFunc0(ThisBase1())()
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, AFAIK that's related to how printer handles names - prints ()
if there are no arguments but avoids printing (_:)
unless you pass it a special option set...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright, if this is expected behavior that's fine. I just wanted to make sure I hadn't done something wrong because names got a bit more complicated since the last time I looked at diagnostics :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I think that's expected :)
lib/Sema/CSApply.cpp
Outdated
if (isSuper && isUnboundInstanceMember) { | ||
context.Diags.diagnose(base->getLoc(), | ||
diag::super_instance_method_on_static_self, | ||
baseTy, DeclNameRef(member->getFullName())) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please make it a fix so we don't even try to apply an invalid (for now) solution? I think it could be added to resolveOverload
method.
test/NameBinding/name_lookup.swift
Outdated
super.baseFunc1(42) // expected-error {{instance member 'baseFunc1' cannot be used on type 'ThisBase1'}} | ||
super.baseFunc1(ThisBase1())(42) | ||
super.baseFunc1(ThisBase1())(42) // expected-error {{'super' instance member 'baseFunc1' cannot be used on type 'ThisBase1'}} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, I don't know why it mentions baseFunc0
here when this expression has nothing to do with it...
This is something I noticed by inspection while working on <https://bugs.swift.org/browse/SR-75>. Inside a static method, 'self' is a metatype value, so 'self.instanceMethod' produces an unbound reference of type (Self) -> (Args...) -> Results. You might guess that 'super.instanceMethod' can similarly be used to produce an unbound method reference that calls the superclass method given any 'self' value, but unfortunately it doesn't work. Instead, 'super.instanceMethod' would produce the same result as 'self.instanceMethod'. Maybe we can implement this later, but for now, let's just diagnose the problem. Note that partially-applied method references with 'super.' -- namely, 'self.staticMethod' inside a static context, or 'self.instanceMethod' inside an instance context, continue to work as before. They have the type (Args...) -> Result; since the self value has already been applied we don't hit the representational issue.
2bf9bc6
to
019452f
Compare
@xedin Here's a new approach where we detect the error in the solver instead of CSApply. I had to refactor the existing code somewhat. This also detects and bans a case we didn't before: unbound references to mutating methods, even with all argument lists applied. Eg, |
@swift-ci Please smoke test |
@swift-ci Please test source compatibility |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!
I wasn't relying on this but I was testing it. (I also don't I mind the ban). That being said, where can I learn more about the new curry thunk implementation? :-) |
|
||
// If base is a metatype it would be ignored (unless this is an initializer | ||
// call), but if it is some other type it means that we have a single | ||
// application level already. | ||
unsigned level = baseTy->is<MetatypeType>() ? 0 : 1; | ||
if (auto *call = dyn_cast_or_null<CallExpr>(cs.getParentExpr(anchor))) { | ||
level += dyn_cast_or_null<CallExpr>(cs.getParentExpr(call)) ? 2 : 1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @slavapestov – I'm trying to understand why this line was removed. Why do we no longer need to check for the grandparent CallExpr
?
@davezarzycki I think #28698 is PR you are looking for :) |
After swiftlang#30097, some valid full applications are diagnosed as partial application failures. This restores the old behavior while preserving the `super` related fixes in swiftlang#30097.
This is something I noticed by inspection while working on
https://bugs.swift.org/browse/SR-75.
Inside a static method, 'self' is a metatype value, so
'self.instanceMethod' produces an unbound reference of type
(Self) -> (Args...) -> Results.
You might guess that 'super.instanceMethod' can similarly
be used to produce an unbound method reference that calls
the superclass method given any 'self' value, but unfortunately
it doesn't work.
Instead, 'super.instanceMethod' would produce the same
result as 'self.instanceMethod'. Maybe we can implement this
later, but for now, let's just diagnose the problem.
Note that partially-applied method references with 'super.'
-- namely, 'self.staticMethod' inside a static context, or
'self.instanceMethod' inside an instance context, continue
to work as before.
They have the type (Args...) -> Result; since the self value
has already been applied we don't hit the representational
issue.