Skip to content

Commit c3693f2

Browse files
authored
Merge pull request #63283 from hborla/member-macro-parse-decl-options
[Parser] Use proper `ParseDeclOptions` when parsing the result of a member macro expansion.
2 parents 0b4e539 + e6d7ea5 commit c3693f2

File tree

6 files changed

+50
-1
lines changed

6 files changed

+50
-1
lines changed

include/swift/AST/DiagnosticsParse.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2049,6 +2049,9 @@ WARNING(macro_expression_attribute_removed,PointsToFirstBadToken,
20492049
ERROR(unexpected_attribute_expansion,PointsToFirstBadToken,
20502050
"unexpected token '%0' in expanded attribute list'",
20512051
(StringRef))
2052+
ERROR(unexpected_member_expansion,PointsToFirstBadToken,
2053+
"unexpected token '%0' in expanded member list'",
2054+
(StringRef))
20522055

20532056
ERROR(parser_round_trip_error,none,
20542057
"source file did not round-trip through the new Swift parser", ())

include/swift/Parse/Parser.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1235,6 +1235,10 @@ class Parser {
12351235
/// the attribute list attached.
12361236
void parseExpandedAttributeList(SmallVectorImpl<ASTNode> &items);
12371237

1238+
/// Parse the result of member macro expansion, which is a floating
1239+
/// member list.
1240+
void parseExpandedMemberList(SmallVectorImpl<ASTNode> &items);
1241+
12381242
ParserResult<FuncDecl> parseDeclFunc(SourceLoc StaticLoc,
12391243
StaticSpellingKind StaticSpelling,
12401244
ParseDeclOptions Flags,

lib/Parse/ParseDecl.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7098,6 +7098,31 @@ void Parser::parseExpandedAttributeList(SmallVectorImpl<ASTNode> &items) {
70987098
return;
70997099
}
71007100

7101+
void Parser::parseExpandedMemberList(SmallVectorImpl<ASTNode> &items) {
7102+
Optional<DiagnosticTransaction> transaction;
7103+
parseSourceFileViaASTGen(items, transaction, /*suppressDiagnostics*/true);
7104+
7105+
if (Tok.is(tok::NUM_TOKENS))
7106+
consumeTokenWithoutFeedingReceiver();
7107+
7108+
auto *decl = CurDeclContext->getAsDecl();
7109+
auto *idc = dyn_cast<IterableDeclContext>(decl);
7110+
bool previousHadSemi = true;
7111+
7112+
while (!Tok.is(tok::eof)) {
7113+
parseDeclItem(previousHadSemi,
7114+
getMemberParseDeclOptions(idc),
7115+
[&](Decl *d) { items.push_back(d); });
7116+
}
7117+
7118+
// Consume remaining tokens.
7119+
while (!Tok.is(tok::eof)) {
7120+
diagnose(Tok.getLoc(), diag::unexpected_member_expansion,
7121+
Tok.getText());
7122+
consumeToken();
7123+
}
7124+
}
7125+
71017126
/// Parse the brace-enclosed getter and setter for a variable.
71027127
ParserResult<VarDecl>
71037128
Parser::parseDeclVarGetSet(PatternBindingEntry &entry, ParseDeclOptions Flags,

lib/Parse/ParseRequests.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,13 +174,17 @@ SourceFileParsingResult ParseSourceFileRequest::evaluate(Evaluator &evaluator,
174174
switch (generatedInfo->kind) {
175175
case GeneratedSourceInfo::FreestandingDeclMacroExpansion:
176176
case GeneratedSourceInfo::ExpressionMacroExpansion:
177-
case GeneratedSourceInfo::MemberMacroExpansion:
178177
case GeneratedSourceInfo::ReplacedFunctionBody:
179178
case GeneratedSourceInfo::PrettyPrinted: {
180179
parser.parseTopLevelItems(items);
181180
break;
182181
}
183182

183+
case GeneratedSourceInfo::MemberMacroExpansion: {
184+
parser.parseExpandedMemberList(items);
185+
break;
186+
}
187+
184188
case GeneratedSourceInfo::AccessorMacroExpansion: {
185189
ASTNode astNode = ASTNode::getFromOpaqueValue(generatedInfo->astNode);
186190
auto attachedDecl = astNode.get<Decl *>();

test/Macros/Inputs/syntax_macro_definitions.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,10 +373,22 @@ public struct AddMembers: MemberMacro {
373373
}
374374
"""
375375

376+
let staticMethod: DeclSyntax =
377+
"""
378+
static func method() {}
379+
"""
380+
381+
let initDecl: DeclSyntax =
382+
"""
383+
init() {}
384+
"""
385+
376386
return [
377387
storageStruct,
378388
storageVariable,
379389
instanceMethod,
390+
staticMethod,
391+
initDecl,
380392
]
381393
}
382394
}

test/Macros/macro_expand_synthesized_members.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
@addMembers
1515
struct S {
1616
func useSynthesized() {
17+
S.method()
1718
print(type(of: getStorage()))
1819
}
1920
}

0 commit comments

Comments
 (0)