Skip to content

Commit 37aaaed

Browse files
committed
[Diagnostics] Add a fix-it for try instead of throws in function decls and types.
Sometimes one would misstype `throws` for `try` so we provide a fix-it for that. It might happen in two cases: In function declarations: ```swift func foo() try {} ``` In function types: ```swift let f = () try -> Int ``` Resolves SR-11574.
1 parent 8743564 commit 37aaaed

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

lib/Parse/ParsePattern.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,10 @@ Parser::parseFunctionSignature(Identifier SimpleName,
786786
throwsLoc = consumeToken();
787787
diagnose(throwsLoc, diag::throw_in_function_type)
788788
.fixItReplace(throwsLoc, "throws");
789+
} else if (Tok.is(tok::kw_try)) {
790+
diagnose(Tok.getLoc(), diag::throw_in_function_type)
791+
.fixItReplace(tryLoc, "throws");
792+
ignoreToken();
789793
}
790794

791795
SourceLoc arrowLoc;

lib/Parse/ParseType.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -399,17 +399,19 @@ ParserResult<TypeRepr> Parser::parseType(Diag<> MessageID,
399399
// Don't consume 'throws', if the next token is not '->', so we can emit a
400400
// more useful diagnostic when parsing a function decl.
401401
SourceLoc throwsLoc;
402-
if (Tok.isAny(tok::kw_throws, tok::kw_rethrows, tok::kw_throw) &&
402+
if (Tok.isAny(tok::kw_throws, tok::kw_rethrows, tok::kw_throw, tok::kw_try) &&
403403
peekToken().is(tok::arrow)) {
404404
if (Tok.isNot(tok::kw_throws)) {
405405
// 'rethrows' is only allowed on function declarations for now.
406-
// 'throw' is probably a typo for 'throws'.
406+
// 'throw' or 'try' are probably typos for 'throws'.
407407
Diag<> DiagID = Tok.is(tok::kw_rethrows) ?
408408
diag::rethrowing_function_type : diag::throw_in_function_type;
409409
diagnose(Tok.getLoc(), DiagID)
410410
.fixItReplace(Tok.getLoc(), "throws");
411411
}
412-
throwsLoc = consumeToken();
412+
if (Tok.isNot(tok::try)) {
413+
throwsLoc = consumeToken();
414+
}
413415
}
414416

415417
if (Tok.is(tok::arrow)) {

test/Parse/errors.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,9 @@ func fixitThrow2() throws {
127127
}
128128

129129
let fn: () -> throws Void // expected-error{{'throws' may only occur before '->'}} {{12-12=throws }} {{15-22=}}
130+
131+
// SR-11574
132+
func fixitTry0<T>(a: T) try where T:ExpressibleByStringLiteral {} // expected-error{{expected throwing specifier; did you mean 'throws'?}} {{25-28=throws}}
133+
func fixitTry1<T>(a: T) try {} // expected-error{{expected throwing specifier; did you mean 'throws'?}} {{25-28=throws}}
134+
func fixitTry2() try {} // expected-error{{expected throwing specifier; did you mean 'throws'?}} {{18-21=throws}}
135+
let fixitTry3 : () try -> Int // expected-error{{expected throwing specifier; did you mean 'throws'?}} {{20-23=throws}}

0 commit comments

Comments
 (0)