Skip to content

Fix type of Tasty.Type.TypeSelect.prefix #4682

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions compiler/src/dotty/tools/dotc/tastyreflect/TastyImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -554,9 +554,16 @@ object TastyImpl extends scala.tasty.Tasty {
}
}

object TypeSelect extends TypeSelectExtractor {
object TermSelect extends TermSelectExtractor {
def unapply(x: TypeTree)(implicit ctx: Context): Option[(Term, String)] = x match {
case x: tpd.Select @unchecked if x.isType => Some(x.qualifier, x.name.toString)
case x: tpd.Select @unchecked if x.isType && x.qualifier.isTerm => Some(x.qualifier, x.name.toString)
case _ => None
}
}

object TypeSelect extends TypeSelectExtractor {
def unapply(x: TypeTree)(implicit ctx: Context): Option[(TypeTree, String)] = x match {
case x: tpd.Select @unchecked if x.isType && x.qualifier.isType => Some(x.qualifier, x.name.toString)
case _ => None
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find the name a little misleading, what about TypeProject? Then we can keep using TypeSelect as before.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Users would write the extractors as Type.TypeSelect(_, _) and Type.TermSelect(_, _). We should have a pass on all names later to make sure all names are good.

}
Expand Down
7 changes: 6 additions & 1 deletion library/src/scala/tasty/Tasty.scala
Original file line number Diff line number Diff line change
Expand Up @@ -433,9 +433,14 @@ abstract class Tasty { tasty =>
def unapply(x: TypeTree)(implicit ctx: Context): Option[String]
}

val TermSelect: TermSelectExtractor
abstract class TermSelectExtractor {
def unapply(x: TypeTree)(implicit ctx: Context): Option[(Term, String)]
}

val TypeSelect: TypeSelectExtractor
abstract class TypeSelectExtractor {
def unapply(x: TypeTree)(implicit ctx: Context): Option[(Term, String)]
def unapply(x: TypeTree)(implicit ctx: Context): Option[(TypeTree, String)]
}

val Singleton: SingletonExtractor
Expand Down
2 changes: 2 additions & 0 deletions library/src/scala/tasty/util/ShowExtractors.scala
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ class ShowExtractors[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
this += "TypeTree.Synthetic()"
case TypeTree.TypeIdent(name) =>
this += "TypeTree.TypeIdent(\"" += name += "\")"
case TypeTree.TermSelect(qualifier, name) =>
this += "TypeTree.TermSelect(" += qualifier += ", \"" += name += "\")"
case TypeTree.TypeSelect(qualifier, name) =>
this += "TypeTree.TypeSelect(" += qualifier += ", \"" += name += "\")"
case TypeTree.Singleton(ref) =>
Expand Down
11 changes: 5 additions & 6 deletions library/src/scala/tasty/util/ShowSourceCode.scala
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty

val parents1 = parents.filter {
case Term.Apply(Term.Select(Term.New(tpt), _, _), _) => !Types.JavaLangObject.unapply(tpt.tpe)
case TypeTree.TypeSelect(Term.Select(Term.Ident("_root_"), "scala", _), "Product") => false
case TypeTree.TermSelect(Term.Select(Term.Ident("_root_"), "scala", _), "Product") => false
case _ => true
}
if (parents1.nonEmpty) {
Expand Down Expand Up @@ -701,12 +701,11 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
case TypeTree.TypeIdent(name) =>
printType(tree.tpe)

case TypeTree.TermSelect(qual, name) =>
printTree(qual) += "." += name

case TypeTree.TypeSelect(qual, name) =>
(qual: Any) match {
case qual @ TypeTree.TypeIdent(_) => printTypeTree(qual) // FIXME: qual is of type Tree buy we are getting a TypeTree qualifier
case _ => printTree(qual)
}
this += "." += name
printTypeTree(qual) += "#" += name

case TypeTree.Singleton(ref) =>
printTree(ref)
Expand Down
3 changes: 2 additions & 1 deletion library/src/scala/tasty/util/TreeAccumulator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ abstract class TreeAccumulator[X, T <: Tasty with Singleton](val tasty: T) {
def foldOverTypeTree(x: X, tree: TypeOrBoundsTree)(implicit ctx: Context): X = tree match {
case TypeTree.Synthetic() => x
case TypeTree.TypeIdent(_) => x
case TypeTree.TypeSelect(qualifier, _) => foldTree(x, qualifier)
case TypeTree.TermSelect(qualifier, _) => foldTree(x, qualifier)
case TypeTree.TypeSelect(qualifier, _) => foldTypeTree(x, qualifier)
case TypeTree.Singleton(ref) => foldTree(x, ref)
case TypeTree.And(left, right) => foldTypeTree(foldTypeTree(x, left), right)
case TypeTree.Or(left, right) => foldTypeTree(foldTypeTree(x, left), right)
Expand Down
8 changes: 8 additions & 0 deletions tests/pos/simpleTypeSelect.decompiled
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/** Decompiled from out/posTestFromTasty/pos/simpleTypeSelect/Foo.class */
trait Foo() extends java.lang.Object {
type Bar
}
/** Decompiled from out/posTestFromTasty/pos/simpleTypeSelect/Test.class */
object Test {
val x: Foo#Bar = scala.Predef.???
}
5 changes: 5 additions & 0 deletions tests/pos/simpleTypeSelect.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

trait Foo { type Bar }
object Test {
val x: Foo#Bar = ???
}
3 changes: 2 additions & 1 deletion tests/pos/tasty/definitions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ object definitions {
def tpe: Type = ???
case Synthetic()
case Ident(name: String, override val tpe: Type)
case Select(prefix: Term, name: String)
case TermSelect(prefix: Term, name: String)
case TypeSelect(prefix: TypeTree, name: String)
case Singleton(ref: Term)
case Refined(underlying: TypeTree, refinements: List[Definition])
case Applied(tycon: TypeTree, args: List[TypeTree | TypeBoundsTree])
Expand Down
4 changes: 2 additions & 2 deletions tests/run/tasty-extractors-2.check
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,10 @@ Type.SymRef(ClassDef("Unit", _, _, _, _), Type.ThisType(Type.SymRef(PackageDef("
Term.Block(List(ClassDef("Foo", DefDef("<init>", Nil, List(List(ValDef("i", TypeTree.TypeIdent("Int"), None))), TypeTree.Synthetic(), None), List(Term.Apply(Term.Select(Term.New(TypeTree.Synthetic()), "<init>", Some(Signature(Nil, java.lang.Object))), Nil)), None, List(ValDef("i", TypeTree.Synthetic(), None))), ClassDef("Bar", DefDef("<init>", Nil, List(Nil), TypeTree.Synthetic(), None), List(Term.Apply(Term.Select(Term.New(TypeTree.TypeIdent("Foo")), "<init>", Some(Signature(List(scala.Int), Test$._$Foo))), List(Term.Literal(Constant.Int(1))))), None, Nil)), Term.Literal(Constant.Unit()))
Type.SymRef(ClassDef("Unit", _, _, _, _), Type.ThisType(Type.SymRef(PackageDef("scala", _), NoPrefix())))

Term.Block(List(ClassDef("Foo", DefDef("<init>", Nil, List(Nil), TypeTree.Synthetic(), None), List(Term.Apply(Term.Select(Term.New(TypeTree.Synthetic()), "<init>", Some(Signature(Nil, java.lang.Object))), Nil)), None, List(TypeDef("X", TypeTree.TypeIdent("Int")))), DefDef("f", Nil, List(List(ValDef("a", TypeTree.TypeIdent("Foo"), None))), TypeTree.TypeSelect(Term.Ident("a"), "X"), Some(Term.Ident("???")))), Term.Literal(Constant.Unit()))
Term.Block(List(ClassDef("Foo", DefDef("<init>", Nil, List(Nil), TypeTree.Synthetic(), None), List(Term.Apply(Term.Select(Term.New(TypeTree.Synthetic()), "<init>", Some(Signature(Nil, java.lang.Object))), Nil)), None, List(TypeDef("X", TypeTree.TypeIdent("Int")))), DefDef("f", Nil, List(List(ValDef("a", TypeTree.TypeIdent("Foo"), None))), TypeTree.TermSelect(Term.Ident("a"), "X"), Some(Term.Ident("???")))), Term.Literal(Constant.Unit()))
Type.SymRef(ClassDef("Unit", _, _, _, _), Type.ThisType(Type.SymRef(PackageDef("scala", _), NoPrefix())))

Term.Block(List(ClassDef("Foo", DefDef("<init>", Nil, List(Nil), TypeTree.Synthetic(), None), List(Term.Apply(Term.Select(Term.New(TypeTree.Synthetic()), "<init>", Some(Signature(Nil, java.lang.Object))), Nil)), None, List(TypeDef("X", TypeBoundsTree(TypeTree.Synthetic(), TypeTree.Synthetic())))), DefDef("f", Nil, List(List(ValDef("a", TypeTree.Refined(TypeTree.TypeIdent("Foo"), List(TypeDef("X", TypeTree.TypeIdent("Int")))), None))), TypeTree.TypeSelect(Term.Ident("a"), "X"), Some(Term.Ident("???")))), Term.Literal(Constant.Unit()))
Term.Block(List(ClassDef("Foo", DefDef("<init>", Nil, List(Nil), TypeTree.Synthetic(), None), List(Term.Apply(Term.Select(Term.New(TypeTree.Synthetic()), "<init>", Some(Signature(Nil, java.lang.Object))), Nil)), None, List(TypeDef("X", TypeBoundsTree(TypeTree.Synthetic(), TypeTree.Synthetic())))), DefDef("f", Nil, List(List(ValDef("a", TypeTree.Refined(TypeTree.TypeIdent("Foo"), List(TypeDef("X", TypeTree.TypeIdent("Int")))), None))), TypeTree.TermSelect(Term.Ident("a"), "X"), Some(Term.Ident("???")))), Term.Literal(Constant.Unit()))
Type.SymRef(ClassDef("Unit", _, _, _, _), Type.ThisType(Type.SymRef(PackageDef("scala", _), NoPrefix())))

Term.Block(List(ValDef("lambda", TypeTree.Applied(TypeTree.Synthetic(), List(TypeTree.TypeIdent("Int"), TypeTree.TypeIdent("Int"))), Some(Term.Block(List(DefDef("$anonfun", Nil, List(List(ValDef("x", TypeTree.Synthetic(), None))), TypeTree.Synthetic(), Some(Term.Ident("x")))), Term.Lambda(Term.Ident("$anonfun"), None))))), Term.Literal(Constant.Unit()))
Expand Down