Skip to content

[Diagnostics] Add a fix-it for misplaced throws in function types #25306

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/Parse/ParseType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -417,8 +417,15 @@ ParserResult<TypeRepr> Parser::parseType(Diag<> MessageID,
if (Tok.is(tok::arrow)) {
// Handle type-function if we have an arrow.
SourceLoc arrowLoc = consumeToken();
if (Tok.is(tok::kw_throws)) {
Diag<> DiagID = diag::throws_in_wrong_position;
diagnose(Tok.getLoc(), DiagID)
.fixItInsert(arrowLoc, "throws ")
.fixItRemove(Tok.getLoc());
throwsLoc = consumeToken();
}
ParserResult<TypeRepr> SecondHalf =
parseType(diag::expected_type_function_result);
parseType(diag::expected_type_function_result);
if (SecondHalf.hasCodeCompletion())
return makeParserCodeCompletionResult<TypeRepr>();
if (SecondHalf.isNull())
Expand Down
2 changes: 2 additions & 0 deletions test/Parse/errors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,5 @@ func fixitThrow2() throws {
throw MSV.Foo
var _: (Int) throw -> Int // expected-error{{expected throwing specifier; did you mean 'throws'?}} {{16-21=throws}}
}

let fn: () -> throws Void // expected-error{{'throws' may only occur before '->'}} {{12-12=throws }} {{15-22=}}