Skip to content

Commit 157ae91

Browse files
authored
Merge pull request swiftlang#74299 from slavapestov/macro-expansion-mangling-6.0
[6.0] Fix request cycle in macro expansion mangling
2 parents 22cce0e + 6f88906 commit 157ae91

18 files changed

+142
-49
lines changed

include/swift/Demangling/DemangleNodes.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ NODE(LazyProtocolWitnessTableAccessor)
163163
NODE(LazyProtocolWitnessTableCacheVariable)
164164
NODE(LocalDeclName)
165165
NODE(Macro)
166+
NODE(MacroExpansionLoc)
166167
NODE(MacroExpansionUniqueName)
167168
CONTEXT_NODE(MaterializeForSet)
168169
NODE(MemberAttachedMacroExpansion)

lib/AST/ASTMangler.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4457,10 +4457,21 @@ void ASTMangler::appendMacroExpansionContext(
44574457
ASTContext &ctx = origDC->getASTContext();
44584458
SourceManager &sourceMgr = ctx.SourceMgr;
44594459

4460+
auto appendMacroExpansionLoc = [&]() {
4461+
appendIdentifier(origDC->getParentModule()->getName().str());
4462+
4463+
auto *SF = origDC->getParentSourceFile();
4464+
appendIdentifier(llvm::sys::path::filename(SF->getFilename()));
4465+
4466+
auto lineColumn = sourceMgr.getLineAndColumnInBuffer(loc);
4467+
appendOperator("fMX", Index(lineColumn.first), Index(lineColumn.second));
4468+
};
4469+
44604470
auto bufferID = sourceMgr.findBufferContainingLoc(loc);
44614471
auto generatedSourceInfo = sourceMgr.getGeneratedSourceInfo(bufferID);
4462-
if (!generatedSourceInfo)
4463-
return appendContext(origDC, nullBase, StringRef());
4472+
if (!generatedSourceInfo) {
4473+
return appendMacroExpansionLoc();
4474+
}
44644475

44654476
SourceLoc outerExpansionLoc;
44664477
DeclContext *outerExpansionDC;
@@ -4479,7 +4490,7 @@ void ASTMangler::appendMacroExpansionContext(
44794490
case GeneratedSourceInfo::PrettyPrinted:
44804491
case GeneratedSourceInfo::ReplacedFunctionBody:
44814492
case GeneratedSourceInfo::DefaultArgument:
4482-
return appendContext(origDC, nullBase, StringRef());
4493+
return appendMacroExpansionLoc();
44834494
}
44844495

44854496
switch (generatedSourceInfo->kind) {
@@ -4536,7 +4547,7 @@ void ASTMangler::appendMacroExpansionContext(
45364547
// If we hit the point where the structure is represented as a DeclContext,
45374548
// we're done.
45384549
if (origDC->isChildContextOf(outerExpansionDC))
4539-
return appendContext(origDC, nullBase, StringRef());
4550+
return appendMacroExpansionLoc();
45404551

45414552
// Append our own context and discriminator.
45424553
appendMacroExpansionContext(outerExpansionLoc, origDC);

lib/AST/Expr.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1934,7 +1934,12 @@ unsigned AbstractClosureExpr::getDiscriminator() const {
19341934
Bits.AbstractClosureExpr.Discriminator = discriminator;
19351935
}
19361936

1937-
assert(getRawDiscriminator() != InvalidDiscriminator);
1937+
if (getRawDiscriminator() == InvalidDiscriminator) {
1938+
llvm::errs() << "Closure does not have an assigned discriminator:\n";
1939+
dump(llvm::errs());
1940+
abort();
1941+
}
1942+
19381943
return getRawDiscriminator();
19391944
}
19401945

lib/Demangling/Demangler.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4294,7 +4294,8 @@ static bool isMacroExpansionNodeKind(Node::Kind kind) {
42944294
kind == Node::Kind::MemberAttachedMacroExpansion ||
42954295
kind == Node::Kind::PeerAttachedMacroExpansion ||
42964296
kind == Node::Kind::ConformanceAttachedMacroExpansion ||
4297-
kind == Node::Kind::ExtensionAttachedMacroExpansion;
4297+
kind == Node::Kind::ExtensionAttachedMacroExpansion ||
4298+
kind == Node::Kind::MacroExpansionLoc;
42984299
}
42994300

43004301
NodePointer Demangler::demangleMacroExpansion() {
@@ -4323,6 +4324,20 @@ NodePointer Demangler::demangleMacroExpansion() {
43234324
isFreestanding = false;
43244325
break;
43254326

4327+
case 'X': {
4328+
kind = Node::Kind::MacroExpansionLoc;
4329+
4330+
int line = demangleIndex();
4331+
int col = demangleIndex();
4332+
4333+
auto lineNode = createNode(Node::Kind::Index, line);
4334+
auto colNode = createNode(Node::Kind::Index, col);
4335+
4336+
NodePointer buffer = popNode(Node::Kind::Identifier);
4337+
NodePointer module = popNode(Node::Kind::Identifier);
4338+
return createWithChildren(kind, module, buffer, lineNode, colNode);
4339+
}
4340+
43264341
default:
43274342
return nullptr;
43284343
}

lib/Demangling/NodePrinter.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,7 @@ class NodePrinter {
456456
case Node::Kind::LazyProtocolWitnessTableCacheVariable:
457457
case Node::Kind::LocalDeclName:
458458
case Node::Kind::Macro:
459+
case Node::Kind::MacroExpansionLoc:
459460
case Node::Kind::MacroExpansionUniqueName:
460461
case Node::Kind::MaterializeForSet:
461462
case Node::Kind::MemberAttributeAttachedMacroExpansion:
@@ -1543,6 +1544,24 @@ NodePointer NodePrinter::print(NodePointer Node, unsigned depth,
15431544
return printEntity(Node, depth, asPrefixContext, TypePrinting::NoType,
15441545
/*hasName*/true, "freestanding macro expansion #",
15451546
(int)Node->getChild(2)->getIndex() + 1);
1547+
case Node::Kind::MacroExpansionLoc:
1548+
if (Node->getNumChildren() > 0) {
1549+
Printer << "module ";
1550+
print(Node->getChild(0), depth + 1);
1551+
}
1552+
if (Node->getNumChildren() > 1) {
1553+
Printer << " file ";
1554+
print(Node->getChild(1), depth + 1);
1555+
}
1556+
if (Node->getNumChildren() > 2) {
1557+
Printer << " line ";
1558+
print(Node->getChild(2), depth + 1);
1559+
}
1560+
if (Node->getNumChildren() > 3) {
1561+
Printer << " column ";
1562+
print(Node->getChild(3), depth + 1);
1563+
}
1564+
return nullptr;
15461565
case Node::Kind::MacroExpansionUniqueName:
15471566
return printEntity(Node, depth, asPrefixContext, TypePrinting::NoType,
15481567
/*hasName*/true, "unique name #",

lib/Demangling/OldRemangler.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1115,6 +1115,11 @@ ManglingError Remangler::mangleMacroExpansionUniqueName(
11151115
return mangleChildNodes(node, depth + 1);
11161116
}
11171117

1118+
ManglingError Remangler::mangleMacroExpansionLoc(
1119+
Node *node, unsigned depth) {
1120+
return MANGLING_ERROR(ManglingError::UnsupportedNodeKind, node);
1121+
}
1122+
11181123
ManglingError Remangler::mangleAccessor(Node *storageNode,
11191124
StringRef accessorCode,
11201125
EntityContext &ctx, unsigned depth) {

lib/Demangling/Remangler.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3103,6 +3103,21 @@ ManglingError Remangler::mangle##Name##AttachedMacroExpansion( \
31033103
}
31043104
#include "swift/Basic/MacroRoles.def"
31053105

3106+
ManglingError Remangler::mangleMacroExpansionLoc(
3107+
Node *node, unsigned depth) {
3108+
RETURN_IF_ERROR(mangleChildNode(node, 0, depth + 1));
3109+
RETURN_IF_ERROR(mangleChildNode(node, 1, depth + 1));
3110+
3111+
auto line = node->getChild(2)->getIndex();
3112+
auto col = node->getChild(3)->getIndex();
3113+
3114+
Buffer << "fMX";
3115+
mangleIndex(line);
3116+
mangleIndex(col);
3117+
3118+
return ManglingError::Success;
3119+
}
3120+
31063121
ManglingError Remangler::mangleMacroExpansionUniqueName(
31073122
Node *node, unsigned depth) {
31083123
RETURN_IF_ERROR(mangleChildNode(node, 0, depth + 1));
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// RUN: %target-swift-frontend -emit-silgen %s -disable-availability-checking
2+
3+
public func hasIsolatedParam<T>(isolation: isolated (any Actor)? = #isolation) async -> T {}
4+
5+
func callee<T>(_: @autoclosure () -> T, _: @autoclosure () -> T) {}
6+
7+
func outer() async {
8+
func inner() async -> String {
9+
let x = #isolation
10+
return await hasIsolatedParam()
11+
}
12+
13+
var value = await inner()
14+
callee(value, "hi")
15+
}

test/Demangle/Inputs/manglings.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,3 +473,5 @@ $sSRyxG15Synchronization19AtomicRepresentableABRi0_zrlMc ---> protocol conforman
473473
$sSRyxG15Synchronization19AtomicRepresentableABRi1_zrlMc ---> protocol conformance descriptor for < where A: ~Swift.<bit 2>> Swift.UnsafeBufferPointer<A> : Synchronization.AtomicRepresentable in Synchronization
474474

475475
$s23variadic_generic_opaque2G2VyAA2S1V_AA2S2VQPGAA1PHPAeA1QHPyHC_AgaJHPyHCHX_HC ---> concrete protocol conformance variadic_generic_opaque.G2<Pack{variadic_generic_opaque.S1, variadic_generic_opaque.S2}> to protocol conformance ref (type's module) variadic_generic_opaque.P with conditional requirements: (pack protocol conformance (concrete protocol conformance variadic_generic_opaque.S1 to protocol conformance ref (type's module) variadic_generic_opaque.Q, concrete protocol conformance variadic_generic_opaque.S2 to protocol conformance ref (type's module) variadic_generic_opaque.Q))
476+
477+
$s9MacroUser0023macro_expandswift_elFCffMX436_4_23bitwidthNumberedStructsfMf_ ---> freestanding macro expansion #1 of bitwidthNumberedStructs in module MacroUser file macro_expand.swift line 437 column 5

test/Macros/freestanding_multifile.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ macro anonymousTypes(public: Bool, _: () -> String) = #externalMacro(module: "Ma
88

99
// RUN: %target-swift-frontend -swift-version 5 -emit-ir -load-plugin-library %t/%target-library-name(MacroDefinition) -module-name MacroUser %S/Inputs/AnonTypes1.swift %S/Inputs/AnonTypes2.swift %s -o - -g | %FileCheck --check-prefix CHECK-IR %s
1010

11-
// CHECK-IR: $s9MacroUser33{{.*}}14anonymousTypesfMf_4namefMu_
12-
// CHECK-IR-NOT: $s9MacroUser33{{.*}}14anonymousTypesfMf0_4namefMu_
13-
// CHECK-IR: $s9MacroUser33{{.*}}14anonymousTypesfMf_4namefMu_
14-
// CHECK-IR-NOT: $s9MacroUser33{{.*}}14anonymousTypesfMf0_4namefMu_
11+
// CHECK-IR: $s9MacroUser{{.*}}fMX{{.*}}_33{{.*}}14anonymousTypesfMf_4namefMu_
12+
// CHECK-IR-NOT: $s9MacroUser{{.*}}fMX{{.*}}_33{{.*}}14anonymousTypesfMf0_4namefMu_
13+
// CHECK-IR: $s9MacroUser{{.*}}fMX{{.*}}_33{{.*}}14anonymousTypesfMf_4namefMu_
14+
// CHECK-IR-NOT: $s9MacroUser{{.*}}fMX{{.*}}_33{{.*}}14anonymousTypesfMf0_4namefMu_

0 commit comments

Comments
 (0)