Skip to content

Commit d0c2d8b

Browse files
committed
[Diagnostics] Add fix-its for unexpected/expected comma in auto-diff attributes
1 parent 3e981b7 commit d0c2d8b

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
@@ -1423,7 +1423,8 @@ bool Parser::parseDifferentiableAttributeArguments(
14231423
if (!consumeIf(tok::comma)) return false;
14241424
// Diagnose trailing comma before 'where' or ')'.
14251425
if (Tok.is(tok::kw_where) || Tok.is(tok::r_paren)) {
1426-
diagnose(Tok, diag::unexpected_separator, ",");
1426+
diagnose(Tok, diag::unexpected_separator, ",")
1427+
.fixItRemove(PreviousLoc);
14271428
return true;
14281429
}
14291430
// Check that token after comma is 'wrt'.
@@ -1665,14 +1666,16 @@ ParserResult<DerivativeAttr> Parser::parseDerivativeAttribute(SourceLoc atLoc,
16651666
// If comma is required but does not exist and ')' has not been reached,
16661667
// diagnose missing comma.
16671668
if (requireComma && !Tok.is(tok::r_paren)) {
1668-
diagnose(getEndOfPreviousLoc(), diag::expected_separator, ",");
1669+
diagnose(getEndOfPreviousLoc(), diag::expected_separator, ",")
1670+
.fixItInsertAfter(PreviousLoc, ",");
16691671
return true;
16701672
}
16711673
return false;
16721674
}
16731675
// Diagnose trailing comma before ')'.
16741676
if (Tok.is(tok::r_paren)) {
1675-
diagnose(Tok, diag::unexpected_separator, ",");
1677+
diagnose(Tok, diag::unexpected_separator, ",")
1678+
.fixItRemove(PreviousLoc);
16761679
return errorAndSkipUntilConsumeRightParen(*this, AttrName);
16771680
}
16781681
// Check that token after comma is 'wrt:'.
@@ -1745,14 +1748,16 @@ ParserResult<TransposeAttr> Parser::parseTransposeAttribute(SourceLoc atLoc,
17451748
// If comma is required but does not exist and ')' has not been reached,
17461749
// diagnose missing comma.
17471750
if (requireComma && !Tok.is(tok::r_paren)) {
1748-
diagnose(Tok, diag::expected_separator, ",");
1751+
diagnose(Tok, diag::expected_separator, ",")
1752+
.fixItInsertAfter(PreviousLoc, ",");
17491753
return true;
17501754
}
17511755
return false;
17521756
}
17531757
// Diagnose trailing comma before ')'.
17541758
if (Tok.is(tok::r_paren)) {
1755-
diagnose(Tok, diag::unexpected_separator, ",");
1759+
diagnose(Tok, diag::unexpected_separator, ",")
1760+
.fixItRemove(PreviousLoc);
17561761
return errorAndSkipUntilConsumeRightParen(*this, AttrName);
17571762
}
17581763
// 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)