@@ -7,6 +7,8 @@ import dotty.tools.dotc.core.StdNames.nme
77import dotty .tools .dotc .core .Symbols .Symbol
88import dotty .tools .dotc .core .Decorators ._
99import dotty .tools .dotc .core .quoted .PickledQuotes
10+ import dotty .tools .dotc .reporting .Reporter
11+ import dotty .tools .dotc .reporting .diagnostic .MessageContainer
1012import dotty .tools .dotc .util .SourcePosition
1113
1214import scala .quoted
@@ -228,9 +230,40 @@ object TastyImpl extends scala.tasty.Tasty {
228230
229231 type Term = tpd.Tree
230232
231- def TermDeco (t : Term ): AbstractTerm = new AbstractTerm {
232- def pos (implicit ctx : Context ): Position = t.pos
233- def tpe (implicit ctx : Context ): Types .Type = t.tpe
233+ def TermDeco (tree : Term ): AbstractTerm = new AbstractTerm {
234+
235+ def pos (implicit ctx : Context ): Position = tree.pos
236+
237+ def tpe (implicit ctx : Context ): Types .Type = tree.tpe
238+
239+ def toExpr [T : quoted.Type ](implicit ctx : Context ): quoted.Expr [T ] = {
240+ typecheck(ctx)
241+ new quoted.Exprs .TastyTreeExpr (tree).asInstanceOf [quoted.Expr [T ]]
242+ }
243+
244+ private def typecheck [T : quoted.Type ](ctx : Contexts .Context ): Unit = {
245+ implicit val ctx0 : Contexts .FreshContext = ctx.fresh
246+ ctx0.setTyperState(ctx0.typerState.fresh())
247+ ctx0.typerState.setReporter(new Reporter {
248+ def doReport (m : MessageContainer )(implicit ctx : Contexts .Context ): Unit = ()
249+ })
250+ val tp = QuotedTypeDeco (implicitly[quoted.Type [T ]]).toTasty
251+ ctx0.typer.typed(tree, tp.tpe)
252+ if (ctx0.reporter.hasErrors) {
253+ val stack = new Exception ().getStackTrace
254+ def filter (elem : StackTraceElement ) =
255+ elem.getClassName.startsWith(" dotty.tools.dotc.tasty.TastyImpl" ) ||
256+ ! elem.getClassName.startsWith(" dotty.tools.dotc" )
257+ throw new scala.tasty.TastyTypecheckError (
258+ s """ Error during tasty reflection while typing term
259+ |term: ${tree.show}
260+ |with expected type: ${tp.tpe.show}
261+ |
262+ | ${stack.takeWhile(filter).mkString(" \n " )}
263+ """ .stripMargin
264+ )
265+ }
266+ }
234267 }
235268
236269 def termClassTag : ClassTag [Term ] = implicitly[ClassTag [Term ]]
0 commit comments