Skip to content

Commit f0ab1d2

Browse files
committed
[test] Add some additional /.../ tests
1 parent fc30104 commit f0ab1d2

File tree

2 files changed

+109
-3
lines changed

2 files changed

+109
-3
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// RUN: %target-typecheck-verify-swift -enable-experimental-string-processing -disable-availability-checking
2+
3+
// REQUIRES: swift_in_compiler
4+
5+
prefix operator /
6+
prefix operator ^/
7+
prefix operator /^/
8+
9+
precedencegroup P {
10+
associativity: left
11+
}
12+
13+
// The divisions in the body of the below operators make sure we don't try and
14+
// consider them to be ending delimiters of a regex.
15+
infix operator /^/ : P
16+
func /^/ (lhs: Int, rhs: Int) -> Int { 1 / 2 }
17+
18+
infix operator /^ : P
19+
func /^ (lhs: Int, rhs: Int) -> Int { 1 / 2 }
20+
21+
infix operator ^^/ : P
22+
func ^^/ (lhs: Int, rhs: Int) -> Int { 1 / 2 }
23+
24+
_ = #/x/#
25+
26+
_ = /x/
27+
// expected-error@-1 {{'/' is not a prefix unary operator}}
28+
// expected-error@-2 {{cannot find 'x' in scope}}
29+
// expected-error@-3 {{'/' is not a postfix unary operator}}
30+
31+
func baz(_ x: (Int, Int) -> Int, _ y: (Int, Int) -> Int) {}
32+
baz(/, /)
33+
baz(/^, /)
34+
baz(^^/, /)

test/StringProcessing/Parse/forward-slash-regex.swift

Lines changed: 75 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,26 @@ prefix operator / // expected-error {{prefix operator may not contain '/'}}
66
prefix operator ^/ // expected-error {{prefix operator may not contain '/'}}
77
prefix operator /^/ // expected-error {{prefix operator may not contain '/'}}
88

9+
prefix operator !!
10+
prefix func !! <T>(_ x: T) -> T { x }
11+
12+
prefix operator ^^
13+
prefix func ^^ <T>(_ x: T) -> T { x }
14+
915
precedencegroup P {
1016
associativity: left
1117
}
1218

13-
// Fine.
19+
// The divisions in the body of the below operators make sure we don't try and
20+
// consider them to be ending delimiters of a regex.
1421
infix operator /^/ : P
15-
func /^/ (lhs: Int, rhs: Int) -> Int { 0 }
22+
func /^/ (lhs: Int, rhs: Int) -> Int { 1 / 2 }
23+
24+
infix operator /^ : P
25+
func /^ (lhs: Int, rhs: Int) -> Int { 1 / 2 }
26+
27+
infix operator ^^/ : P
28+
func ^^/ (lhs: Int, rhs: Int) -> Int { 1 / 2 }
1629

1730
let i = 0 /^/ 1/^/3
1831

@@ -41,6 +54,21 @@ _ = /x
4154
_ = !/x/
4255
// expected-error@-1 {{cannot convert value of type 'Regex<Substring>' to expected argument type 'Bool'}}
4356

57+
_ = (!/x/)
58+
// expected-error@-1 {{cannot convert value of type 'Regex<Substring>' to expected argument type 'Bool'}}
59+
60+
_ = !/ /
61+
// expected-error@-1 {{unary operator cannot be separated from its operand}}
62+
// expected-error@-2 {{cannot find operator '!/' in scope}}
63+
// expected-error@-3 {{unterminated regex literal}}
64+
65+
_ = !!/x/
66+
_ = (!!/x/)
67+
68+
_ = /^)
69+
// expected-error@-1 {{unterminated regex literal}}
70+
// expected-error@-2 {{closing ')' does not balance any groups openings}}
71+
4472
_ = /x/! // expected-error {{cannot force unwrap value of non-optional type 'Regex<Substring>'}}
4573
_ = /x/ + /y/ // expected-error {{binary operator '+' cannot be applied to two 'Regex<Substring>' operands}}
4674

