From: Robert Griesemer Date: Fri, 11 Dec 2009 23:31:06 +0000 (-0800) Subject: fix printer test for new syntax X-Git-Tag: weekly.2009-12-22~84 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=b9b89f56ad7ee2394b5225a7628b6da59581e0be;p=gostls13.git fix printer test for new syntax R=rsc https://golang.org/cl/175048 --- diff --git a/src/pkg/go/printer/nodes.go b/src/pkg/go/printer/nodes.go index e5ecdd2789..545a5f048a 100644 --- a/src/pkg/go/printer/nodes.go +++ b/src/pkg/go/printer/nodes.go @@ -843,8 +843,20 @@ func (p *printer) stmtList(list []ast.Stmt, _indent int) { } +func (p *printer) moveCommentsAfter(pos token.Position) { + // TODO(gri): Make sure a comment doesn't accidentally introduce + // a newline and thus cause a semicolon to be inserted. + // Remove this after transitioning to new semicolon + // syntax and some reasonable grace period (12/11/09). + if p.commentBefore(pos) { + p.comment.List[0].Position = pos + } +} + + // block prints an *ast.BlockStmt; it always spans at least two lines. func (p *printer) block(s *ast.BlockStmt, indent int) { + p.moveCommentsAfter(s.Pos()); p.print(s.Pos(), token.LBRACE); p.stmtList(s.List, indent); p.linebreak(s.Rbrace.Line, 1, maxStmtNewlines, ignore, true); @@ -1109,12 +1121,18 @@ func (p *printer) spec(spec ast.Spec, n int, context declContext, multiLine *boo p.expr(s.Name, multiLine); p.print(blank); } + p.moveCommentsAfter(s.Path[0].Pos()); p.expr(&ast.StringList{s.Path}, multiLine); comment = s.Comment; case *ast.ValueSpec: p.leadComment(s.Doc); p.identList(s.Names, multiLine); // always present + if s.Values != nil { + p.moveCommentsAfter(s.Values[0].Pos()) + } else if s.Type != nil { + p.moveCommentsAfter(s.Type.Pos()) + } if n == 1 { if s.Type != nil { p.print(blank); @@ -1147,6 +1165,7 @@ func (p *printer) spec(spec ast.Spec, n int, context declContext, multiLine *boo case *ast.TypeSpec: p.leadComment(s.Doc); p.expr(s.Name, multiLine); + p.moveCommentsAfter(s.Type.Pos()); if n == 1 { p.print(blank) } else { diff --git a/src/pkg/go/printer/printer_test.go b/src/pkg/go/printer/printer_test.go index c85ddb00f8..823f61740d 100644 --- a/src/pkg/go/printer/printer_test.go +++ b/src/pkg/go/printer/printer_test.go @@ -6,10 +6,12 @@ package printer import ( "bytes"; + oldParser "exp/parser"; "flag"; "io/ioutil"; "go/ast"; "go/parser"; + "os"; "path"; "testing"; ) @@ -38,12 +40,19 @@ type checkMode uint const ( export checkMode = 1 << iota; rawFormat; + oldSyntax; ) func check(t *testing.T, source, golden string, mode checkMode) { // parse source - prog, err := parser.ParseFile(source, nil, parser.ParseComments); + var prog *ast.File; + var err os.Error; + if mode&oldSyntax != 0 { + prog, err = oldParser.ParseFile(source, nil, parser.ParseComments) + } else { + prog, err = parser.ParseFile(source, nil, parser.ParseComments) + } if err != nil { t.Error(err); return; @@ -127,7 +136,7 @@ func Test(t *testing.T) { for _, e := range data { source := path.Join(dataDir, e.source); golden := path.Join(dataDir, e.golden); - check(t, source, golden, e.mode); + check(t, source, golden, e.mode|oldSyntax); // TODO(gri) check that golden is idempotent //check(t, golden, golden, e.mode); } diff --git a/src/pkg/go/printer/testdata/declarations.golden b/src/pkg/go/printer/testdata/declarations.golden index 70be9aa2ea..089fac448c 100644 --- a/src/pkg/go/printer/testdata/declarations.golden +++ b/src/pkg/go/printer/testdata/declarations.golden @@ -23,7 +23,6 @@ import ( aLongRename "io"; b "io"; - c "i" "o"; ) // no newlines between consecutive single imports, but @@ -41,6 +40,10 @@ import _ "fmt" import _ "fmt" import _ "fmt" +// make sure a comment doesn't cause semicolons to be inserted +import _ "foo" // a comment +import "bar" // a comment + // at least one empty line between declarations of different kind import _ "io" @@ -444,6 +447,13 @@ func _() { } +func _() { + var _ = T{ + a, // must introduce trailing comma + }; +} + + // formatting of consecutive single-line functions func _() {} func _() {} @@ -456,3 +466,67 @@ func _() {} func _() { f(1, 2, 3) } func _(x int) int { return x + 1 } func _() int { type T struct{} } + + +// making function declarations safe for new semicolon rules +func _() { /* one-line func */ } + +func _() { // opening "{" must move up /* one-line func */ } + +func _() { // opening "{" must move up// multi-line func + + // in the following declarations, a comment must not + // introduce a newline and thus cause a semicolon to + // be inserted + const _ T = x // comment + ; + const _ = x // comment + ; + + type _ T // comment + ; + type _ struct // comment + { + + } + type _ interface // comment + { + + } + type _ * // comment + T; + type _ [ // comment + ]T; + type _ [ // comment + 10]T; + type _ chan // comment + T; + type _ map // comment + [T]T; + + var _ T // comment + ; + var _ T = x // comment + ; + var _ struct // comment + { + + } + var _ interface // comment + { + + } + var _ * // comment + T; + var _ [ // comment + ]T; + var _ [ // comment + 10]T; + var _ chan // comment + T; + var _ map // comment + [T]T; + + var _ = x // comment + ; +} diff --git a/src/pkg/go/printer/testdata/declarations.input b/src/pkg/go/printer/testdata/declarations.input index c54a2ce222..b876815f22 100644 --- a/src/pkg/go/printer/testdata/declarations.input +++ b/src/pkg/go/printer/testdata/declarations.input @@ -23,7 +23,6 @@ import ( aLongRename "io"; b "io"; - c "i" "o"; ) // no newlines between consecutive single imports, but @@ -41,6 +40,12 @@ import _ "fmt" import _ "fmt" import _ "fmt" +// make sure a comment doesn't cause semicolons to be inserted +import _ // a comment + "foo" +import // a comment + "bar" + // at least one empty line between declarations of different kind import _ "io" @@ -266,11 +271,11 @@ func _() { ) // respect original line breaks var _ = []T { - T{0x20, "Telugu"} + T{0x20, "Telugu"}, }; var _ = []T { // respect original line breaks - T{0x20, "Telugu"} + T{0x20, "Telugu"}, }; } @@ -436,7 +441,14 @@ func _() { "panicln": nil, "print": nil, "println": nil, - } + }, + } +} + + +func _() { + var _ = T{ + a // must introduce trailing comma } } @@ -459,3 +471,62 @@ func _(x int) int { func _() int { type T struct{} } + + +// making function declarations safe for new semicolon rules +func _() +{ /* one-line func */ } + +func _() // opening "{" must move up +{ /* one-line func */ } + +func _() // opening "{" must move up +// multi-line func +{ + // in the following declarations, a comment must not + // introduce a newline and thus cause a semicolon to + // be inserted + const _ // comment + T = x; + const _ // comment + = x; + + type _ // comment + T; + type _ // comment + struct {}; + type _ // comment + interface {}; + type _ // comment + *T; + type _ // comment + []T; + type _ // comment + [10]T; + type _ // comment + chan T; + type _ // comment + map[T]T; + + var _ // comment + T; + var _ // comment + T = x; + var _ // comment + struct {}; + var _ // comment + interface {}; + var _ // comment + *T; + var _ // comment + []T; + var _ // comment + [10]T; + var _ // comment + chan T; + var _ // comment + map[T]T; + + var _ // comment + = x; +} diff --git a/src/pkg/go/printer/testdata/expressions.golden b/src/pkg/go/printer/testdata/expressions.golden index 0530e81da7..4eab165d6f 100644 --- a/src/pkg/go/printer/testdata/expressions.golden +++ b/src/pkg/go/printer/testdata/expressions.golden @@ -222,52 +222,52 @@ func _() { func _() { // not not add extra indentation to multi-line string lists - _ = "foo" "bar"; - _ = "foo" - "bar" + _ = "foo" + "bar"; + _ = "foo" + + "bar" + "bah"; _ = []string{ - "abc" + "abc" + "def", - "foo" + "foo" + "bar", }; } const _ = F1 + - `string = "%s";` - `ptr = *;` - `datafmt.T2 = s ["-" p "-"];` + `string = "%s";` + + `ptr = *;` + + `datafmt.T2 = s ["-" p "-"];` -const _ = `datafmt "datafmt";` - `default = "%v";` - `array = *;` +const _ = `datafmt "datafmt";` + + `default = "%v";` + + `array = *;` + `datafmt.T3 = s {" " a a / ","};` -const _ = `datafmt "datafmt";` - `default = "%v";` - `array = *;` +const _ = `datafmt "datafmt";` + + `default = "%v";` + + `array = *;` + `datafmt.T3 = s {" " a a / ","};` func _() { _ = F1 + - `string = "%s";` - `ptr = *;` - `datafmt.T2 = s ["-" p "-"];`; + `string = "%s";` + + `ptr = *;` + + `datafmt.T2 = s ["-" p "-"];`; _ = - `datafmt "datafmt";` - `default = "%v";` - `array = *;` + `datafmt "datafmt";` + + `default = "%v";` + + `array = *;` + `datafmt.T3 = s {" " a a / ","};`; - _ = `datafmt "datafmt";` - `default = "%v";` - `array = *;` + _ = `datafmt "datafmt";` + + `default = "%v";` + + `array = *;` + `datafmt.T3 = s {" " a a / ","};`; } @@ -279,8 +279,8 @@ func _() { c; _ = a < b || b < a; - _ = "933262154439441526816992388562667004907159682643816214685929" - "638952175999932299156089414639761565182862536979208272237582" + _ = "933262154439441526816992388562667004907159682643816214685929" + + "638952175999932299156089414639761565182862536979208272237582" + "51185210916864000000000000000000000000"; // 100! _ = "170141183460469231731687303715884105727"; // prime } @@ -290,8 +290,8 @@ func _() { const ( _ = "991"; _ = "2432902008176640000"; // 20! - _ = "933262154439441526816992388562667004907159682643816214685929" - "638952175999932299156089414639761565182862536979208272237582" + _ = "933262154439441526816992388562667004907159682643816214685929" + + "638952175999932299156089414639761565182862536979208272237582" + "51185210916864000000000000000000000000"; // 100! _ = "170141183460469231731687303715884105727"; // prime ) diff --git a/src/pkg/go/printer/testdata/expressions.input b/src/pkg/go/printer/testdata/expressions.input index decb58196d..b271e1c626 100644 --- a/src/pkg/go/printer/testdata/expressions.input +++ b/src/pkg/go/printer/testdata/expressions.input @@ -226,53 +226,53 @@ func _() { func _() { // not not add extra indentation to multi-line string lists - _ = "foo" "bar"; - _ = "foo" - "bar" + _ = "foo" + "bar"; + _ = "foo" + + "bar" + "bah"; _ = []string { - "abc" + "abc" + "def", - "foo" - "bar" + "foo" + + "bar", } } const _ = F1 + - `string = "%s";` - `ptr = *;` + `string = "%s";` + + `ptr = *;` + `datafmt.T2 = s ["-" p "-"];` const _ = - `datafmt "datafmt";` - `default = "%v";` - `array = *;` + `datafmt "datafmt";` + + `default = "%v";` + + `array = *;` + `datafmt.T3 = s {" " a a / ","};` -const _ = `datafmt "datafmt";` -`default = "%v";` -`array = *;` +const _ = `datafmt "datafmt";` + +`default = "%v";` + +`array = *;` + `datafmt.T3 = s {" " a a / ","};` func _() { _ = F1 + - `string = "%s";` - `ptr = *;` + `string = "%s";` + + `ptr = *;` + `datafmt.T2 = s ["-" p "-"];`; _ = - `datafmt "datafmt";` - `default = "%v";` - `array = *;` + `datafmt "datafmt";` + + `default = "%v";` + + `array = *;` + `datafmt.T3 = s {" " a a / ","};`; - _ = `datafmt "datafmt";` - `default = "%v";` - `array = *;` + _ = `datafmt "datafmt";` + + `default = "%v";` + + `array = *;` + `datafmt.T3 = s {" " a a / ","};` } @@ -284,8 +284,8 @@ func _() { c; _ = a < b || b < a; - _ = "933262154439441526816992388562667004907159682643816214685929" - "638952175999932299156089414639761565182862536979208272237582" + _ = "933262154439441526816992388562667004907159682643816214685929" + + "638952175999932299156089414639761565182862536979208272237582" + "51185210916864000000000000000000000000"; // 100! _ = "170141183460469231731687303715884105727"; // prime } @@ -295,8 +295,8 @@ func _() { const ( _ = "991"; _ = "2432902008176640000"; // 20! - _ = "933262154439441526816992388562667004907159682643816214685929" - "638952175999932299156089414639761565182862536979208272237582" + _ = "933262154439441526816992388562667004907159682643816214685929" + + "638952175999932299156089414639761565182862536979208272237582" + "51185210916864000000000000000000000000"; // 100! _ = "170141183460469231731687303715884105727"; // prime ) @@ -304,15 +304,15 @@ const ( func same(t, u *Time) bool { // respect source lines in multi-line expressions - return t.Year == u.Year - && t.Month == u.Month - && t.Day == u.Day - && t.Hour == u.Hour - && t.Minute == u.Minute - && t.Second == u.Second - && t.Weekday == u.Weekday - && t.ZoneOffset == u.ZoneOffset - && t.Zone == u.Zone + return t.Year == u.Year && + t.Month == u.Month && + t.Day == u.Day && + t.Hour == u.Hour && + t.Minute == u.Minute && + t.Second == u.Second && + t.Weekday == u.Weekday && + t.ZoneOffset == u.ZoneOffset && + t.Zone == u.Zone } diff --git a/src/pkg/go/printer/testdata/expressions.raw b/src/pkg/go/printer/testdata/expressions.raw index 3e4f326488..2c4bb254f6 100644 --- a/src/pkg/go/printer/testdata/expressions.raw +++ b/src/pkg/go/printer/testdata/expressions.raw @@ -222,52 +222,52 @@ func _() { func _() { // not not add extra indentation to multi-line string lists - _ = "foo" "bar"; - _ = "foo" - "bar" + _ = "foo" + "bar"; + _ = "foo" + + "bar" + "bah"; _ = []string{ - "abc" + "abc" + "def", - "foo" + "foo" + "bar", }; } const _ = F1 + - `string = "%s";` - `ptr = *;` - `datafmt.T2 = s ["-" p "-"];` + `string = "%s";` + + `ptr = *;` + + `datafmt.T2 = s ["-" p "-"];` -const _ = `datafmt "datafmt";` - `default = "%v";` - `array = *;` +const _ = `datafmt "datafmt";` + + `default = "%v";` + + `array = *;` + `datafmt.T3 = s {" " a a / ","};` -const _ = `datafmt "datafmt";` - `default = "%v";` - `array = *;` +const _ = `datafmt "datafmt";` + + `default = "%v";` + + `array = *;` + `datafmt.T3 = s {" " a a / ","};` func _() { _ = F1 + - `string = "%s";` - `ptr = *;` - `datafmt.T2 = s ["-" p "-"];`; + `string = "%s";` + + `ptr = *;` + + `datafmt.T2 = s ["-" p "-"];`; _ = - `datafmt "datafmt";` - `default = "%v";` - `array = *;` + `datafmt "datafmt";` + + `default = "%v";` + + `array = *;` + `datafmt.T3 = s {" " a a / ","};`; - _ = `datafmt "datafmt";` - `default = "%v";` - `array = *;` + _ = `datafmt "datafmt";` + + `default = "%v";` + + `array = *;` + `datafmt.T3 = s {" " a a / ","};`; } @@ -279,8 +279,8 @@ func _() { c; _ = a < b || b < a; - _ = "933262154439441526816992388562667004907159682643816214685929" - "638952175999932299156089414639761565182862536979208272237582" + _ = "933262154439441526816992388562667004907159682643816214685929" + + "638952175999932299156089414639761565182862536979208272237582" + "51185210916864000000000000000000000000"; // 100! _ = "170141183460469231731687303715884105727"; // prime } @@ -290,8 +290,8 @@ func _() { const ( _ = "991"; _ = "2432902008176640000"; // 20! - _ = "933262154439441526816992388562667004907159682643816214685929" - "638952175999932299156089414639761565182862536979208272237582" + _ = "933262154439441526816992388562667004907159682643816214685929" + + "638952175999932299156089414639761565182862536979208272237582" + "51185210916864000000000000000000000000"; // 100! _ = "170141183460469231731687303715884105727"; // prime ) diff --git a/src/pkg/go/printer/testdata/linebreaks.golden b/src/pkg/go/printer/testdata/linebreaks.golden index 22ac8dd303..a4602c90ce 100644 --- a/src/pkg/go/printer/testdata/linebreaks.golden +++ b/src/pkg/go/printer/testdata/linebreaks.golden @@ -177,15 +177,15 @@ var facts = map[int]string{ 2: "2", 10: "3628800", 20: "2432902008176640000", - 100: "933262154439441526816992388562667004907159682643816214685929" - "638952175999932299156089414639761565182862536979208272237582" + 100: "933262154439441526816992388562667004907159682643816214685929" + + "638952175999932299156089414639761565182862536979208272237582" + "51185210916864000000000000000000000000", } func usage() { fmt.Fprintf(os.Stderr, // TODO(gri): the 2nd string of this string list should not be indented - "usage: godoc package [name ...]\n" + "usage: godoc package [name ...]\n"+ " godoc -http=:6060\n"); flag.PrintDefaults(); os.Exit(2); diff --git a/src/pkg/go/printer/testdata/linebreaks.input b/src/pkg/go/printer/testdata/linebreaks.input index 6624e55d0b..9aa7bc075c 100644 --- a/src/pkg/go/printer/testdata/linebreaks.input +++ b/src/pkg/go/printer/testdata/linebreaks.input @@ -56,7 +56,7 @@ var writerTests = []*writerTest{ }, contents: "Google.com\n", }, - } + }, }, // The truncated test file was produced using these commands: // dd if=/dev/zero bs=1048576 count=16384 > /tmp/16gig.txt @@ -177,17 +177,16 @@ var facts = map[int] string { 2: "2", 10: "3628800", 20: "2432902008176640000", - 100: "933262154439441526816992388562667004907159682643816214685929" - "638952175999932299156089414639761565182862536979208272237582" + 100: "933262154439441526816992388562667004907159682643816214685929" + + "638952175999932299156089414639761565182862536979208272237582" + "51185210916864000000000000000000000000", } func usage() { fmt.Fprintf(os.Stderr, // TODO(gri): the 2nd string of this string list should not be indented - "usage: godoc package [name ...]\n" - " godoc -http=:6060\n" - ); + "usage: godoc package [name ...]\n" + + " godoc -http=:6060\n"); flag.PrintDefaults(); os.Exit(2); }