Skip to content

Commit 96da2f8

Browse files
committed
Fix parent extractor
1 parent 1ebd4af commit 96da2f8

File tree

6 files changed

+43
-12
lines changed

6 files changed

+43
-12
lines changed

compiler/src/dotty/tools/dotc/tasty/internal/ClassDef.scala

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,15 @@ object ClassDef {
1414
def unapplyClassDef(term: statements.TopLevelStatement): Option[statements.ClassDef.Data] = term match {
1515
case Impl(cdef @ Trees.TypeDef(name, impl@Trees.Template(constr, parents, self, _)), ctx) =>
1616
implicit val ctx_ = ctx
17-
if (cdef.symbol.isClass) Some((TypeName(name), DefDef(constr),
18-
parents.map(Term(_)), // FIXME can also be Type(_)
19-
if (self.isEmpty) None else Some(ValDef(self)), impl.body.map(Statement(_)), Modifiers(cdef)))
17+
if (cdef.symbol.isClass) {
18+
val className = TypeName(name)
19+
val constructor = DefDef(constr)
20+
val classParents = parents.map(TermOrTypeTree(_))
21+
val selfVal = if (self.isEmpty) None else Some(ValDef(self))
22+
val body = impl.body.map(Statement(_))
23+
val mods = Modifiers(cdef)
24+
Some((className, constructor, classParents, selfVal, body, mods))
25+
}
2026
else None
2127
case _ => None
2228
}
@@ -32,7 +38,7 @@ object ClassDef {
3238
override def toString: String = {
3339
import Toolbox.extractor
3440
val statements.ClassDef(name, constructor, parents, self, body, mods) = this
35-
s"ClassDef($name, $constructor, $parents, $self, $body, $mods)"
41+
s"ClassDef($name, $constructor, ${parents.map { case Left(p) => p; case Right(p) => p} }, $self, $body, $mods)"
3642
}
3743
}
3844

compiler/src/dotty/tools/dotc/tasty/internal/Term.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package dotty.tools.dotc.tasty
22
package internal
33

4-
import dotty.tools.dotc.ast.Trees.NamedArg
54
import dotty.tools.dotc.ast.{Trees, tpd}
65
import dotty.tools.dotc.core.Contexts.Context
76
import dotty.tools.dotc.core.NameKinds._
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package dotty.tools.dotc.tasty.internal
2+
3+
import dotty.tools.dotc.ast.tpd
4+
import dotty.tools.dotc.core.Contexts.Context
5+
6+
import scala.tasty.terms
7+
import scala.tasty.typetrees
8+
9+
object TermOrTypeTree {
10+
11+
// compat.TermOrTypeTree is used where Term | TypeTree should go in dotty
12+
def apply(arg: tpd.Tree)(implicit ctx: Context): scala.util.Either[terms.Term, typetrees.TypeTree] =
13+
if (!arg.isType) Left(Term(arg)) else Right(TypeTree(arg))
14+
15+
}

library/src/scala/tasty/statements/ClassDef.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import scala.tasty.modifiers.Modifier
77
trait ClassDef extends Definition
88

99
object ClassDef {
10-
type Data = (names.TypeName, DefDef, List[terms.Term], Option[ValDef], List[Statement], List[Modifier])
10+
// TODO Replace when bootstrapped
11+
// List[scala.util.Either[terms.Term, typetrees.TypeTree] is used where we should have Term | Type in dotty.
12+
type Data = (names.TypeName, DefDef, List[scala.util.Either[terms.Term, typetrees.TypeTree]], Option[ValDef], List[Statement], List[Modifier])
1113
def unapply(arg: TopLevelStatement)(implicit toolbox: Toolbox): Option[Data] = toolbox.unapplyClassDef(arg)
1214
}

tests/run-with-compiler/tasty-extractors.check

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,9 @@ Block(List(ClassDef(Foo, DefDef(<init>, List(), List(List()), Synthetic(), None,
184184
Block(List(ClassDef(Foo, DefDef(<init>, List(), List(List()), Synthetic(), None, List()), List(Apply(Select(New(Synthetic()), <init>), Nil)), None, List(DefDef(a, List(), List(), Synthetic(), Some(Literal(Int(0))), List(QualifiedProtected(###)))), List())), Literal(Unit()))
185185
###
186186

187+
Block(List(ClassDef(Foo, DefDef(<init>, List(), List(List()), Synthetic(), None, List()), List(Apply(Select(New(Synthetic()), <init>), Nil), Select(Select(Ident(_root_), scala), Product)), None, List(DefDef(copy, List(), List(List()), Synthetic(), Some(Apply(Select(New(Synthetic()), <init>), Nil)), List()), DefDef(hashCode, List(), List(List()), Synthetic(), Some(Literal(Int(394005536))), List()), DefDef(equals, List(), List(List(ValDef(x$0, Synthetic(), None, List()))), Synthetic(), Some(Apply(Select(Apply(Select(This(Some(Id(Foo))), eq), List(TypeApply(Select(Ident(x$0), asInstanceOf), List(Synthetic())))), ||), List(Match(Ident(x$0), List(CaseDef(Bind(x$0, TypeTest(Synthetic())), None, Literal(Boolean(true))), CaseDef(Value(Ident(_)), None, Literal(Boolean(false)))))))), List()), DefDef(toString, List(), List(List()), Synthetic(), Some(Apply(Ident(_toString), List(This(Some(Id(Foo)))))), List()), DefDef(canEqual, List(), List(List(ValDef(that, Synthetic(), None, List()))), Synthetic(), Some(TypeApply(Select(Ident(that), isInstanceOf), List(Synthetic()))), List()), DefDef(productArity, List(), List(), Synthetic(), Some(Literal(Int(0))), List()), DefDef(productPrefix, List(), List(), Synthetic(), Some(Literal(String("Foo"))), List()), DefDef(productElement, List(), List(List(ValDef(n, Synthetic(), None, List()))), Synthetic(), Some(Match(Ident(n), List(CaseDef(Value(Ident(_)), None, Apply(Ident(throw), List(Apply(Select(New(Synthetic()), <init>), List(Apply(Select(Ident(n), toString), Nil))))))))), List())), List()), ValDef(Foo, Ident(Foo$), Some(Apply(Select(New(Ident(Foo$)), <init>), Nil)), List()), ClassDef(Foo$, DefDef(<init>, List(), List(List()), Synthetic(), None, List()), List(Apply(Select(New(Synthetic()), <init>), Nil), Applied(Synthetic(), List(Synthetic()))), Some(ValDef(_, Singleton(Ident(Foo)), None, List())), List(DefDef(apply, List(), List(List()), Synthetic(), Some(Apply(Select(New(Synthetic()), <init>), Nil)), List()), DefDef(unapply, List(), List(List(ValDef(x$1, Synthetic(), None, List()))), Synthetic(), Some(Literal(Boolean(true))), List())), List())), Literal(Unit()))
188+
###
189+
187190
Block(List(ClassDef(Foo1, DefDef(<init>, List(), List(List(ValDef(a, Ident(Int), None, List()))), Synthetic(), None, List()), List(Apply(Select(New(Synthetic()), <init>), Nil)), None, List(ValDef(a, Synthetic(), None, List())), List())), Literal(Unit()))
188191
###
189192

@@ -217,6 +220,9 @@ Block(List(ClassDef(Foo12, DefDef(<init>, List(), List(List()), Synthetic(), Non
217220
Block(List(ClassDef(Foo, DefDef(<init>, List(), List(List()), Synthetic(), None, List()), List(Apply(Select(New(Synthetic()), <init>), Nil)), None, List(), List()), ClassDef(Bar, DefDef(<init>, List(), List(List()), Synthetic(), None, List()), List(Apply(Select(New(Synthetic()), <init>), Nil)), None, List(), List())), Literal(Unit()))
218221
###
219222

223+
Block(List(ClassDef(Foo2, DefDef(<init>, List(), List(List()), Synthetic(), None, List()), List(Synthetic()), None, List(), List()), ClassDef(Bar, DefDef(<init>, List(), List(List()), Synthetic(), None, List()), List(Apply(Select(New(Synthetic()), <init>), Nil), Ident(Foo2)), None, List(), List())), Literal(Unit()))
224+
###
225+
220226
Block(List(ClassDef(Foo, DefDef(<init>, List(), List(List(ValDef(i, Ident(Int), None, List()))), Synthetic(), None, List()), List(Apply(Select(New(Synthetic()), <init>), Nil)), None, List(ValDef(i, Synthetic(), None, List())), List()), ClassDef(Bar, DefDef(<init>, List(), List(List()), Synthetic(), None, List()), List(Apply(Select(New(Ident(Foo)), <init>), List(Literal(Int(1))))), None, List(), List())), Literal(Unit()))
221227
###
222228

tests/run-with-compiler/tasty-extractors.scala

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ object Test {
8787
'{ class Foo { final def a = 0 } }, // FIXME modifier not printed
8888
'{ class Foo { private[Foo] def a = 0 } },
8989
'{ class Foo { protected[Foo] def a = 0 } },
90-
// '{ case class Foo() },
90+
'{ case class Foo() },
9191
'{ class Foo1(a: Int) },
9292
'{ class Foo2(val b: Int) },
9393
'{ class Foo3(a: Int = 5) },
@@ -100,7 +100,7 @@ object Test {
100100
'{ class Foo11 { var a = 10 } },
101101
'{ class Foo12 { lazy val a = 11 } },
102102
'{ class Foo; class Bar extends Foo },
103-
// '{ trait Foo; class Bar extends Foo },
103+
'{ trait Foo2; class Bar extends Foo2 },
104104
'{ class Foo(i: Int); class Bar extends Foo(1) },
105105
'{ class Foo { type X = Int }; def f(a: Foo): a.X = ??? },
106106
'{ class Foo { type X }; def f(a: Foo { type X = Int }): a.X = ??? },
@@ -148,10 +148,13 @@ object Test {
148148
case ClassDef(name, contraverseuctor, parents, self, body, mods) =>
149149
traverse(name)
150150
traverse(contraverseuctor)
151-
parents.map(traverse)
152-
self.map(traverse)
153-
body.map(traverse)
154-
mods.map(traverse)
151+
parents.foreach {
152+
case Left(term) => traverse(term)
153+
case Right(typetree) => traverse(typetree)
154+
}
155+
self.foreach(traverse)
156+
body.foreach(traverse)
157+
mods.foreach(traverse)
155158
case _ =>
156159
}
157160
}

0 commit comments

Comments
 (0)