Skip to content

Commit

Permalink
Merge pull request #10655 from som-snytt/followup/11850-leftovers
Browse files Browse the repository at this point in the history
Additional non-essential tweaks [ci: last-only]
  • Loading branch information
lrytz committed Jan 11, 2024
2 parents c1067d3 + 62ad8e4 commit 1498a12
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 102 deletions.
4 changes: 2 additions & 2 deletions src/compiler/scala/reflect/reify/utils/Extractors.scala
Expand Up @@ -213,8 +213,8 @@ trait Extractors {

object FreeRef {
def unapply(tree: Tree): Option[(Tree, TermName)] = tree match {
case Apply(Select(Select(Select(uref @ Ident(_), internal), rs), mkIdent), List(Ident(name: TermName)))
if internal == nme.internal && rs == nme.reificationSupport && mkIdent == nme.mkIdent && name.startsWith(nme.REIFY_FREE_PREFIX) =>
case Apply(Select(Select(Select(uref @ Ident(_), nme.internal), nme.reificationSupport), nme.mkIdent), List(Ident(name: TermName)))
if name.startsWith(nme.REIFY_FREE_PREFIX) =>
Some((uref, name))
case _ =>
None
Expand Down
79 changes: 36 additions & 43 deletions src/compiler/scala/tools/nsc/transform/SpecializeTypes.scala
Expand Up @@ -15,7 +15,7 @@ package tools.nsc
package transform

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

type TypeEnv = Map[Symbol, Type]
def emptyEnv: TypeEnv = Map[Symbol, Type]()
def emptyEnv: TypeEnv = Map.empty[Symbol, Type]

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


/** TODO - this is a lot of maps.
*/

/** For a given class and concrete type arguments, give its specialized class */
val specializedClass = perRunCaches.newAnyRefMap[Symbol, mutable.AnyRefMap[TypeEnv, Symbol]]()
val specializedClass = perRunCaches.newAnyRefMap[Symbol, AnyRefMap[TypeEnv, Symbol]]()

// read-only map, where missing value defaults to empty immutable.Map
def specializationOf(sym: Symbol) = specializedClass.getOrElse(sym, Map.empty[TypeEnv, Symbol])

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

def specializedFunctionName(sym: Symbol, args: List[Type]) = exitingSpecialize {
require(isFunctionSymbol(sym), sym)
val env: TypeEnv = TypeEnv.fromSpecialization(sym, args)
specializedClass.getOrElse(sym, Map.empty[TypeEnv, Symbol]).get(env) match {
case Some(x) =>
x.name
case None =>
sym.name
specializationOf(sym).get(TypeEnv.fromSpecialization(sym, args)) match {
case Some(x) => x.name
case None => sym.name
}
}

Expand Down Expand Up @@ -463,12 +459,12 @@ abstract class SpecializeTypes extends InfoTransform with TypingTransformers {
case _ => false
}
def specializedTypeVars(tpes: List[Type]): Set[Symbol] = {
val result = mutable.ListBuffer.empty[Symbol]
val result = ListBuffer.empty[Symbol]
tpes.foreach(specializedTypeVarsBuffer(_, result))
result.toSet
}
def specializedTypeVars(sym: Symbol): Set[Symbol] = {
val result = mutable.ListBuffer.empty[Symbol]
val result = ListBuffer.empty[Symbol]
specializedTypeVarsBuffer(sym, result)
result.toSet
}
Expand All @@ -480,12 +476,12 @@ abstract class SpecializeTypes extends InfoTransform with TypingTransformers {
* (arrays are considered as Array[@specialized T])
*/
def specializedTypeVars(tpe: Type): Set[Symbol] = {
val result = new mutable.ListBuffer[Symbol]()
val result = ListBuffer.empty[Symbol]
specializedTypeVarsBuffer(tpe, result)
result.toSet
}

def specializedTypeVarsBuffer(sym: Symbol, result: mutable.Buffer[Symbol]): Unit =
def specializedTypeVarsBuffer(sym: Symbol, result: Buffer[Symbol]): Unit =
if (!neverHasTypeParameters(sym))
enteringTyper(specializedTypeVarsBuffer(sym.info, result))

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

val env = mapAnyRefsInSpecSym(env0, clazz, sClass)
typeEnv(sClass) = env
this.specializedClass.getOrElseUpdate(clazz, new mutable.AnyRefMap()).update(env0, sClass)
specializedClass.getOrElseUpdate(clazz, AnyRefMap.empty).update(env0, sClass)

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

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

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

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

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

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

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

/** Create specialized class definitions */
def implSpecClasses(trees: List[Tree]): List[Tree] = {
trees flatMap {
def implSpecClasses(trees: List[Tree]): List[Tree] =
trees.flatMap {
case tree @ ClassDef(_, _, _, impl) =>
tree.symbol.info // force specialization
specializedClass.getOrNull(tree.symbol) match {
case null => Nil
case map =>
val sym1 = tree.symbol
map.iterator.map {
case (env, specCls) =>
debuglog("created synthetic class: " + specCls + " of " + sym1 + " in " + pp(env))
val parents = specCls.info.parents.map(TypeTree)
ClassDef(specCls, atPos(impl.pos)(Template(parents, noSelfType, List()))
.setSymbol(specCls.newLocalDummy(sym1.pos))) setPos tree.pos
}.toList
}
val sym1 = tree.symbol
sym1.info // force specialization
val specMap = specializationOf(sym1)
if (specMap.isEmpty) Nil
else specMap.iterator.map {
case (env, specCls) =>
debuglog(s"created synthetic class: $specCls of $sym1 in ${pp(env)}")
val parents = specCls.info.parents.map(TypeTree)
ClassDef(specCls, atPos(impl.pos)(Template(parents, noSelfType, List()))
.setSymbol(specCls.newLocalDummy(sym1.pos))) setPos tree.pos
}.toList
case _ => Nil
} sortBy (_.name.decoded)
}
}.sortBy(_.name.decoded)
}

private def forwardCall(pos: scala.reflect.internal.util.Position, receiver: Tree, paramss: List[List[ValDef]]): Tree = {
Expand Down
18 changes: 6 additions & 12 deletions src/compiler/scala/tools/nsc/typechecker/Namers.scala
Expand Up @@ -1040,21 +1040,15 @@ trait Namers extends MethodSynthesis {

private def refersToSymbolLessAccessibleThan(tp: Type, sym: Symbol): Boolean = {
val accessibilityReference =
if (sym.isValue && sym.owner.isClass && sym.isPrivate)
sym.getterIn(sym.owner)
if (sym.isValue && sym.owner.isClass && sym.isPrivate) sym.getterIn(sym.owner)
else sym

@annotation.tailrec def loop(tp: Type): Boolean = tp match {
case SingleType(pre, sym) =>
(sym isLessAccessibleThan accessibilityReference) || loop(pre)
case ThisType(sym) =>
sym isLessAccessibleThan accessibilityReference
case p: SimpleTypeProxy =>
loop(p.underlying)
case _ =>
false
@tailrec def loop(tp: Type): Boolean = tp match {
case SingleType(pre, sym) => sym.isLessAccessibleThan(accessibilityReference) || loop(pre)
case ThisType(sym) => sym.isLessAccessibleThan(accessibilityReference)
case p: SimpleTypeProxy => loop(p.underlying)
case _ => false
}

loop(tp)
}

Expand Down
14 changes: 8 additions & 6 deletions src/compiler/scala/tools/nsc/util/DocStrings.scala
Expand Up @@ -208,15 +208,17 @@ object DocStrings {
str.substring(start, finish)
}

private val sectionTags = List("@param", "@tparam", "@throws")

/** Extract the section text, except for the tag and comment newlines */
def extractSectionText(str: String, section: (Int, Int)): (Int, Int) = {
val (beg, end) = section
if (str.startsWith("@param", beg) ||
str.startsWith("@tparam", beg) ||
str.startsWith("@throws", beg))
(skipWhitespace(str, skipIdent(str, skipWhitespace(str, skipTag(str, beg)))), end)
else
(skipWhitespace(str, skipTag(str, beg)), end)
val skipped =
if (sectionTags.exists(str.startsWith(_, beg)))
skipIdent(str, skipWhitespace(str, skipTag(str, beg)))
else
skipTag(str, beg)
(skipWhitespace(str, skipped), end)
}

/** Cleanup section text */
Expand Down
4 changes: 1 addition & 3 deletions src/compiler/scala/tools/tasty/Signature.scala
Expand Up @@ -40,9 +40,7 @@ object Signature {
* @param result represents the type of the method result
*/
case class MethodSignature[T] private[Signature](params: List[ParamSig[T]], result: T) extends Signature[T] {
def map[U](f: T => U): MethodSignature[U] = this match {
case MethodSignature(params, result) => MethodSignature(params.map(_.map(f)), f(result))
}
def map[U](f: T => U): MethodSignature[U] = MethodSignature(params.map(_.map(f)), f(result))
}

}
7 changes: 3 additions & 4 deletions src/interactive/scala/tools/nsc/interactive/Global.scala
Expand Up @@ -975,16 +975,15 @@ class Global(settings: Settings, _reporter: Reporter, projectName: String = "")
singleType(NoPrefix, tree.symbol)
case Select(qual, _) if treeInfo.admitsTypeSelection(tree) =>
singleType(qual.tpe, tree.symbol)
case Import(expr, selectors) =>
case Import(_, _) =>
tree.symbol.info match {
case ImportType(expr) => expr match {
case s@Select(qual, name) if treeInfo.admitsTypeSelection(expr) => singleType(qual.tpe, s.symbol)
case i : Ident => i.tpe
case Select(qual, _) if treeInfo.admitsTypeSelection(expr) => singleType(qual.tpe, expr.symbol)
case _: Ident => expr.tpe
case _ => tree.tpe
}
case _ => tree.tpe
}

case _ => tree.tpe
}

Expand Down
12 changes: 7 additions & 5 deletions src/reflect/scala/reflect/internal/SymbolTable.scala
Expand Up @@ -457,12 +457,14 @@ abstract class SymbolTable extends macros.Universe
javaCaches = javaCaches.filter(_.isValid)
}

def newWeakMap[K, V]() = recordCache(mutable.WeakHashMap[K, V]())
def newMap[K, V]() = recordCache(mutable.HashMap[K, V]())
def newSet[K]() = recordCache(mutable.HashSet[K]())
def newWeakSet[K <: AnyRef]() = recordCache(new WeakHashSet[K]())
def newWeakMap[K, V]() = recordCache(mutable.WeakHashMap.empty[K, V])
def newMap[K, V]() = recordCache(mutable.HashMap.empty[K, V])
def newSet[K]() = recordCache(mutable.HashSet.empty[K])
def newWeakSet[K <: AnyRef]() = recordCache(WeakHashSet.empty[K])

def newAnyRefMap[K <: AnyRef, V]() = recordCache(mutable.AnyRefMap.empty[K, V])
def newAnyRefMap[K <: AnyRef, V](default: K => V) = recordCache(mutable.AnyRefMap.withDefault[K, V](default))

def newAnyRefMap[K <: AnyRef, V]() = recordCache(mutable.AnyRefMap[K, V]())
/**
* Register a cache specified by a factory function and (optionally) a cleanup function.
*
Expand Down
2 changes: 2 additions & 0 deletions src/reflect/scala/reflect/internal/util/WeakHashSet.scala
Expand Up @@ -425,4 +425,6 @@ object WeakHashSet {
val defaultLoadFactor = .75

def apply[A <: AnyRef](initialCapacity: Int = WeakHashSet.defaultInitialCapacity, loadFactor: Double = WeakHashSet.defaultLoadFactor) = new WeakHashSet[A](initialCapacity, defaultLoadFactor)

def empty[A <: AnyRef]: WeakHashSet[A] = new WeakHashSet[A]()
}

0 comments on commit 1498a12

Please sign in to comment.