From: Robert Griesemer Date: Fri, 16 Nov 2012 21:17:12 +0000 (-0800) Subject: go/printer: leave indentation alone when printing nodes from different files X-Git-Tag: go1.1rc2~1854 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=de58eb9091d24abd9d837b8a787ba90eadd1ab0a;p=gostls13.git go/printer: leave indentation alone when printing nodes from different files ASTs may be created by various tools and built from nodes of different files. An incorrectly constructed AST will likely not print at all, but a (structurally) correct AST with bad position information should still print structurally correct. One heuristic used was to reset indentation when the filename in the position information of nodes changed. However, this can lead to wrong indentation for structurally correct ASTs. Fix: Don't change the indentation in this case. Related to issue 4300. R=r CC=golang-dev https://golang.org/cl/6849066 --- diff --git a/src/pkg/go/printer/printer.go b/src/pkg/go/printer/printer.go index 030bc2387e..fa591c5764 100644 --- a/src/pkg/go/printer/printer.go +++ b/src/pkg/go/printer/printer.go @@ -225,7 +225,14 @@ func (p *printer) writeString(pos token.Position, s string, isLit bool) { // (used when printing merged ASTs of different files // e.g., the result of ast.MergePackageFiles) if p.last.IsValid() && p.last.Filename != pos.Filename { - p.indent = 0 + // Note: Do not set p.indent to 0 - this seems to be a bad heuristic. + // ASTs may be created by various tools and built from nodes of + // different files. An incorrectly constructed AST will likely + // not print at all, but a (structurally) correct AST with bad + // position information should still print structurally correct. + // If p.indent is reset, indentation may be off, and likely lead + // to indentation underflow (to detect set: debug = true). + // See also issue 4300 (11/16/2012). p.mode = 0 p.wsbuf = p.wsbuf[0:0] }