Skip to content

Fix generic method overrides #5424

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 5 commits into from
Oct 24, 2016

Conversation

slavapestov
Copy link
Contributor

@slavapestov slavapestov commented Oct 24, 2016

This fixes a tricky corner case where method overrides did not work in the following case:

  • the method itself had generic parameters
  • the base class was generic and the derived class was not, or vice versa

Eg,

class Base { func generic<T>(t: T) {} }
class Derived<U> : Base { override func generic<T>(t: T) {} }

Fixes rdar://problem/18560464, https://bugs.swift.org/browse/SR-2427,
https://bugs.swift.org/browse/SR-2721.

@slavapestov
Copy link
Contributor Author

@swift-ci Please smoke test

This generalizes some code in Sema to fix the problem where
generic method overrides don't work if the base class is
more or less generic than the derived class.

The problem here was that we were checking types for equality
when matching overrides, which failed if generic parameters
had different depths. Now, map the generic parameters of the
base class member to the generic signature of the derived
member, so that the equality check can succeed.

Since SIL type lowering needs to perform a similar check,
move this from Sema to a method on TypeBase to complement
the existing getTypeOfMember().

Note that getTypeOfMember() still does a superclass walk, but
ideally this will go away soon.
The isTypeReference parameter is now always true, so change
the function to just take a TypeDecl.
We can use the new adjustSuperclassMemberDeclType() method here
to eliminate some code duplication and fix some corner cases with
generic method overrides.

Fixes <rdar://problem/18560464>, <https://bugs.swift.org/browse/SR-2427>,
<https://bugs.swift.org/browse/SR-2721>.
@slavapestov slavapestov force-pushed the fix-generic-method-overrides branch from 59a0619 to 2876437 Compare October 24, 2016 09:16
@slavapestov
Copy link
Contributor Author

@swift-ci Please test and merge

1 similar comment
@slavapestov
Copy link
Contributor Author

@swift-ci Please test and merge

@swift-ci swift-ci merged commit 6ebb627 into swiftlang:master Oct 24, 2016
/// Get the type of a superclass member as seen from the subclass,
/// substituting generic parameters, dynamic Self return, and the
/// 'self' argument type as appropriate.
Type adjustSuperclassMemberDeclType(const ValueDecl *decl,
Copy link
Contributor

Choose a reason for hiding this comment

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

How is this different from getTypeOfMember?

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