Skip to content

Commit 26786c1

Browse files
committed
[Macros] Make macro expansions always have an argument list. If an explicit
argument list isn't written, an implicit, empty list is created.
1 parent 7c50f95 commit 26786c1

File tree

6 files changed

+30
-28
lines changed

6 files changed

+30
-28
lines changed

include/swift/AST/Expr.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6086,9 +6086,16 @@ class MacroExpansionExpr final : public Expr {
60866086
MacroName(macroName), MacroNameLoc(macroNameLoc),
60876087
LeftAngleLoc(leftAngleLoc), RightAngleLoc(rightAngleLoc),
60886088
GenericArgs(genericArgs),
6089-
ArgList(argList),
60906089
Rewritten(nullptr), Roles(roles) {
60916090
Bits.MacroExpansionExpr.Discriminator = InvalidDiscriminator;
6091+
6092+
// Macro expansions always have an argument list. If one is not provided, create
6093+
// an implicit one.
6094+
if (argList) {
6095+
ArgList = argList;
6096+
} else {
6097+
ArgList = ArgumentList::createImplicit(dc->getASTContext(), {});
6098+
}
60926099
}
60936100

60946101
DeclNameRef getMacroName() const { return MacroName; }

lib/Sema/CSDiagnostics.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4634,7 +4634,8 @@ ASTNode MissingArgumentsFailure::getAnchor() const {
46344634
}
46354635

46364636
SourceLoc MissingArgumentsFailure::getLoc() const {
4637-
if (auto *argList = getArgumentListFor(getLocator()))
4637+
auto *argList = getArgumentListFor(getLocator());
4638+
if (argList && !argList->isImplicit())
46384639
return argList->getLoc();
46394640
return FailureDiagnostic::getLoc();
46404641
}
@@ -4729,17 +4730,25 @@ bool MissingArgumentsFailure::diagnoseAsError() {
47294730

47304731
// TODO(diagnostics): We should be able to suggest this fix-it
47314732
// unconditionally.
4732-
if (args && args->empty()) {
4733-
SmallString<32> scratch;
4734-
llvm::raw_svector_ostream fixIt(scratch);
4733+
SmallString<32> scratch;
4734+
llvm::raw_svector_ostream fixIt(scratch);
4735+
auto appendMissingArgsToFix = [&]() {
47354736
interleave(
47364737
SynthesizedArgs,
47374738
[&](const SynthesizedArg &arg) {
47384739
forFixIt(fixIt, arg.param);
47394740
},
47404741
[&] { fixIt << ", "; });
4742+
};
47414743

4744+
if (args && args->empty() && !args->isImplicit()) {
4745+
appendMissingArgsToFix();
47424746
diag.fixItInsertAfter(args->getLParenLoc(), fixIt.str());
4747+
} else if (isExpr<MacroExpansionExpr>(getRawAnchor())) {
4748+
fixIt << "(";
4749+
appendMissingArgsToFix();
4750+
fixIt << ")";
4751+
diag.fixItInsertAfter(getRawAnchor().getEndLoc(), fixIt.str());
47434752
}
47444753

47454754
diag.flush();

lib/Sema/CSGen.cpp

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3799,16 +3799,11 @@ namespace {
37993799
auto &ctx = CS.getASTContext();
38003800
auto locator = CS.getConstraintLocator(expr);
38013801

3802-
// For calls, set up the argument list.
3803-
bool isCall = expr->getArgs() != nullptr;
3804-
if (isCall) {
3805-
CS.associateArgumentList(locator, expr->getArgs());
3806-
}
3802+
CS.associateArgumentList(locator, expr->getArgs());
38073803

38083804
// Look up the macros with this name.
38093805
auto macroIdent = expr->getMacroName().getBaseIdentifier();
3810-
FunctionRefKind functionRefKind = isCall ? FunctionRefKind::SingleApply
3811-
: FunctionRefKind::Unapplied;
3806+
FunctionRefKind functionRefKind = FunctionRefKind::SingleApply;
38123807
auto macros = lookupMacros(
38133808
macroIdent, expr->getMacroNameLoc().getBaseNameLoc(),
38143809
functionRefKind, expr->getMacroRoles());
@@ -3831,11 +3826,7 @@ namespace {
38313826
return Type();
38323827
}
38333828

3834-
// For non-calls, the type variable is the result.
3835-
if (!isCall)
3836-
return macroRefType;
3837-
3838-
// For calls, form the applicable-function constraint. The result type
3829+
// Form the applicable-function constraint. The result type
38393830
// is the result of that call.
38403831
SmallVector<AnyFunctionType::Param, 8> params;
38413832
getMatchingParams(expr->getArgs(), params);

lib/Sema/TypeCheckMacros.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,11 +1257,6 @@ ResolveMacroRequest::evaluate(Evaluator &evaluator,
12571257
if (foundMacros.empty())
12581258
return nullptr;
12591259

1260-
// Extract macro arguments, or create an empty list.
1261-
auto *argList = macroRef.getArgs();
1262-
if (!argList)
1263-
argList = ArgumentList::createImplicit(ctx, {});
1264-
12651260
// If we already have a MacroExpansionExpr, use that. Otherwise,
12661261
// create one.
12671262
MacroExpansionExpr *macroExpansion;
@@ -1273,7 +1268,7 @@ ResolveMacroRequest::evaluate(Evaluator &evaluator,
12731268
dc, macroRef.getSigilLoc(), macroRef.getMacroName(),
12741269
macroRef.getMacroNameLoc(), genericArgsRange.Start,
12751270
macroRef.getGenericArgs(), genericArgsRange.End,
1276-
argList, roles);
1271+
macroRef.getArgs(), roles);
12771272
}
12781273

12791274
Expr *result = macroExpansion;

test/Macros/macro_expand.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
// FIXME: Swift parser is not enabled on Linux CI yet.
2020
// REQUIRES: OS=macosx
2121

22-
@freestanding(expression) macro customFileID: String = #externalMacro(module: "MacroDefinition", type: "FileIDMacro")
22+
@freestanding(expression) macro customFileID() -> String = #externalMacro(module: "MacroDefinition", type: "FileIDMacro")
2323
@freestanding(expression) macro stringify<T>(_ value: T) -> (T, String) = #externalMacro(module: "MacroDefinition", type: "StringifyMacro")
24-
@freestanding(expression) macro fileID<T: ExpressibleByStringLiteral>: T = #externalMacro(module: "MacroDefinition", type: "FileIDMacro")
24+
@freestanding(expression) macro fileID<T: ExpressibleByStringLiteral>() -> T = #externalMacro(module: "MacroDefinition", type: "FileIDMacro")
2525
@freestanding(expression) macro recurse(_: Bool) = #externalMacro(module: "MacroDefinition", type: "RecursiveMacro")
2626

2727
func testFileID(a: Int, b: Int) {
@@ -116,7 +116,7 @@ func testAddBlocker(a: Int, b: Int, c: Int, oa: OnlyAdds) {
116116
}
117117

118118
// Make sure we don't crash with declarations produced by expansions.
119-
@freestanding(expression) macro nestedDeclInExpr: () -> Void = #externalMacro(module: "MacroDefinition", type: "NestedDeclInExprMacro")
119+
@freestanding(expression) macro nestedDeclInExpr() -> () -> Void = #externalMacro(module: "MacroDefinition", type: "NestedDeclInExprMacro")
120120

121121
func testNestedDeclInExpr() {
122122
let _: () -> Void = #nestedDeclInExpr

test/Macros/macros_diagnostics.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func overloaded1(_ p: Any) { }
6464
// expected-warning@-1{{external macro implementation type}}
6565

6666
@freestanding(expression) macro intIdentity(value: Int, _: Float) -> Int = #externalMacro(module: "MissingModule", type: "MissingType")
67-
// expected-note@-1{{macro 'intIdentity(value:_:)' declared here}}
67+
// expected-note@-1{{'intIdentity(value:_:)' declared here}}
6868
// expected-warning@-2{{external macro implementation type}}
6969

7070
@freestanding(declaration) macro unaryDeclMacro(_ x: String)
@@ -94,7 +94,7 @@ func testDiags(a: Int, b: Int) {
9494
_ = stringify(a + b)
9595
// expected-error@-1{{expansion of macro 'stringify' requires leading '#'}}{{7-7=#}}
9696

97-
_ = #intIdentity // expected-error{{expansion of macro 'intIdentity(value:_:)' requires arguments}}{{19-19=(value: <#Int#>, <#Float#>)}}
97+
_ = #intIdentity // expected-error{{missing arguments for parameters 'value', #2 in macro expansion}}{{19-19=(value: <#Int#>, <#Float#>)}}
9898

9999
overloaded1(a) // okay, calls the function
100100
#overloaded1(a) // expected-error{{argument type 'Int' does not conform to expected type 'P'}}

0 commit comments

Comments
 (0)