@@ -1548,12 +1548,56 @@ swift::expandAttributes(CustomAttr *attr, MacroDecl *macro, Decl *member) {
15481548 return macroSourceFile->getBufferID ();
15491549}
15501550
1551+ // Collect the protocol conformances that the macro asked about but were
1552+ // not already present on the declaration.
1553+ static TinyPtrVector<ProtocolDecl *> getIntroducedConformances (
1554+ NominalTypeDecl *nominal, MacroRole role, MacroDecl *macro,
1555+ SmallVectorImpl<ProtocolDecl *> *potentialConformances = nullptr ) {
1556+ SmallVector<ProtocolDecl *, 2 > potentialConformancesBuffer;
1557+ if (!potentialConformances)
1558+ potentialConformances = &potentialConformancesBuffer;
1559+ macro->getIntroducedConformances (nominal, role, *potentialConformances);
1560+
1561+ TinyPtrVector<ProtocolDecl *> introducedConformances;
1562+ for (auto protocol : *potentialConformances) {
1563+ SmallVector<ProtocolConformance *, 2 > existingConformances;
1564+ nominal->lookupConformance (protocol, existingConformances);
1565+
1566+ bool hasExistingConformance = llvm::any_of (
1567+ existingConformances,
1568+ [&](ProtocolConformance *conformance) {
1569+ return conformance->getSourceKind () !=
1570+ ConformanceEntryKind::PreMacroExpansion;
1571+ });
1572+
1573+ if (!hasExistingConformance) {
1574+ introducedConformances.push_back (protocol);
1575+ }
1576+ }
1577+
1578+ return introducedConformances;
1579+ }
1580+
15511581llvm::Optional<unsigned > swift::expandMembers (CustomAttr *attr,
15521582 MacroDecl *macro, Decl *decl) {
1583+ auto nominal = dyn_cast<NominalTypeDecl>(decl);
1584+ if (!nominal) {
1585+ auto ext = dyn_cast<ExtensionDecl>(decl);
1586+ if (!ext)
1587+ return llvm::None;
1588+
1589+ nominal = ext->getExtendedNominal ();
1590+ if (!nominal)
1591+ return llvm::None;
1592+ }
1593+ auto introducedConformances = getIntroducedConformances (
1594+ nominal, MacroRole::Member, macro);
1595+
15531596 // Evaluate the macro.
15541597 auto macroSourceFile =
15551598 ::evaluateAttachedMacro (macro, decl, attr,
1556- /* passParentContext=*/ false , MacroRole::Member);
1599+ /* passParentContext=*/ false , MacroRole::Member,
1600+ introducedConformances);
15571601 if (!macroSourceFile)
15581602 return llvm::None;
15591603
@@ -1625,22 +1669,9 @@ swift::expandExtensions(CustomAttr *attr, MacroDecl *macro,
16251669 return llvm::None;
16261670 }
16271671
1628- // Collect the protocol conformances that the macro can add. The
1629- // macro should not add conformances that are already stated in
1630- // the original source.
1631-
16321672 SmallVector<ProtocolDecl *, 2 > potentialConformances;
1633- macro->getIntroducedConformances (nominal, potentialConformances);
1634-
1635- SmallVector<ProtocolDecl *, 2 > introducedConformances;
1636- for (auto protocol : potentialConformances) {
1637- SmallVector<ProtocolConformance *, 2 > existingConformances;
1638- nominal->lookupConformance (protocol, existingConformances);
1639- if (existingConformances.empty ()) {
1640- introducedConformances.push_back (protocol);
1641- }
1642- }
1643-
1673+ auto introducedConformances = getIntroducedConformances (
1674+ nominal, MacroRole::Extension, macro, &potentialConformances);
16441675 auto macroSourceFile = ::evaluateAttachedMacro (macro, nominal, attr,
16451676 /* passParentContext=*/ false ,
16461677 role, introducedConformances);
@@ -1830,9 +1861,9 @@ ConcreteDeclRef ResolveMacroRequest::evaluate(Evaluator &evaluator,
18301861}
18311862
18321863ArrayRef<Type>
1833- ResolveExtensionMacroConformances ::evaluate (Evaluator &evaluator,
1834- const MacroRoleAttr *attr,
1835- const Decl *decl) const {
1864+ ResolveMacroConformances ::evaluate (Evaluator &evaluator,
1865+ const MacroRoleAttr *attr,
1866+ const Decl *decl) const {
18361867 auto *dc = decl->getDeclContext ();
18371868 auto &ctx = dc->getASTContext ();
18381869
0 commit comments