Skip to content

Commit 2c08dc2

Browse files
authored
Merge pull request #65468 from DougGregor/macro-mangle-not-initializer
2 parents aff5b20 + 101f018 commit 2c08dc2

File tree

5 files changed

+51
-0
lines changed

5 files changed

+51
-0
lines changed

include/swift/AST/MacroDiscriminatorContext.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ struct MacroDiscriminatorContext
3131
static MacroDiscriminatorContext getParentOf(
3232
SourceLoc loc, DeclContext *origDC
3333
);
34+
35+
/// Return the innermost declaration context that is suitable for
36+
/// use in identifying a macro.
37+
static DeclContext *getInnermostMacroContext(DeclContext *dc);
3438
};
3539

3640
}

lib/AST/ASTMangler.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3845,6 +3845,8 @@ std::string ASTMangler::mangleRuntimeAttributeGeneratorEntity(
38453845
void ASTMangler::appendMacroExpansionContext(
38463846
SourceLoc loc, DeclContext *origDC
38473847
) {
3848+
origDC = MacroDiscriminatorContext::getInnermostMacroContext(origDC);
3849+
38483850
if (loc.isInvalid())
38493851
return appendContext(origDC, StringRef());
38503852

lib/AST/Decl.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10490,9 +10490,50 @@ ArrayRef<CustomAttr *> Decl::getRuntimeDiscoverableAttrs() const {
1049010490
nullptr);
1049110491
}
1049210492

10493+
/// Adjust the declaration context to find a point in the context hierarchy
10494+
/// that the macro can be anchored on.
10495+
DeclContext *
10496+
MacroDiscriminatorContext::getInnermostMacroContext(DeclContext *dc) {
10497+
switch (dc->getContextKind()) {
10498+
case DeclContextKind::SubscriptDecl:
10499+
case DeclContextKind::EnumElementDecl:
10500+
case DeclContextKind::AbstractFunctionDecl:
10501+
case DeclContextKind::SerializedLocal:
10502+
case DeclContextKind::Package:
10503+
case DeclContextKind::Module:
10504+
case DeclContextKind::FileUnit:
10505+
case DeclContextKind::GenericTypeDecl:
10506+
case DeclContextKind::ExtensionDecl:
10507+
case DeclContextKind::MacroDecl:
10508+
// These contexts are always fine
10509+
return dc;
10510+
10511+
case DeclContextKind::TopLevelCodeDecl:
10512+
// For top-level code, use the enclosing source file as the context.
10513+
return getInnermostMacroContext(dc->getParent());
10514+
10515+
case DeclContextKind::AbstractClosureExpr: {
10516+
// For closures, we can mangle the closure if we're in a context we can
10517+
// mangle. Check that context.
10518+
auto adjustedParentDC = getInnermostMacroContext(dc->getParent());
10519+
if (adjustedParentDC == dc->getParent())
10520+
return dc;
10521+
10522+
return adjustedParentDC;
10523+
}
10524+
10525+
case DeclContextKind::Initializer:
10526+
// Initializers can be part of inferring types for variables, so we need
10527+
// their context.
10528+
return getInnermostMacroContext(dc->getParent());
10529+
}
10530+
}
10531+
1049310532
/// Retrieve the parent discriminator context for the given macro.
1049410533
MacroDiscriminatorContext MacroDiscriminatorContext::getParentOf(
1049510534
SourceLoc loc, DeclContext *origDC) {
10535+
origDC = getInnermostMacroContext(origDC);
10536+
1049610537
if (loc.isInvalid())
1049710538
return origDC;
1049810539

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
#anonymousTypes { "hello2" }
2+
3+
var globalVar = #stringify(1 + 1)

test/Macros/top_level_freestanding.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,5 @@ struct HasInnerClosure {
6464
func testArbitraryAtGlobal() {
6565
_ = MyIntGlobal16()
6666
}
67+
68+
@freestanding(expression) macro stringify<T>(_ value: T) -> (T, String) = #externalMacro(module: "MacroDefinition", type: "StringifyMacro")

0 commit comments

Comments
 (0)