Skip to content

Commit

Permalink
Patvar tracking in functions, absolve element names, apply fixes
Browse files Browse the repository at this point in the history
som-snytt committed Aug 8, 2024
1 parent 1d7770f commit 060bc32
Showing 45 changed files with 162 additions and 145 deletions.
1 change: 1 addition & 0 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -538,6 +538,7 @@ lazy val compiler = configureAsSubproject(project)
).get
},
Compile / scalacOptions ++= Seq(
//"-Wunused", "-Wnonunit-statement",
"-Wconf:cat=deprecation&msg=early initializers:s", // compiler heavily relies upon early initializers
),
Compile / doc / scalacOptions ++= Seq(
2 changes: 1 addition & 1 deletion src/compiler/scala/reflect/macros/compiler/Resolvers.scala
Original file line number Diff line number Diff line change
@@ -34,7 +34,7 @@ trait Resolvers {
lazy val (macroImplRef, isBlackbox, macroImplOwner, macroImpl, targs) =
typer.silent(_.typed(markMacroImplRef(untypedMacroImplRef)), reportAmbiguousErrors = false) match {
case SilentResultValue(macroImplRef @ MacroImplReference(_, isBlackbox, owner, meth, targs)) => (macroImplRef, isBlackbox, owner, meth, targs)
case SilentResultValue(macroImplRef) => MacroImplReferenceWrongShapeError()
case SilentResultValue(_) => MacroImplReferenceWrongShapeError()
case ste: SilentTypeError => abort(ste.err.errPos, ste.err.errMsg)
}
}
Original file line number Diff line number Diff line change
@@ -127,7 +127,7 @@ trait Validators {
*/
private lazy val macroImplSig: MacroImplSig = {
val tparams = macroImpl.typeParams
val paramss = transformTypeTagEvidenceParams(macroImplRef, (param, tparam) => NoSymbol)
val paramss = transformTypeTagEvidenceParams(macroImplRef, (_, _) => NoSymbol)
val ret = macroImpl.info.finalResultType
MacroImplSig(tparams, paramss, ret)
}
18 changes: 9 additions & 9 deletions src/compiler/scala/reflect/quasiquotes/Parsers.scala
Original file line number Diff line number Diff line change
@@ -42,16 +42,16 @@ trait Parsers { self: Quasiquotes =>
val posMapList = posMap.toList
def containsOffset(start: Int, end: Int) = start <= offset && offset < end
def fallbackPosition = posMapList match {
case (pos1, (start1, end1)) :: _ if start1 > offset => pos1
case _ :+ ((pos2, (start2, end2))) if end2 <= offset => pos2.withPoint(pos2.point + (end2 - start2))
case x => throw new MatchError(x)
case (pos1, (start1, _)) :: _ if start1 > offset => pos1
case _ :+ ((pos2, (start2, end2))) if end2 <= offset => pos2.withPoint(pos2.point + (end2 - start2))
case x => throw new MatchError(x)
}
posMapList.sliding(2).collect {
case (pos1, (start1, end1)) :: _ if containsOffset(start1, end1) => (pos1, offset - start1)
case (pos1, (start1, end1)) :: (pos2, (start2, _)) :: _ if containsOffset(end1, start2) => (pos1, end1 - start1)
case _ :: (pos2, (start2, end2)) :: _ if containsOffset(start2, end2) => (pos2, offset - start2)
}.map { case (pos, offset) =>
pos.withPoint(pos.point + offset)
case (pos1, (start1, end1)) :: _ if containsOffset(start1, end1) => (pos1, offset - start1)
case (pos1, (start1, end1)) :: (_, (start2, _)) :: _ if containsOffset(end1, start2) => (pos1, end1 - start1)
case _ :: (pos2, (start2, end2)) :: _ if containsOffset(start2, end2) => (pos2, offset - start2)
}.map {
case (pos, offset) => pos.withPoint(pos.point + offset)
}.toList.headOption.getOrElse(fallbackPosition)
}

@@ -92,7 +92,7 @@ trait Parsers { self: Quasiquotes =>
case _ => gen.mkBlock(stats, doFlatten = true)
}
case nme.unapply => gen.mkBlock(stats, doFlatten = false)
case other => global.abort("unreachable")
case _ => global.abort("unreachable")
}

// tq"$a => $b"
4 changes: 2 additions & 2 deletions src/compiler/scala/reflect/quasiquotes/Reifiers.scala
Original file line number Diff line number Diff line change
@@ -319,7 +319,7 @@ trait Reifiers { self: Quasiquotes =>
*/
def group[T](lst: List[T])(similar: (T, T) => Boolean) = lst.foldLeft[List[List[T]]](List()) {
case (Nil, el) => List(List(el))
case (ll :+ (last @ (lastinit :+ lastel)), el) if similar(lastel, el) => ll :+ (last :+ el)
case (ll :+ (last @ _ :+ lastel), el) if similar(lastel, el) => ll :+ (last :+ el)
case (ll, el) => ll :+ List(el)
}

@@ -363,7 +363,7 @@ trait Reifiers { self: Quasiquotes =>
case ParamPlaceholder(Hole(tree, DotDot)) => tree
case SyntacticPatDef(mods, pat, tpt, rhs) =>
reifyBuildCall(nme.SyntacticPatDef, mods, pat, tpt, rhs)
case SyntacticValDef(mods, p @ Placeholder(h: ApplyHole), tpt, rhs) if h.tpe <:< treeType =>
case SyntacticValDef(mods, Placeholder(h: ApplyHole), tpt, rhs) if h.tpe <:< treeType =>
mirrorBuildCall(nme.SyntacticPatDef, reify(mods), h.tree, reify(tpt), reify(rhs))
}

6 changes: 3 additions & 3 deletions src/compiler/scala/reflect/reify/Taggers.scala
Original file line number Diff line number Diff line change
@@ -78,21 +78,21 @@ abstract class Taggers {
translatingReificationErrors(materializer)
}
try c.typecheck(result)
catch { case terr @ TypecheckException(pos, msg) => failTag(result, terr) }
catch { case terr: TypecheckException => failTag(result, terr) }
}

def materializeExpr(universe: Tree, mirror: Tree, expr: Tree): Tree = {
val result = translatingReificationErrors(c.reifyTree(universe, mirror, expr))
try c.typecheck(result)
catch { case terr @ TypecheckException(pos, msg) => failExpr(result, terr) }
catch { case terr: TypecheckException => failExpr(result, terr) }
}

private def translatingReificationErrors(materializer: => Tree): Tree = {
try materializer
catch {
case ReificationException(pos, msg) =>
c.abort(pos.asInstanceOf[c.Position], msg) // this cast is a very small price for the confidence of exception handling
case UnexpectedReificationException(pos, err, cause) if cause != null =>
case UnexpectedReificationException(_, _, cause) if cause != null =>
throw cause
}
}
4 changes: 2 additions & 2 deletions src/compiler/scala/reflect/reify/codegen/GenPositions.scala
Original file line number Diff line number Diff line change
@@ -23,6 +23,6 @@ trait GenPositions {
// but I can hardly imagine when one would need a position that points to the reified code
// usually reified trees are used to compose macro expansions or to be fed to the runtime compiler
// however both macros and toolboxes have their own means to report errors in synthetic trees
def reifyPosition(pos: Position): Tree =
reifyMirrorObject(NoPosition)
@annotation.nowarn
def reifyPosition(pos: Position): Tree = reifyMirrorObject(NoPosition)
}
38 changes: 19 additions & 19 deletions src/compiler/scala/reflect/reify/codegen/GenTypes.scala
Original file line number Diff line number Diff line change
@@ -46,35 +46,35 @@ trait GenTypes {
if (tsym.isClass && tpe == tsym.typeConstructor && tsym.isStatic)
Select(Select(reify(tsym), nme.asType), nme.toTypeConstructor)
else tpe match {
case tpe : NoType.type =>
case tpe: NoType.type =>
reifyMirrorObject(tpe)
case tpe : NoPrefix.type =>
case tpe: NoPrefix.type =>
reifyMirrorObject(tpe)
case tpe @ ThisType(root) if root.isRoot =>
case ThisType(root) if root.isRoot =>
mirrorBuildCall(nme.thisPrefix, mirrorMirrorSelect(nme.RootClass))
case tpe @ ThisType(empty) if empty.isEmptyPackageClass =>
case ThisType(empty) if empty.isEmptyPackageClass =>
mirrorBuildCall(nme.thisPrefix, mirrorMirrorSelect(nme.EmptyPackageClass))
case tpe @ ThisType(clazz) if clazz.isModuleClass && clazz.isStatic =>
case ThisType(clazz) if clazz.isModuleClass && clazz.isStatic =>
val module = reify(clazz.sourceModule)
val moduleClass = Select(Select(module, nme.asModule), nme.moduleClass)
mirrorBuildCall(nme.ThisType, moduleClass)
case tpe @ ThisType(sym) =>
case ThisType(sym) =>
reifyBuildCall(nme.ThisType, sym)
case tpe @ SuperType(thistpe, supertpe) =>
case SuperType(thistpe, supertpe) =>
reifyBuildCall(nme.SuperType, thistpe, supertpe)
case tpe @ SingleType(pre, sym) =>
case SingleType(pre, sym) =>
reifyBuildCall(nme.SingleType, pre, sym)
case tpe @ ConstantType(value) =>
case ConstantType(value) =>
mirrorBuildCall(nme.ConstantType, reifyProduct(value))
case tpe @ TypeRef(pre, sym, args) =>
case TypeRef(pre, sym, args) =>
reifyBuildCall(nme.TypeRef, pre, sym, args)
case tpe @ TypeBounds(lo, hi) =>
case TypeBounds(lo, hi) =>
reifyBuildCall(nme.TypeBounds, lo, hi)
case tpe @ NullaryMethodType(restpe) =>
case NullaryMethodType(restpe) =>
reifyBuildCall(nme.NullaryMethodType, restpe)
case tpe @ AnnotatedType(anns, underlying) =>
case tpe: AnnotatedType =>
reifyAnnotatedType(tpe)
case _ =>
case tpe =>
reifyToughType(tpe)
}
}
@@ -165,7 +165,7 @@ trait GenTypes {
* I.e. we can compile the code that involves `ru.Type`, but we cannot serialize an instance of `ru.Type`.
*/
private def reifySemiConcreteTypeMember(tpe: Type): Tree = tpe match {
case tpe @ TypeRef(pre @ SingleType(prepre, presym), sym, args) if sym.isAbstractType && !sym.isExistential =>
case TypeRef(pre @ SingleType(_, _), sym, args) if sym.isAbstractType && !sym.isExistential =>
mirrorBuildCall(nme.TypeRef, reify(pre), mirrorBuildCall(nme.selectType, reify(sym.owner), reify(sym.name.toString)), reify(args))
case x => throw new MatchError(x)
}
@@ -189,16 +189,16 @@ trait GenTypes {
case tpe @ RefinedType(parents, decls) =>
reifySymDef(tpe.typeSymbol)
mirrorBuildCall(nme.RefinedType, reify(parents), reifyScope(decls), reify(tpe.typeSymbol))
case tpe @ ExistentialType(tparams, underlying) =>
tparams foreach reifySymDef
case ExistentialType(tparams, underlying) =>
tparams.foreach(reifySymDef)
reifyBuildCall(nme.ExistentialType, tparams, underlying)
case tpe @ ClassInfoType(parents, decls, clazz) =>
reifySymDef(clazz)
mirrorBuildCall(nme.ClassInfoType, reify(parents), reifyScope(decls), reify(tpe.typeSymbol))
case tpe @ MethodType(params, restpe) =>
case MethodType(params, restpe) =>
params foreach reifySymDef
reifyBuildCall(nme.MethodType, params, restpe)
case tpe @ PolyType(tparams, underlying) =>
case PolyType(tparams, underlying) =>
tparams foreach reifySymDef
reifyBuildCall(nme.PolyType, tparams, underlying)
case _ =>
2 changes: 1 addition & 1 deletion src/compiler/scala/reflect/reify/codegen/GenUtils.scala
Original file line number Diff line number Diff line change
@@ -117,7 +117,7 @@ trait GenUtils {
@tailrec
final def isCrossStageTypeBearer(tree: Tree): Boolean = tree match {
case TypeApply(hk, _) => isCrossStageTypeBearer(hk)
case Select(sym @ Select(_, ctor), nme.apply) if ctor == nme.WeakTypeTag || ctor == nme.TypeTag || ctor == nme.Expr => true
case Select(Select(_, nme.WeakTypeTag|nme.TypeTag|nme.Expr), nme.apply) => true
case _ => false
}

15 changes: 7 additions & 8 deletions src/compiler/scala/reflect/reify/phases/Reshape.scala
Original file line number Diff line number Diff line change
@@ -13,7 +13,7 @@
package scala.reflect.reify
package phases

import scala.annotation.tailrec
import scala.annotation.{tailrec, unused}
import scala.tools.nsc.symtab.Flags._

trait Reshape {
@@ -192,7 +192,7 @@ trait Reshape {

@tailrec
private def toPreTyperTypedOrAnnotated(tree: Tree): Tree = tree match {
case ty @ Typed(expr1, tpt) =>
case ty @ Typed(_, tpt) =>
if (reifyDebug) println("reify typed: " + tree)
val original = tpt match {
case tt @ TypeTree() => tt.original
@@ -201,11 +201,10 @@ trait Reshape {
val annotatedArg = {
@tailrec
def loop(tree: Tree): Tree = tree match {
case annotated1 @ Annotated(ann, annotated2 @ Annotated(_, _)) => loop(annotated2)
case annotated1 @ Annotated(ann, arg) => arg
case Annotated(_, annotated2 @ Annotated(_, _)) => loop(annotated2)
case Annotated(_, arg) => arg
case _ => EmptyTree
}

loop(original)
}
if (annotatedArg != EmptyTree) {
@@ -220,7 +219,7 @@ trait Reshape {
if (reifyDebug) println("verdict: wasn't annotated, reify as usual")
ty
}
case at @ Annotated(annot, arg) =>
case at @ Annotated(_, arg) =>
if (reifyDebug) println("reify type annotations for: " + tree)
assert(at.tpe.isInstanceOf[AnnotatedType], "%s (%s)".format(at.tpe, at.tpe.kind))
val annot1 = toPreTyperAnnotation(at.tpe.asInstanceOf[AnnotatedType].annotations(0))
@@ -251,7 +250,7 @@ trait Reshape {
New(TypeTree(ann.atp) setOriginal extractOriginal(ann.original), List(args))
}

private def trimAccessors(deff: Tree, stats: List[Tree]): List[Tree] = {
private def trimAccessors(@unused deff: Tree, stats: List[Tree]): List[Tree] = {
val symdefs = (stats collect { case vodef: ValOrDefDef => vodef } map (vodeff => vodeff.symbol -> vodeff)).toMap
val accessors = scala.collection.mutable.Map[ValDef, List[DefDef]]()
stats collect { case ddef: DefDef => ddef } foreach (defdef => {
@@ -311,7 +310,7 @@ trait Reshape {
stats1
}

private def trimSyntheticCaseClassMembers(deff: Tree, stats: List[Tree]): List[Tree] =
private def trimSyntheticCaseClassMembers(@unused deff: Tree, stats: List[Tree]): List[Tree] =
stats filterNot (memberDef => memberDef.isDef && {
val isSynthetic = memberDef.symbol.isSynthetic
// this doesn't work for local classes, e.g. for ones that are top-level to a quasiquote (see comments to companionClass)
4 changes: 2 additions & 2 deletions src/compiler/scala/reflect/reify/utils/Extractors.scala
Original file line number Diff line number Diff line change
@@ -242,8 +242,8 @@ trait Extractors {

object TypeRefToFreeType {
def unapply(tree: Tree): Option[TermName] = tree match {
case Apply(Select(Select(uref @ Ident(_), typeRef), apply), List(Select(_, noSymbol), Ident(freeType: TermName), nil))
if (uref.name == nme.UNIVERSE_SHORT && typeRef == nme.TypeRef && noSymbol == nme.NoSymbol && freeType.startsWith(nme.REIFY_FREE_PREFIX)) =>
case Apply(Select(Select(Ident(nme.UNIVERSE_SHORT), nme.TypeRef), apply@_), List(Select(_, nme.NoSymbol), Ident(freeType: TermName), _))
if freeType.startsWith(nme.REIFY_FREE_PREFIX) =>
Some(freeType)
case _ =>
None
Original file line number Diff line number Diff line change
@@ -23,7 +23,7 @@ trait StdAttachments {
def reifyBinding(tree: Tree): Tree =
tree.attachments.get[ReifyBindingAttachment] match {
case Some(ReifyBindingAttachment(binding)) => binding
case other => Ident(NoSymbol)
case _ => Ident(NoSymbol)
}

case class ReifyAliasAttachment(sym: Symbol, alias: TermName)
3 changes: 2 additions & 1 deletion src/compiler/scala/reflect/reify/utils/SymbolTables.scala
Original file line number Diff line number Diff line change
@@ -13,6 +13,7 @@
package scala.reflect.reify
package utils

import scala.annotation.unused
import scala.collection.{immutable, mutable}, mutable.{ArrayBuffer, ListBuffer}
import java.lang.System.{lineSeparator => EOL}

@@ -89,7 +90,7 @@ trait SymbolTables {
new SymbolTable(newSymtab, newAliases)
}

def add(sym: Symbol, name0: TermName, reification: Tree): SymbolTable = {
def add(@unused sym: Symbol, name0: TermName, reification: Tree): SymbolTable = {
def freshName(name0: TermName): TermName = {
var name = name0.toString
name = name.replace(".type", "$type")
6 changes: 3 additions & 3 deletions src/compiler/scala/tools/nsc/Global.scala
Original file line number Diff line number Diff line change
@@ -376,7 +376,7 @@ class Global(var currentSettings: Settings, reporter0: Reporter)
def ccon = Class.forName(name).getConstructor(classOf[CharsetDecoder], classOf[InternalReporter])

try Some(ccon.newInstance(charset.newDecoder(), reporter).asInstanceOf[SourceReader])
catch { case ex: Throwable =>
catch { case _: Throwable =>
globalError("exception while trying to instantiate source reader '" + name + "'")
None
}
@@ -1651,7 +1651,7 @@ class Global(var currentSettings: Settings, reporter0: Reporter)
compileSources(sources)
}
catch {
case ex: InterruptedException => reporter.cancelled = true
case _: InterruptedException => reporter.cancelled = true
case ex: IOException => globalError(ex.getMessage())
}
}
@@ -1672,7 +1672,7 @@ class Global(var currentSettings: Settings, reporter0: Reporter)
compileSources(sources)
}
catch {
case ex: InterruptedException => reporter.cancelled = true
case _: InterruptedException => reporter.cancelled = true
case ex: IOException => globalError(ex.getMessage())
}
}
2 changes: 1 addition & 1 deletion src/compiler/scala/tools/nsc/PhaseAssembly.scala
Original file line number Diff line number Diff line change
@@ -190,7 +190,7 @@ class DependencyGraph(order: Int, start: String, val components: Map[String, Sub
def sortVertex(i: Int, j: Int): Boolean = sortComponents(componentOf(i), componentOf(j))

distance.zipWithIndex.groupBy(_._1).toList.sortBy(_._1)
.flatMap { case (d, dis) =>
.flatMap { case (_, dis) =>
val vs = dis.map { case (_, i) => i }
val (anchors, followers) = vs.partition(v => edgeTo(v) == null || edgeTo(v).weight != FollowsNow)
//if (debugging) println(s"d=$d, anchors=${anchors.toList.map(n => names(n))}, followers=${followers.toList.map(n => names(n))}")
2 changes: 1 addition & 1 deletion src/compiler/scala/tools/nsc/PipelineMain.scala
Original file line number Diff line number Diff line change
@@ -67,7 +67,7 @@ class PipelineMainClass(argFiles: Seq[Path], pipelineSettings: PipelineMain.Pipe
reporter.echo(NoPosition, msg)
}
private def reporterError(pos: Position, msg: String): Unit = synchronized {
reporter.echo(msg)
reporter.error(pos, msg)
}

private object handler extends UncaughtExceptionHandler {
6 changes: 3 additions & 3 deletions src/compiler/scala/tools/nsc/ast/DocComments.scala
Original file line number Diff line number Diff line change
@@ -185,7 +185,7 @@ trait DocComments { self: Global =>
}

def mergeSection(srcSec: Option[(Int, Int)], dstSec: Option[(Int, Int)]) = dstSec match {
case Some((start, end)) =>
case Some((_, end)) =>
if (end > tocopy) tocopy = end
case None =>
srcSec match {
@@ -312,7 +312,7 @@ trait DocComments { self: Global =>

searchList collectFirst { case x if defs(x) contains vble => defs(x)(vble) } match {
case Some(str) if str startsWith "$" => lookupVariable(str.tail, site)
case s @ Some(str) => s
case s @ Some(_) => s
case None => lookupVariable(vble, site.owner)
}
}
@@ -560,5 +560,5 @@ trait DocComments { self: Global =>
}
}

class ExpansionLimitExceeded(str: String) extends Exception
class ExpansionLimitExceeded(str: String) extends Exception(str)
}
4 changes: 2 additions & 2 deletions src/compiler/scala/tools/nsc/ast/NodePrinters.scala
Original file line number Diff line number Diff line change
@@ -89,7 +89,7 @@ abstract class NodePrinters {
tree match {
case SelectFromTypeTree(qual, name) => showRefTreeName(qual) + "#" + showName(name)
case Select(qual, name) => showRefTreeName(qual) + "." + showName(name)
case id @ Ident(name) => showNameAndPos(id)
case id: Ident => showNameAndPos(id)
case _ => "" + tree
}
}
@@ -358,7 +358,7 @@ abstract class NodePrinters {
tree match {
case t: RefTree => println(showRefTree(t))
case t if t.productArity == 0 => println(treePrefix(t))
case t => printMultiline(tree)(tree.productIterator foreach traverseAny)
case _ => printMultiline(tree)(tree.productIterator foreach traverseAny)
}
}
}
Loading
Oops, something went wrong.

0 comments on commit 060bc32

Please sign in to comment.