Merge pull request #80581 from hamishknight/mangle-less-6.2

[6.2] [Mangler] Avoid mangling local discriminator for attached macros
This commit is contained in:
Hamish Knight
2025-04-07 18:58:30 +01:00
committed by GitHub
3 changed files with 44 additions and 10 deletions

View File

@@ -453,7 +453,8 @@ protected:
const ValueDecl *forDecl = nullptr); const ValueDecl *forDecl = nullptr);
void appendDeclName( void appendDeclName(
const ValueDecl *decl, DeclBaseName name = DeclBaseName()); const ValueDecl *decl, DeclBaseName name = DeclBaseName(),
bool skipLocalDiscriminator = false);
GenericTypeParamType *appendAssocType(DependentMemberType *DepTy, GenericTypeParamType *appendAssocType(DependentMemberType *DepTy,
GenericSignature sig, GenericSignature sig,

View File

@@ -1199,7 +1199,8 @@ getOverriddenSwiftProtocolObjCName(const ValueDecl *decl,
return std::nullopt; 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"); ASSERT(!getABIDecl(decl) && "caller should make sure we get ABI decls");
if (name.empty()) if (name.empty())
name = decl->getBaseName(); name = decl->getBaseName();
@@ -1240,6 +1241,11 @@ void ASTMangler::appendDeclName(const ValueDecl *decl, DeclBaseName name) {
} }
if (decl->getDeclContext()->isLocalContext()) { 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<ParamDecl>(decl)) { if (auto *paramDecl = dyn_cast<ParamDecl>(decl)) {
if (!decl->hasName()) { if (!decl->hasName()) {
// Mangle unnamed params with their ordering. // 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. // If we needed a local discriminator, stuff that into the name itself.
// This is hack, but these names aren't stable anyway. // This is hack, but these names aren't stable anyway.
bool skipLocalDiscriminator = false;
if (auto discriminator = precheckedMangleContext.second) { if (auto discriminator = precheckedMangleContext.second) {
skipLocalDiscriminator = true;
name = encodeLocalPrecheckedDiscriminator( name = encodeLocalPrecheckedDiscriminator(
decl->getASTContext(), name, *discriminator); decl->getASTContext(), name, *discriminator);
} }
if (auto valueDecl = dyn_cast<ValueDecl>(decl)) if (auto valueDecl = dyn_cast<ValueDecl>(decl))
appendDeclName(valueDecl, name); appendDeclName(valueDecl, name, skipLocalDiscriminator);
else if (!name.empty()) else if (!name.empty())
appendIdentifier(name.str()); appendIdentifier(name.str());
else else

View File

@@ -142,14 +142,12 @@ func invalidDeclarationMacro() {
@AccidentalCodeItem struct S {} @AccidentalCodeItem struct S {}
// expected-note@-1 {{in expansion of macro 'AccidentalCodeItem' on struct 'S' here}} // 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 { do {
func f() { @AccidentalCodeItem struct S {}
#accidentalCodeItem // expected-note@-1 {{in expansion of macro 'AccidentalCodeItem' on struct 'S' here}}
// expected-note@-1 {{in expansion of macro 'accidentalCodeItem' here}} // CHECK-DIAGS: @__swiftmacro_9MacroUser018invalidDeclarationA0yyF5S_$l118AccidentalCodeItemfMp_.swift:1:1: error: expected macro expansion to produce a declaration
// CHECK-DIAGS: @__swiftmacro_9MacroUser018invalidDeclarationA0yyF5S_$l0L_18AccidentalCodeItemfMp_.swift
}
} }
} }
#endif #endif
@@ -648,6 +646,19 @@ struct HasNestedType {
#DefineComparableType #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 #if TEST_DIAGNOSTICS
@freestanding(expression) @freestanding(expression)
macro missingMacro() = #externalMacro(module: "MacroDefinition", type: "BluhBlah") macro missingMacro() = #externalMacro(module: "MacroDefinition", type: "BluhBlah")
@@ -655,4 +666,18 @@ macro missingMacro() = #externalMacro(module: "MacroDefinition", type: "BluhBlah
@freestanding(expression) @freestanding(expression)
macro notMacro() = #externalMacro(module: "MacroDefinition", type: "NotMacroStruct") 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 '}} // 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 #endif