Skip to content

Commit bc6951d

Browse files
committed
Followup to r19830: preserve the original token so we report the
correct location for the operator error, and added a test for the error reporting for operators in non-global scopes. Swift SVN r19862
1 parent 09be2f3 commit bc6951d

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

lib/Parse/ParseDecl.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -3043,6 +3043,7 @@ Parser::parseDeclFunc(SourceLoc StaticLoc, StaticSpellingKind StaticSpelling,
30433043
}
30443044
Identifier SimpleName;
30453045
SourceLoc NameLoc = Tok.getLoc();
3046+
Token NonglobalTok = Tok;
30463047
bool NonglobalError = false;
30473048

30483049
if (!(Flags & PD_AllowTopLevel) &&
@@ -3067,7 +3068,7 @@ Parser::parseDeclFunc(SourceLoc StaticLoc, StaticSpellingKind StaticSpelling,
30673068

30683069
if (NonglobalError && !DCC.movedToTopLevel()) {
30693070
// FIXME: Recovery here is awful.
3070-
diagnose(Tok, diag::func_decl_nonglobal_operator);
3071+
diagnose(NonglobalTok, diag::func_decl_nonglobal_operator);
30713072
return nullptr;
30723073
}
30733074

test/decl/operator/operators.swift

+6
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,9 @@ func test_14705150() {
144144
operator postfix! {} // expected-error{{cannot declare a custom postfix '!' operator}}
145145
@postfix func !(x: Int) { } // expected-error{{cannot declare a custom postfix '!' operator}}
146146
@postfix func!(x: Int8) { } // expected-error{{cannot declare a custom postfix '!' operator}}
147+
148+
// Only allow operators at global scope:
149+
func operator_in_func_bad () {
150+
@prefix func + (input: String) -> String { return "+" + input } // expected-error {{operators are only allowed at global scope}} \
151+
// expected-error {{braced block of statements is an unused closure}}
152+
}

0 commit comments

Comments
 (0)