Skip to content

Commit b5627c9

Browse files
authored
Merge pull request #85145 from a7medev/autodiff-comma-fix-its
[Diagnostics] Add missing fix-its for unexpected/expected comma in attribute arguments
2 parents 43e9bcb + d0c2d8b commit b5627c9

File tree

6 files changed

+25
-20
lines changed

6 files changed

+25
-20
lines changed

lib/Parse/ParseDecl.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1426,7 +1426,8 @@ bool Parser::parseDifferentiableAttributeArguments(
14261426
if (!consumeIf(tok::comma)) return false;
14271427
// Diagnose trailing comma before 'where' or ')'.
14281428
if (Tok.is(tok::kw_where) || Tok.is(tok::r_paren)) {
1429-
diagnose(Tok, diag::unexpected_separator, ",");
1429+
diagnose(Tok, diag::unexpected_separator, ",")
1430+
.fixItRemove(PreviousLoc);
14301431
return true;
14311432
}
14321433
// Check that token after comma is 'wrt'.
@@ -1668,14 +1669,16 @@ ParserResult<DerivativeAttr> Parser::parseDerivativeAttribute(SourceLoc atLoc,
16681669
// If comma is required but does not exist and ')' has not been reached,
16691670
// diagnose missing comma.
16701671
if (requireComma && !Tok.is(tok::r_paren)) {
1671-
diagnose(getEndOfPreviousLoc(), diag::expected_separator, ",");
1672+
diagnose(getEndOfPreviousLoc(), diag::expected_separator, ",")
1673+
.fixItInsertAfter(PreviousLoc, ",");
16721674
return true;
16731675
}
16741676
return false;
16751677
}
16761678
// Diagnose trailing comma before ')'.
16771679
if (Tok.is(tok::r_paren)) {
1678-
diagnose(Tok, diag::unexpected_separator, ",");
1680+
diagnose(Tok, diag::unexpected_separator, ",")
1681+
.fixItRemove(PreviousLoc);
16791682
return errorAndSkipUntilConsumeRightParen(*this, AttrName);
16801683
}
16811684
// Check that token after comma is 'wrt:'.
@@ -1748,14 +1751,16 @@ ParserResult<TransposeAttr> Parser::parseTransposeAttribute(SourceLoc atLoc,
17481751
// If comma is required but does not exist and ')' has not been reached,
17491752
// diagnose missing comma.
17501753
if (requireComma && !Tok.is(tok::r_paren)) {
1751-
diagnose(Tok, diag::expected_separator, ",");
1754+
diagnose(Tok, diag::expected_separator, ",")
1755+
.fixItInsertAfter(PreviousLoc, ",");
17521756
return true;
17531757
}
17541758
return false;
17551759
}
17561760
// Diagnose trailing comma before ')'.
17571761
if (Tok.is(tok::r_paren)) {
1758-
diagnose(Tok, diag::unexpected_separator, ",");
1762+
diagnose(Tok, diag::unexpected_separator, ",")
1763+
.fixItRemove(PreviousLoc);
17591764
return errorAndSkipUntilConsumeRightParen(*this, AttrName);
17601765
}
17611766
// Check that token after comma is 'wrt:'.

test/AutoDiff/Parse/derivative_attr_parse.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func dfoo(x: Float) -> (value: Float, differential: (Float) -> (Float)) {
8181
return (x, { $0 })
8282
}
8383

84-
// expected-error @+1 {{unexpected ',' separator}}
84+
// expected-error @+1 {{unexpected ',' separator}} {{20-21=}}
8585
@derivative(of: foo,)
8686
func dfoo(x: Float) -> (value: Float, differential: (Float) -> (Float)) {
8787
return (x, { $0 })
@@ -100,14 +100,14 @@ func dfoo(x: Float) -> (value: Float, differential: (Float) -> (Float)) {
100100
return (x, { $0 })
101101
}
102102

103-
// expected-error @+1 {{unexpected ',' separator}}
103+
// expected-error @+1 {{unexpected ',' separator}} {{20-21=}}
104104
@derivative(of: foo,)
105105
func dfoo(x: Float) -> (value: Float, differential: (Float) -> (Float)) {
106106
return (x, { $0 })
107107
}
108108

109109
// TF-1168: missing comma before `wrt:`.
110-
// expected-error @+2 {{expected ',' separator}}
110+
// expected-error @+2 {{expected ',' separator}} {{20-20=,}}
111111
// expected-error @+1 {{expected declaration}}
112112
@derivative(of: foo wrt: x)
113113
func dfoo(x: Float) -> (value: Float, differential: (Float) -> (Float)) {

test/AutoDiff/Parse/differentiable_attr_parse.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ func two(x: Float, y: Float) -> Float {
149149
return x + y
150150
}
151151

152-
// expected-error @+1 {{unexpected ',' separator}}
152+
// expected-error @+1 {{unexpected ',' separator}} {{32-33=}}
153153
@differentiable(reverse, wrt: 0,)
154154
func two(x: Float, y: Float) -> Float {
155155
return x + y
@@ -173,13 +173,13 @@ func bar(_ x: Float, _: Float) -> Float {
173173
return 1 + x
174174
}
175175

176-
// expected-error @+1 {{unexpected ',' separator}}
176+
// expected-error @+1 {{unexpected ',' separator}} {{34-35=}}
177177
@differentiable(reverse, wrt: (x),)
178178
func bar(_ x: Float, _: Float) -> Float {
179179
return 1 + x
180180
}
181181

182-
// expected-error @+1 {{unexpected ',' separator}}
182+
// expected-error @+1 {{unexpected ',' separator}} {{34-35=}}
183183
@differentiable(reverse, wrt: (x), where T)
184184
func bar<T : Numeric>(_ x: T, _: T) -> T {
185185
return 1 + x

test/AutoDiff/Parse/transpose_attr_parse.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func transpose(v: Float) -> Float
6161
@transpose(of: foo, wrt)
6262
func transpose(v: Float) -> Float
6363

64-
// expected-error @+1 {{unexpected ',' separator}}
64+
// expected-error @+1 {{unexpected ',' separator}} {{19-20=}}
6565
@transpose(of: foo,)
6666
func transpose(v: Float) -> Float
6767

@@ -95,7 +95,7 @@ func transpose(v: Float) -> Float
9595
func transpose(v: Float) -> Float
9696

9797
// TF-1168: missing comma before `wrt:`.
98-
// expected-error @+2 {{expected ',' separator}}
98+
// expected-error @+2 {{expected ',' separator}} {{19-19=,}}
9999
// expected-error @+1 {{expected declaration}}
100100
@transpose(of: foo wrt: x)
101101
func transpose(v: Float) -> Float

test/Parse/trailing-comma.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ if #available(iOS 15,) { } // expected-error {{expected platform name}}
7474

7575
// Built-in Attributes
7676

77-
@attached(extension, conformances: OptionSet,) // expected-error {{unexpected ',' separator}}
77+
@attached(extension, conformances: OptionSet,) // expected-error {{unexpected ',' separator}} {{45-46=}}
7878
macro OptionSet<RawType>() = #externalMacro(module: "SwiftMacros", type: "OptionSetMacro")
7979

8080
@inline(never,) // expected-error {{expected declaration}} expected-error {{expected ')' in 'inline' attribute}}
@@ -83,7 +83,7 @@ func foo() { }
8383
@available(iOS 15,) // expected-error {{expected platform name}} expected-error {{expected declaration}}
8484
func foo() { }
8585

86-
@backDeployed(before: SwiftStdlib 6.0,) // expected-error {{unexpected ',' separator}}
86+
@backDeployed(before: SwiftStdlib 6.0,) // expected-error {{unexpected ',' separator}} {{38-39=}}
8787
func foo() { }
8888

8989
struct Foo {
@@ -104,10 +104,10 @@ struct Foo {
104104

105105
func f(in: @differentiable(reverse,) (Int) -> Int) { } // expected-warning {{@differentiable' has been renamed to '@differentiable(reverse)' and will be removed in the next release}} expected-error {{expected ',' separator}} expected-error {{unnamed parameters must be written with the empty name '_'}}
106106

107-
@derivative(of: Self.other,) // expected-error {{unexpected ',' separator}}
107+
@derivative(of: Self.other,) // expected-error {{unexpected ',' separator}} {{27-28=}}
108108
func foo() {}
109109

110-
@transpose(of: S.instanceMethod,) // expected-error {{unexpected ',' separator}}
110+
@transpose(of: S.instanceMethod,) // expected-error {{unexpected ',' separator}} {{32-33=}}
111111
func transposeInstanceMethodWrtSelf(_ other: S, t: S) -> S {
112112
other + t
113113
}
@@ -145,6 +145,6 @@ if #available(OSX 51,) { // expected-error {{expected platform name}}
145145
}
146146

147147
@available(OSX 10.7, iOS 7.0, *,) // expected-error {{expected platform name}} expected-error {{expected declaration}}
148-
@_originallyDefinedIn(module: "HighLevel", OSX 10.9, iOS 13.0,) // expected-error {{unexpected ',' separator}}
149-
@backDeployed(before: OSX 10.9,) // expected-error {{unexpected ',' separator}}
148+
@_originallyDefinedIn(module: "HighLevel", OSX 10.9, iOS 13.0,) // expected-error {{unexpected ',' separator}} {{62-63=}}
149+
@backDeployed(before: OSX 10.9,) // expected-error {{unexpected ',' separator}} {{31-32=}}
150150
public struct StructWithAvailability {}

test/attr/attr_backDeployed.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ public func missingVersionFunc3() {}
452452
@backDeployed(before: macOS 0) // expected-warning {{expected version number in '@backDeployed' attribute; this is an error in the Swift 6 language mode}}
453453
public func missingVersionFunc4() {}
454454

455-
@backDeployed(before: macOS 12.0, iOS 15.0,) // expected-error {{unexpected ',' separator}}
455+
@backDeployed(before: macOS 12.0, iOS 15.0,) // expected-error {{unexpected ',' separator}} {{43-44=}}
456456
public func unexpectedSeparatorFunc() {}
457457

458458
@backDeployed(before: macOS 12.0.1) // expected-warning {{'@backDeployed' only uses major and minor version number}}

0 commit comments

Comments
 (0)