Skip to content

Commit c3aebfe

Browse files
committed
[Parse] Add fix-it for 'rethrows' in function type
1 parent 2ba0263 commit c3aebfe

File tree

4 files changed

+12
-10
lines changed

4 files changed

+12
-10
lines changed

include/swift/AST/DiagnosticsParse.def

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,8 @@ ERROR(expected_type_function_result,PointsToFirstBadToken,
620620
ERROR(generic_non_function,PointsToFirstBadToken,
621621
"only syntactic function types can be generic", ())
622622
ERROR(rethrowing_function_type,none,
623-
"only function declarations may be marked 'rethrows'", ())
623+
"only function declarations may be marked 'rethrows'; "
624+
"did you mean 'throws'?", ())
624625
ERROR(throws_in_wrong_position,none,
625626
"'throws' may only occur before '->'", ())
626627
ERROR(rethrows_in_wrong_position,none,

lib/Parse/ParseExpr.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2200,7 +2200,8 @@ parseClosureSignatureIfPresent(SmallVectorImpl<CaptureListEntry> &captureList,
22002200
throwsLoc = consumeToken();
22012201
} else if (Tok.is(tok::kw_rethrows)) {
22022202
throwsLoc = consumeToken();
2203-
diagnose(throwsLoc, diag::rethrowing_function_type);
2203+
diagnose(throwsLoc, diag::rethrowing_function_type)
2204+
.fixItReplace(throwsLoc, "throws");
22042205
}
22052206

22062207
// Parse the optional explicit return type.

lib/Parse/ParseType.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -189,12 +189,12 @@ ParserResult<TypeRepr> Parser::parseType(Diag<> MessageID,
189189
SourceLoc throwsLoc;
190190
if (Tok.isAny(tok::kw_throws, tok::kw_rethrows, tok::kw_throw) &&
191191
peekToken().is(tok::arrow)) {
192-
if (Tok.is(tok::kw_rethrows)) {
192+
if (Tok.isNot(tok::kw_throws)) {
193193
// 'rethrows' is only allowed on function declarations for now.
194-
diagnose(Tok.getLoc(), diag::rethrowing_function_type);
195-
} else if (Tok.is(tok::kw_throw)) {
196-
// 'throw' is probably a typo for 'throws'
197-
diagnose(Tok.getLoc(), diag::throw_in_function_type)
194+
// 'throw' is probably a typo for 'throws'.
195+
Diag<> DiagID = Tok.is(tok::kw_rethrows) ?
196+
diag::rethrowing_function_type : diag::throw_in_function_type;
197+
diagnose(Tok.getLoc(), DiagID)
198198
.fixItReplace(Tok.getLoc(), "throws");
199199
}
200200
throwsLoc = consumeToken();

test/decl/func/rethrows.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
/** Basics *******************************************************************/
44

55
// Function types can't be rethrows right now.
6-
let r1 = {() rethrows -> Int in 0} // expected-error {{only function declarations may be marked 'rethrows'}}
7-
let r2 : () rethrows -> Int = { 0 } // expected-error {{only function declarations may be marked 'rethrows'}}
8-
let r3 : Optional<() rethrows -> ()> = nil // expected-error {{only function declarations may be marked 'rethrows'}}
6+
let r1 = {() rethrows -> Int in 0} // expected-error {{only function declarations may be marked 'rethrows'; did you mean 'throws'?}} {{14-22=throws}}
7+
let r2 : () rethrows -> Int = { 0 } // expected-error {{only function declarations may be marked 'rethrows'; did you mean 'throws'?}} {{13-21=throws}}
8+
let r3 : Optional<() rethrows -> ()> = nil // expected-error {{only function declarations may be marked 'rethrows'; did you mean 'throws'?}} {{22-30=throws}}
99

1010
func f1(_ f: () throws -> ()) rethrows { try f() }
1111
func f2(_ f: () -> ()) rethrows { f() } // expected-error {{'rethrows' function must take a throwing function argument}}

0 commit comments

Comments
 (0)