Permalink
Browse files

SI-7345 Improved Context.toString

    ticket/7345-2 ~/code/scala qbin/scala
    Welcome to Scala version 2.11.0-20130416-231609-7829011884 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_37).
    Type in expressions to have them evaluated.
    Type :help for more information.

    scala> import language.experimental._import language.experimental._

    scala> import reflect.macros.Contextimport reflect.macros.Context

    scala> def showContextImpl(c: Context) = {println(c.asInstanceOf[reflect.macros.runtime.Context].callsiteTyper.context.enclosingContextChain.mkString("\n\n")); c.literalUnit}
    showContextImpl: (c: scala.reflect.macros.Context)c.Expr[Unit]

    scala> def showContext = macro showContextImpldefined term macro showContext: Unit

    scala> object Foo { def foo(a: Any) { {class C { println("") }; showContext } } }Context(<console>) {
       owner       = method foo
       tree        = Block:{ class C extends scala.AnyRef { def <init>(): C = { super.<init>(); (
       scope       = 1 decls
       contextMode = AmbiguousErrors ImplicitsEnabled MacrosEnabled ReportErrors
       outer.owner = method foo
    }

    Context(<console>) {
       owner       = method foo
       tree        = DefDef:def foo(a: Any): Unit = { class C extends scala.AnyRef { def <init>():
       scope       = 1 decls
       contextMode = AmbiguousErrors ImplicitsEnabled MacrosEnabled ReportErrors
       outer.owner = object Foo
    }

    Context(<console>) {
       owner       = object Foo
       tree        = Template(scala.AnyRef, _, 2 stats)
       scope       = 0 decls
       contextMode = AmbiguousErrors ImplicitsEnabled MacrosEnabled ReportErrors
       outer.owner = object Foo
    }

    Context(<console>) {
       owner       = object Foo
       tree        = ModuleDef:object Foo extends scala.AnyRef { def <init>(): Foo.type = { super.<in
       scope       = 0 decls
       contextMode = AmbiguousErrors ImplicitsEnabled MacrosEnabled ReportErrors
       outer.owner = object $iw
    }

    Context(<console>) {
       owner       = object $iw
       tree        = Template(scala.AnyRef, _, 2 stats)
       scope       = 0 decls
       contextMode = AmbiguousErrors ImplicitsEnabled MacrosEnabled ReportErrors
       outer.owner = object $iw
    }

    Context(<console>) {
       owner       = object $iw
       tree        = ModuleDef:object $iw extends scala.AnyRef { def <init>(): type = { super.<init>(
       scope       = 0 decls
       contextMode = AmbiguousErrors ImplicitsEnabled MacrosEnabled ReportErrors
       outer.owner = object $iw
    }

    ImportContext { import $line18.$read.$iw.$iw.$iw.$iw.showContext; outer.owner = object $iw }

    ImportContext { import $line17.$read.$iw.$iw.$iw.$iw.showContextImpl; outer.owner = object $iw }

    ImportContext { import reflect.macros.Context; outer.owner = object $iw }

    Context(<console>) {
       owner       = object $iw
       tree        = Template(scala.AnyRef, _, 5 stats)
       scope       = 0 decls
       contextMode = AmbiguousErrors ImplicitsEnabled MacrosEnabled ReportErrors
       outer.owner = object $iw
    }

    Context(<console>) {
       owner       = object $iw
       tree        = ModuleDef:object $iw extends scala.AnyRef { def <init>(): type = { super.<init>(
       scope       = 0 decls
       contextMode = AmbiguousErrors ImplicitsEnabled MacrosEnabled ReportErrors
       outer.owner = object $iw
    }

    ImportContext { import language.experimental._; outer.owner = object $iw }

    Context(<console>) {
       owner       = object $iw
       tree        = Template(scala.AnyRef, _, 3 stats)
       scope       = 0 decls
       contextMode = AmbiguousErrors ImplicitsEnabled MacrosEnabled ReportErrors
       outer.owner = object $iw
    }

    Context(<console>) {
       owner       = object $iw
       tree        = ModuleDef:object $iw extends scala.AnyRef { def <init>(): type = { super.<init>(
       scope       = 0 decls
       contextMode = AmbiguousErrors ImplicitsEnabled MacrosEnabled ReportErrors
       outer.owner = object $iw
    }

    Context(<console>) {
       owner       = object $iw
       tree        = Template(scala.AnyRef, _, 2 stats)
       scope       = 0 decls
       contextMode = AmbiguousErrors ImplicitsEnabled MacrosEnabled ReportErrors
       outer.owner = object $iw
    }

    Context(<console>) {
       owner       = object $iw
       tree        = ModuleDef:object $iw extends scala.AnyRef { def <init>(): type = { super.<init>(
       scope       = 0 decls
       contextMode = AmbiguousErrors ImplicitsEnabled MacrosEnabled ReportErrors
       outer.owner = object $read
    }

    Context(<console>) {
       owner       = object $read
       tree        = Template(scala.AnyRef, _, 2 stats)
       scope       = 0 decls
       contextMode = AmbiguousErrors ImplicitsEnabled MacrosEnabled ReportErrors
       outer.owner = object $read
    }

    Context(<console>) {
       owner       = object $read
       tree        = ModuleDef:object $read extends scala.AnyRef { def <init>(): $line19.$read.type =
       scope       = 1 decls
       contextMode = AmbiguousErrors ImplicitsEnabled MacrosEnabled ReportErrors
       outer.owner = package $line19
    }

    Context(<console>) {
       owner       = package $line19
       tree        = PackageDef:package $line19 { object $read extends scala.AnyRef { def <init>(): $l
       scope       = 1 decls
       contextMode = AmbiguousErrors ImplicitsEnabled MacrosEnabled ReportErrors
       outer.owner = package <root>
    }

    Context(<console>) {
       owner       = package <root>
       tree        = EmptyTree:<empty>
       scope       = 50 decls
       contextMode = AmbiguousErrors ImplicitsEnabled MacrosEnabled ReportErrors
       outer.owner = package <root>
    }

    ImportContext { import scala.this.Predef._; outer.owner = package <root> }

    ImportContext { import scala._; outer.owner = package <root> }

    ImportContext { import java.this.lang._; outer.owner = package <root> }

    Context(NoCompilationUnit) {
       owner       = package <root>
       tree        = Template(Nil, _, 0 stats)
       scope       = 50 decls
       contextMode = MacrosEnabled
       outer.owner = <none>
    }
    defined object Foo
  • Loading branch information...
1 parent 85af192 commit c598e764b91307587388ab2a3f5188e64f42af44 @retronym retronym committed Apr 17, 2013
Showing with 20 additions and 4 deletions.
  1. +20 −4 src/compiler/scala/tools/nsc/typechecker/Contexts.scala
@@ -557,9 +557,25 @@ trait Contexts { self: Analyzer =>
def enclosingContextChain: List[Context] = this :: outer.enclosingContextChain
- override def toString = "Context(%s@%s unit=%s scope=%s errors=%b, reportErrors=%b, throwErrors=%b)".format(
- owner.fullName, tree.shortClass, unit, scope.##, hasErrors, reportErrors, throwErrors
- )
+ private def treeTruncated = tree.toString.replaceAll("\\s+", " ").lines.mkString("\\n").take(70)
+ private def treeIdString = if (settings.uniqid.value) "#" + System.identityHashCode(tree).toString.takeRight(3) else ""
+ private def treeString = tree match {
+ case x: Import => "" + x
+ case Template(parents, `emptyValDef`, body) =>
+ val pstr = if ((parents eq null) || parents.isEmpty) "Nil" else parents mkString " "
+ val bstr = if (body eq null) "" else body.length + " stats"
+ s"""Template($pstr, _, $bstr)"""
+ case x => s"${tree.shortClass}${treeIdString}:${treeTruncated}"
+ }
+
+ override def toString =
+ sm"""|Context($unit) {
+ | owner = $owner
+ | tree = $treeString
+ | scope = ${scope.size} decls
+ | contextMode = $contextMode
+ | outer.owner = ${outer.owner}
+ |}"""
//
// Accessibility checking
@@ -1133,7 +1149,7 @@ trait Contexts { self: Analyzer =>
override final def imports = impInfo :: super.imports
override final def firstImport = Some(impInfo)
override final def isRootImport = impInfo.isRootImport
- override final def toString = "<import>"
+ override final def toString = s"ImportContext { $impInfo; outer.owner = ${outer.owner} }"
}
/** A buffer for warnings and errors that are accumulated during speculative type checking. */

0 comments on commit c598e76

Please sign in to comment.