@@ -233,7 +261,7 @@ _ = /x/*comment*/
233261
// expected-error@-1 {{unterminated regex literal}}
234262

235263
// These become regex literals, unless surrounded in parens.
236-
func baz(_ x: (Int, Int) -> Int, _ y: (Int, Int) -> Int) {} // expected-note 2{{'baz' declared here}}
264+
func baz(_ x: (Int, Int) -> Int, _ y: (Int, Int) -> Int) {} // expected-note 3{{'baz' declared here}}
237265
baz(/, /)
238266
// expected-error@-1 {{cannot convert value of type 'Regex<Substring>' to expected argument type '(Int, Int) -> Int'}}
239267
// expected-error@-2 {{missing argument for parameter #2 in call}}
@@ -242,8 +270,23 @@ baz(/,/)
242270
// expected-error@-2 {{missing argument for parameter #2 in call}}
243271
baz((/), /)
244272

273+
baz(/^, /)
274+
// expected-error@-1 {{cannot convert value of type 'Regex<Substring>' to expected argument type '(Int, Int) -> Int'}}
275+
// expected-error@-2 {{missing argument for parameter #2 in call}}
276+
277+
do {
278+
baz((/^), /)
279+
// expected-error@-1 {{closing ')' does not balance any groups openings}}
280+
// expected-note@-2 {{to match this opening '('}}
281+
} // expected-error {{expected ')' in expression list}}
282+
283+
// TODO: Should we do prefix operator splitting here?
284+
baz(^^/, /)
285+
baz((^^/), /)
286+
245287
func bazbaz(_ x: (Int, Int) -> Int, _ y: Int) {}
246288
bazbaz(/, 0)
289+
bazbaz(^^/, 0)
247290

248291
func qux<T>(_ x: (Int, Int) -> Int, _ y: T) -> Int { 0 }
249292
do {
@@ -257,6 +300,23 @@ do {
257300
// expected-error@-2:21 {{expected ',' separator}}
258301
}
259302
_ = qux(/, 1) // this comment tests to make sure we don't try and end the regex on the starting '/' of '//'.
303+
_ = qux(/, 1) /* same thing with a block comment */
304+
305+
func quxqux(_ x: (Int, Int) -> Int) {}
306+
quxqux(/^/) // expected-error {{cannot convert value of type 'Regex<Substring>' to expected argument type '(Int, Int) -> Int'}}
307+
quxqux((/^/)) // expected-error {{cannot convert value of type 'Regex<Substring>' to expected argument type '(Int, Int) -> Int'}}
308+
quxqux({ $0 /^/ $1 })
309+
310+
// FIXME: We should be able to do operator splitting here and form `!(/^/)`.
311+
quxqux(!/^/) // expected-error {{unary operators must not be juxtaposed; parenthesize inner expression}}
312+
313+
quxqux(/^)
314+
315+
do {
316+
quxqux(/^) / 1
317+
// expected-error@-1 {{closing ')' does not balance any groups openings}}
318+
// expected-error@-2 {{expected ',' separator}}
319+
}
260320

261321
let arr: [Double] = [2, 3, 4]
262322
_ = arr.reduce(1, /) / 3
@@ -284,3 +344,15 @@ _ = /0oG/
284344
_ = /"/
285345
_ = /'/
286346
_ = /<#placeholder#>/
347+
348+
_ = ^^/0xG/
349+
_ = ^^/0oG/
350+
_ = ^^/"/
351+
_ = ^^/'/
352+
_ = ^^/<#placeholder#>/
353+
354+
_ = (^^/0xG/)
355+
_ = (^^/0oG/)
356+
_ = (^^/"/)
357+
_ = (^^/'/)
358+
_ = (^^/<#placeholder#>/)

0 commit comments

Comments
 (0)