Skip to content

Commit fb73606

Browse files
committed
Move sliceToTermName logic to Decorators
That way we can keep Contexts out of Names.
1 parent 6ccfdc1 commit fb73606

File tree

5 files changed

+25
-33
lines changed

5 files changed

+25
-33
lines changed

compiler/src/dotty/tools/dotc/config/Feature.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ package config
55
import core._
66
import Contexts._, Symbols._, Names._, NameOps._, Phases._
77
import StdNames.nme
8-
import Decorators.{given _}
8+
import Decorators.{_, given _}
99
import util.SrcPos
1010
import SourceVersion._
1111
import reporting.Message

compiler/src/dotty/tools/dotc/config/SourceVersion.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ package config
55
import core.Contexts._
66
import core.Names.TermName
77
import core.StdNames.nme
8-
import core.Decorators.{given _}
8+
import core.Decorators.{_, given _}
99
import util.Property
1010

1111
enum SourceVersion:

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

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,34 @@ object Decorators {
2020
* with a normal wildcard. In the future, once #9255 is in trunk, replace with
2121
* a simple collective extension.
2222
*/
23-
implicit object PreNamedString:
24-
extension (pn: PreName) def toTypeName: TypeName = pn match
25-
case s: String => typeName(s)
26-
case n: Name => n.toTypeName
27-
extension (pn: PreName) def toTermName: TermName = pn match
23+
extension (pn: PreName)
24+
def toTermName: TermName = pn match
2825
case s: String => termName(s)
2926
case n: Name => n.toTermName
27+
def toTypeName: TypeName = pn match
28+
case s: String => typeName(s)
29+
case n: Name => n.toTypeName
3030

3131
extension (s: String):
32-
def splitWhere(f: Char => Boolean, doDropIndex: Boolean): Option[(String, String)] = {
32+
def splitWhere(f: Char => Boolean, doDropIndex: Boolean): Option[(String, String)] =
3333
def splitAt(idx: Int, doDropIndex: Boolean): Option[(String, String)] =
3434
if (idx == -1) None
3535
else Some((s.take(idx), s.drop(if (doDropIndex) idx + 1 else idx)))
36-
3736
splitAt(s.indexWhere(f), doDropIndex)
38-
}
37+
38+
/** Create a term name from a string slice, using a common buffer.
39+
* This avoids some allocation relative to `termName(s)`
40+
*/
41+
def sliceToTermName(start: Int, end: Int)(using Context): SimpleName =
42+
val base = ctx.base
43+
val len = end - start
44+
while len > base.nameCharBuffer.length do
45+
base.nameCharBuffer = new Array[Char](base.nameCharBuffer.length * 2)
46+
s.getChars(start, end, base.nameCharBuffer, 0)
47+
termName(base.nameCharBuffer, 0, len)
48+
49+
def sliceToTypeName(start: Int, end: Int)(using Context): TypeName =
50+
sliceToTermName(start, end).toTypeName
3951

4052
/** Implements a findSymbol method on iterators of Symbols that
4153
* works like find but avoids Option, replacing None with NoSymbol.

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

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import scala.internal.Chars.isIdentifierStart
1111
import collection.immutable
1212
import config.Config
1313
import java.util.HashMap
14-
import Contexts.{Context, ctx}
1514

1615
import scala.annotation.internal.sharable
1716

@@ -640,33 +639,14 @@ object Names {
640639
termName(bs, offset, len).toTypeName
641640

642641
/** Create a term name from a string.
643-
* See `termName(s: String, from: Int, len: Int) for a more efficient version
644-
* which however requires a Context for its operation
642+
* See `sliceToTermName` in `Decorators` for a more efficient version
643+
* which however requires a Context for its operation.
645644
*/
646645
def termName(s: String): SimpleName = termName(s.toCharArray, 0, s.length)
647646

648647
/** Create a type name from a string */
649648
def typeName(s: String): TypeName = typeName(s.toCharArray, 0, s.length)
650649

651-
/** Create a term name from a string slice, using a common buffer.
652-
* This avoids some allocation relative to `termName(s: String)`
653-
*/
654-
def termName(s: String, from: Int, len: Int)(using ctx: Contexts.Context): TermName = {
655-
val base = ctx.base
656-
657-
while (len > base.nameCharBuffer.length)
658-
base.nameCharBuffer = new Array[Char](base.nameCharBuffer.length * 2)
659-
660-
s.getChars(from, from + len, base.nameCharBuffer, 0)
661-
termName(base.nameCharBuffer, 0, len)
662-
}
663-
664-
/** Create a type name from a string slice, using a common buffer.
665-
* This avoids some allocation relative to `typeName(s: String)`
666-
*/
667-
def typeName(s: String, from: Int, len: Int)(using Context): TypeName =
668-
termName(s, from, len).toTypeName
669-
670650
table(0) = new SimpleName(-1, 0, null)
671651

672652
/** The term name represented by the empty string */

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ object SymbolLoaders {
205205
src.lastModified >= bin.lastModified
206206

207207
private def nameOf(classRep: ClassRepresentation)(using Context): TermName =
208-
termName(classRep.fileName, 0, classRep.nameLength)
208+
classRep.fileName.sliceToTermName(0, classRep.nameLength)
209209

210210
/** Load contents of a package
211211
*/

0 commit comments

Comments
 (0)