From a23d39bdfb619f5c308893970de28230ef6a97f1 Mon Sep 17 00:00:00 2001 From: Doug Gregor Date: Sat, 8 Apr 2023 13:03:35 -0700 Subject: [PATCH 1/4] [Macros] Mangle attached macro expansions based only on syntactic information The mangling of attached macro expansions based on the declaration to which they are attached requires semantic information (specifically, the interface type of that declaration) that caused cyclic dependencies during type checking. Replace the mangling with a less-complete mangling that only requires syntactic information from the declaration, i.e., the name of the declaration to which the macro was attached. This eliminates reference cycles that occur with attached macros that produce arbitrary names. --- docs/ABI/Mangling.rst | 12 +++---- include/swift/AST/ASTMangler.h | 3 +- lib/AST/ASTMangler.cpp | 34 ++++++++++++++------ lib/Demangling/Demangler.cpp | 35 ++++++++++++++++++--- lib/Demangling/NodePrinter.cpp | 30 ++++++++++++------ lib/Demangling/Remangler.cpp | 25 ++++++--------- test/Demangle/Inputs/manglings.txt | 4 +-- test/Macros/accessor_macros.swift | 4 +-- test/Macros/macro_expand.swift | 2 +- test/Macros/macro_expand_conformances.swift | 4 +-- test/Macros/macro_expand_peers.swift | 32 ++++++++++++++----- test/SourceKit/Macros/macro_basic.swift | 20 ++++++------ 12 files changed, 135 insertions(+), 70 deletions(-) diff --git a/docs/ABI/Mangling.rst b/docs/ABI/Mangling.rst index f31ef21d54ab8..8d86f9c0565f7 100644 --- a/docs/ABI/Mangling.rst +++ b/docs/ABI/Mangling.rst @@ -396,13 +396,13 @@ Entities macro-discriminator-list ::= macro-discriminator-list? file-discriminator? macro-expansion-operator INDEX - macro-expansion-operator ::= identifier 'fMa' // attached accessor macro - macro-expansion-operator ::= identifier 'fMA' // attached member-attribute macro + macro-expansion-operator ::= decl-name identifier 'fMa' // attached accessor macro + macro-expansion-operator ::= decl-name identifier 'fMA' // attached member-attribute macro macro-expansion-operator ::= identifier 'fMf' // freestanding macro - macro-expansion-operator ::= identifier 'fMm' // attached member macro - macro-expansion-operator ::= identifier 'fMp' // attached peer macro - macro-expansion-operator ::= identifier 'fMc' // attached conformance macro - macro-expansion-operator ::= identifier 'fMu' // uniquely-named entity + macro-expansion-operator ::= decl-name identifier 'fMm' // attached member macro + macro-expansion-operator ::= decl-name identifier 'fMp' // attached peer macro + macro-expansion-operator ::= decl-name identifier 'fMc' // attached conformance macro + macro-expansion-operator ::= decl-name identifier 'fMu' // uniquely-named entity file-discriminator ::= identifier 'Ll' // anonymous file-discriminated declaration diff --git a/include/swift/AST/ASTMangler.h b/include/swift/AST/ASTMangler.h index de07d203299d3..141aa27e4e0eb 100644 --- a/include/swift/AST/ASTMangler.h +++ b/include/swift/AST/ASTMangler.h @@ -401,7 +401,8 @@ class ASTMangler : public Mangler { void appendType(Type type, GenericSignature sig, const ValueDecl *forDecl = nullptr); - void appendDeclName(const ValueDecl *decl); + void appendDeclName( + const ValueDecl *decl, DeclBaseName name = DeclBaseName()); GenericTypeParamType *appendAssocType(DependentMemberType *DepTy, GenericSignature sig, diff --git a/lib/AST/ASTMangler.cpp b/lib/AST/ASTMangler.cpp index 793595a9789d7..fd22e49d7bffa 100644 --- a/lib/AST/ASTMangler.cpp +++ b/lib/AST/ASTMangler.cpp @@ -1006,8 +1006,9 @@ static Optional getOverriddenSwiftProtocolObjCName( return None; } -void ASTMangler::appendDeclName(const ValueDecl *decl) { - DeclBaseName name = decl->getBaseName(); +void ASTMangler::appendDeclName(const ValueDecl *decl, DeclBaseName name) { + if (name.empty()) + name = decl->getBaseName(); assert(!name.isSpecial() && "Cannot print special names"); auto *synthesizedTypeAttr = @@ -4022,21 +4023,34 @@ std::string ASTMangler::mangleAttachedMacroExpansion( const Decl *decl, CustomAttr *attr, MacroRole role) { beginMangling(); + // Append the context and name of the declaration. + // We don't mangle the declaration itself because doing so requires semantic + // information (e.g., its interface type), which introduces cyclic + // dependencies. const Decl *attachedTo = decl; + DeclBaseName attachedToName; if (auto valueDecl = dyn_cast(decl)) { - if (role != MacroRole::MemberAttribute) { - appendAnyDecl(valueDecl); - } else { - // Appending the member would result in a cycle since `VarDecl` appends - // its type, which would then loop back around to getting the attributes - // again. We'll instead add a discriminator for each member. - appendContextOf(valueDecl); + appendContextOf(valueDecl); + + // Mangle the name, replacing special names with their user-facing names. + attachedToName = valueDecl->getName().getBaseName(); + if (attachedToName.isSpecial()) { + attachedToName = + decl->getASTContext().getIdentifier(attachedToName.userFacingName()); + } + appendDeclName(valueDecl, attachedToName); + + // For member attribute macros, the attribute is attached to the enclosing + // declaration. + if (role == MacroRole::MemberAttribute) { attachedTo = decl->getDeclContext()->getAsDecl(); } } else { appendContext(decl->getDeclContext(), ""); + appendIdentifier("_"); } + // Determine the name of the macro. DeclBaseName macroName; if (auto *macroDecl = attachedTo->getResolvedMacro(attr)) { macroName = macroDecl->getName().getBaseName(); @@ -4044,6 +4058,8 @@ std::string ASTMangler::mangleAttachedMacroExpansion( macroName = decl->getASTContext().getIdentifier("__unknown_macro__"); } + // FIXME: attached macro discriminators should take attachedToName into + // account. appendMacroExpansionOperator( macroName.userFacingName(), role, decl->getAttachedMacroDiscriminator(macroName, role, attr)); diff --git a/lib/Demangling/Demangler.cpp b/lib/Demangling/Demangler.cpp index c8dfa0d7f2ebc..69dd9c86ade2c 100644 --- a/lib/Demangling/Demangler.cpp +++ b/lib/Demangling/Demangler.cpp @@ -4061,47 +4061,74 @@ static bool isMacroExpansionNodeKind(Node::Kind kind) { NodePointer Demangler::demangleMacroExpansion() { Node::Kind kind; + bool isAttached; + bool isFreestanding; switch (nextChar()) { case 'a': kind = Node::Kind::AccessorAttachedMacroExpansion; + isAttached = true; + isFreestanding = false; break; case 'A': kind = Node::Kind::MemberAttributeAttachedMacroExpansion; + isAttached = true; + isFreestanding = false; break; case 'f': kind = Node::Kind::FreestandingMacroExpansion; + isAttached = false; + isFreestanding = true; break; case 'm': kind = Node::Kind::MemberAttachedMacroExpansion; + isAttached = true; + isFreestanding = false; break; case 'p': kind = Node::Kind::PeerAttachedMacroExpansion; + isAttached = true; + isFreestanding = false; break; case 'c': kind = Node::Kind::ConformanceAttachedMacroExpansion; + isAttached = true; + isFreestanding = false; break; case 'u': kind = Node::Kind::MacroExpansionUniqueName; + isAttached = false; + isFreestanding = false; break; default: return nullptr; } - NodePointer name = popNode(Node::Kind::Identifier); - NodePointer privateDiscriminator = popNode(Node::Kind::PrivateDeclName); + NodePointer macroName = popNode(Node::Kind::Identifier); + NodePointer privateDiscriminator = nullptr; + if (isFreestanding) + privateDiscriminator = popNode(Node::Kind::PrivateDeclName); + NodePointer attachedName = nullptr; + if (isAttached) + attachedName = popNode(isDeclName); + NodePointer context = popNode(isMacroExpansionNodeKind); if (!context) context = popContext(); NodePointer discriminator = demangleIndexAsNode(); - auto result = createWithChildren( - kind, context, name, discriminator); + NodePointer result; + if (isAttached) { + result = createWithChildren( + kind, context, attachedName, macroName, discriminator); + } else { + result = createWithChildren(kind, context, macroName, discriminator); + } if (privateDiscriminator) result->addChild(privateDiscriminator, *this); return result; diff --git a/lib/Demangling/NodePrinter.cpp b/lib/Demangling/NodePrinter.cpp index 9ca114bcff9ce..959b15cb9cf64 100644 --- a/lib/Demangling/NodePrinter.cpp +++ b/lib/Demangling/NodePrinter.cpp @@ -1434,28 +1434,38 @@ NodePointer NodePrinter::print(NodePointer Node, unsigned depth, /*hasName*/ true); case Node::Kind::AccessorAttachedMacroExpansion: return printEntity(Node, depth, asPrefixContext, TypePrinting::NoType, - /*hasName*/true, "accessor macro expansion #", - (int)Node->getChild(2)->getIndex() + 1); + /*hasName*/true, + ("accessor macro @" + + nodeToString(Node->getChild(2)) + " expansion #"), + (int)Node->getChild(3)->getIndex() + 1); case Node::Kind::FreestandingMacroExpansion: return printEntity(Node, depth, asPrefixContext, TypePrinting::NoType, /*hasName*/true, "freestanding macro expansion #", (int)Node->getChild(2)->getIndex() + 1); case Node::Kind::MemberAttributeAttachedMacroExpansion: return printEntity(Node, depth, asPrefixContext, TypePrinting::NoType, - /*hasName*/true, "member attribute macro expansion #", - (int)Node->getChild(2)->getIndex() + 1); + /*hasName*/true, + ("member attribute macro @" + + nodeToString(Node->getChild(2)) + " expansion #"), + (int)Node->getChild(3)->getIndex() + 1); case Node::Kind::MemberAttachedMacroExpansion: return printEntity(Node, depth, asPrefixContext, TypePrinting::NoType, - /*hasName*/true, "member macro expansion #", - (int)Node->getChild(2)->getIndex() + 1); + /*hasName*/true, + ("member macro @" + nodeToString(Node->getChild(2)) + + " expansion #"), + (int)Node->getChild(3)->getIndex() + 1); case Node::Kind::PeerAttachedMacroExpansion: return printEntity(Node, depth, asPrefixContext, TypePrinting::NoType, - /*hasName*/true, "peer macro expansion #", - (int)Node->getChild(2)->getIndex() + 1); + /*hasName*/true, + ("peer macro @" + nodeToString(Node->getChild(2)) + + " expansion #"), + (int)Node->getChild(3)->getIndex() + 1); case Node::Kind::ConformanceAttachedMacroExpansion: return printEntity(Node, depth, asPrefixContext, TypePrinting::NoType, - /*hasName*/true, "conformance macro expansion #", - (int)Node->getChild(2)->getIndex() + 1); + /*hasName*/true, + ("conformance macro @" + nodeToString(Node->getChild(2)) + + " expansion #"), + (int)Node->getChild(3)->getIndex() + 1); case Node::Kind::MacroExpansionUniqueName: return printEntity(Node, depth, asPrefixContext, TypePrinting::NoType, /*hasName*/true, "unique name #", diff --git a/lib/Demangling/Remangler.cpp b/lib/Demangling/Remangler.cpp index bec02c27fb30e..bf2e80636571a 100644 --- a/lib/Demangling/Remangler.cpp +++ b/lib/Demangling/Remangler.cpp @@ -2908,51 +2908,46 @@ ManglingError Remangler::mangleFreestandingMacroExpansion( ManglingError Remangler::mangleAccessorAttachedMacroExpansion( Node *node, unsigned depth) { RETURN_IF_ERROR(mangleChildNode(node, 0, depth + 1)); - if (auto privateDiscriminator = node->getChild(3)) - RETURN_IF_ERROR(mangle(privateDiscriminator, depth + 1)); RETURN_IF_ERROR(mangleChildNode(node, 1, depth + 1)); + RETURN_IF_ERROR(mangleChildNode(node, 2, depth + 1)); Buffer << "fMa"; - return mangleChildNode(node, 2, depth + 1); + return mangleChildNode(node, 3, depth + 1); } ManglingError Remangler::mangleMemberAttributeAttachedMacroExpansion( Node *node, unsigned depth) { RETURN_IF_ERROR(mangleChildNode(node, 0, depth + 1)); - if (auto privateDiscriminator = node->getChild(3)) - RETURN_IF_ERROR(mangle(privateDiscriminator, depth + 1)); RETURN_IF_ERROR(mangleChildNode(node, 1, depth + 1)); + RETURN_IF_ERROR(mangleChildNode(node, 2, depth + 1)); Buffer << "fMA"; - return mangleChildNode(node, 2, depth + 1); + return mangleChildNode(node, 3, depth + 1); } ManglingError Remangler::mangleMemberAttachedMacroExpansion( Node *node, unsigned depth) { RETURN_IF_ERROR(mangleChildNode(node, 0, depth + 1)); - if (auto privateDiscriminator = node->getChild(3)) - RETURN_IF_ERROR(mangle(privateDiscriminator, depth + 1)); RETURN_IF_ERROR(mangleChildNode(node, 1, depth + 1)); + RETURN_IF_ERROR(mangleChildNode(node, 2, depth + 1)); Buffer << "fMm"; - return mangleChildNode(node, 2, depth + 1); + return mangleChildNode(node, 3, depth + 1); } ManglingError Remangler::manglePeerAttachedMacroExpansion( Node *node, unsigned depth) { RETURN_IF_ERROR(mangleChildNode(node, 0, depth + 1)); - if (auto privateDiscriminator = node->getChild(3)) - RETURN_IF_ERROR(mangle(privateDiscriminator, depth + 1)); RETURN_IF_ERROR(mangleChildNode(node, 1, depth + 1)); + RETURN_IF_ERROR(mangleChildNode(node, 2, depth + 1)); Buffer << "fMp"; - return mangleChildNode(node, 2, depth + 1); + return mangleChildNode(node, 3, depth + 1); } ManglingError Remangler::mangleConformanceAttachedMacroExpansion( Node *node, unsigned depth) { RETURN_IF_ERROR(mangleChildNode(node, 0, depth + 1)); - if (auto privateDiscriminator = node->getChild(3)) - RETURN_IF_ERROR(mangle(privateDiscriminator, depth + 1)); RETURN_IF_ERROR(mangleChildNode(node, 1, depth + 1)); + RETURN_IF_ERROR(mangleChildNode(node, 2, depth + 1)); Buffer << "fMc"; - return mangleChildNode(node, 2, depth + 1); + return mangleChildNode(node, 3, depth + 1); } ManglingError Remangler::mangleMacroExpansionUniqueName( diff --git a/test/Demangle/Inputs/manglings.txt b/test/Demangle/Inputs/manglings.txt index 844f2ba526d45..ba5f391d61d35 100644 --- a/test/Demangle/Inputs/manglings.txt +++ b/test/Demangle/Inputs/manglings.txt @@ -454,7 +454,5 @@ $s4main4TestVAA3ABCV4hereyySiFfa ---> runtime attribute generator of main.ABC.he $s9MacroUser13testStringify1a1bySi_SitF9stringifyfMf1_ ---> freestanding macro expansion #3 of stringify in MacroUser.testStringify(a: Swift.Int, b: Swift.Int) -> () $s9MacroUser016testFreestandingA9ExpansionyyF4Foo3L_V23bitwidthNumberedStructsfMf_6methodfMu0_ ---> unique name #2 of method in freestanding macro expansion #1 of bitwidthNumberedStructs in Foo3 #1 in MacroUser.testFreestandingMacroExpansion() -> () @__swiftmacro_1a13testStringifyAA1bySi_SitF9stringifyfMf_ ---> freestanding macro expansion #1 of stringify in a.testStringify(a: Swift.Int, b: Swift.Int) -> () -@__swiftmacro_15accessor_macros8MyStructV4nameSSvp17myPropertyWrapperfMa_ ---> accessor macro expansion #1 of myPropertyWrapper in accessor_macros.MyStruct.name : Swift.String -@__swiftmacro_18macro_expand_peers1SV1f1a3for_SSSi_SSSdtYaF20addCompletionHandlerfMp_ ---> peer macro expansion #1 of addCompletionHandler in macro_expand_peers.S.f(a: Swift.Int, for: Swift.String, _: Swift.Double) async -> Swift.String -@__swiftmacro_25macro_expand_conformances7GenericV20DelegatedConformancefMc_ ---> conformance macro expansion #1 of DelegatedConformance in macro_expand_conformances.Generic +@__swiftmacro_18macro_expand_peers1SV1f20addCompletionHandlerfMp_ ---> peer macro @addCompletionHandler expansion #1 of f in macro_expand_peers.S @__swiftmacro_9MacroUser16MemberNotCoveredV33_4361AD9339943F52AE6186DD51E04E91Ll0dE0fMf0_ ---> freestanding macro expansion #2 of NotCovered(in _4361AD9339943F52AE6186DD51E04E91) in MacroUser.MemberNotCovered diff --git a/test/Macros/accessor_macros.swift b/test/Macros/accessor_macros.swift index d00e710969d72..3b9df438b34b5 100644 --- a/test/Macros/accessor_macros.swift +++ b/test/Macros/accessor_macros.swift @@ -43,7 +43,7 @@ struct MyStruct { @myPropertyWrapper var name: String - // CHECK-DUMP: @__swiftmacro_15accessor_macros8MyStructV4nameSSvp17myPropertyWrapperfMa_.swift + // CHECK-DUMP: @__swiftmacro_15accessor_macros8MyStructV4name17myPropertyWrapperfMa_.swift // CHECK-DUMP: get { // CHECK-DUMP: _name.wrappedValue // CHECK-DUMP: } @@ -53,7 +53,7 @@ struct MyStruct { @myPropertyWrapper var birthDate: Date? - // CHECK-DUMP: @__swiftmacro_15accessor_macros8MyStructV9birthDateAA0F0VSgvp17myPropertyWrapperfMa_.swift + // CHECK-DUMP: @__swiftmacro_15accessor_macros8MyStructV9birthDate17myPropertyWrapperfMa_.swift // CHECK-DUMP: get { // CHECK-DUMP: _birthDate.wrappedValue // CHECK-DUMP: } diff --git a/test/Macros/macro_expand.swift b/test/Macros/macro_expand.swift index 238cd7ec24573..63fab70bae5fe 100644 --- a/test/Macros/macro_expand.swift +++ b/test/Macros/macro_expand.swift @@ -67,7 +67,7 @@ struct Bad {} // CHECK-DIAGS: error: macro expansion cannot introduce default literal type '_ImageLiteralType' // CHECK-DIAGS: error: macro expansion cannot introduce default literal type '_FileReferenceLiteralType' -// CHECK-DIAGS: CONTENTS OF FILE @__swiftmacro_9MacroUser3BadV7InvalidfMp_.swift +// CHECK-DIAGS: CONTENTS OF FILE @__swiftmacro_9MacroUser3Bad7InvalidfMp_.swift // CHECK-DIAGS: import Swift // CHECK-DIAGS: precedencegroup MyPrecedence {} // CHECK-DIAGS: @attached(member) macro myMacro() diff --git a/test/Macros/macro_expand_conformances.swift b/test/Macros/macro_expand_conformances.swift index 39840a9f9bc90..6c9116b06e0ed 100644 --- a/test/Macros/macro_expand_conformances.swift +++ b/test/Macros/macro_expand_conformances.swift @@ -29,7 +29,7 @@ struct S {} @Hashable struct S2 {} -// CHECK-DUMP: @__swiftmacro_25macro_expand_conformances1SV9EquatablefMc_.swift +// CHECK-DUMP: @__swiftmacro_25macro_expand_conformances1S9EquatablefMc_.swift // CHECK-DUMP: extension S : Equatable {} // CHECK: true @@ -55,7 +55,7 @@ struct Wrapped: P { @DelegatedConformance struct Generic {} -// CHECK-DUMP: @__swiftmacro_25macro_expand_conformances7GenericV20DelegatedConformancefMc_.swift +// CHECK-DUMP: @__swiftmacro_25macro_expand_conformances7Generic20DelegatedConformancefMc_.swift // CHECK-DUMP: extension Generic : P where Element: P {} func requiresP(_ value: (some P).Type) { diff --git a/test/Macros/macro_expand_peers.swift b/test/Macros/macro_expand_peers.swift index 7622b959b391e..5b0a8f0f0c114 100644 --- a/test/Macros/macro_expand_peers.swift +++ b/test/Macros/macro_expand_peers.swift @@ -33,13 +33,16 @@ macro addCompletionHandler() = #externalMacro(module: "MacroDefinition", type: " macro AddClassReferencingSelf() = #externalMacro(module: "MacroDefinition", type: "AddClassReferencingSelfMacro") #endif +@attached(peer, names: arbitrary) +macro addCompletionHandlerArbitrarily(_: Int) = #externalMacro(module: "MacroDefinition", type: "AddCompletionHandler") + struct S { @addCompletionHandler func f(a: Int, for b: String, _ value: Double) async -> String { return b } - // CHECK-DUMP: @__swiftmacro_18macro_expand_peers1SV1f1a3for_SSSi_SSSdtYaF20addCompletionHandlerfMp_.swift + // CHECK-DUMP: @__swiftmacro_18macro_expand_peers1SV1f20addCompletionHandlerfMp_.swift // CHECK-DUMP: func f(a: Int, for b: String, _ value: Double, completionHandler: @escaping (String) -> Void) { // CHECK-DUMP: Task { // CHECK-DUMP: completionHandler(await f(a: a, for: b, value)) @@ -59,10 +62,10 @@ extension S { return b } - // CHECK-DUMP: @__swiftmacro_18macro_expand_peers1SV1g1a3for_SSSi_SSSdtYaF20addCompletionHandlerfMp_.swift - // CHECK-DUMP: func f(a: Int, for b: String, _ value: Double, completionHandler: @escaping (String) -> Void) { + // CHECK-DUMP: @__swiftmacro_18macro_expand_peers1SV1g20addCompletionHandlerfMp_.swift + // CHECK-DUMP: func g(a: Int, for b: String, _ value: Double, completionHandler: @escaping (String) -> Void) { // CHECK-DUMP: Task { - // CHECK-DUMP: completionHandler(await f(a: a, for: b, value)) + // CHECK-DUMP: completionHandler(await g(a: a, for: b, value)) // CHECK-DUMP: } // CHECK-DUMP: } @@ -101,9 +104,9 @@ func global(a: Int, b: String) { print(a, b) } -// CHECK-DUMP: @__swiftmacro_18macro_expand_peers6global1a1bySi_SStF10wrapInTypefMp_.swift -// CHECK-DUMP: struct $s18macro_expand_peers6global1a1bySi_SStF10wrapInTypefMp_6globalfMu0_ { -// CHECK-DUMP: func $s18macro_expand_peers6global1a1bySi_SStF10wrapInTypefMp_6globalfMu_(a: Int, b: String) { +// CHECK-DUMP: @__swiftmacro_18macro_expand_peers6global10wrapInTypefMp_.swift +// CHECK-DUMP: struct $s18macro_expand_peers6global10wrapInTypefMp_6globalfMu0_ { +// CHECK-DUMP: func $s18macro_expand_peers6global10wrapInTypefMp_6globalfMu_(a: Int, b: String) { // CHECK-DUMP: global(a: a, b: b) // CHECK-DUMP: } // CHECK-DUMP: } @@ -130,3 +133,18 @@ struct Main { @AddClassReferencingSelf protocol MyProto { } + +// Reference cycles amongst arbitrary peer macros and macro arguments. +let x = 10 +let y = 10 +struct S2 { + @addCompletionHandlerArbitrarily(x) + func f(a: Int, for b: String, _ value: Double) async -> String { + return b + } + + @addCompletionHandlerArbitrarily(y) + func g(a: Int, for b: String, _ value: Double) async -> String { + return b + } +} diff --git a/test/SourceKit/Macros/macro_basic.swift b/test/SourceKit/Macros/macro_basic.swift index 05f909356cdcf..1b83d297bc8d8 100644 --- a/test/SourceKit/Macros/macro_basic.swift +++ b/test/SourceKit/Macros/macro_basic.swift @@ -165,11 +165,11 @@ macro anonymousTypes(_: () -> String) = #externalMacro(module: "MacroDefinition" // RUN: %sourcekitd-test -req=refactoring.expand.macro -pos=21:1 %s -- ${COMPILER_ARGS[@]} | %FileCheck -check-prefix=ATTACHED_EXPAND %s // RUN: %sourcekitd-test -req=refactoring.expand.macro -pos=21:2 %s -- ${COMPILER_ARGS[@]} | %FileCheck -check-prefix=ATTACHED_EXPAND %s // ATTACHED_EXPAND: source.edit.kind.active: -// ATTACHED_EXPAND-NEXT: 23:3-23:3 (@__swiftmacro_9MacroUser1SV13myTypeWrapperfMA_.swift) "@accessViaStorage " +// ATTACHED_EXPAND-NEXT: 23:3-23:3 (@__swiftmacro_9MacroUser1SV1x13myTypeWrapperfMA_.swift) "@accessViaStorage " // ATTACHED_EXPAND-NEXT: source.edit.kind.active: -// ATTACHED_EXPAND-NEXT: 24:3-24:3 (@__swiftmacro_9MacroUser1SV13myTypeWrapperfMA0_.swift) "@accessViaStorage " +// ATTACHED_EXPAND-NEXT: 24:3-24:3 (@__swiftmacro_9MacroUser1SV1y13myTypeWrapperfMA0_.swift) "@accessViaStorage " // ATTACHED_EXPAND-NEXT: source.edit.kind.active: -// ATTACHED_EXPAND-NEXT: 25:1-25:1 (@__swiftmacro_9MacroUser1SV13myTypeWrapperfMm_.swift) " +// ATTACHED_EXPAND-NEXT: 25:1-25:1 (@__swiftmacro_9MacroUser1S13myTypeWrapperfMm_.swift) " // ATTACHED_EXPAND-EMPTY: // ATTACHED_EXPAND-NEXT: private var _storage = _Storage() // ATTACHED_EXPAND-NEXT: " @@ -177,7 +177,7 @@ macro anonymousTypes(_: () -> String) = #externalMacro(module: "MacroDefinition" // ATTACHED_EXPAND-NEXT: 21:1-21:15 "" //##-- Cursor info on the attribute expanded by @myTypeWrapper -// RUN: %sourcekitd-test -req=cursor -cursor-action -req-opts=retrieve_symbol_graph=1 -offset=2 @__swiftmacro_9MacroUser1SV13myTypeWrapperfMA_.swift -primary-file %s -- ${COMPILER_ARGS[@]} | %FileCheck -check-prefix=NESTED_ATTACHED_CURSOR %s +// RUN: %sourcekitd-test -req=cursor -cursor-action -req-opts=retrieve_symbol_graph=1 -offset=2 @__swiftmacro_9MacroUser1SV1x13myTypeWrapperfMA_.swift -primary-file %s -- ${COMPILER_ARGS[@]} | %FileCheck -check-prefix=NESTED_ATTACHED_CURSOR %s // NESTED_ATTACHED_CURSOR: source.lang.swift.ref.macro // NESTED_ATTACHED_CURSOR-SAME: macro_basic.swift:10:27-10:43 // NESTED_ATTACHED_CURSOR-LABEL: SYMBOL GRAPH BEGIN @@ -196,9 +196,9 @@ macro anonymousTypes(_: () -> String) = #externalMacro(module: "MacroDefinition" // NESTED_ATTACHED_CURSOR-NEXT: ACTIONS END //##-- Expansion on the attribute expanded by @myTypeWrapper -// RUN: %sourcekitd-test -req=refactoring.expand.macro -pos=1:2 @__swiftmacro_9MacroUser1SV13myTypeWrapperfMA_.swift -primary-file %s -- ${COMPILER_ARGS[@]} | %FileCheck -check-prefix=NESTED_ATTACHED_EXPAND %s +// RUN: %sourcekitd-test -req=refactoring.expand.macro -pos=1:2 @__swiftmacro_9MacroUser1SV1x13myTypeWrapperfMA_.swift -primary-file %s -- ${COMPILER_ARGS[@]} | %FileCheck -check-prefix=NESTED_ATTACHED_EXPAND %s // NESTED_ATTACHED_EXPAND: source.edit.kind.active: -// NESTED_ATTACHED_EXPAND-NEXT: Macros/macro_basic.swift 23:13-23:13 (@__swiftmacro_9MacroUser1SV1xSivp16accessViaStoragefMa_.swift) "{ +// NESTED_ATTACHED_EXPAND-NEXT: Macros/macro_basic.swift 23:13-23:13 (@__swiftmacro_9MacroUser1SV1x16accessViaStoragefMa_.swift) "{ // NESTED_ATTACHED_EXPAND-NEXT: get { _storage.x } // NESTED_ATTACHED_EXPAND-EMPTY: // NESTED_ATTACHED_EXPAND-NEXT: set { _storage.x = newValue } @@ -209,7 +209,7 @@ macro anonymousTypes(_: () -> String) = #externalMacro(module: "MacroDefinition" //##-- Expansion on the first accessor macro // RUN: %sourcekitd-test -req=refactoring.expand.macro -pos=30:4 %s -- ${COMPILER_ARGS[@]} | %FileCheck -check-prefix=ACCESSOR1_EXPAND %s // ACCESSOR1_EXPAND: source.edit.kind.active: -// ACCESSOR1_EXPAND-NEXT: 31:13-31:13 (@__swiftmacro_9MacroUser2S2V1xSivp16accessViaStoragefMa_.swift) "{ +// ACCESSOR1_EXPAND-NEXT: 31:13-31:13 (@__swiftmacro_9MacroUser2S2V1x16accessViaStoragefMa_.swift) "{ // ACCESSOR1_EXPAND-NEXT: get { _storage.x } // ACCESSOR1_EXPAND-EMPTY: // ACCESSOR1_EXPAND-NEXT: set { _storage.x = newValue } @@ -220,7 +220,7 @@ macro anonymousTypes(_: () -> String) = #externalMacro(module: "MacroDefinition" //##-- Expansion on the second accessor macro // RUN: %sourcekitd-test -req=refactoring.expand.macro -pos=33:13 %s -- ${COMPILER_ARGS[@]} | %FileCheck -check-prefix=ACCESSOR2_EXPAND %s // ACCESSOR2_EXPAND: source.edit.kind.active: -// ACCESSOR2_EXPAND-NEXT: 34:14-34:18 (@__swiftmacro_9MacroUser2S2V1ySivp16accessViaStoragefMa_.swift) "{ +// ACCESSOR2_EXPAND-NEXT: 34:14-34:18 (@__swiftmacro_9MacroUser2S2V1y16accessViaStoragefMa_.swift) "{ // ACCESSOR2_EXPAND-NEXT: get { _storage.y } // ACCESSOR2_EXPAND-EMPTY: // ACCESSOR2_EXPAND-NEXT: set { _storage.y = newValue } @@ -231,7 +231,7 @@ macro anonymousTypes(_: () -> String) = #externalMacro(module: "MacroDefinition" //##-- Expansion on the addCompletionHandler macro. // RUN: %sourcekitd-test -req=refactoring.expand.macro -pos=42:5 %s -- ${COMPILER_ARGS[@]} | %FileCheck -check-prefix=PEER_EXPAND %s // PEER_EXPAND: source.edit.kind.active: -// PEER_EXPAND-NEXT: 45:4-45:4 (@__swiftmacro_9MacroUser2S3V1f1a3for_SSSi_SSSdtYaF20addCompletionHandlerfMp_.swift) " +// PEER_EXPAND-NEXT: 45:4-45:4 (@__swiftmacro_9MacroUser2S3V1f20addCompletionHandlerfMp_.swift) " // PEER_EXPAND-EMPTY: // PEER_EXPAND-NEXT: func f(a: Int, for b: String, _ value: Double, completionHandler: @escaping (String) -> Void) { // PEER_EXPAND-NEXT: Task { @@ -245,7 +245,7 @@ macro anonymousTypes(_: () -> String) = #externalMacro(module: "MacroDefinition" //##-- Expansion on a conformance macro. // RUN: %sourcekitd-test -req=refactoring.expand.macro -pos=51:5 %s -- ${COMPILER_ARGS[@]} | %FileCheck -check-prefix=CONFORMANCE_EXPAND %s // CONFORMANCE_EXPAND: source.edit.kind.active: -// CONFORMANCE_EXPAND-NEXT: 52:14-52:14 (@__swiftmacro_9MacroUser2S4V8HashablefMc_.swift) " +// CONFORMANCE_EXPAND-NEXT: 52:14-52:14 (@__swiftmacro_9MacroUser2S48HashablefMc_.swift) " // CONFORMANCE_EXPAND-EMPTY: // CONFORMANCE_EXPAND-NEXT: extension S4 : Hashable {} // CONFORMANCE_EXPAND-NEXT: " From ef7970bad3a3c3dc71dab87ec064c9f719c43106 Mon Sep 17 00:00:00 2001 From: Doug Gregor Date: Sun, 9 Apr 2023 22:12:37 -0400 Subject: [PATCH 2/4] [Macros] Ensure that we compute the interface type before we add accessors Adding accessors to a stored property, which removes its initializer. Force computation of the interface type first. There are better ways to model this in the AST, but it requires reworking how we handle initializers to break them into requests. --- lib/Sema/TypeCheckMacros.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/Sema/TypeCheckMacros.cpp b/lib/Sema/TypeCheckMacros.cpp index fc115c774c911..5fc3efecc2a8c 100644 --- a/lib/Sema/TypeCheckMacros.cpp +++ b/lib/Sema/TypeCheckMacros.cpp @@ -1330,6 +1330,7 @@ evaluateAttachedMacro(MacroDecl *macro, Decl *attachedTo, CustomAttr *attr, Optional swift::expandAccessors( AbstractStorageDecl *storage, CustomAttr *attr, MacroDecl *macro ) { + (void)storage->getInterfaceType(); // Evaluate the macro. auto macroSourceFile = evaluateAttachedMacro(macro, storage, attr, /*passParentContext*/false, From 097ecfd545b7b7880838c6e52821a5ceeec8eef2 Mon Sep 17 00:00:00 2001 From: Doug Gregor Date: Tue, 11 Apr 2023 07:41:12 -0400 Subject: [PATCH 3/4] Fix fragile test --- test/stdlib/symbol-visibility-linux.test-sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/stdlib/symbol-visibility-linux.test-sh b/test/stdlib/symbol-visibility-linux.test-sh index a8c59bd1a7af5..044c4dd6f7c98 100644 --- a/test/stdlib/symbol-visibility-linux.test-sh +++ b/test/stdlib/symbol-visibility-linux.test-sh @@ -27,6 +27,8 @@ // RUN: -e _ZNSt3_V28__rotateIPcEET_S2_S2_S2_St26random_access_iterator_tag \ // RUN: -e _ZN9__gnu_cxx12__to_xstringINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEcEET_PFiPT0_mPKS8_P13__va_list_tagEmSB_z \ // RUN: -e _ZN9__gnu_cxx12__to_xstringINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEcEET_PFiPT0_mPKS8_St9__va_listEmSB_z \ +// RUN: -e _ZStplIcSt11char_traitsIcESaIcEENSt7__cxx1112basic_stringIT_T0_T1_EEOS8_PKS5_ \ +// RUN: -e _ZStplIcSt11char_traitsIcESaIcEENSt7__cxx1112basic_stringIT_T0_T1_EEPKS5_OS8_ \ // RUN: -e _ZZNSt19_Sp_make_shared_tag5_S_tiEvE5__tag \ // RUN: -e _ZSt16__once_call_implISt12_Bind_simpleIFPFvPvEDnEEEvv \ // RUN: -e '_ZSt16__once_call_implISt12_Bind_simpleIFPFvPvEPA[0-9]\+_cEEEvv' \ From fb82b8f7110716cf0c1f6713847d7eb883fac35a Mon Sep 17 00:00:00 2001 From: Doug Gregor Date: Tue, 11 Apr 2023 22:36:41 -0400 Subject: [PATCH 4/4] Fix Linux flaky test completely --- test/stdlib/symbol-visibility-linux.test-sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/stdlib/symbol-visibility-linux.test-sh b/test/stdlib/symbol-visibility-linux.test-sh index 044c4dd6f7c98..bfcb2343543fa 100644 --- a/test/stdlib/symbol-visibility-linux.test-sh +++ b/test/stdlib/symbol-visibility-linux.test-sh @@ -286,6 +286,8 @@ // RUN: -e _ZSteqIcSt11char_traitsIcESaIcEEbRKNSt7__cxx1112basic_stringIT_T0_T1_EEPKS5_ \ // RUN: -e _ZStplIcSt11char_traitsIcESaIcEENSt7__cxx1112basic_stringIT_T0_T1_EEPKS5_RKS8_ \ // RUN: -e _ZN9__gnu_cxx32__throw_concurrence_unlock_errorEv \ +// RUN: -e _ZStplIcSt11char_traitsIcESaIcEENSt7__cxx1112basic_stringIT_T0_T1_EEOS8_PKS5_ \ +// RUN: -e _ZStplIcSt11char_traitsIcESaIcEENSt7__cxx1112basic_stringIT_T0_T1_EEPKS5_OS8_ \ // RUN: -e _ZNKSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE11_M_disjunctEPKc \ // RUN: -e _ZNKSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE11_M_is_localEv \ // RUN: -e _ZNKSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE12find_last_ofEPKcm \