Skip to content

Commit

Permalink
convert type members
Browse files Browse the repository at this point in the history
  • Loading branch information
xeno-by committed Oct 19, 2016
1 parent eee78e7 commit 84b6b5d
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,13 @@ class LogicalTrees[G <: Global](val global: G, root: G#Tree) extends ReflectTool
object TypeDef {
def unapply(
tree: g.TypeDef): Option[(List[l.Modifier], l.TypeName, List[l.TypeParamDef], g.Tree)] = {
???
tree match {
case g.TypeDef(mods, name, tparams, rhs) if !mods.hasFlag(DEFERRED) =>
val ltparams = mkTparams(tparams, Nil)
Some((l.Modifiers(tree), l.TypeName(tree), ltparams, rhs))
case _ =>
None
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,13 @@ trait ToMtree { self: Converter =>
val mrhs = lrhs.toMtree[m.Term]
m.Defn.Def(mmods, mname, mtparams, mparamss, mtpt, mrhs)

case l.TypeDef(lmods, lname, ltparams, lbody) =>
val mmods = lmods.toMtrees[m.Mod]
val mname = lname.toMtree[m.Type.Name]
val mtparams = ltparams.toMtrees[m.Type.Param]
val mbody = lbody.toMtree[m.Type]
m.Defn.Type(mmods, mname, mtparams, mbody)

case l.ClassDef(lmods, lname, ltparams, lctor, limpl) =>
val mmods = lmods.toMtrees[m.Mod]
val mname = lname.toMtree[m.Type.Name]
Expand Down
15 changes: 14 additions & 1 deletion tests/converter/src/main/scala/ConverterSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,20 @@ trait ConverterSuite extends FunSuite {
import g._
val reporter = new StoreReporter()
g.reporter = reporter
val tree = gen.mkTreeOrBlock(newUnitParser(code, "<toolbox>").parseStatsOrPackages())
val tree = {
// NOTE: `parseStatsOrPackages` fails to parse abstract type defs without bounds,
// so we need to apply a workaround to ensure that we correctly process those.
def somewhatBrokenParse(code: String) =
gen.mkTreeOrBlock(newUnitParser(code, "<toolbox>").parseStatsOrPackages())
val rxAbstractTypeNobounds = """^type (\w+)(\[[^=]*?\])?$""".r
code match {
case rxAbstractTypeNobounds(_ *) =>
val tdef @ TypeDef(mods, name, tparams, _) = somewhatBrokenParse(code + " <: Dummy")
treeCopy.TypeDef(tdef, mods, name, tparams, TypeBoundsTree(EmptyTree, EmptyTree))
case _ =>
somewhatBrokenParse(code)
}
}
val errors = reporter.infos.filter(_.severity == g.reporter.ERROR)
errors.foreach(error => fail(s"scalac parse error: ${error.msg} at ${error.pos}"))
tree
Expand Down
12 changes: 12 additions & 0 deletions tests/converter/src/test/scala/Syntactic.scala
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,18 @@ class Syntactic extends ConverterSuite {
syntactic("a match { case x @ _ => }")
syntactic("a match { case x @ (_: T) => }")

// definitions
syntactic("type Age = Int")
syntactic("type Age")
syntactic("type Age >: Int <: Any")
syntactic("type Age <: Int + Boolean")
syntactic("type Container[T]")
syntactic("type Container[T] = List[T]")
syntactic("type Container[T] <: List[T] { def isEmpty: Boolean; type M }")
syntactic("type Container[T] <: List[T] with Set[T] { def isEmpty: Boolean; type M }")
syntactic("type Container[T] <: List[T] with Set[T] { def isEmpty: Boolean; type M = Int }")
syntactic("type Container[T] <: List[T] with Set[T] { def isEmpty: Boolean; type M <: Int }")

// types
syntactic("val a: A with B = ???")
syntactic("val a: A { def x: Int } = ???")
Expand Down

0 comments on commit 84b6b5d

Please sign in to comment.