diff --git a/include/swift/AST/ASTMangler.h b/include/swift/AST/ASTMangler.h index 037cd4e4f73..8f292e19ab2 100644 --- a/include/swift/AST/ASTMangler.h +++ b/include/swift/AST/ASTMangler.h @@ -453,7 +453,8 @@ protected: const ValueDecl *forDecl = nullptr); void appendDeclName( - const ValueDecl *decl, DeclBaseName name = DeclBaseName()); + const ValueDecl *decl, DeclBaseName name = DeclBaseName(), + bool skipLocalDiscriminator = false); GenericTypeParamType *appendAssocType(DependentMemberType *DepTy, GenericSignature sig, diff --git a/lib/AST/ASTMangler.cpp b/lib/AST/ASTMangler.cpp index 6cab5461b47..6366195a23f 100644 --- a/lib/AST/ASTMangler.cpp +++ b/lib/AST/ASTMangler.cpp @@ -1199,7 +1199,8 @@ getOverriddenSwiftProtocolObjCName(const ValueDecl *decl, return std::nullopt; } -void ASTMangler::appendDeclName(const ValueDecl *decl, DeclBaseName name) { +void ASTMangler::appendDeclName(const ValueDecl *decl, DeclBaseName name, + bool skipLocalDiscriminator) { ASSERT(!getABIDecl(decl) && "caller should make sure we get ABI decls"); if (name.empty()) name = decl->getBaseName(); @@ -1240,6 +1241,11 @@ void ASTMangler::appendDeclName(const ValueDecl *decl, DeclBaseName name) { } if (decl->getDeclContext()->isLocalContext()) { + // If we don't need a local discriminator (attached macros receive a + // separate discriminator), we're done. + if (skipLocalDiscriminator) + return; + if (auto *paramDecl = dyn_cast(decl)) { if (!decl->hasName()) { // Mangle unnamed params with their ordering. @@ -5158,13 +5164,15 @@ std::string ASTMangler::mangleAttachedMacroExpansion( // If we needed a local discriminator, stuff that into the name itself. // This is hack, but these names aren't stable anyway. + bool skipLocalDiscriminator = false; if (auto discriminator = precheckedMangleContext.second) { + skipLocalDiscriminator = true; name = encodeLocalPrecheckedDiscriminator( decl->getASTContext(), name, *discriminator); } if (auto valueDecl = dyn_cast(decl)) - appendDeclName(valueDecl, name); + appendDeclName(valueDecl, name, skipLocalDiscriminator); else if (!name.empty()) appendIdentifier(name.str()); else diff --git a/test/Macros/macro_expand.swift b/test/Macros/macro_expand.swift index 8d7fbf3c36c..f52394d6a29 100644 --- a/test/Macros/macro_expand.swift +++ b/test/Macros/macro_expand.swift @@ -142,14 +142,12 @@ func invalidDeclarationMacro() { @AccidentalCodeItem struct S {} // expected-note@-1 {{in expansion of macro 'AccidentalCodeItem' on struct 'S' here}} - // CHECK-DIAGS: @__swiftmacro_9MacroUser018invalidDeclarationA0yyF5S_$l0L_18AccidentalCodeItemfMp_.swift:1:1: error: expected macro expansion to produce a declaration + // CHECK-DIAGS: @__swiftmacro_9MacroUser018invalidDeclarationA0yyF5S_$l018AccidentalCodeItemfMp_.swift:1:1: error: expected macro expansion to produce a declaration - struct LocalThing1 { - func f() { - #accidentalCodeItem - // expected-note@-1 {{in expansion of macro 'accidentalCodeItem' here}} - // CHECK-DIAGS: @__swiftmacro_9MacroUser018invalidDeclarationA0yyF5S_$l0L_18AccidentalCodeItemfMp_.swift - } + do { + @AccidentalCodeItem struct S {} + // expected-note@-1 {{in expansion of macro 'AccidentalCodeItem' on struct 'S' here}} + // CHECK-DIAGS: @__swiftmacro_9MacroUser018invalidDeclarationA0yyF5S_$l118AccidentalCodeItemfMp_.swift:1:1: error: expected macro expansion to produce a declaration } } #endif @@ -648,6 +646,19 @@ struct HasNestedType { #DefineComparableType } +@attached(accessor) +macro AddGetter() = #externalMacro(module: "MacroDefinition", type: "AddGetterMacro") + +// Make sure the mangling for the accessor macro doesn't kick local +// discriminator assignment, which would miss the autoclosure. +func testLocalAccessorMacroWithAutoclosure() { + @AddGetter + var x: Int = 2 + + func takesAutoclosure(_ x: @autoclosure () -> Int) {} + takesAutoclosure(1) +} + #if TEST_DIAGNOSTICS @freestanding(expression) macro missingMacro() = #externalMacro(module: "MacroDefinition", type: "BluhBlah") @@ -655,4 +666,18 @@ macro missingMacro() = #externalMacro(module: "MacroDefinition", type: "BluhBlah @freestanding(expression) macro notMacro() = #externalMacro(module: "MacroDefinition", type: "NotMacroStruct") // FIXME: xpected-warning@-1 {{macro implementation type 'MacroDefinition.NotMacroStruct' could not be found for macro 'notMacro()'; 'MacroDefinition.NotMacroStruct' is not a valid macro implementation type in library plugin '}} + +// Because this is a method in a local decl, it ends up getting delayed, so +// we need to check it at the end of the file. +// FIXME: We either need to switch to using CHECK-DAG, or ideally teach +// the diagnostic verifier about macro expansion buffers. +func invalidDeclarationMacro2() { + struct LocalThing1 { + func f() { + #accidentalCodeItem + // expected-note@-1 {{in expansion of macro 'accidentalCodeItem' here}} + // CHECK-DIAGS: @__swiftmacro_9MacroUser0023macro_expandswift_elFCffMX[[@LINE-3]]_6_18accidentalCodeItemfMf_.swift:1:1: error: expected macro expansion to produce a declaration + } + } +} #endif