Skip to content

Commit 67b1f03

Browse files
Merge pull request #4679 from dotty-staging/fix-tasty-applied-type-tree-on-bounds
Fix tasty applied type tree on bounds
2 parents 09e6599 + a264e86 commit 67b1f03

File tree

9 files changed

+53
-33
lines changed

9 files changed

+53
-33
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ object TastyImpl extends scala.tasty.Tasty {
572572
}
573573

574574
object Applied extends AppliedExtractor {
575-
def unapply(x: TypeTree)(implicit ctx: Context): Option[(TypeTree, List[TypeTree])] = x match {
575+
def unapply(x: TypeTree)(implicit ctx: Context): Option[(TypeTree, List[TypeOrBoundsTree])] = x match {
576576
case x: tpd.AppliedTypeTree @unchecked => Some(x.tpt, x.args)
577577
case _ => None
578578
}

compiler/test/dotty/tools/dotc/FromTastyTests.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,11 @@ class FromTastyTests extends ParallelTesting {
3636

3737
// MatchError in SymDenotation.sourceModule on a ThisType
3838
"t3612.scala",
39+
40+
// Fails on MacOS
41+
"annot-bootstrap.scala",
3942
),
4043
recompilationBlacklist = Set(
41-
"annot-bootstrap.scala",
4244
)
4345
).checkCompile()
4446
}

