]> Cypherpunks repositories - gostls13.git/commitdiff
go/types: fix scope printing (debugging support)
authorRobert Griesemer <gri@golang.org>
Wed, 12 Sep 2018 23:45:10 +0000 (16:45 -0700)
committerRobert Griesemer <gri@golang.org>
Thu, 13 Sep 2018 18:23:02 +0000 (18:23 +0000)
Printing of scopes was horribly wrong: If a scope contained
no object declarations, it would abort printing even if the
scope had children scopes. Also, the line breaks were not
inserted consistently. The actual test case (ExampleScope)
was incorrect as well.

Fixed and simplified printing, and adjusted example which
tests the printing output.

Change-Id: If21c1d4ad71b15a517d4a54da16de5e6228eb4b5
Reviewed-on: https://go-review.googlesource.com/135115
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alan Donovan <adonovan@google.com>
src/go/types/example_test.go
src/go/types/scope.go

index 2a2fb3fc591d3880fc0e269859425b37cfcf9694..492127bbab5764568f5ba15b7dee55e247f9d4db 100644 (file)
@@ -51,6 +51,7 @@ type Celsius float64
 func (c Celsius) String() string { return fmt.Sprintf("%g°C", c) }
 func FToC(f float64) Celsius { return Celsius(f - 32 / 9 * 5) }
 const Boiling Celsius = 100
+func Unused() { {}; {{ var x int; _ = x }} } // make sure empty block scopes get printed
 `},
        } {
                f, err := parser.ParseFile(fset, file.name, file.input, 0)
@@ -81,23 +82,33 @@ const Boiling Celsius = 100
        // .  const temperature.Boiling temperature.Celsius
        // .  type temperature.Celsius float64
        // .  func temperature.FToC(f float64) temperature.Celsius
+       // .  func temperature.Unused()
        // .  func temperature.main()
-       //
        // .  main.go scope {
        // .  .  package fmt
-       //
        // .  .  function scope {
        // .  .  .  var freezing temperature.Celsius
-       // .  .  }.  }
+       // .  .  }
+       // .  }
        // .  celsius.go scope {
        // .  .  package fmt
-       //
        // .  .  function scope {
        // .  .  .  var c temperature.Celsius
        // .  .  }
        // .  .  function scope {
        // .  .  .  var f float64
-       // .  .  }.  }}
+       // .  .  }
+       // .  .  function scope {
+       // .  .  .  block scope {
+       // .  .  .  }
+       // .  .  .  block scope {
+       // .  .  .  .  block scope {
+       // .  .  .  .  .  var x int
+       // .  .  .  .  }
+       // .  .  .  }
+       // .  .  }
+       // .  }
+       // }
 }
 
 // ExampleMethodSet prints the method sets of various types.
index 39e42d758add8eed1e840b3b436c3d285524a210..839a60db2e100cab3291f97f1fc7c4db14635c21 100644 (file)
@@ -161,13 +161,8 @@ func (s *Scope) WriteTo(w io.Writer, n int, recurse bool) {
        const ind = ".  "
        indn := strings.Repeat(ind, n)
 
-       fmt.Fprintf(w, "%s%s scope %p {", indn, s.comment, s)
-       if len(s.elems) == 0 {
-               fmt.Fprintf(w, "}\n")
-               return
-       }
+       fmt.Fprintf(w, "%s%s scope %p {\n", indn, s.comment, s)
 
-       fmt.Fprintln(w)
        indn1 := indn + ind
        for _, name := range s.Names() {
                fmt.Fprintf(w, "%s%s\n", indn1, s.elems[name])
@@ -175,12 +170,11 @@ func (s *Scope) WriteTo(w io.Writer, n int, recurse bool) {
 
        if recurse {
                for _, s := range s.children {
-                       fmt.Fprintln(w)
                        s.WriteTo(w, n+1, recurse)
                }
        }
 
-       fmt.Fprintf(w, "%s}", indn)
+       fmt.Fprintf(w, "%s}\n", indn)
 }
 
 // String returns a string representation of the scope, for debugging.