Skip to content

Commit 14f1172

Browse files
committed
[ASTScopeCreation] Teach ASTScope to find the enclosing type context for
synthesized member macro expansion buffers.
1 parent e04a791 commit 14f1172

File tree

4 files changed

+27
-9
lines changed

4 files changed

+27
-9
lines changed

lib/AST/ASTScopeCreation.cpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,28 @@ ASTSourceFileScope::ASTSourceFileScope(SourceFile *SF,
248248
ScopeCreator *scopeCreator)
249249
: SF(SF), scopeCreator(scopeCreator) {
250250
if (auto enclosingSF = SF->getEnclosingSourceFile()) {
251-
SourceLoc parentLoc = SF->getMacroExpansion().getStartLoc();
251+
SourceLoc parentLoc;
252+
auto macroRole = SF->getFulfilledMacroRole();
253+
auto expansion = SF->getMacroExpansion();
254+
255+
// Determine the parent source location based on the macro role.
256+
switch (*macroRole) {
257+
case MacroRole::Expression:
258+
case MacroRole::FreestandingDeclaration:
259+
case MacroRole::Accessor:
260+
case MacroRole::MemberAttribute:
261+
parentLoc = expansion.getStartLoc();
262+
break;
263+
case MacroRole::SynthesizedMembers: {
264+
// For synthesized member macros, take the end loc of the
265+
// enclosing declaration (before the closing brace), because
266+
// the macro expansion is inside this scope.
267+
auto *decl = expansion.getAsDeclContext()->getAsDecl();
268+
parentLoc = decl->getEndLoc();
269+
break;
270+
}
271+
}
272+
252273
if (auto parentScope = findStartingScopeForLookup(enclosingSF, parentLoc)) {
253274
parentAndWasExpanded.setPointer(const_cast<ASTScopeImpl *>(parentScope));
254275
}

lib/Sema/TypeCheckMacros.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1251,7 +1251,6 @@ bool swift::expandSynthesizedMembers(CustomAttr *attr, MacroDecl *macro,
12511251
extension->addMember(member);
12521252
}
12531253

1254-
TypeChecker::typeCheckDecl(member);
12551254
synthesizedMembers = true;
12561255
}
12571256

test/Macros/Inputs/syntax_macro_definitions.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,12 +355,12 @@ public struct AddMembers: MemberDeclarationMacro {
355355
) throws -> [DeclSyntax] {
356356
let storageStruct: StructDeclSyntax =
357357
"""
358-
struct Nested {}
358+
struct Storage {}
359359
"""
360360

361361
let storageVariable: VariableDeclSyntax =
362362
"""
363-
var value: Int = 0
363+
var storage = Storage()
364364
"""
365365

366366
let instanceMethod: FunctionDeclSyntax =

test/Macros/macro_expand_synthesized_members.swift

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,13 @@
1414
@addMembers
1515
struct S {
1616
func useSynthesized() {
17-
print(s.value)
18-
print(S.Nested())
19-
s.method()
17+
print(type(of: storage))
18+
method()
2019
}
2120
}
2221

2322
let s = S()
2423

25-
// CHECK: 0
26-
// CHECK: Nested
24+
// CHECK: Storage
2725
// CHECK: synthesized method
2826
s.useSynthesized()

0 commit comments

Comments
 (0)