Skip to content

Commit d75c32d

Browse files
authored
Merge pull request #14431 from dotty-staging/scala-compiletime-ops-string-charat
Add compiletime.ops.string.CharAt
2 parents bdb471a + 96a4c1b commit d75c32d

File tree

11 files changed

+33
-42
lines changed

11 files changed

+33
-42
lines changed

compiler/src/dotty/tools/dotc/core/Definitions.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1167,7 +1167,7 @@ class Definitions {
11671167
)
11681168
private val compiletimePackageBooleanTypes: Set[Name] = Set(tpnme.Not, tpnme.Xor, tpnme.And, tpnme.Or)
11691169
private val compiletimePackageStringTypes: Set[Name] = Set(
1170-
tpnme.Plus, tpnme.Length, tpnme.Substring, tpnme.Matches
1170+
tpnme.Plus, tpnme.Length, tpnme.Substring, tpnme.Matches, tpnme.CharAt
11711171
)
11721172
private val compiletimePackageOpTypes: Set[Name] =
11731173
Set(tpnme.S)

compiler/src/dotty/tools/dotc/core/StdNames.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ object StdNames {
234234
final val Plus: N = "+"
235235
final val S: N = "S"
236236
final val Substring: N = "Substring"
237+
final val CharAt: N = "CharAt"
237238
final val Times: N = "*"
238239
final val ToInt: N = "ToInt"
239240
final val ToLong: N = "ToLong"

compiler/src/dotty/tools/dotc/core/Types.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4456,6 +4456,8 @@ object Types {
44564456
case tpnme.Matches => constantFold2(stringValue, _ matches _)
44574457
case tpnme.Substring =>
44584458
constantFold3(stringValue, intValue, intValue, (s, b, e) => s.substring(b, e))
4459+
case tpnme.CharAt =>
4460+
constantFold2AB(stringValue, intValue, _ charAt _)
44594461
case _ => None
44604462
} else if (owner == defn.CompiletimeOpsBooleanModuleClass) name match {
44614463
case tpnme.Not => constantFold1(boolValue, x => !x)

library/src/scala/compiletime/ops/any.scala

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package scala.compiletime
22
package ops
33

4-
import annotation.experimental
5-
64
object any:
75
/** Equality comparison of two singleton types.
86
* ```scala
@@ -41,7 +39,6 @@ object any:
4139
* ```
4240
* @syntax markdown
4341
*/
44-
@experimental
4542
type IsConst[X] <: Boolean
4643

4744
/** String conversion of a constant singleton type.
@@ -51,5 +48,4 @@ object any:
5148
* ```
5249
* @syntax markdown
5350
*/
54-
@experimental
5551
type ToString[+X] <: String

library/src/scala/compiletime/ops/double.scala

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package scala.compiletime
22
package ops
33

4-
import scala.annotation.experimental
5-
6-
@experimental
74
object double:
85
/** Addition of two `Double` singleton types.
96
* ```scala

library/src/scala/compiletime/ops/float.scala

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package scala.compiletime
22
package ops
33

4-
import scala.annotation.experimental
5-
6-
@experimental
74
object float:
85
/** Addition of two `Float` singleton types.
96
* ```scala

library/src/scala/compiletime/ops/int.scala

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package scala.compiletime
22
package ops
33

4-
import annotation.experimental
5-
64
object int:
75
/** Successor of a natural number where zero is the type 0 and successors are reduced as if the definition was:
86
*
@@ -192,7 +190,6 @@ object int:
192190
* ```
193191
* @syntax markdown
194192
*/
195-
@experimental
196193
type ToLong[+X <: Int] <: Long
197194

198195
/** Float conversion of an `Int` singleton type.
@@ -201,7 +198,6 @@ object int:
201198
* ```
202199
* @syntax markdown
203200
*/
204-
@experimental
205201
type ToFloat[+X <: Int] <: Float
206202

207203
/** Double conversion of an `Int` singleton type.
@@ -210,7 +206,6 @@ object int:
210206
* ```
211207
* @syntax markdown
212208
*/
213-
@experimental
214209
type ToDouble[+X <: Int] <: Double
215210

216211
/** Number of zero bits preceding the highest-order ("leftmost")
@@ -225,5 +220,4 @@ object int:
225220
* ```
226221
* @syntax markdown
227222
*/
228-
@experimental
229223
type NumberOfLeadingZeros[+X <: Int] <: Int

library/src/scala/compiletime/ops/long.scala

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package scala.compiletime
22
package ops
33

4-
import scala.annotation.experimental
5-
6-
@experimental
74
object long:
85
/** Successor of a natural number where zero is the type 0 and successors are reduced as if the definition was:
96
*

library/src/scala/compiletime/ops/string.scala

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package scala.compiletime
22
package ops
33

4-
import scala.annotation.experimental
5-
64
object string:
75
/** Concatenation of two `String` singleton types.
86
* ```scala
@@ -18,7 +16,6 @@ object string:
1816
* ```
1917
* @syntax markdown
2018
*/
21-
@experimental
2219
type Length[+X <: String] <: Int
2320

2421
/** Substring of a `String` singleton type, with a singleton type
@@ -31,7 +28,6 @@ object string:
3128
* ```
3229
* @syntax markdown
3330
*/
34-
@experimental
3531
type Substring[+S <: String, +IBeg <: Int, +IEnd <: Int] <: String
3632

3733
/** Tests if this `String` singleton type matches the given
@@ -41,5 +37,14 @@ object string:
4137
* ```
4238
* @syntax markdown
4339
*/
44-
@experimental
4540
type Matches[+S <: String, +Regex <: String] <: Boolean
41+
42+
/** Returns the Char type at the specified index.
43+
* An index ranges from 0 to Length[S] - 1. The first Char of
44+
* the sequence is at index 0, the next at index 1, and so on.
45+
* ```scala
46+
* val c: CharAt["hello", 0] = 'h'
47+
* ```
48+
* @syntax markdown
49+
*/
50+
type CharAt[+S <: String, +Idx <: Int] <: Char

tests/neg/singleton-ops-string.scala

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,23 @@ object Test {
1515

1616
val t9: Matches["hamburger", "ham.*"] = true
1717
val t10: Matches["hamburger", "ham.*"] = false // error
18+
19+
val t11: CharAt["String", 0] = 'S'
20+
val t12: CharAt["String", 1] = 't'
21+
val t13: CharAt["String", 2] = '!' // error
22+
// ^^^
23+
// Found: ('!' : Char)
24+
// Required: ('r' : Char)
25+
val t14: CharAt["String", 3] = '!' // error
26+
// ^^^
27+
// Found: ('!' : Char)
28+
// Required: ('i' : Char)
29+
val t15: CharAt["String", 4] = 'n'
30+
val t16: CharAt["String", 5] = 'g'
31+
val t17: CharAt["String", 6] = '!' // error
32+
// ^
33+
// String index out of range: 6
34+
val t18: CharAt["String", -1] = '?' // error
35+
// ^
36+
// String index out of range: -1
1837
}

tests/run-custom-args/tasty-inspector/stdlibExperimentalDefinitions.scala

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,6 @@ val experimentalDefinitionInLibrary = Set(
3232
"scala.annotation.MainAnnotation",
3333
"scala.annotation.MainAnnotation$",
3434

35-
//// New APIs: compiletime.ops
36-
// Can be stabilized in 3.3.0 or later.
37-
// Needs user feedback
38-
"scala.compiletime.ops.any$.IsConst",
39-
"scala.compiletime.ops.any$.ToString",
40-
"scala.compiletime.ops.double", "scala.compiletime.ops.double$",
41-
"scala.compiletime.ops.float",
42-
"scala.compiletime.ops.float$",
43-
"scala.compiletime.ops.int$.NumberOfLeadingZeros",
44-
"scala.compiletime.ops.int$.ToDouble",
45-
"scala.compiletime.ops.int$.ToFloat",
46-
"scala.compiletime.ops.int$.ToLong",
47-
"scala.compiletime.ops.long", "scala.compiletime.ops.long$",
48-
"scala.compiletime.ops.string$.Length",
49-
"scala.compiletime.ops.string$.Matches",
50-
"scala.compiletime.ops.string$.Substring",
51-
5235
//// New APIs: Mirror
5336
// Can be stabilized in 3.3.0 or later.
5437
"scala.deriving.Mirror$.fromProductTyped", // This API is a bit convoluted. We may need some more feedback before we can stabilize it.

0 commit comments

Comments
 (0)