Skip to content

[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

Merged
merged 8 commits into from
Jun 29, 2023

Conversation

hborla
Copy link
Member

@hborla hborla commented Jun 28, 2023

This change adds a generalization of conformance macros as extension macros (swiftlang/swift-evolution#2090)

@hborla hborla force-pushed the extension-macro branch 2 times, most recently from a69c99d to b8b71fb Compare June 28, 2023 02:25
@hborla
Copy link
Member Author

hborla commented Jun 28, 2023

swiftlang/swift-syntax#1859

@swift-ci please smoke test

@hborla hborla force-pushed the extension-macro branch from b8b71fb to d3e2562 Compare June 28, 2023 04:22
@hborla
Copy link
Member Author

hborla commented Jun 28, 2023

swiftlang/swift-syntax#1859

@swift-ci please smoke test

@hborla hborla marked this pull request as ready for review June 28, 2023 07:43
@hborla
Copy link
Member Author

hborla commented Jun 28, 2023

swiftlang/swift-syntax#1859

@swift-ci please smoke test

Copy link
Contributor

@bnbarham bnbarham left a 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) {
Copy link
Contributor

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?

Copy link
Member Author

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)

Copy link
Contributor

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 😅

Copy link
Member Author

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

Choose a reason for hiding this comment

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

Suggested change
// trialed by introduced conformances
// trailed by introduced conformances

struct Local<Element> {}
// expected-error@-1{{local type cannot have attached extension macro}}
}
#endif
Copy link
Contributor

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

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

Copy link
Member Author

Choose a reason for hiding this comment

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

....finally added in #67889 😅

Copy link
Member

@DougGregor DougGregor left a 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(
Copy link
Member

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));
Copy link
Member

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

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.

Copy link
Member Author

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 😂

@hborla
Copy link
Member Author

hborla commented Jun 28, 2023

swiftlang/swift-syntax#1859

@swift-ci please smoke test

@hborla hborla changed the title [Macros] Implement attached extension macros. [Macros] Generalize conformance macros as extension macros Jun 28, 2023
@hborla
Copy link
Member Author

hborla commented Jun 29, 2023

swiftlang/swift-syntax#1859

@swift-ci please smoke test macOS

1 similar comment
@hborla
Copy link
Member Author

hborla commented Jun 29, 2023

swiftlang/swift-syntax#1859

@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 &&
Copy link
Member

Choose a reason for hiding this comment

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

Fantastic, thank you!

@hborla
Copy link
Member Author

hborla commented Jun 29, 2023

swiftlang/swift-syntax#1859

@swift-ci please smoke test

@hborla
Copy link
Member Author

hborla commented Jun 29, 2023

swiftlang/swift-syntax#1859

@swift-ci please smoke test Linux

@hborla hborla merged commit 7a46466 into swiftlang:main Jun 29, 2023
@hborla hborla deleted the extension-macro branch June 29, 2023 15:57
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