Skip to content

Commit ebc4951

Browse files
author
Tobias Bordenca
committed
Covariant-contravariant flag on type args
1 parent 31a8cd7 commit ebc4951

File tree

3 files changed

+33
-13
lines changed

3 files changed

+33
-13
lines changed

compiler/src/dotty/tools/dotc/tastyreflect/SymbolOpsImpl.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ trait SymbolOpsImpl extends scala.tasty.reflect.SymbolOps with CoreImpl {
9494

9595
def TypeSymbolDeco(symbol: TypeSymbol): TypeSymbolAPI = new TypeSymbolAPI {
9696
def tree(implicit ctx: Context): TypeDef = FromSymbol.typeDefFromSym(symbol)
97+
98+
def isTypeParam(implicit ctx: Context): Boolean = symbol.isTypeParam
9799
}
98100

99101
object ClassSymbol extends ClassSymbolModule {

library/src/scala/tasty/reflect/Printers.scala

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -586,8 +586,9 @@ trait Printers
586586
else if (flags.is(Flags.Abstract)) this += highlightKeyword("abstract class ", color) += highlightTypeDef(name, color)
587587
else this += highlightKeyword("class ", color) += highlightTypeDef(name, color)
588588

589+
val typeParams = stats.collect { case IsTypeDef(targ) => targ }.filter(_.symbol.isTypeParam).zip(targs)
589590
if (!flags.is(Flags.Object)) {
590-
printTargsDefs(targs)
591+
printTargsDefs(typeParams)
591592
val it = argss.iterator
592593
while (it.hasNext)
593594
printArgsDefs(it.next())
@@ -601,15 +602,20 @@ trait Printers
601602
if (parents1.nonEmpty)
602603
this += highlightKeyword(" extends ", color)
603604

604-
def printParent(parent: TermOrTypeTree): Unit = parent match {
605+
def printParent(parent: TermOrTypeTree, needEmptyParens: Boolean = false): Unit = parent match {
605606
case IsTypeTree(parent) =>
606607
printTypeTree(parent)
607608
case IsTerm(Term.TypeApply(fun, targs)) =>
608609
printParent(fun)
609610
inSquare(printTypeOrBoundsTrees(targs, ", "))
611+
case IsTerm(Term.Apply(fun@Term.Apply(_,_), args)) =>
612+
printParent(fun, true)
613+
if (!args.isEmpty || needEmptyParens)
614+
inParens(printTrees(args, ", "))
610615
case IsTerm(Term.Apply(fun, args)) =>
611616
printParent(fun)
612-
inParens(printTrees(args, ", "))
617+
if (!args.isEmpty || needEmptyParens)
618+
inParens(printTrees(args, ", "))
613619
case IsTerm(Term.Select(Term.New(tpt), _)) =>
614620
printTypeTree(tpt)
615621
case IsTerm(parent) =>
@@ -682,7 +688,7 @@ trait Printers
682688
case IsTypeDef(tdef @ TypeDef(name, rhs)) =>
683689
printDefAnnotations(tdef)
684690
this += highlightKeyword("type ", color)
685-
printTargDef(tdef, isMember = true)
691+
printTargDef((tdef, tdef), isMember = true)
686692

687693
case IsValDef(vdef @ ValDef(name, tpt, rhs)) =>
688694
printDefAnnotations(vdef)
@@ -743,7 +749,7 @@ trait Printers
743749
printProtectedOrPrivate(ddef)
744750

745751
this += highlightKeyword("def ", color) += highlightValDef((if (isConstructor) "this" else name), color)
746-
printTargsDefs(targs)
752+
printTargsDefs(targs.zip(targs))
747753
val it = argss.iterator
748754
while (it.hasNext)
749755
printArgsDefs(it.next())
@@ -1105,13 +1111,13 @@ trait Printers
11051111
this
11061112
}
11071113

1108-
def printTargsDefs(targs: List[TypeDef]): Unit = {
1114+
def printTargsDefs(targs: List[(TypeDef, TypeDef)], isDef:Boolean = true): Unit = {
11091115
if (!targs.isEmpty) {
1110-
def printSeparated(list: List[TypeDef]): Unit = list match {
1116+
def printSeparated(list: List[(TypeDef, TypeDef)]): Unit = list match {
11111117
case Nil =>
1112-
case x :: Nil => printTargDef(x)
1118+
case x :: Nil => printTargDef(x, isDef = isDef)
11131119
case x :: xs =>
1114-
printTargDef(x)
1120+
printTargDef(x, isDef = isDef)
11151121
this += ", "
11161122
printSeparated(xs)
11171123
}
@@ -1120,9 +1126,19 @@ trait Printers
11201126
}
11211127
}
11221128

1123-
def printTargDef(arg: TypeDef, isMember: Boolean = false): Buffer = {
1124-
this += arg.name
1125-
arg.rhs match {
1129+
def printTargDef(arg: (TypeDef, TypeDef), isMember: Boolean = false, isDef:Boolean = true): Buffer = {
1130+
val (argDef, argCons) = arg
1131+
1132+
if (isDef) {
1133+
if (argDef.symbol.flags.is(Flags.Covariant)) {
1134+
this += highlightValDef("+", color)
1135+
} else if (argDef.symbol.flags.is(Flags.Contravariant)) {
1136+
this += highlightValDef("-", color)
1137+
}
1138+
}
1139+
1140+
this += argCons.name
1141+
argCons.rhs match {
11261142
case IsTypeBoundsTree(rhs) => printBoundsTree(rhs)
11271143
case rhs @ WildcardTypeTree() =>
11281144
printTypeOrBound(rhs.tpe)
@@ -1393,7 +1409,7 @@ trait Printers
13931409
printTypeTree(result)
13941410

13951411
case TypeTree.LambdaTypeTree(tparams, body) =>
1396-
printTargsDefs(tparams)
1412+
printTargsDefs(tparams.zip(tparams), isDef = false)
13971413
this += highlightTypeDef(" => ", color)
13981414
printTypeOrBoundsTree(body)
13991415

library/src/scala/tasty/reflect/SymbolOps.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ trait SymbolOps extends Core {
123123
trait TypeSymbolAPI {
124124
/** TypeDef tree of this defintion. */
125125
def tree(implicit ctx: Context): TypeDef
126+
127+
def isTypeParam(implicit ctx: Context): Boolean
126128
}
127129
implicit def TypeSymbolDeco(symbol: TypeSymbol): TypeSymbolAPI
128130

0 commit comments

Comments
 (0)