-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[Macros] Generalize conformance
macros as extension
macros
#66967
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
a69c99d
to
b8b71fb
Compare
@swift-ci please smoke test |
@swift-ci please smoke test |
@swift-ci please smoke test |
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's various IDE tests that need to be updated as well:
test/IDE/complete_macros.swift
test/IDE/complete_macros_expanded.swift
test/IDE/complete_macro_attribute.swift
test/Index/index_macros.swift
I believe all but complete_macro_attribute.swift
should work, getAttributeDeclParamCompletions
needs to be updated for complete_macro_attribute.swift
. Happy for this to be a separate PR as well, doesn't need to block this one.
@@ -60,14 +60,15 @@ enum MacroPluginKind: UInt8 { | |||
extension MacroRole { | |||
init(rawMacroRole: UInt8) { |
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.
Am I missing why swift_ASTGen_expandFreestandingMacro
and swift_ASTGen_expandAttachedMacro
can't just be updated to take a UInt32
instead?
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.
They could but it's not necessary; swift::MacroRole
can only represent 32 distinct roles (because we use it for bitmasking where one bit corresponds to one role)
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.
Sure, it would just avoid the mapping is all 😅
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.
We already do the exact same mapping for serialization::MacroRole
. We can probably just have one RawMacroRole
that does this mapping between uint32_t
and uint8_t
values for macro roles.
BCArray<IdentifierIDField> // introduced names, where each is encoded as | ||
// - introduced kind | ||
// - base name | ||
// - # of argument labels + 1 (or 0 if none) | ||
// - argument labels | ||
// trialed by introduced conformances |
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.
// trialed by introduced conformances | |
// trailed by introduced conformances |
struct Local<Element> {} | ||
// expected-error@-1{{local type cannot have attached extension macro}} | ||
} | ||
#endif |
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.
Could also test putting @DelegatedConformance
on an extension fails?
// RUN: %target-codesign %t/main | ||
// RUN: %target-run %t/main | %FileCheck %s | ||
|
||
@attached(extension, conformances: P, names: named(requirement)) |
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.
Could test for multiple conformances as well
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.
....finally added in #67889 😅
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.
This looks fantastic! Just a few comments, all of which can be follow-up PRs.
@@ -286,6 +286,10 @@ void ConformanceLookupTable::updateLookupTable(NominalTypeDecl *nominal, | |||
ASTContext &ctx = nominal->getASTContext(); | |||
(void)evaluateOrDefault( | |||
ctx.evaluator, ExpandConformanceMacros{nominal}, { }); | |||
|
|||
// Expand extension macros. | |||
(void)evaluateOrDefault( |
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.
This expansion is very "eager", in that we're expanding all extension macros even if they don't provide any conformances. I think it'll be okay for now.
if (resolved->is<ErrorType>()) { | ||
attr->setInvalid(); | ||
} else { | ||
typeExpr->setType(MetatypeType::get(resolved)); |
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.
We'll need to check that this refers to a protocol type (or, I suppose, a protocol composition type).
}; | ||
using MacroRoleField = BCFixed<3>; | ||
using MacroRoleField = BCFixed<4>; |
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.
I imagine a horrible hour of debugging behind this.
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.
Yes, that is EXACTLY what happened 😂
module interfaces.
@swift-ci please smoke test |
extension
macros.conformance
macros as extension
macros
@swift-ci please smoke test macOS |
1 similar comment
@swift-ci please smoke test macOS |
@@ -1371,6 +1371,13 @@ bool DeclAttribute::printImpl(ASTPrinter &Printer, const PrintOptions &Options, | |||
|
|||
case DAK_MacroRole: { | |||
auto Attr = cast<MacroRoleAttr>(this); | |||
|
|||
// Suppress @attached(extension) if needed. | |||
if (!Options.PrintExtensionMacroAttributes && |
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.
Fantastic, thank you!
@swift-ci please smoke test |
@swift-ci please smoke test Linux |
This change adds a generalization of
conformance
macros asextension
macros (swiftlang/swift-evolution#2090)