Skip to content

Commit 9dce7c5

Browse files
committed
[Diagnostics] Extend fix-it if no parenthesis and no args
1 parent 099eb46 commit 9dce7c5

File tree

2 files changed

+40
-11
lines changed

2 files changed

+40
-11
lines changed

lib/Sema/CSDiagnostics.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5338,9 +5338,16 @@ bool MissingArgumentsFailure::diagnoseSingleMissingArgument() const {
53385338
// fn(argX, argY):
53395339
// fn(argX, argY[, argMissing])
53405340
if (args->empty()) {
5341-
if (!args->getRParenLoc().isValid())
5342-
return false;
5343-
insertLoc = args->getRParenLoc();
5341+
if (args->getRParenLoc().isInvalid()) {
5342+
// Extend fix-it if no parenthesis and no args
5343+
insertBuf.insert(insertBuf.begin(), '(');
5344+
insertBuf.insert(insertBuf.end(), ')');
5345+
insertLoc =
5346+
Lexer::getLocForEndOfToken(ctx.SourceMgr, fnExpr->getEndLoc());
5347+
if (insertLoc.isInvalid())
5348+
return false;
5349+
} else
5350+
insertLoc = args->getRParenLoc();
53445351
} else if (position != 0) {
53455352
auto argPos = std::min(args->size(), position) - 1;
53465353
insertLoc = Lexer::getLocForEndOfToken(

test/Macros/attached_macros_diags.swift

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,40 @@
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{{'m4(_:label2:)' declared here}}
21+
// expected-note@-2{{'m4(_:label2:)' declared here}}
22+
// expected-note@-3{{'m4(_:label2:)' declared here}}
23+
24+
@attached(peer) macro m5(label1: Int, label2: String) = #externalMacro(module: "MacroDefinition", type: "EmptyPeerMacro")
25+
// expected-note@-1{{'m5(label1:label2:)' declared here}}
26+
27+
@attached(peer) macro m6(label: Int = 42) = #externalMacro(module: "MacroDefinition", type: "EmptyPeerMacro")
1828

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

2333
@m1 struct X1 { }
2434

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

2745
// Check for nesting rules.
2846
struct SkipNestedType {
@@ -54,6 +72,10 @@ struct TestMacroArgs {
5472

5573
@m2(Nested.x) struct Args5 {}
5674

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

@@ -62,15 +84,15 @@ struct TestMacroArgs {
6284
@m2(Nested.x) struct Args2 {}
6385
}
6486

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

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

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

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

0 commit comments

Comments
 (0)