Skip to content
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

Fix @dynamicMemberLookup for protocol/archetype types. #20516

Merged

Conversation

dan-zheng
Copy link
Contributor

@dan-zheng dan-zheng commented Nov 12, 2018

Enable subscript(dynamicMember:) as a requirement for @dynamicMemberLookup protocols.

Previously, @dynamicMemberLookup protocols could only define subscript(dynamicMember:) in an extension. This patch fixes that.

@dynamicMemberLookup
protocol DynamicProtocol {
  // This works now.
  subscript(dynamicMember name: String) -> Self { get }
}
// Previously, only this worked (method in protocol extension).
extension DynamicProtocol {
  subscript(dynamicMember name: String) -> Self { return self }
}

Resolves SR-8077.

There's also some @dynamicCallable-related gardening.

@dan-zheng
Copy link
Contributor Author

dan-zheng commented Nov 12, 2018

@swift-ci Please smoke test

EDIT: Linux tests failed with a weird unrelated error: error: compilation involves pch was enabled in PCH file but is currently disabled.

@slavapestov
Copy link
Contributor

@dan-zheng Take a look at extractDirectlyReferencedNominalTypes() in lib/AST/NameLookup.cpp. Do you think this can be useful for @dynamicCallable and @dynamicMemberLookup?

More generally it would be nice to factor out a common superclass/conforming protocol walk for use by name lookup and these attributes. If you think about it what they're doing is very similar to name lookup except they're searching for an attribute and not a member.

@dan-zheng
Copy link
Contributor Author

@slavapestov Thanks for the suggestion!

extractDirectlyReferencedNominalTypes does seem useful. It populates a SmallVectorImpl with decls, but the order of the decls might not be ideal here - need to investigate.

A common superclass/protocol type walk function definitely makes sense. I'm happy to look into it, perhaps we can merge this PR to unblock progress in the meantime.

@dan-zheng dan-zheng force-pushed the dynamic-member-lookup-protocol-fix branch from 9f8da92 to 35a8a16 Compare November 12, 2018 21:57
@dan-zheng
Copy link
Contributor Author

dan-zheng commented Nov 12, 2018

I got some weird test failures regarding pch. Retriggering tests in hopes that things pass.
@swift-ci Please smoke test

@dan-zheng
Copy link
Contributor Author

@shahmishal: you're probably aware of this already, but do you know why CI is failing with error: compilation involves pch was enabled in PCH file but is currently disabled?

Example build job.
CI for other PRs seems to fail with the same error currently.

@shahmishal
Copy link
Member

@milseman Is this related to cherry-pick on clang/llvm branch?

@slavapestov
Copy link
Contributor

@dan-zheng @shahmishal Jordan pushed a fix for the PCH issue, tests should pass now.

@slavapestov
Copy link
Contributor

@dan-zheng Why would the order matter?

@dan-zheng
Copy link
Contributor Author

dan-zheng commented Nov 13, 2018

@slavapestov It shouldn't, I was being overly cautious. 😃

EDIT: extractDirectlyReferencedNominalTypes is currently a fileprivate static function, would you recommend declaring it in NameLookup.h under the swift namespace?

I'll probably augment extractDirectlyReferencedNominalTypes and dedupe code for @dynamicCallable and @dynamicMemberLookup in a follow-up PR.

@dan-zheng
Copy link
Contributor Author

@swift-ci Please smoke test

@slavapestov
Copy link
Contributor

@dan-zheng Yeah, I would first make the function public and use it in the implementation of those attributes, and make sure there are no regressions, etc. Then I would think about extracting the nominal type visitation logic as a separate task.

@lattner
Copy link
Contributor

lattner commented Nov 13, 2018

I'm +1 on this patch (including the testcase), but am sufficiently far enough away from the code to not be the best reviewer. Is anyone else able to give this a sniff test?

@dan-zheng
Copy link
Contributor Author

cc @slavapestov @rudkx: could you please review?

As suggested by Slava, in a follow-up PR, I plan to make extractDirectlyReferencedNominalTypes public and use it to dedupe type walking code in hasDynamicMemberLookupAttribute and getDynamicCallableMethods.

- Enable `subscript(dynamicMember:)` as a requirement for
  `@dynamicMemberLookup` protocols.
- Add tests.
- Minor `@dynamicCallable`-related gardening.

Resolves SR-8077.
@dan-zheng dan-zheng force-pushed the dynamic-member-lookup-protocol-fix branch from 35a8a16 to 70becc3 Compare November 13, 2018 13:03
@dan-zheng
Copy link
Contributor Author

Added a // SR-8077 test case comment in the test file.
@swift-ci Please smoke test

@dan-zheng dan-zheng merged commit eebccb7 into swiftlang:master Nov 13, 2018
@dan-zheng dan-zheng deleted the dynamic-member-lookup-protocol-fix branch November 13, 2018 23:13
@@ -3552,8 +3569,8 @@ performMemberLookup(ConstraintKind constraintKind, DeclName memberName,
constraintKind == ConstraintKind::ValueMember &&
memberName.isSimpleName() && !memberName.isSpecial()) {
auto name = memberName.getBaseIdentifier();
if (hasDynamicMemberLookupAttribute(instanceTy->getCanonicalType(),
DynamicMemberLookupCache)) {
// TC.extract
Copy link
Contributor Author

@dan-zheng dan-zheng Nov 14, 2018

Choose a reason for hiding this comment

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

Oops, accidentally committed this rogue comment.
I'll remove in my next PR, if it's not gone by then.
Removed in #20567.

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.

4 participants