Skip to content

Commit 5a8dff0

Browse files
committed
[Parse] Emit error on prefix operator containing /
When forward slash regex is enabled, start emitting an error on prefix operators containing the `/` character.
1 parent 63b8db1 commit 5a8dff0

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

include/swift/AST/DiagnosticsParse.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ ERROR(forbidden_extended_escaping_string,none,
9494
ERROR(regex_literal_parsing_error,none,
9595
"%0", (StringRef))
9696

97+
ERROR(prefix_slash_not_allowed,none,
98+
"prefix operator may not contain '/'", ())
99+
97100
//------------------------------------------------------------------------------
98101
// MARK: Lexer diagnostics
99102
//------------------------------------------------------------------------------

lib/Parse/ParseDecl.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8528,6 +8528,13 @@ Parser::parseDeclOperator(ParseDeclOptions Flags, DeclAttributes &Attributes) {
85288528
Tok.getRawText().front() == '!'))
85298529
diagnose(Tok, diag::postfix_operator_name_cannot_start_with_unwrap);
85308530

8531+
// Prefix operators may not contain the `/` character when `/.../` regex
8532+
// literals are enabled.
8533+
if (Context.LangOpts.EnableBareSlashRegexLiterals) {
8534+
if (Attributes.hasAttribute<PrefixAttr>() && Tok.getText().contains("/"))
8535+
diagnose(Tok, diag::prefix_slash_not_allowed);
8536+
}
8537+
85318538
// A common error is to try to define an operator with something in the
85328539
// unicode plane considered to be an operator, or to try to define an
85338540
// operator like "not". Analyze and diagnose this specifically.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// RUN: %target-typecheck-verify-swift -enable-bare-slash-regex
2+
3+
prefix operator / // expected-error {{prefix operator may not contain '/'}}
4+
prefix operator ^/ // expected-error {{prefix operator may not contain '/'}}
5+
prefix operator /^/ // expected-error {{prefix operator may not contain '/'}}

0 commit comments

Comments
 (0)