Skip to content

Commit 1e3739c

Browse files
authored
Refactor Tree parameterization, with tweaks (#16301)
- Refactor `Tree` parameterization - Some trailing Tree refactor changes
2 parents b50a29a + 1685473 commit 1e3739c

14 files changed

+188
-204
lines changed

compiler/src/dotty/tools/dotc/ast/TreeInfo.scala

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@ import scala.collection.mutable
1414

1515
import scala.annotation.tailrec
1616

17-
trait TreeInfo[T >: Untyped <: Type] { self: Trees.Instance[T] =>
18-
19-
// Note: the <: Type constraint looks necessary (and is needed to make the file compile in dotc).
20-
// But Scalac accepts the program happily without it. Need to find out why.
17+
trait TreeInfo[T <: Untyped] { self: Trees.Instance[T] =>
2118

2219
def unsplice(tree: Trees.Tree[T]): Trees.Tree[T] = tree
2320

compiler/src/dotty/tools/dotc/ast/Trees.scala

Lines changed: 153 additions & 155 deletions
Large diffs are not rendered by default.

compiler/src/dotty/tools/dotc/ast/untpd.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ object untpd extends Trees.Instance[Untyped] with UntypedTreeInfo {
4242
/** mods object name impl */
4343
case class ModuleDef(name: TermName, impl: Template)(implicit @constructorOnly src: SourceFile)
4444
extends MemberDef {
45-
type ThisTree[-T >: Untyped] <: Trees.NameTree[T] with Trees.MemberDef[T] with ModuleDef
45+
type ThisTree[+T <: Untyped] <: Trees.NameTree[T] with Trees.MemberDef[T] with ModuleDef
4646
def withName(name: Name)(using Context): ModuleDef = cpy.ModuleDef(this)(name.toTermName, impl)
4747
}
4848

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,9 @@ object Contexts {
156156
final def owner: Symbol = _owner
157157

158158
/** The current tree */
159-
private var _tree: Tree[? >: Untyped]= _
160-
protected def tree_=(tree: Tree[? >: Untyped]): Unit = _tree = tree
161-
final def tree: Tree[? >: Untyped] = _tree
159+
private var _tree: Tree[?]= _
160+
protected def tree_=(tree: Tree[?]): Unit = _tree = tree
161+
final def tree: Tree[?] = _tree
162162

163163
/** The current scope */
164164
private var _scope: Scope = _
@@ -469,7 +469,7 @@ object Contexts {
469469
}
470470

471471
/** The context of expression `expr` seen as a member of a statement sequence */
472-
def exprContext(stat: Tree[? >: Untyped], exprOwner: Symbol): Context =
472+
def exprContext(stat: Tree[?], exprOwner: Symbol): Context =
473473
if (exprOwner == this.owner) this
474474
else if (untpd.isSuperConstrCall(stat) && this.owner.isClass) superCallContext
475475
else fresh.setOwner(exprOwner)
@@ -592,7 +592,7 @@ object Contexts {
592592
assert(owner != NoSymbol)
593593
this.owner = owner
594594
this
595-
def setTree(tree: Tree[? >: Untyped]): this.type =
595+
def setTree(tree: Tree[?]): this.type =
596596
util.Stats.record("Context.setTree")
597597
this.tree = tree
598598
this

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

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -745,16 +745,6 @@ object Types {
745745
// which means that we always defensively copy the type in the future. This second
746746
// measure is necessary because findMember calls might be cached, so do not
747747
// necessarily appear in nested order.
748-
// Without the defensive copy, Typer.scala fails to compile at the line
749-
//
750-
// untpd.rename(lhsCore, setterName).withType(setterType), WildcardType)
751-
//
752-
// because the subtype check
753-
//
754-
// ThisTree[Untyped]#ThisTree[Typed] <: Tree[Typed]
755-
//
756-
// fails (in fact it thinks the underlying type of the LHS is `Tree[Untyped]`.)
757-
//
758748
// Without the `openedTwice` trick, Typer.scala fails to Ycheck
759749
// at phase resolveSuper.
760750
val rt =

compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,7 @@ class PlainPrinter(_ctx: Context) extends Printer {
607607
def toText(sc: Scope): Text =
608608
("Scope{" ~ dclsText(sc.toList) ~ "}").close
609609

610-
def toText[T >: Untyped](tree: Tree[T]): Text = {
610+
def toText[T <: Untyped](tree: Tree[T]): Text = {
611611
def toTextElem(elem: Any): Text = elem match {
612612
case elem: Showable => elem.toText(this)
613613
case elem: List[?] => "List(" ~ Text(elem map toTextElem, ",") ~ ")"

compiler/src/dotty/tools/dotc/printing/Printer.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ abstract class Printer {
149149
def toText(sc: Scope): Text
150150

151151
/** Textual representation of tree */
152-
def toText[T >: Untyped](tree: Tree[T]): Text
152+
def toText[T <: Untyped](tree: Tree[T]): Text
153153

154154
/** Textual representation of source position */
155155
def toText(pos: SourcePosition): Text

compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
4040

4141
override def printerContext: Context = myCtx
4242

43-
def withEnclosingDef(enclDef: Tree[? >: Untyped])(op: => Text): Text = {
43+
def withEnclosingDef(enclDef: Tree[?])(op: => Text): Text = {
4444
val savedCtx = myCtx
4545
if (enclDef.hasType && enclDef.symbol.exists)
4646
myCtx = ctx.withOwner(enclDef.symbol)
@@ -308,15 +308,15 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
308308
protected def exprToText(tp: ExprType): Text =
309309
"=> " ~ toText(tp.resType)
310310

311-
protected def blockToText[T >: Untyped](block: Block[T]): Text =
311+
protected def blockToText[T <: Untyped](block: Block[T]): Text =
312312
blockText(block.stats :+ block.expr)
313313

314-
protected def blockText[T >: Untyped](trees: List[Tree[T]]): Text =
314+
protected def blockText[T <: Untyped](trees: List[Tree[T]]): Text =
315315
inContextBracket {
316316
("{" ~ toText(trees, "\n") ~ "}").close
317317
}
318318

319-
protected def typeApplyText[T >: Untyped](tree: TypeApply[T]): Text = {
319+
protected def typeApplyText[T <: Untyped](tree: TypeApply[T]): Text = {
320320
val funText = toTextLocal(tree.fun)
321321
tree.fun match {
322322
case Select(New(tpt), nme.CONSTRUCTOR) if tpt.typeOpt.dealias.isInstanceOf[AppliedType] =>
@@ -326,7 +326,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
326326
}
327327
}
328328

329-
protected def toTextCore[T >: Untyped](tree: Tree[T]): Text = {
329+
protected def toTextCore[T <: Untyped](tree: Tree[T]): Text = {
330330
import untpd._
331331

332332
def isLocalThis(tree: Tree) = tree.typeOpt match {
@@ -739,7 +739,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
739739
}
740740
}
741741

742-
override def toText[T >: Untyped](tree: Tree[T]): Text = controlled {
742+
override def toText[T <: Untyped](tree: Tree[T]): Text = controlled {
743743
import untpd._
744744

745745
var txt = toTextCore(tree)
@@ -826,7 +826,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
826826

827827
protected def dropAnnotForModText(sym: Symbol): Boolean = sym == defn.BodyAnnot
828828

829-
protected def optAscription[T >: Untyped](tpt: Tree[T]): Text = optText(tpt)(": " ~ _)
829+
protected def optAscription[T <: Untyped](tpt: Tree[T]): Text = optText(tpt)(": " ~ _)
830830

831831
private def idText(tree: untpd.Tree): Text =
832832
(if showUniqueIds && tree.hasType && tree.symbol.exists then s"#${tree.symbol.id}" else "") ~
@@ -842,7 +842,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
842842
private def useSymbol(tree: untpd.Tree) =
843843
tree.hasType && tree.symbol.exists && ctx.settings.YprintSyms.value
844844

845-
protected def nameIdText[T >: Untyped](tree: NameTree[T]): Text =
845+
protected def nameIdText[T <: Untyped](tree: NameTree[T]): Text =
846846
if (tree.hasType && tree.symbol.exists) {
847847
val str = nameString(tree.symbol)
848848
tree match {
@@ -856,13 +856,13 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
856856
private def toTextOwner(tree: Tree[?]) =
857857
"[owner = " ~ tree.symbol.maybeOwner.show ~ "]" provided ctx.settings.YprintDebugOwners.value
858858

859-
protected def dclTextOr[T >: Untyped](tree: Tree[T])(treeText: => Text): Text =
859+
protected def dclTextOr[T <: Untyped](tree: Tree[T])(treeText: => Text): Text =
860860
toTextOwner(tree) ~ {
861861
if (useSymbol(tree)) annotsText(tree.symbol) ~~ dclText(tree.symbol)
862862
else treeText
863863
}
864864

865-
def paramsText[T>: Untyped](params: ParamClause[T]): Text = (params: @unchecked) match
865+
def paramsText[T <: Untyped](params: ParamClause[T]): Text = (params: @unchecked) match
866866
case Nil =>
867867
"()"
868868
case untpd.ValDefs(vparams @ (vparam :: _)) =>
@@ -872,18 +872,18 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
872872
case untpd.TypeDefs(tparams) =>
873873
"[" ~ toText(tparams, ", ") ~ "]"
874874

875-
def addParamssText[T >: Untyped](leading: Text, paramss: List[ParamClause[T]]): Text =
875+
def addParamssText[T <: Untyped](leading: Text, paramss: List[ParamClause[T]]): Text =
876876
paramss.foldLeft(leading)((txt, params) => txt ~ paramsText(params))
877877

878-
protected def valDefToText[T >: Untyped](tree: ValDef[T]): Text = {
878+
protected def valDefToText[T <: Untyped](tree: ValDef[T]): Text = {
879879
dclTextOr(tree) {
880880
modText(tree.mods, tree.symbol, keywordStr(if (tree.mods.is(Mutable)) "var" else "val"), isType = false) ~~
881881
valDefText(nameIdText(tree)) ~ optAscription(tree.tpt) ~
882882
withEnclosingDef(tree) { optText(tree.rhs)(" = " ~ _) }
883883
}
884884
}
885885

886-
protected def defDefToText[T >: Untyped](tree: DefDef[T]): Text = {
886+
protected def defDefToText[T <: Untyped](tree: DefDef[T]): Text = {
887887
import untpd._
888888
dclTextOr(tree) {
889889
val defKeyword = modText(tree.mods, tree.symbol, keywordStr("def"), isType = false)
@@ -989,8 +989,8 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
989989
)
990990
}
991991

992-
protected def toTextPackageId[T >: Untyped](pid: Tree[T]): Text =
993-
if (homogenizedView && pid.hasType) toTextLocal(pid.tpe.asInstanceOf[Showable])
992+
protected def toTextPackageId[T <: Untyped](pid: Tree[T]): Text =
993+
if (homogenizedView && pid.hasType) toTextLocal(pid.typeOpt)
994994
else toTextLocal(pid)
995995

996996
protected def packageDefText(tree: PackageDef): Text = {
@@ -1044,10 +1044,10 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
10441044
def optText(name: Name)(encl: Text => Text): Text =
10451045
if (name.isEmpty) "" else encl(toText(name))
10461046

1047-
def optText[T >: Untyped](tree: Tree[T])(encl: Text => Text): Text =
1047+
def optText[T <: Untyped](tree: Tree[T])(encl: Text => Text): Text =
10481048
if (tree.isEmpty) "" else encl(toText(tree))
10491049

1050-
def optText[T >: Untyped](tree: List[Tree[T]])(encl: Text => Text): Text =
1050+
def optText[T <: Untyped](tree: List[Tree[T]])(encl: Text => Text): Text =
10511051
if (tree.exists(!_.isEmpty)) encl(blockText(tree)) else ""
10521052

10531053
override protected def treatAsTypeParam(sym: Symbol): Boolean = sym.is(TypeParam)

compiler/src/dotty/tools/dotc/reporting/messages.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1390,7 +1390,7 @@ import cc.CaptureSet.IdentityCaptRefMap
13901390
|""".stripMargin
13911391
}
13921392

1393-
class TypeDoesNotTakeParameters(tpe: Type, params: List[Trees.Tree[Trees.Untyped]])(using Context)
1393+
class TypeDoesNotTakeParameters(tpe: Type, params: List[untpd.Tree])(using Context)
13941394
extends TypeMsg(TypeDoesNotTakeParametersID) {
13951395
private def fboundsAddendum =
13961396
if tpe.typeSymbol.isAllOf(Provisional | TypeParam) then

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ object Erasure {
614614
* are handled separately by [[typedDefDef]], [[typedValDef]] and [[typedTyped]].
615615
*/
616616
override def typedTypeTree(tree: untpd.TypeTree, pt: Type)(using Context): TypeTree =
617-
checkNotErasedClass(tree.withType(erasure(tree.tpe)))
617+
checkNotErasedClass(tree.withType(erasure(tree.typeOpt)))
618618

619619
/** This override is only needed to semi-erase type ascriptions */
620620
override def typedTyped(tree: untpd.Typed, pt: Type)(using Context): Tree =

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class InterceptedMethods extends MiniPhase {
6565
override def transformApply(tree: Apply)(using Context): Tree = {
6666
lazy val qual = tree.fun match {
6767
case Select(qual, _) => qual
68-
case ident @ Ident(_) =>
68+
case ident: Ident =>
6969
ident.tpe match {
7070
case TermRef(prefix: TermRef, _) =>
7171
tpd.ref(prefix)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ class TreeChecker extends Phase with SymTransformer {
376376

377377
override def typedIdent(tree: untpd.Ident, pt: Type)(using Context): Tree = {
378378
assert(tree.isTerm || !ctx.isAfterTyper, tree.show + " at " + ctx.phase)
379-
assert(tree.isType || ctx.mode.is(Mode.Pattern) && untpd.isWildcardArg(tree) || !needsSelect(tree.tpe), i"bad type ${tree.tpe} for $tree # ${tree.uniqueId}")
379+
assert(tree.isType || ctx.mode.is(Mode.Pattern) && untpd.isWildcardArg(tree) || !needsSelect(tree.typeOpt), i"bad type ${tree.tpe} for $tree # ${tree.uniqueId}")
380380
assertDefined(tree)
381381

382382
checkNotRepeated(super.typedIdent(tree, pt))

compiler/src/dotty/tools/dotc/typer/Applications.scala

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import core._
66
import ast.{Trees, tpd, untpd, desugar}
77
import util.Stats.record
88
import util.{SrcPos, NoSourcePosition}
9-
import Trees.Untyped
109
import Contexts._
1110
import Flags._
1211
import Symbols._
@@ -491,7 +490,7 @@ trait Applications extends Compatibility {
491490
i"${err.refStr(methRef)}$infoStr"
492491

493492
/** Re-order arguments to correctly align named arguments */
494-
def reorder[T >: Untyped](args: List[Trees.Tree[T]]): List[Trees.Tree[T]] = {
493+
def reorder[T <: Untyped](args: List[Trees.Tree[T]]): List[Trees.Tree[T]] = {
495494

496495
/** @param pnames The list of parameter names that are missing arguments
497496
* @param args The list of arguments that are not yet passed, or that are waiting to be dropped
@@ -754,7 +753,7 @@ trait Applications extends Compatibility {
754753
/** Subclass of Application for type checking an Apply node, where
755754
* types of arguments are either known or unknown.
756755
*/
757-
abstract class TypedApply[T >: Untyped](
756+
abstract class TypedApply[T <: Untyped](
758757
app: untpd.Apply, fun: Tree, methRef: TermRef, args: List[Trees.Tree[T]], resultType: Type,
759758
override val applyKind: ApplyKind)(using Context)
760759
extends Application(methRef, fun.tpe, args, resultType) {

compiler/src/dotty/tools/dotc/typer/ReTyper.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class ReTyper(nestingLevel: Int = 0) extends Typer(nestingLevel) with ReChecking
7171
promote(tree)
7272

7373
override def typedRefinedTypeTree(tree: untpd.RefinedTypeTree)(using Context): TypTree =
74-
promote(TypeTree(tree.tpe).withSpan(tree.span))
74+
promote(TypeTree(tree.typeOpt).withSpan(tree.span))
7575

7676
override def typedExport(exp: untpd.Export)(using Context): Export =
7777
promote(exp)
@@ -87,8 +87,8 @@ class ReTyper(nestingLevel: Int = 0) extends Typer(nestingLevel) with ReChecking
8787
// retract PatternOrTypeBits like in typedExpr
8888
withoutMode(Mode.PatternOrTypeBits)(typedUnadapted(tree.fun, AnyFunctionProto))
8989
val implicits1 = tree.implicits.map(typedExpr(_))
90-
val patterns1 = tree.patterns.mapconserve(pat => typed(pat, pat.tpe))
91-
untpd.cpy.UnApply(tree)(fun1, implicits1, patterns1).withType(tree.tpe)
90+
val patterns1 = tree.patterns.mapconserve(pat => typed(pat, pat.typeOpt))
91+
untpd.cpy.UnApply(tree)(fun1, implicits1, patterns1).withType(tree.typeOpt)
9292
}
9393

9494
override def typedUnApply(tree: untpd.Apply, selType: Type)(using Context): Tree =

0 commit comments

Comments
 (0)