Skip to content

Commit f9a6a8b

Browse files
committed
[test] Add some additional /.../ tests
1 parent 1618cf0 commit f9a6a8b

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

@@ -231,7 +259,7 @@ _ = /x/*comment*/
231259
// expected-error@-1 {{unterminated regex literal}}
232260

233261
// These become regex literals, unless surrounded in parens.
234-
func baz(_ x: (Int, Int) -> Int, _ y: (Int, Int) -> Int) {} // expected-note 2{{'baz' declared here}}
262+
func baz(_ x: (Int, Int) -> Int, _ y: (Int, Int) -> Int) {} // expected-note 3{{'baz' declared here}}
235263
baz(/, /)
236264
// expected-error@-1 {{cannot convert value of type 'Regex<Substring>' to expected argument type '(Int, Int) -> Int'}}
237265
// expected-error@-2 {{missing argument for parameter #2 in call}}
@@ -240,8 +268,23 @@ baz(/,/)
240268
// expected-error@-2 {{missing argument for parameter #2 in call}}
241269
baz((/), /)
242270

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

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

259319
let arr: [Double] = [2, 3, 4]
260320
_ = arr.reduce(1, /) / 3
@@ -282,3 +342,15 @@ _ = /0oG/
282342
_ = /"/
283343
_ = /'/
284344
_ = /<#placeholder#>/
345+
346+
_ = ^^/0xG/
347+
_ = ^^/0oG/
348+
_ = ^^/"/
349+
_ = ^^/'/
350+
_ = ^^/<#placeholder#>/
351+
352+
_ = (^^/0xG/)
353+
_ = (^^/0oG/)
354+
_ = (^^/"/)
355+
_ = (^^/'/)
356+
_ = (^^/<#placeholder#>/)

0 commit comments

Comments
 (0)