Skip to content

Commit 07e6940

Browse files
tapthakerbeccadax
authored andcommitted
[Diagnostics] Add a fix-it for misplaced throws in function types (#25306)
Adds a diagnostic error for misplaced throws in function types. For e.g: ```swift let fn: () -> throws Void // expected-error{{'throws' may only occur before '->'}} {{12-12=throws }} {{15-22=}} ``` Resolves SR-10703.
1 parent 9eb0ebe commit 07e6940

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

lib/Parse/ParseType.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,8 +417,15 @@ ParserResult<TypeRepr> Parser::parseType(Diag<> MessageID,
417417
if (Tok.is(tok::arrow)) {
418418
// Handle type-function if we have an arrow.
419419
SourceLoc arrowLoc = consumeToken();
420+
if (Tok.is(tok::kw_throws)) {
421+
Diag<> DiagID = diag::throws_in_wrong_position;
422+
diagnose(Tok.getLoc(), DiagID)
423+
.fixItInsert(arrowLoc, "throws ")
424+
.fixItRemove(Tok.getLoc());
425+
throwsLoc = consumeToken();
426+
}
420427
ParserResult<TypeRepr> SecondHalf =
421-
parseType(diag::expected_type_function_result);
428+
parseType(diag::expected_type_function_result);
422429
if (SecondHalf.hasCodeCompletion())
423430
return makeParserCodeCompletionResult<TypeRepr>();
424431
if (SecondHalf.isNull())

test/Parse/errors.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,5 @@ func fixitThrow2() throws {
125125
throw MSV.Foo
126126
var _: (Int) throw -> Int // expected-error{{expected throwing specifier; did you mean 'throws'?}} {{16-21=throws}}
127127
}
128+
129+
let fn: () -> throws Void // expected-error{{'throws' may only occur before '->'}} {{12-12=throws }} {{15-22=}}

0 commit comments

Comments
 (0)