Skip to content

Commit a20bfb0

Browse files
committed
Correct 'throw' to 'throws' in function signatures and types.
rdar://21328447 Swift SVN r30514
1 parent d33f404 commit a20bfb0

File tree

4 files changed

+26
-2
lines changed

4 files changed

+26
-2
lines changed

include/swift/AST/DiagnosticsParse.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,8 @@ ERROR(throws_after_function_result,type_parsing,none,
585585
"'throws' may only occur before '->'", ())
586586
ERROR(rethrows_after_function_result,type_parsing,none,
587587
"'rethrows' may only occur before '->'", ())
588+
ERROR(throw_in_function_type,type_parsing,none,
589+
"expected throwing specifier; did you mean 'throws'?", ())
588590

589591
// Enum Types
590592
ERROR(expected_expr_enum_case_raw_value,type_parsing,PointsToFirstBadToken,

lib/Parse/ParsePattern.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,10 @@ Parser::parseFunctionSignature(Identifier SimpleName,
640640
} else if (Tok.is(tok::kw_rethrows)) {
641641
throwsLoc = consumeToken();
642642
rethrows = true;
643+
} else if (Tok.is(tok::kw_throw)) {
644+
throwsLoc = consumeToken();
645+
diagnose(throwsLoc, diag::throw_in_function_type)
646+
.fixItReplace(throwsLoc, "throws");
643647
}
644648

645649
SourceLoc arrowLoc;

lib/Parse/ParseType.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,20 @@ ParserResult<TypeRepr> Parser::parseType(Diag<> MessageID) {
162162

163163
if (ty.isNull())
164164
return nullptr;
165-
165+
166+
// Parse a throws specifier. 'throw' is probably a typo for 'throws',
167+
// but in local contexts we could just be at the end of a statement,
168+
// so we need to check for the arrow.
166169
ParserPosition beforeThrowsPos;
167170
SourceLoc throwsLoc;
168171
bool rethrows = false;
169-
if (Tok.isAny(tok::kw_throws, tok::kw_rethrows)) {
172+
if (Tok.isAny(tok::kw_throws, tok::kw_rethrows) ||
173+
(Tok.is(tok::kw_throw) && peekToken().is(tok::arrow))) {
174+
if (Tok.is(tok::kw_throw)) {
175+
diagnose(Tok.getLoc(), diag::throw_in_function_type)
176+
.fixItReplace(Tok.getLoc(), "throws");
177+
}
178+
170179
beforeThrowsPos = getParserPosition();
171180
rethrows = Tok.is(tok::kw_rethrows);
172181
throwsLoc = consumeToken();

test/Parse/errors.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,12 @@ func postThrows() -> Int throws { // expected-error{{'throws' may only occur bef
9797
func postRethrows(f: () throws -> Int) -> Int rethrows { // expected-error{{'rethrows' may only occur before '->'}}{{40-40=rethrows }}{{46-55=}}
9898
return try f()
9999
}
100+
101+
// rdar://21328447
102+
func fixitThrow0() throw {} // expected-error{{expected throwing specifier; did you mean 'throws'?}} {{20-25=throws}}
103+
func fixitThrow1() throw -> Int {} // expected-error{{expected throwing specifier; did you mean 'throws'?}} {{20-25=throws}}
104+
func fixitThrow2() throws {
105+
var _: (Int)
106+
throw MSV.Foo
107+
var _: Int throw -> Int // expected-error{{expected throwing specifier; did you mean 'throws'?}} {{14-19=throws}}
108+
}

0 commit comments

Comments
 (0)