Skip to content

Commit 4c0ab7b

Browse files
committed
Optimise isSpecializedNameOf & misc docs/renames
1 parent 1124199 commit 4c0ab7b

File tree

3 files changed

+18
-13
lines changed

3 files changed

+18
-13
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1330,9 +1330,9 @@ class Definitions {
13301330
@tu lazy val TupleType: Array[TypeRef | Null] = mkArityArray("scala.Tuple", MaxTupleArity, 1)
13311331

13321332
def isSpecializedTuple(cls: Symbol)(using Context): Boolean =
1333-
TupleSpecializedClasses.exists(tupleCls => cls.name.isSpecializedNameOf(tupleCls.name))
1333+
cls.isClass && TupleSpecializedClasses.exists(tupleCls => cls.name.isSpecializedNameOf(tupleCls.name))
13341334

1335-
def SpecialisedTuple(base: Symbol, args: List[Type])(using Context): Symbol =
1335+
def SpecializedTuple(base: Symbol, args: List[Type])(using Context): Symbol =
13361336
base.owner.requiredClass(base.name.specializedName(args))
13371337

13381338
private class FunType(prefix: String):

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

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import scala.io.Codec
88
import Int.MaxValue
99
import Names._, StdNames._, Contexts._, Symbols._, Flags._, NameKinds._, Types._
1010
import util.Chars.{isOperatorPart, digit2int}
11+
import Decorators.*
1112
import Definitions._
1213
import nme._
1314

@@ -278,16 +279,20 @@ object NameOps {
278279
classTags.fold(nme.EMPTY)(_ ++ _) ++ nme.specializedTypeNames.suffix)
279280
}
280281

282+
/** Determines if the current name is the specialized name of the given base name.
283+
* For example `typeName("Tuple2$mcII$sp").isSpecializedNameOf(tpnme.Tuple2) == true`
284+
*/
281285
def isSpecializedNameOf(base: N)(using Context): Boolean =
282-
import Decorators.*
283-
val sb = new StringBuilder
284-
sb.append(base.toString)
285-
sb.append(nme.specializedTypeNames.prefix.toString)
286-
sb.append(nme.specializedTypeNames.separator)
287-
val prefix = sb.toString()
288-
val suffix = nme.specializedTypeNames.suffix.toString
289-
name.startsWith(prefix) && name.endsWith(suffix)
290-
286+
var i = 0
287+
inline def nextString(str: String) = name.startsWith(str, i) && { i += str.length; true }
288+
nextString(base.toString)
289+
&& nextString(nme.specializedTypeNames.prefix.toString)
290+
&& nextString(nme.specializedTypeNames.separator.toString)
291+
&& name.endsWith(nme.specializedTypeNames.suffix.toString)
292+
293+
/** Returns the name of the class specialised to the provided types,
294+
* in the given order. Used for the specialized tuple classes.
295+
*/
291296
def specializedName(args: List[Type])(using Context): N =
292297
val sb = new StringBuilder
293298
sb.append(name.toString)

compiler/src/dotty/tools/dotc/transform/SpecializeTuples.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ class SpecializeTuples extends MiniPhase:
2727
if fun.symbol.name == nme.apply && fun.symbol.exists && defn.isSpecializableTuple(fun.symbol.owner.companionClass, targs.map(_.tpe))
2828
&& isElideableExpr(tree)
2929
=>
30-
cpy.Apply(tree)(Select(New(defn.SpecialisedTuple(fun.symbol.owner.companionClass, targs.map(_.tpe)).typeRef), nme.CONSTRUCTOR), args).withType(tree.tpe)
30+
cpy.Apply(tree)(Select(New(defn.SpecializedTuple(fun.symbol.owner.companionClass, targs.map(_.tpe)).typeRef), nme.CONSTRUCTOR), args).withType(tree.tpe)
3131
case Apply(TypeApply(fun: NameTree, targs), args)
3232
if fun.symbol.name == nme.CONSTRUCTOR && fun.symbol.exists && defn.isSpecializableTuple(fun.symbol.owner, targs.map(_.tpe))
3333
&& isElideableExpr(tree)
3434
=>
35-
cpy.Apply(tree)(Select(New(defn.SpecialisedTuple(fun.symbol.owner, targs.map(_.tpe)).typeRef), nme.CONSTRUCTOR), args).withType(tree.tpe)
35+
cpy.Apply(tree)(Select(New(defn.SpecializedTuple(fun.symbol.owner, targs.map(_.tpe)).typeRef), nme.CONSTRUCTOR), args).withType(tree.tpe)
3636
case _ => tree
3737
end transformApply
3838

0 commit comments

Comments
 (0)