Skip to content

Commit 8b2a3a4

Browse files
authored
Merge pull request #77226 from JanBaig/issue-macro-diag
[Diagnostics] Extend fix-it logic for misssing arguments without parenthesis
2 parents 8960144 + a310af8 commit 8b2a3a4

File tree

2 files changed

+38
-12
lines changed

2 files changed

+38
-12
lines changed

lib/Sema/CSDiagnostics.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5365,7 +5365,16 @@ bool MissingArgumentsFailure::diagnoseSingleMissingArgument() const {
53655365
// fn(argX, argY):
53665366
// fn(argX, argY[, argMissing])
53675367
if (args->empty()) {
5368-
insertLoc = args->getRParenLoc();
5368+
if (args->getRParenLoc().isInvalid()) {
5369+
// Extend fix-it if no parenthesis and no args
5370+
insertBuf.insert(insertBuf.begin(), '(');
5371+
insertBuf.insert(insertBuf.end(), ')');
5372+
insertLoc =
5373+
Lexer::getLocForEndOfToken(ctx.SourceMgr, fnExpr->getEndLoc());
5374+
if (insertLoc.isInvalid())
5375+
return false;
5376+
} else
5377+
insertLoc = args->getRParenLoc();
53695378
} else if (position != 0) {
53705379
auto argPos = std::min(args->size(), position) - 1;
53715380
insertLoc = Lexer::getLocForEndOfToken(
@@ -5404,9 +5413,6 @@ bool MissingArgumentsFailure::diagnoseSingleMissingArgument() const {
54045413
}
54055414
}
54065415

5407-
if (insertLoc.isInvalid())
5408-
return false;
5409-
54105416
// If we are trying to insert a trailing closure but the parameter
54115417
// corresponding to the missing argument doesn't support a trailing closure,
54125418
// don't provide a Fix-It.

test/Macros/attached_macros_diags.swift

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,38 @@
77
@attached(peer) macro m1() = #externalMacro(module: "MacroDefinition", type: "EmptyPeerMacro")
88

99
@attached(peer) macro m2(_: Int) = #externalMacro(module: "MacroDefinition", type: "EmptyPeerMacro")
10-
// expected-note@-1{{candidate has partially matching parameter list (Int)}}
10+
// expected-note@-1{{'m2' declared here}}
1111
// expected-note@-2{{candidate expects value of type 'Int' for parameter #1 (got 'String')}}
1212

1313
@attached(peer) macro m2(_: Double) = #externalMacro(module: "MacroDefinition", type: "EmptyPeerMacro")
14-
// expected-note@-1{{candidate has partially matching parameter list (Double)}}
15-
// expected-note@-2{{candidate expects value of type 'Double' for parameter #1 (got 'String')}}
14+
// expected-note@-1{{candidate expects value of type 'Double' for parameter #1 (got 'String')}}
1615

1716
@attached(peer) macro m3(message: String) = #externalMacro(module: "MacroDefinition", type: "EmptyPeerMacro")
17+
// expected-note@-1{{'m3(message:)' declared here}}
18+
19+
@attached(peer) macro m4(_ param1: Int, label2 param2: String) = #externalMacro(module: "MacroDefinition", type: "EmptyPeerMacro")
20+
// expected-note@-1 3 {{'m4(_:label2:)' declared here}}
21+
22+
@attached(peer) macro m5(label1: Int, label2: String) = #externalMacro(module: "MacroDefinition", type: "EmptyPeerMacro")
23+
// expected-note@-1{{'m5(label1:label2:)' declared here}}
24+
25+
@attached(peer) macro m6(label: Int = 42) = #externalMacro(module: "MacroDefinition", type: "EmptyPeerMacro")
1826

1927
@freestanding(expression) macro stringify<T>(_ value: T) -> (T, String) = #externalMacro(module: "MyMacros", type: "StringifyMacro")
2028
// expected-warning@-1{{external macro implementation type 'MyMacros.StringifyMacro' could not be found for macro 'stringify'}}
2129
// expected-note@-2{{'stringify' declared here}}
2230

2331
@m1 struct X1 { }
2432

25-
@m2 struct X2 { } // expected-error{{no exact matches in call to macro 'm2'}}
33+
@m2 struct X2 { } // expected-error{{missing argument for parameter #1 in macro expansion}}{{4-4=(<#Int#>)}}
34+
35+
@m3 struct X3 { } // expected-error{{missing argument for parameter 'message' in macro expansion}}{{4-4=(message: <#String#>)}}
36+
37+
@m4 struct X4 { } // expected-error{{missing arguments for parameters #1, 'label2' in macro expansion}}{{4-4=(<#Int#>, label2: <#String#>)}}
38+
39+
@m5 struct X5 { } // expected-error{{missing arguments for parameters 'label1', 'label2' in macro expansion}}{{4-4=(label1: <#Int#>, label2: <#String#>)}}
40+
41+
@m6 struct X6 { }
2642

2743
// Check for nesting rules.
2844
struct SkipNestedType {
@@ -54,6 +70,10 @@ struct TestMacroArgs {
5470

5571
@m2(Nested.x) struct Args5 {}
5672

73+
@m4(10) struct Args6 { } // expected-error{{missing argument for parameter 'label2' in macro expansion}}{{9-9=, label2: <#String#>}}
74+
75+
@m4(label2: "test") struct Args7 { } // expected-error{{missing argument for parameter #1 in macro expansion}}{{7-7=<#Int#>, }}
76+
5777
struct Nested {
5878
static let x = 10
5979

@@ -62,15 +82,15 @@ struct TestMacroArgs {
6282
@m2(Nested.x) struct Args2 {}
6383
}
6484

65-
@m3(message: stringify(Nested.x).1) struct Args6 {}
85+
@m3(message: stringify(Nested.x).1) struct Args8 {}
6686
// expected-error@-1{{expansion of macro 'stringify' requires leading '#'}}
6787

68-
@m3(message: #stringify().1) struct Args7 {}
88+
@m3(message: #stringify().1) struct Args9 {}
6989
// expected-error@-1{{missing argument for parameter #1 in macro expansion}}
7090

71-
@m3(message: #stringify(Nested.x).1) struct Args8 {}
91+
@m3(message: #stringify(Nested.x).1) struct Args10 {}
7292

7393
// Allow macros to have arbitrary generic specialization lists, but warn
7494
// https://github.com/swiftlang/swift/issues/75500
75-
@m1<UInt> struct Args9 {} // expected-warning {{cannot specialize a non-generic external macro 'm1()'}}
95+
@m1<UInt> struct Args11 {} // expected-warning {{cannot specialize a non-generic external macro 'm1()'}}
7696
}

0 commit comments

Comments
 (0)