Skip to content

Commit 362a239

Browse files
authored
Merge pull request #80581 from hamishknight/mangle-less-6.2
[6.2] [Mangler] Avoid mangling local discriminator for attached macros
2 parents 5d81f4d + e1a864d commit 362a239

File tree

3 files changed

+44
-10
lines changed

3 files changed

+44
-10
lines changed

include/swift/AST/ASTMangler.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,8 @@ class ASTMangler : public Mangler {
453453
const ValueDecl *forDecl = nullptr);
454454

455455
void appendDeclName(
456-
const ValueDecl *decl, DeclBaseName name = DeclBaseName());
456+
const ValueDecl *decl, DeclBaseName name = DeclBaseName(),
457+
bool skipLocalDiscriminator = false);
457458

458459
GenericTypeParamType *appendAssocType(DependentMemberType *DepTy,
459460
GenericSignature sig,

lib/AST/ASTMangler.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,7 +1199,8 @@ getOverriddenSwiftProtocolObjCName(const ValueDecl *decl,
11991199
return std::nullopt;
12001200
}
12011201

1202-
void ASTMangler::appendDeclName(const ValueDecl *decl, DeclBaseName name) {
1202+
void ASTMangler::appendDeclName(const ValueDecl *decl, DeclBaseName name,
1203+
bool skipLocalDiscriminator) {
12031204
ASSERT(!getABIDecl(decl) && "caller should make sure we get ABI decls");
12041205
if (name.empty())
12051206
name = decl->getBaseName();
@@ -1240,6 +1241,11 @@ void ASTMangler::appendDeclName(const ValueDecl *decl, DeclBaseName name) {
12401241
}
12411242

12421243
if (decl->getDeclContext()->isLocalContext()) {
1244+
// If we don't need a local discriminator (attached macros receive a
1245+
// separate discriminator), we're done.
1246+
if (skipLocalDiscriminator)
1247+
return;
1248+
12431249
if (auto *paramDecl = dyn_cast<ParamDecl>(decl)) {
12441250
if (!decl->hasName()) {
12451251
// Mangle unnamed params with their ordering.
@@ -5158,13 +5164,15 @@ std::string ASTMangler::mangleAttachedMacroExpansion(
51585164

51595165
// If we needed a local discriminator, stuff that into the name itself.
51605166
// This is hack, but these names aren't stable anyway.
5167+
bool skipLocalDiscriminator = false;
51615168
if (auto discriminator = precheckedMangleContext.second) {
5169+
skipLocalDiscriminator = true;
51625170
name = encodeLocalPrecheckedDiscriminator(
51635171
decl->getASTContext(), name, *discriminator);
51645172
}
51655173

51665174
if (auto valueDecl = dyn_cast<ValueDecl>(decl))
5167-
appendDeclName(valueDecl, name);
5175+
appendDeclName(valueDecl, name, skipLocalDiscriminator);
51685176
else if (!name.empty())
51695177
appendIdentifier(name.str());
51705178
else

test/Macros/macro_expand.swift

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -142,14 +142,12 @@ func invalidDeclarationMacro() {
142142

143143
@AccidentalCodeItem struct S {}
144144
// expected-note@-1 {{in expansion of macro 'AccidentalCodeItem' on struct 'S' here}}
145-
// CHECK-DIAGS: @__swiftmacro_9MacroUser018invalidDeclarationA0yyF5S_$l0L_18AccidentalCodeItemfMp_.swift:1:1: error: expected macro expansion to produce a declaration
145+
// CHECK-DIAGS: @__swiftmacro_9MacroUser018invalidDeclarationA0yyF5S_$l018AccidentalCodeItemfMp_.swift:1:1: error: expected macro expansion to produce a declaration
146146

147-
struct LocalThing1 {
148-
func f() {
149-
#accidentalCodeItem
150-
// expected-note@-1 {{in expansion of macro 'accidentalCodeItem' here}}
151-
// CHECK-DIAGS: @__swiftmacro_9MacroUser018invalidDeclarationA0yyF5S_$l0L_18AccidentalCodeItemfMp_.swift
152-
}
147+
do {
148+
@AccidentalCodeItem struct S {}
149+
// expected-note@-1 {{in expansion of macro 'AccidentalCodeItem' on struct 'S' here}}
150+
// CHECK-DIAGS: @__swiftmacro_9MacroUser018invalidDeclarationA0yyF5S_$l118AccidentalCodeItemfMp_.swift:1:1: error: expected macro expansion to produce a declaration
153151
}
154152
}
155153
#endif
@@ -648,11 +646,38 @@ struct HasNestedType {
648646
#DefineComparableType
649647
}
650648

649+
@attached(accessor)
650+
macro AddGetter() = #externalMacro(module: "MacroDefinition", type: "AddGetterMacro")
651+
652+
// Make sure the mangling for the accessor macro doesn't kick local
653+
// discriminator assignment, which would miss the autoclosure.
654+
func testLocalAccessorMacroWithAutoclosure() {
655+
@AddGetter
656+
var x: Int = 2
657+
658+
func takesAutoclosure(_ x: @autoclosure () -> Int) {}
659+
takesAutoclosure(1)
660+
}
661+
651662
#if TEST_DIAGNOSTICS
652663
@freestanding(expression)
653664
macro missingMacro() = #externalMacro(module: "MacroDefinition", type: "BluhBlah")
654665
// FIXME: xpected-warning@-1 {{external macro implementation type 'MacroDefinition.BluhBlah' could not be found for macro 'missingMacro()'; 'MacroDefinition.BluhBlah' could not be found in library plugin '}}
655666
@freestanding(expression)
656667
macro notMacro() = #externalMacro(module: "MacroDefinition", type: "NotMacroStruct")
657668
// 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 '}}
669+
670+
// Because this is a method in a local decl, it ends up getting delayed, so
671+
// we need to check it at the end of the file.
672+
// FIXME: We either need to switch to using CHECK-DAG, or ideally teach
673+
// the diagnostic verifier about macro expansion buffers.
674+
func invalidDeclarationMacro2() {
675+
struct LocalThing1 {
676+
func f() {
677+
#accidentalCodeItem
678+
// expected-note@-1 {{in expansion of macro 'accidentalCodeItem' here}}
679+
// CHECK-DIAGS: @__swiftmacro_9MacroUser0023macro_expandswift_elFCffMX[[@LINE-3]]_6_18accidentalCodeItemfMf_.swift:1:1: error: expected macro expansion to produce a declaration
680+
}
681+
}
682+
}
658683
#endif

0 commit comments

Comments
 (0)