Skip to content

Commit b11019b

Browse files
Tobias Bordencanicolasstucki
Tobias Bordenca
authored andcommitted
Covariant-contravariant flag on type args
1 parent 5323e4d commit b11019b

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
@@ -590,8 +590,9 @@ trait Printers
590590
else if (flags.is(Flags.Abstract)) this += highlightKeyword("abstract class ", color) += highlightTypeDef(name, color)
591591
else this += highlightKeyword("class ", color) += highlightTypeDef(name, color)
592592

593+
val typeParams = stats.collect { case IsTypeDef(targ) => targ }.filter(_.symbol.isTypeParam).zip(targs)
593594
if (!flags.is(Flags.Object)) {
594-
printTargsDefs(targs)
595+
printTargsDefs(typeParams)
595596
val it = argss.iterator
596597
while (it.hasNext)
597598
printArgsDefs(it.next())
@@ -606,14 +607,19 @@ trait Printers
606607
if (parents1.nonEmpty)
607608
this += highlightKeyword(" extends ", color)
608609

609-
def printParent(parent: TermOrTypeTree): Unit = parent match {
610+
def printParent(parent: TermOrTypeTree, needEmptyParens: Boolean = false): Unit = parent match {
610611
case IsTypeTree(parent) =>
611612
printTypeTree(parent)
612613
case IsTerm(Term.TypeApply(fun, targs)) =>
613614
printParent(fun)
615+
case IsTerm(Term.Apply(fun@Term.Apply(_,_), args)) =>
616+
printParent(fun, true)
617+
if (!args.isEmpty || needEmptyParens)
618+
inParens(printTrees(args, ", "))
614619
case IsTerm(Term.Apply(fun, args)) =>
615620
printParent(fun)
616-
inParens(printTrees(args, ", "))
621+
if (!args.isEmpty || needEmptyParens)
622+
inParens(printTrees(args, ", "))
617623
case IsTerm(Term.Select(Term.New(tpt), _)) =>
618624
printTypeTree(tpt)
619625
case IsTerm(parent) =>
@@ -691,7 +697,7 @@ trait Printers
691697
case IsTypeDef(tdef @ TypeDef(name, rhs)) =>
692698
printDefAnnotations(tdef)
693699
this += highlightKeyword("type ", color)
694-
printTargDef(tdef, isMember = true)
700+
printTargDef((tdef, tdef), isMember = true)
695701

696702
case IsValDef(vdef @ ValDef(name, tpt, rhs)) =>
697703
printDefAnnotations(vdef)
@@ -754,7 +760,7 @@ trait Printers
754760
printProtectedOrPrivate(ddef)
755761

756762
this += highlightKeyword("def ", color) += highlightValDef((if (isConstructor) "this" else name), color)
757-
printTargsDefs(targs)
763+
printTargsDefs(targs.zip(targs))
758764
val it = argss.iterator
759765
while (it.hasNext)
760766
printArgsDefs(it.next())
@@ -1125,13 +1131,13 @@ trait Printers
11251131
this
11261132
}
11271133

1128-
def printTargsDefs(targs: List[TypeDef]): Unit = {
1134+
def printTargsDefs(targs: List[(TypeDef, TypeDef)], isDef:Boolean = true): Unit = {
11291135
if (!targs.isEmpty) {
1130-
def printSeparated(list: List[TypeDef]): Unit = list match {
1136+
def printSeparated(list: List[(TypeDef, TypeDef)]): Unit = list match {
11311137
case Nil =>
1132-
case x :: Nil => printTargDef(x)
1138+
case x :: Nil => printTargDef(x, isDef = isDef)
11331139
case x :: xs =>
1134-
printTargDef(x)
1140+
printTargDef(x, isDef = isDef)
11351141
this += ", "
11361142
printSeparated(xs)
11371143
}
@@ -1140,9 +1146,19 @@ trait Printers
11401146
}
11411147
}
11421148

1143-
def printTargDef(arg: TypeDef, isMember: Boolean = false): Buffer = {
1144-
this += arg.name
1145-
arg.rhs match {
1149+
def printTargDef(arg: (TypeDef, TypeDef), isMember: Boolean = false, isDef:Boolean = true): Buffer = {
1150+
val (argDef, argCons) = arg
1151+
1152+
if (isDef) {
1153+
if (argDef.symbol.flags.is(Flags.Covariant)) {
1154+
this += highlightValDef("+", color)
1155+
} else if (argDef.symbol.flags.is(Flags.Contravariant)) {
1156+
this += highlightValDef("-", color)
1157+
}
1158+
}
1159+
1160+
this += argCons.name
1161+
argCons.rhs match {
11461162
case IsTypeBoundsTree(rhs) => printBoundsTree(rhs)
11471163
case rhs @ WildcardTypeTree() =>
11481164
printTypeOrBound(rhs.tpe)
@@ -1412,7 +1428,7 @@ trait Printers
14121428
printTypeTree(result)
14131429

14141430
case TypeTree.LambdaTypeTree(tparams, body) =>
1415-
printTargsDefs(tparams)
1431+
printTargsDefs(tparams.zip(tparams), isDef = false)
14161432
this += highlightTypeDef(" => ", color)
14171433
printTypeOrBoundsTree(body)
14181434

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)