library/src/scala/tasty/Tasty.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ abstract class Tasty { tasty =>
417417

418418
val Applied: AppliedExtractor
419419
abstract class AppliedExtractor {
420-
def unapply(x: TypeTree)(implicit ctx: Context): Option[(TypeTree, List[TypeTree])]
420+
def unapply(x: TypeTree)(implicit ctx: Context): Option[(TypeTree, List[TypeOrBoundsTree])]
421421
}
422422

423423
val Annotated: AnnotatedExtractor

library/src/scala/tasty/util/ShowExtractors.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,8 @@ class ShowExtractors[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
253253

254254
private implicit class TypeTreeOps(buff: Buffer) {
255255
def +=(x: TypeOrBoundsTree): Buffer = { visitTypeTree(x); buff }
256-
def +=(x: Option[TypeTree]): Buffer = { visitOption(x, visitTypeTree); buff }
257-
def ++=(x: List[TypeTree]): Buffer = { visitList(x, visitTypeTree); buff }
256+
def +=(x: Option[TypeOrBoundsTree]): Buffer = { visitOption(x, visitTypeTree); buff }
257+
def ++=(x: List[TypeOrBoundsTree]): Buffer = { visitList(x, visitTypeTree); buff }
258258
}
259259

260260
private implicit class TypeOps(buff: Buffer) {

library/src/scala/tasty/util/ShowSourceCode.scala

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,12 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
6868
printDefAnnotations(cdef)
6969

7070
val flags = cdef.flags
71+
if (flags.isImplicit) this += "implicit "
7172
if (flags.isFinal && !flags.isObject) this += "final "
7273
if (flags.isCase) this += "case "
7374

7475
if (flags.isObject) this += "object " += name.stripSuffix("$")
76+
else if (flags.isTrait) this += "trait " += name
7577
else this += "class " += name
7678

7779
if (!flags.isObject) {
@@ -93,7 +95,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
9395
this += " "
9496
printTypeTree(tpt)
9597
this += "["
96-
printTypeTrees(targs, ", ")
98+
printTypeOrBoundsTrees(targs, ", ")
9799
this += "]"
98100
if (args.nonEmpty) {
99101
this += "("
@@ -157,6 +159,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
157159
printDefAnnotations(vdef)
158160

159161
val flags = vdef.flags
162+
if (flags.isImplicit) this += "implicit "
160163
if (flags.isOverride) this += "override "
161164

162165
if (flags.isLazy) this += "lazy "
@@ -210,7 +213,8 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
210213
printDefAnnotations(ddef)
211214

212215
val flags = ddef.flags
213-
if (flags.isOverride) sb.append("override ")
216+
if (flags.isImplicit) this += "implicit "
217+
if (flags.isOverride) this += "override "
214218

215219
this += "def " += name
216220
printTargsDefs(targs)
@@ -271,7 +275,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
271275
case Term.TypeApply(fn, args) =>
272276
printTree(fn)
273277
this += "["
274-
printTypeTrees(args, ", ")
278+
printTypeOrBoundsTrees(args, ", ")
275279
this += "]"
276280

277281
case Term.Super(qual, tptOpt) =>
@@ -451,12 +455,12 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
451455
this
452456
}
453457

454-
def printTypeTrees(typesTrees: List[TypeTree], sep: String): Buffer = {
455-
def printSeparated(list: List[TypeTree]): Unit = list match {
458+
def printTypeOrBoundsTrees(typesTrees: List[TypeOrBoundsTree], sep: String): Buffer = {
459+
def printSeparated(list: List[TypeOrBoundsTree]): Unit = list match {
456460
case Nil =>
457-
case x :: Nil => printTypeTree(x)
461+
case x :: Nil => printTypeOrBoundsTree(x)
458462
case x :: xs =>
459-
printTypeTree(x)
463+
printTypeOrBoundsTree(x)
460464
this += sep
461465
printSeparated(xs)
462466
}
@@ -496,34 +500,41 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
496500

497501
def printTargDef(arg: TypeDef, isMember: Boolean = false): Buffer = {
498502
val TypeDef(name, rhs) = arg
503+
def printBounds(bounds: TypeBoundsTree): Buffer = {
504+
val TypeBoundsTree(lo, hi) = bounds
505+
lo match {
506+
case TypeTree.Synthetic() =>
507+
case _ =>
508+
this += " >: "
509+
printTypeTree(lo)
510+
}
511+
hi match {
512+
case TypeTree.Synthetic() => this
513+
case _ =>
514+
this += " <: "
515+
printTypeTree(hi)
516+
}
517+
}
499518
this += name
500519
rhs match {
501-
case TypeBoundsTree(lo, hi) =>
502-
lo match {
503-
case TypeTree.Synthetic() => this
504-
case _ =>
505-
this += " >: "
506-
printTypeTree(lo)
507-
}
508-
hi match {
509-
case TypeTree.Synthetic() => this
510-
case _ =>
511-
this += " <: "
512-
printTypeTree(hi)
513-
}
520+
case rhs @ TypeBoundsTree(lo, hi) => printBounds(rhs)
514521
case rhs @ SyntheticBounds() =>
515522
printTypeOrBound(rhs.tpe)
516523
case rhs @ TypeTree.TypeLambdaTree(tparams, body) =>
524+
def printParam(t: TypeOrBoundsTree): Unit = t match {
525+
case t @ TypeBoundsTree(_, _) => printBounds(t)
526+
case t @ TypeTree() => printTypeTree(t)
527+
}
517528
def printSeparated(list: List[TypeDef]): Unit = list match {
518529
case Nil =>
519530
case x :: Nil =>
520531
val TypeDef(name, trhs) = x
521532
this += name
522-
printTypeOrBoundsTree(trhs)
533+
printParam(trhs)
523534
case x :: xs =>
524535
val TypeDef(name, trhs) = x
525536
this += name
526-
printTypeOrBoundsTree(trhs)
537+
printParam(trhs)
527538
this += ", "
528539
printSeparated(xs)
529540
}
@@ -664,7 +675,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
664675

665676
def printTypeOrBoundsTree(tpt: TypeOrBoundsTree): Buffer = tpt match {
666677
case TypeBoundsTree(lo, hi) =>
667-
this += " >: "
678+
this += "_ >: "
668679
printTypeTree(lo)
669680
this += " <: "
670681
printTypeTree(hi)
@@ -723,7 +734,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
723734
case TypeTree.Applied(tpt, args) =>
724735
printTypeTree(tpt)
725736
this += "["
726-
printTypeTrees(args, ", ")
737+
printTypeOrBoundsTrees(args, ", ")
727738
this += "]"
728739

729740
case TypeTree.Annotated(tpt, annot) =>

tests/pos/i1181.decompiled

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/** Decompiled from out/posTestFromTasty/pos/i1181/Test.class */
22
object Test {
3-
def foo[M[_$1 >: scala.Nothing <: scala.Any]](x: M[scala.Int]): M[scala.Int] = x
4-
type Alias[A >: scala.Nothing <: scala.Any] = scala.Tuple2[A, A]
3+
def foo[M[_$1]](x: M[scala.Int]): M[scala.Int] = x
4+
type Alias[A] = scala.Tuple2[A, A]
55
val x: Test.Alias[scala.Int] = scala.Tuple2.apply[scala.Int, scala.Int](1, 2)
66
Test.foo[Test.Alias](Test.x)
77
Test.foo[[A >: scala.Nothing <: scala.Any] => scala.Tuple2[A, A]](Test.x)

tests/pos/i2104b.decompiled

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/** Decompiled from out/posTestFromTasty/pos/i2104b/Cons.class */
2-
class Cons[H, T]() extends java.lang.Object
2+
trait Cons[H, T]() extends java.lang.Object
33
object Cons {
44
def apply[H, T](h: H, t: T): Cons[H, T] = scala.Predef.???
55
def unapply[H, T](t: Cons[H, T]): scala.Option[Pair[H, T]] = scala.Predef.???

tests/pos/t0905.decompiled

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/** Decompiled from out/posTestFromTasty/pos/t0905/Test.class */
2+
object Test {
3+
trait A[T]() extends java.lang.Object
4+
def f(implicit p: Test.A[_ >: scala.Nothing <: scala.Any]): scala.Null = null
5+
implicit val x: Test.A[_ >: scala.Nothing <: scala.Any] = null
6+
scala.Predef.println(Test.f(Test.x))
7+
}

tests/pos/tasty/definitions.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ object definitions {
9595
case Select(prefix: Term, name: String)
9696
case Singleton(ref: Term)
9797
case Refined(underlying: TypeTree, refinements: List[Definition])
98-
case Applied(tycon: TypeTree, args: List[TypeTree])
98+
case Applied(tycon: TypeTree, args: List[TypeTree | TypeBoundsTree])
9999
case Annotated(tpt: TypeTree, annotation: Term)
100100
case And(left: TypeTree, right: TypeTree)
101101
case Or(left: TypeTree, right: TypeTree)

0 commit comments

Comments
 (0)