Skip to content

Commit 62ad8e4

Browse files
som-snyttlrytz
authored andcommitted
Tweaks and cleanups
1 parent c1067d3 commit 62ad8e4

File tree

9 files changed

+81
-102
lines changed

9 files changed

+81
-102
lines changed

src/compiler/scala/reflect/reify/utils/Extractors.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,8 @@ trait Extractors {
213213

214214
object FreeRef {
215215
def unapply(tree: Tree): Option[(Tree, TermName)] = tree match {
216-
case Apply(Select(Select(Select(uref @ Ident(_), internal), rs), mkIdent), List(Ident(name: TermName)))
217-
if internal == nme.internal && rs == nme.reificationSupport && mkIdent == nme.mkIdent && name.startsWith(nme.REIFY_FREE_PREFIX) =>
216+
case Apply(Select(Select(Select(uref @ Ident(_), nme.internal), nme.reificationSupport), nme.mkIdent), List(Ident(name: TermName)))
217+
if name.startsWith(nme.REIFY_FREE_PREFIX) =>
218218
Some((uref, name))
219219
case _ =>
220220
None

src/compiler/scala/tools/nsc/transform/SpecializeTypes.scala

Lines changed: 36 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ package tools.nsc
1515
package transform
1616

1717
import scala.annotation.{nowarn, tailrec}
18-
import scala.collection.mutable
18+
import scala.collection.mutable, mutable.{AnyRefMap, Buffer, ListBuffer}
1919
import scala.tools.nsc.symtab.Flags
2020
import scala.tools.nsc.Reporting.WarningCategory
2121
import scala.util.chaining._
@@ -75,16 +75,15 @@ abstract class SpecializeTypes extends InfoTransform with TypingTransformers {
7575
override def keepsTypeParams = true
7676

7777
type TypeEnv = Map[Symbol, Type]
78-
def emptyEnv: TypeEnv = Map[Symbol, Type]()
78+
def emptyEnv: TypeEnv = Map.empty[Symbol, Type]
7979

8080
private implicit val typeOrdering: Ordering[Type] = Ordering[String] on ("" + _.typeSymbol.name)
8181

82-
83-
/** TODO - this is a lot of maps.
84-
*/
85-
8682
/** For a given class and concrete type arguments, give its specialized class */
87-
val specializedClass = perRunCaches.newAnyRefMap[Symbol, mutable.AnyRefMap[TypeEnv, Symbol]]()
83+
val specializedClass = perRunCaches.newAnyRefMap[Symbol, AnyRefMap[TypeEnv, Symbol]]()
84+
85+
// read-only map, where missing value defaults to empty immutable.Map
86+
def specializationOf(sym: Symbol) = specializedClass.getOrElse(sym, Map.empty[TypeEnv, Symbol])
8887

8988
/** Map a method symbol to a list of its specialized overloads in the same class. */
9089
private val overloads = perRunCaches.newMap[Symbol, List[Overload]]() withDefaultValue Nil
@@ -334,7 +333,7 @@ abstract class SpecializeTypes extends InfoTransform with TypingTransformers {
334333
if (isSpecializedAnyRefSubtype(tp, orig)) AnyRefTpe
335334
else tp
336335
)
337-
specializedClass.getOrElse(sym, Map.empty[TypeEnv, Symbol]).get(TypeEnv.fromSpecialization(sym, args1)) match {
336+
specializationOf(sym).get(TypeEnv.fromSpecialization(sym, args1)) match {
338337
case Some(sym1) => typeRef(pre1, sym1, survivingArgs(sym, args))
339338
case None => typeRef(pre1, sym, args)
340339
}
@@ -344,12 +343,9 @@ abstract class SpecializeTypes extends InfoTransform with TypingTransformers {
344343

345344
def specializedFunctionName(sym: Symbol, args: List[Type]) = exitingSpecialize {
346345
require(isFunctionSymbol(sym), sym)
347-
val env: TypeEnv = TypeEnv.fromSpecialization(sym, args)
348-
specializedClass.getOrElse(sym, Map.empty[TypeEnv, Symbol]).get(env) match {
349-
case Some(x) =>
350-
x.name
351-
case None =>
352-
sym.name
346+
specializationOf(sym).get(TypeEnv.fromSpecialization(sym, args)) match {
347+
case Some(x) => x.name
348+
case None => sym.name
353349
}
354350
}
355351

@@ -463,12 +459,12 @@ abstract class SpecializeTypes extends InfoTransform with TypingTransformers {
463459
case _ => false
464460
}
465461
def specializedTypeVars(tpes: List[Type]): Set[Symbol] = {
466-
val result = mutable.ListBuffer.empty[Symbol]
462+
val result = ListBuffer.empty[Symbol]
467463
tpes.foreach(specializedTypeVarsBuffer(_, result))
468464
result.toSet
469465
}
470466
def specializedTypeVars(sym: Symbol): Set[Symbol] = {
471-
val result = mutable.ListBuffer.empty[Symbol]
467+
val result = ListBuffer.empty[Symbol]
472468
specializedTypeVarsBuffer(sym, result)
473469
result.toSet
474470
}
@@ -480,12 +476,12 @@ abstract class SpecializeTypes extends InfoTransform with TypingTransformers {
480476
* (arrays are considered as Array[@specialized T])
481477
*/
482478
def specializedTypeVars(tpe: Type): Set[Symbol] = {
483-
val result = new mutable.ListBuffer[Symbol]()
479+
val result = ListBuffer.empty[Symbol]
484480
specializedTypeVarsBuffer(tpe, result)
485481
result.toSet
486482
}
487483

488-
def specializedTypeVarsBuffer(sym: Symbol, result: mutable.Buffer[Symbol]): Unit =
484+
def specializedTypeVarsBuffer(sym: Symbol, result: Buffer[Symbol]): Unit =
489485
if (!neverHasTypeParameters(sym))
490486
enteringTyper(specializedTypeVarsBuffer(sym.info, result))
491487

@@ -495,7 +491,7 @@ abstract class SpecializeTypes extends InfoTransform with TypingTransformers {
495491
* - as arguments to type constructors in @specialized positions
496492
* (arrays are considered as Array[@specialized T])
497493
*/
498-
def specializedTypeVarsBuffer(tpe: Type, result: mutable.Buffer[Symbol]): Unit = tpe match {
494+
def specializedTypeVarsBuffer(tpe: Type, result: Buffer[Symbol]): Unit = tpe match {
499495
case TypeRef(pre, sym, args) =>
500496
if (sym.isAliasType)
501497
specializedTypeVarsBuffer(tpe.dealiasWiden, result)
@@ -601,7 +597,7 @@ abstract class SpecializeTypes extends InfoTransform with TypingTransformers {
601597
* each combination of `stps`.
602598
*/
603599
def specializeClass(clazz: Symbol, outerEnv: TypeEnv): List[Symbol] = {
604-
def specializedClass(env0: TypeEnv, normMembers: List[Symbol]): Symbol = {
600+
def toSpecializedClass(env0: TypeEnv, normMembers: List[Symbol]): Symbol = {
605601
/* It gets hard to follow all the clazz and cls, and specializedClass
606602
* was both already used for a map and mucho long. So "sClass" is the
607603
* specialized subclass of "clazz" throughout this file.
@@ -642,7 +638,7 @@ abstract class SpecializeTypes extends InfoTransform with TypingTransformers {
642638

643639
val env = mapAnyRefsInSpecSym(env0, clazz, sClass)
644640
typeEnv(sClass) = env
645-
this.specializedClass.getOrElseUpdate(clazz, new mutable.AnyRefMap()).update(env0, sClass)
641+
specializedClass.getOrElseUpdate(clazz, AnyRefMap.empty).update(env0, sClass)
646642

647643
val decls1 = newScope // declarations of the newly specialized class 'sClass'
648644
var oldClassTParams: List[Symbol] = Nil // original unspecialized type parameters
@@ -911,7 +907,7 @@ abstract class SpecializeTypes extends InfoTransform with TypingTransformers {
911907
val subclasses = specializations(clazz.info.typeParams).filter(satisfiable(_))
912908
subclasses foreach {
913909
env =>
914-
val spc = specializedClass(env, decls1)
910+
val spc = toSpecializedClass(env, decls1)
915911
val existing = clazz.owner.info.decl(spc.name)
916912

917913
// a symbol for the specialized class already exists if there's a classfile for it.
@@ -1541,10 +1537,10 @@ abstract class SpecializeTypes extends InfoTransform with TypingTransformers {
15411537
}
15421538

15431539
/** Map a specializable method to its rhs, when not deferred. */
1544-
val body = new mutable.AnyRefMap[Symbol, Tree]()
1540+
val body = AnyRefMap.empty[Symbol, Tree]
15451541

15461542
/** Map a specializable method to its value parameter symbols. */
1547-
val parameters = new mutable.AnyRefMap[Symbol, List[Symbol]]()
1543+
val parameters = AnyRefMap.empty[Symbol, List[Symbol]]
15481544

15491545
/** Collect method bodies that are concrete specialized methods.
15501546
*/
@@ -1746,7 +1742,7 @@ abstract class SpecializeTypes extends InfoTransform with TypingTransformers {
17461742

17471743
case Template(parents, self, body) =>
17481744
def transformTemplate = {
1749-
val specMembers = makeSpecializedMembers(tree.symbol.enclClass) ::: (implSpecClasses(body) map localTyper.typed)
1745+
val specMembers = makeSpecializedMembers(tree.symbol.enclClass) ::: implSpecClasses(body).map(localTyper.typed)
17501746
if (!symbol.isPackageClass)
17511747
new CollectMethodBodies()(tree)
17521748
// currentOwner.info.parents.map(tpe => TypeTree(tpe) setPos parents.head.pos)
@@ -1981,7 +1977,7 @@ abstract class SpecializeTypes extends InfoTransform with TypingTransformers {
19811977
// add special overrides first
19821978
// if (!specializedClass.hasFlag(SPECIALIZED))
19831979
// for (m <- specialOverrides(specializedClass)) specializedClass.info.decls.enter(m)
1984-
val mbrs = new mutable.ListBuffer[Tree]
1980+
val mbrs = ListBuffer.empty[Tree]
19851981
var hasSpecializedFields = false
19861982

19871983
for (m <- sClass.info.decls
@@ -2031,25 +2027,22 @@ abstract class SpecializeTypes extends InfoTransform with TypingTransformers {
20312027
}
20322028

20332029
/** Create specialized class definitions */
2034-
def implSpecClasses(trees: List[Tree]): List[Tree] = {
2035-
trees flatMap {
2030+
def implSpecClasses(trees: List[Tree]): List[Tree] =
2031+
trees.flatMap {
20362032
case tree @ ClassDef(_, _, _, impl) =>
2037-
tree.symbol.info // force specialization
2038-
specializedClass.getOrNull(tree.symbol) match {
2039-
case null => Nil
2040-
case map =>
2041-
val sym1 = tree.symbol
2042-
map.iterator.map {
2043-
case (env, specCls) =>
2044-
debuglog("created synthetic class: " + specCls + " of " + sym1 + " in " + pp(env))
2045-
val parents = specCls.info.parents.map(TypeTree)
2046-
ClassDef(specCls, atPos(impl.pos)(Template(parents, noSelfType, List()))
2047-
.setSymbol(specCls.newLocalDummy(sym1.pos))) setPos tree.pos
2048-
}.toList
2049-
}
2033+
val sym1 = tree.symbol
2034+
sym1.info // force specialization
2035+
val specMap = specializationOf(sym1)
2036+
if (specMap.isEmpty) Nil
2037+
else specMap.iterator.map {
2038+
case (env, specCls) =>
2039+
debuglog(s"created synthetic class: $specCls of $sym1 in ${pp(env)}")
2040+
val parents = specCls.info.parents.map(TypeTree)
2041+
ClassDef(specCls, atPos(impl.pos)(Template(parents, noSelfType, List()))
2042+
.setSymbol(specCls.newLocalDummy(sym1.pos))) setPos tree.pos
2043+
}.toList
20502044
case _ => Nil
2051-
} sortBy (_.name.decoded)
2052-
}
2045+
}.sortBy(_.name.decoded)
20532046
}
20542047

20552048
private def forwardCall(pos: scala.reflect.internal.util.Position, receiver: Tree, paramss: List[List[ValDef]]): Tree = {

src/compiler/scala/tools/nsc/typechecker/Namers.scala

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,21 +1040,15 @@ trait Namers extends MethodSynthesis {
10401040

10411041
private def refersToSymbolLessAccessibleThan(tp: Type, sym: Symbol): Boolean = {
10421042
val accessibilityReference =
1043-
if (sym.isValue && sym.owner.isClass && sym.isPrivate)
1044-
sym.getterIn(sym.owner)
1043+
if (sym.isValue && sym.owner.isClass && sym.isPrivate) sym.getterIn(sym.owner)
10451044
else sym
10461045

1047-
@annotation.tailrec def loop(tp: Type): Boolean = tp match {
1048-
case SingleType(pre, sym) =>
1049-
(sym isLessAccessibleThan accessibilityReference) || loop(pre)
1050-
case ThisType(sym) =>
1051-
sym isLessAccessibleThan accessibilityReference
1052-
case p: SimpleTypeProxy =>
1053-
loop(p.underlying)
1054-
case _ =>
1055-
false
1046+
@tailrec def loop(tp: Type): Boolean = tp match {
1047+
case SingleType(pre, sym) => sym.isLessAccessibleThan(accessibilityReference) || loop(pre)
1048+
case ThisType(sym) => sym.isLessAccessibleThan(accessibilityReference)
1049+
case p: SimpleTypeProxy => loop(p.underlying)
1050+
case _ => false
10561051
}
1057-
10581052
loop(tp)
10591053
}
10601054

src/compiler/scala/tools/nsc/util/DocStrings.scala

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -208,15 +208,17 @@ object DocStrings {
208208
str.substring(start, finish)
209209
}
210210

211+
private val sectionTags = List("@param", "@tparam", "@throws")
212+
211213
/** Extract the section text, except for the tag and comment newlines */
212214
def extractSectionText(str: String, section: (Int, Int)): (Int, Int) = {
213215
val (beg, end) = section
214-
if (str.startsWith("@param", beg) ||
215-
str.startsWith("@tparam", beg) ||
216-
str.startsWith("@throws", beg))
217-
(skipWhitespace(str, skipIdent(str, skipWhitespace(str, skipTag(str, beg)))), end)
218-
else
219-
(skipWhitespace(str, skipTag(str, beg)), end)
216+
val skipped =
217+
if (sectionTags.exists(str.startsWith(_, beg)))
218+
skipIdent(str, skipWhitespace(str, skipTag(str, beg)))
219+
else
220+
skipTag(str, beg)
221+
(skipWhitespace(str, skipped), end)
220222
}
221223

222224
/** Cleanup section text */

src/compiler/scala/tools/tasty/Signature.scala

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ object Signature {
4040
* @param result represents the type of the method result
4141
*/
4242
case class MethodSignature[T] private[Signature](params: List[ParamSig[T]], result: T) extends Signature[T] {
43-
def map[U](f: T => U): MethodSignature[U] = this match {
44-
case MethodSignature(params, result) => MethodSignature(params.map(_.map(f)), f(result))
45-
}
43+
def map[U](f: T => U): MethodSignature[U] = MethodSignature(params.map(_.map(f)), f(result))
4644
}
4745

4846
}

src/interactive/scala/tools/nsc/interactive/Global.scala

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -975,16 +975,15 @@ class Global(settings: Settings, _reporter: Reporter, projectName: String = "")
975975
singleType(NoPrefix, tree.symbol)
976976
case Select(qual, _) if treeInfo.admitsTypeSelection(tree) =>
977977
singleType(qual.tpe, tree.symbol)
978-
case Import(expr, selectors) =>
978+
case Import(_, _) =>
979979
tree.symbol.info match {
980980
case ImportType(expr) => expr match {
981-
case s@Select(qual, name) if treeInfo.admitsTypeSelection(expr) => singleType(qual.tpe, s.symbol)
982-
case i : Ident => i.tpe
981+
case Select(qual, _) if treeInfo.admitsTypeSelection(expr) => singleType(qual.tpe, expr.symbol)
982+
case _: Ident => expr.tpe
983983
case _ => tree.tpe
984984
}
985985
case _ => tree.tpe
986986
}
987-
988987
case _ => tree.tpe
989988
}
990989

src/reflect/scala/reflect/internal/SymbolTable.scala

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -457,12 +457,14 @@ abstract class SymbolTable extends macros.Universe
457457
javaCaches = javaCaches.filter(_.isValid)
458458
}
459459

460-
def newWeakMap[K, V]() = recordCache(mutable.WeakHashMap[K, V]())
461-
def newMap[K, V]() = recordCache(mutable.HashMap[K, V]())
462-
def newSet[K]() = recordCache(mutable.HashSet[K]())
463-
def newWeakSet[K <: AnyRef]() = recordCache(new WeakHashSet[K]())
460+
def newWeakMap[K, V]() = recordCache(mutable.WeakHashMap.empty[K, V])
461+
def newMap[K, V]() = recordCache(mutable.HashMap.empty[K, V])
462+
def newSet[K]() = recordCache(mutable.HashSet.empty[K])
463+
def newWeakSet[K <: AnyRef]() = recordCache(WeakHashSet.empty[K])
464+
465+
def newAnyRefMap[K <: AnyRef, V]() = recordCache(mutable.AnyRefMap.empty[K, V])
466+
def newAnyRefMap[K <: AnyRef, V](default: K => V) = recordCache(mutable.AnyRefMap.withDefault[K, V](default))
464467

465-
def newAnyRefMap[K <: AnyRef, V]() = recordCache(mutable.AnyRefMap[K, V]())
466468
/**
467469
* Register a cache specified by a factory function and (optionally) a cleanup function.
468470
*

src/reflect/scala/reflect/internal/util/WeakHashSet.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,4 +425,6 @@ object WeakHashSet {
425425
val defaultLoadFactor = .75
426426

427427
def apply[A <: AnyRef](initialCapacity: Int = WeakHashSet.defaultInitialCapacity, loadFactor: Double = WeakHashSet.defaultLoadFactor) = new WeakHashSet[A](initialCapacity, defaultLoadFactor)
428+
429+
def empty[A <: AnyRef]: WeakHashSet[A] = new WeakHashSet[A]()
428430
}

0 commit comments

Comments
 (0)