aboutsummaryrefslogblamecommitdiff
path: root/compiler/test/dotty/tools/DottyTypeStealer.scala
blob: 727cd9e7da195d13a188114dea2d94dfb7f9126c (plain) (tree)
1
2
3
4
5
6
7
8
9
                   
 





                                 
 
                                           





                                                                                                         
                                             



                                                                  
                                                                    



                                                                          
                                     




                        
package dotty.tools

import dotc.ast.tpd
import dotc.core.Names._
import dotc.ast.tpd._
import dotc.core.Contexts.Context
import dotc.core.Decorators._
import dotc.core.Types.Type

object DottyTypeStealer extends DottyTest {
  def stealType(source: String, typeStrings: String*): (Context, List[Type]) = {
    val dummyName = "x_x_x"
    val vals = typeStrings.zipWithIndex.map{case (s, x)=> s"val ${dummyName}$x: $s = ???"}.mkString("\n")
    val gatheredSource = s" ${source}\n object A$dummyName {$vals}"
    var scontext : Context = null
    var tp: List[Type] = null
    checkCompile("frontend",gatheredSource) {
      (tree, context) =>
        implicit val ctx = context
        val findValDef: (List[ValDef], tpd.Tree) => List[ValDef] =
          (acc , tree) =>  { tree match {
          case t: ValDef if t.name.startsWith(dummyName) => t :: acc
          case _ => acc
        }
      }
      val d = new DeepFolder[List[ValDef]](findValDef).foldOver(Nil, tree)
      tp = d.map(_.tpe.widen).reverse
      scontext = context
    }
    (scontext, tp)
  }
}