Skip to content

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

Merged
merged 1 commit into from
Feb 28, 2020

Conversation

slavapestov
Copy link
Contributor

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.

@slavapestov
Copy link
Contributor Author

@swift-ci Please smoke test

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'}}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xedin @hborla do you know why the diagnostic output is inconsistent here? It's printing baseFunc0() vs baseFunc1

Copy link
Contributor

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...

Copy link
Contributor Author

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())().

Copy link
Contributor

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...

Copy link
Contributor Author

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 :)

Copy link
Contributor

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 :)

if (isSuper && isUnboundInstanceMember) {
context.Diags.diagnose(base->getLoc(),
diag::super_instance_method_on_static_self,
baseTy, DeclNameRef(member->getFullName()))
Copy link
Contributor

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.

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'}}
Copy link
Contributor

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.
@slavapestov
Copy link
Contributor Author

@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, Foo.bar(&self)(). These actually do work today, but they won't with the new curry thunk implementation. Hopefully this won't cause any fallout, if it does, I'll hack something up.

@slavapestov
Copy link
Contributor Author

@swift-ci Please smoke test

@slavapestov
Copy link
Contributor Author

@swift-ci Please test source compatibility

Copy link
Contributor

@xedin xedin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@slavapestov slavapestov merged commit ea9806d into swiftlang:master Feb 28, 2020
@davezarzycki
Copy link
Contributor

davezarzycki commented Feb 28, 2020

This also detects and bans a case we didn't before: unbound references to mutating methods, even with all argument lists applied. Eg, Foo.bar(&self)(). These actually do work today, but they won't with the new curry thunk implementation. Hopefully this won't cause any fallout, if it does, I'll hack something up.

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;
Copy link
Contributor

@davezarzycki davezarzycki Feb 28, 2020

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?

@xedin
Copy link
Contributor

xedin commented Feb 28, 2020

@davezarzycki I think #28698 is PR you are looking for :)

davezarzycki added a commit to davezarzycki/swift that referenced this pull request Feb 28, 2020
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants