Skip to content

Commit 31595e3

Browse files
committed
[Parse] Adjust diagnostics message for duplicated effects specifiers
1 parent c72f9e5 commit 31595e3

File tree

5 files changed

+19
-19
lines changed

5 files changed

+19
-19
lines changed

include/swift/AST/DiagnosticsParse.def

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -732,8 +732,7 @@ ERROR(rethrowing_function_type,none,
732732
"only function declarations may be marked 'rethrows'; "
733733
"did you mean 'throws'?", ())
734734
ERROR(async_or_throws_in_wrong_position,none,
735-
"%select{'throws'|'rethrows'|'async'}0 may only occur before '->'",
736-
(unsigned))
735+
"'%0' may only occur before '->'", (StringRef))
737736
ERROR(throw_in_function_type,none,
738737
"expected throwing specifier; did you mean 'throws'?", ())
739738
ERROR(expected_type_before_arrow,none,
@@ -748,8 +747,8 @@ ERROR(expected_dynamic_func_attr,none,
748747
ERROR(async_after_throws,none,
749748
"'async' must precede %select{'throws'|'rethrows'}0", (bool))
750749
ERROR(async_init,none, "initializer cannot be marked 'async'", ())
751-
ERROR(duplicate_effect_specifier,none,
752-
"unexpected '%0' specifier", (StringRef))
750+
ERROR(duplicate_effects_specifier,none,
751+
"'%0' has already been specified", (StringRef))
753752

754753
// Enum Types
755754
ERROR(expected_expr_enum_case_raw_value,PointsToFirstBadToken,

lib/Parse/ParseExpr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ ParserResult<Expr> Parser::parseExprArrow() {
139139
assert(throwsLoc.isValid() || asyncLoc.isValid());
140140
diagnose(throwsLoc.isValid() ? throwsLoc : asyncLoc,
141141
diag::async_or_throws_in_wrong_position,
142-
throwsLoc.isValid() ? 0 : 2);
142+
throwsLoc.isValid() ? "throws" : "async");
143143
return nullptr;
144144
}
145145

lib/Parse/ParsePattern.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -848,14 +848,15 @@ ParserStatus Parser::parseEffectsSpecifiers(SourceLoc existingArrowLoc,
848848
Tok.isContextualKeyword("async")) {
849849

850850
if (asyncLoc.isValid()) {
851-
diagnose(Tok, diag::duplicate_effect_specifier, Tok.getText())
851+
diagnose(Tok, diag::duplicate_effects_specifier, Tok.getText())
852+
.highlight(asyncLoc)
852853
.fixItRemove(Tok.getLoc());
853854
} else if (existingArrowLoc.isValid()) {
854855
SourceLoc insertLoc = existingArrowLoc;
855856
if (throwsLoc.isValid() &&
856857
SourceMgr.isBeforeInBuffer(throwsLoc, insertLoc))
857858
insertLoc = throwsLoc;
858-
diagnose(Tok, diag::async_or_throws_in_wrong_position, 2)
859+
diagnose(Tok, diag::async_or_throws_in_wrong_position, "async")
859860
.fixItRemove(Tok.getLoc())
860861
.fixItInsert(insertLoc, "async ");
861862
} else if (throwsLoc.isValid()) {
@@ -877,7 +878,8 @@ ParserStatus Parser::parseEffectsSpecifiers(SourceLoc existingArrowLoc,
877878
bool isRethrows = Tok.is(tok::kw_rethrows);
878879

879880
if (throwsLoc.isValid()) {
880-
diagnose(Tok, diag::duplicate_effect_specifier, Tok.getText())
881+
diagnose(Tok, diag::duplicate_effects_specifier, Tok.getText())
882+
.highlight(throwsLoc)
881883
.fixItRemove(Tok.getLoc());
882884
} else if (Tok.isAny(tok::kw_throw, tok::kw_try)) {
883885
// Replace 'throw' or 'try' with 'throws'.
@@ -888,8 +890,7 @@ ParserStatus Parser::parseEffectsSpecifiers(SourceLoc existingArrowLoc,
888890
diagnose(Tok, diag::rethrowing_function_type)
889891
.fixItReplace(Tok.getLoc(), "throws");
890892
} else if (existingArrowLoc.isValid()) {
891-
diagnose(Tok, diag::async_or_throws_in_wrong_position,
892-
rethrows ? (isRethrows ? 1 : 0) : 0)
893+
diagnose(Tok, diag::async_or_throws_in_wrong_position, Tok.getText())
893894
.fixItRemove(Tok.getLoc())
894895
.fixItInsert(existingArrowLoc, (Tok.getText() + " ").str());
895896
}

test/Parse/async.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ func asyncGlobal6() -> Int throws async { }
2323
func asyncGlobal7() throws -> Int async { } // expected-error{{'async' may only occur before '->'}}{{35-41=}}{{21-21=async }}
2424

2525
func asyncGlobal8() async throws async -> async Int async {}
26-
// expected-error@-1{{unexpected 'async' specifier}} {{34-40=}}
27-
// expected-error@-2{{unexpected 'async' specifier}} {{43-49=}}
28-
// expected-error@-3{{unexpected 'async' specifier}} {{53-59=}}
26+
// expected-error@-1{{'async' has already been specified}} {{34-40=}}
27+
// expected-error@-2{{'async' has already been specified}} {{43-49=}}
28+
// expected-error@-3{{'async' has already been specified}} {{53-59=}}
2929

3030
class X {
3131
init() async { } // expected-error{{initializer cannot be marked 'async'}}

test/Parse/errors.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,20 +121,20 @@ func postThrows3() {
121121
}
122122

123123
func dupThrows1() throws rethrows -> throws Int throw {}
124-
// expected-error@-1 {{unexpected 'rethrows' specifier}} {{26-35=}}
125-
// expected-error@-2 {{unexpected 'throws' specifier}} {{38-45=}}
126-
// expected-error@-3 {{unexpected 'throw' specifier}} {{49-55=}}
124+
// expected-error@-1 {{'rethrows' has already been specified}} {{26-35=}}
125+
// expected-error@-2 {{'throws' has already been specified}} {{38-45=}}
126+
// expected-error@-3 {{'throw' has already been specified}} {{49-55=}}
127127

128128
func dupThrows2(_ f: () throws -> rethrows Int) {}
129-
// expected-error@-1 {{unexpected 'rethrows' specifier}} {{35-44=}}
129+
// expected-error@-1 {{'rethrows' has already been specified}} {{35-44=}}
130130

131131
func dupThrows3() {
132132
_ = { () try throws in }
133133
// expected-error@-1 {{expected throwing specifier; did you mean 'throws'?}} {{12-15=throws}}
134-
// expected-error@-2 {{unexpected 'throws' specifier}} {{16-23=}}
134+
// expected-error@-2 {{'throws' has already been specified}} {{16-23=}}
135135

136136
_ = { () throws -> Int throws in }
137-
// expected-error@-1 {{unexpected 'throws' specifier}} {{26-33=}}
137+
// expected-error@-1 {{'throws' has already been specified}} {{26-33=}}
138138
}
139139

140140
func incompleteThrowType() {

0 commit comments

Comments
 (0)