Skip to content

Commit 7ffce19

Browse files
authored
Merge pull request #68086 from DougGregor/macro-expansion-with-operators-5.9
[5.9] [Macros] Ensure that we find operators in any macro-expanded context.
2 parents 31e1783 + 48624c6 commit 7ffce19

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1327,6 +1327,13 @@ static bool contextMayExpandOperator(
13271327
else
13281328
return false;
13291329

1330+
// If the context declaration itself is within a macro expansion, it may
1331+
// have an operator.
1332+
if (auto sourceFile = dc->getParentSourceFile()) {
1333+
if (sourceFile->getFulfilledMacroRole())
1334+
return true;
1335+
}
1336+
13301337
ASTContext &ctx = dc->getASTContext();
13311338
auto potentialExpansions = evaluateOrDefault(
13321339
ctx.evaluator, PotentialMacroExpansionsInContextRequest{decl},

test/Macros/Inputs/syntax_macro_definitions.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1623,6 +1623,28 @@ public struct DefineStructWithUnqualifiedLookupMacro: DeclarationMacro {
16231623
}
16241624
}
16251625

1626+
public struct DefineComparableTypeMacro: DeclarationMacro {
1627+
public static func expansion(
1628+
of node: some FreestandingMacroExpansionSyntax,
1629+
in context: some MacroExpansionContext
1630+
) throws -> [DeclSyntax] {
1631+
return ["""
1632+
struct ComparableType: Comparable {
1633+
static func <(lhs: ComparableType, rhs: ComparableType) -> Bool {
1634+
return false
1635+
}
1636+
1637+
enum Inner: String, Comparable {
1638+
case hello = "hello"
1639+
static func <(lhs: ComparableType.Inner, rhs: ComparableType.Inner) -> Bool {
1640+
return lhs.rawValue < rhs.rawValue
1641+
}
1642+
}
1643+
}
1644+
"""]
1645+
}
1646+
}
1647+
16261648
public struct AddMemberWithFixIt: MemberMacro {
16271649
public static func expansion<
16281650
Declaration: DeclGroupSyntax, Context: MacroExpansionContext

test/Macros/macro_expand.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,3 +566,11 @@ struct DeprecatedStructWrapper {
566566
#deprecatedStringifyAsDeclMacro(1 + 1)
567567
}
568568
#endif
569+
570+
571+
@freestanding(declaration, names: named(ComparableType))
572+
macro DefineComparableType() = #externalMacro(module: "MacroDefinition", type: "DefineComparableTypeMacro")
573+
574+
struct HasNestedType {
575+
#DefineComparableType
576+
}

0 commit comments

Comments
 (0)