1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
16 var validFiles = []string{
23 func TestParse(t *testing.T) {
24 for _, filename := range validFiles {
25 _, err := ParseFile(token.NewFileSet(), filename, nil, DeclarationErrors)
27 t.Fatalf("ParseFile(%s): %v", filename, err)
32 func nameFilter(filename string) bool {
34 case "parser.go", "interface.go", "parser_test.go":
36 case "parser.go.orig":
37 return true // permit but should be ignored by ParseDir
42 func dirFilter(f fs.FileInfo) bool { return nameFilter(f.Name()) }
44 func TestParseFile(t *testing.T) {
45 src := "package p\nvar _=s[::]+\ns[::]+\ns[::]+\ns[::]+\ns[::]+\ns[::]+\ns[::]+\ns[::]+\ns[::]+\ns[::]+\ns[::]+\ns[::]"
46 _, err := ParseFile(token.NewFileSet(), "", src, 0)
48 t.Errorf("ParseFile(%s) succeeded unexpectedly", src)
52 func TestParseExprFrom(t *testing.T) {
53 src := "s[::]+\ns[::]+\ns[::]+\ns[::]+\ns[::]+\ns[::]+\ns[::]+\ns[::]+\ns[::]+\ns[::]+\ns[::]+\ns[::]"
54 _, err := ParseExprFrom(token.NewFileSet(), "", src, 0)
56 t.Errorf("ParseExprFrom(%s) succeeded unexpectedly", src)
60 func TestParseDir(t *testing.T) {
62 pkgs, err := ParseDir(token.NewFileSet(), path, dirFilter, 0)
64 t.Fatalf("ParseDir(%s): %v", path, err)
66 if n := len(pkgs); n != 1 {
67 t.Errorf("got %d packages; want 1", n)
71 t.Errorf(`package "parser" not found`)
74 if n := len(pkg.Files); n != 3 {
75 t.Errorf("got %d package files; want 3", n)
77 for filename := range pkg.Files {
78 if !nameFilter(filename) {
79 t.Errorf("unexpected package file: %s", filename)
84 func TestIssue42951(t *testing.T) {
85 path := "./testdata/issue42951"
86 _, err := ParseDir(token.NewFileSet(), path, nil, 0)
88 t.Errorf("ParseDir(%s): %v", path, err)
92 func TestParseExpr(t *testing.T) {
93 // just kicking the tires:
94 // a valid arithmetic expression
96 x, err := ParseExpr(src)
98 t.Errorf("ParseExpr(%q): %v", src, err)
101 if _, ok := x.(*ast.BinaryExpr); !ok {
102 t.Errorf("ParseExpr(%q): got %T, want *ast.BinaryExpr", src, x)
105 // a valid type expression
106 src = "struct{x *int}"
107 x, err = ParseExpr(src)
109 t.Errorf("ParseExpr(%q): %v", src, err)
112 if _, ok := x.(*ast.StructType); !ok {
113 t.Errorf("ParseExpr(%q): got %T, want *ast.StructType", src, x)
116 // an invalid expression
118 x, err = ParseExpr(src)
120 t.Errorf("ParseExpr(%q): got no error", src)
123 t.Errorf("ParseExpr(%q): got no (partial) result", src)
125 if _, ok := x.(*ast.BinaryExpr); !ok {
126 t.Errorf("ParseExpr(%q): got %T, want *ast.BinaryExpr", src, x)
129 // a valid expression followed by extra tokens is invalid
131 if _, err := ParseExpr(src); err == nil {
132 t.Errorf("ParseExpr(%q): got no error", src)
135 // a semicolon is not permitted unless automatically inserted
137 if _, err := ParseExpr(src); err != nil {
138 t.Errorf("ParseExpr(%q): got error %s", src, err)
141 if _, err := ParseExpr(src); err == nil {
142 t.Errorf("ParseExpr(%q): got no error", src)
145 // various other stuff following a valid expression
146 const validExpr = "a + b"
147 const anything = "dh3*#D)#_"
148 for _, c := range "!)]};," {
149 src := validExpr + string(c) + anything
150 if _, err := ParseExpr(src); err == nil {
151 t.Errorf("ParseExpr(%q): got no error", src)
155 // ParseExpr must not crash
156 for _, src := range valids {
161 func TestColonEqualsScope(t *testing.T) {
162 f, err := ParseFile(token.NewFileSet(), "", `package p; func f() { x, y, z := x, y, z }`, 0)
167 // RHS refers to undefined globals; LHS does not.
168 as := f.Decls[0].(*ast.FuncDecl).Body.List[0].(*ast.AssignStmt)
169 for _, v := range as.Rhs {
172 t.Errorf("rhs %s has Obj, should not", id.Name)
175 for _, v := range as.Lhs {
178 t.Errorf("lhs %s does not have Obj, should", id.Name)
183 func TestVarScope(t *testing.T) {
184 f, err := ParseFile(token.NewFileSet(), "", `package p; func f() { var x, y, z = x, y, z }`, 0)
189 // RHS refers to undefined globals; LHS does not.
190 as := f.Decls[0].(*ast.FuncDecl).Body.List[0].(*ast.DeclStmt).Decl.(*ast.GenDecl).Specs[0].(*ast.ValueSpec)
191 for _, v := range as.Values {
194 t.Errorf("rhs %s has Obj, should not", id.Name)
197 for _, id := range as.Names {
199 t.Errorf("lhs %s does not have Obj, should", id.Name)
204 func TestObjects(t *testing.T) {
214 f, err := ParseFile(token.NewFileSet(), "", src, 0)
219 objects := map[string]ast.ObjKind{
220 "p": ast.Bad, // not in a scope
221 "fmt": ast.Bad, // not resolved yet
225 "int": ast.Bad, // not resolved yet
230 ast.Inspect(f, func(n ast.Node) bool {
231 if ident, ok := n.(*ast.Ident); ok {
234 if objects[ident.Name] != ast.Bad {
235 t.Errorf("no object for %s", ident.Name)
239 if obj.Name != ident.Name {
240 t.Errorf("names don't match: obj.Name = %s, ident.Name = %s", obj.Name, ident.Name)
242 kind := objects[ident.Name]
243 if obj.Kind != kind {
244 t.Errorf("%s: obj.Kind = %s; want %s", ident.Name, obj.Kind, kind)
251 func TestUnresolved(t *testing.T) {
252 f, err := ParseFile(token.NewFileSet(), "", `
256 func f2a(byte, int, float)
257 func f3a(a, b int, c float)
259 func f5a(a s1a, b ...complex)
262 func f2b([]byte, (int), *float)
263 func f3b(a, b *int, c []float)
264 func f4b(...*complex)
265 func f5b(a s1a, b ...[]complex)
267 type s1a struct { int }
268 type s2a struct { byte; int; s1a }
269 type s3a struct { a, b int; c float }
271 type s1b struct { *int }
272 type s2b struct { byte; int; *float }
273 type s3b struct { a, b *s3b; c []float }
279 want := "int " + // f1a
280 "byte int float " + // f2a
281 "int float " + // f3a
286 "byte int float " + // f2b
287 "int float " + // f3b
293 "int float " + // s3a
296 "byte int float " + // s2a
299 // collect unresolved identifiers
300 var buf strings.Builder
301 for _, u := range f.Unresolved {
302 buf.WriteString(u.Name)
308 t.Errorf("\ngot: %s\nwant: %s", got, want)
312 func TestCommentGroups(t *testing.T) {
313 f, err := ParseFile(token.NewFileSet(), "", `
314 package p /* 1a */ /* 1b */ /* 1c */ // 1d
320 /* 3c */ const e = 2.7182
322 // Example from go.dev/issue/3139
323 func ExampleCount() {
324 fmt.Println(strings.Count("cheese", "e"))
325 fmt.Println(strings.Count("five", "")) // before & after each rune
334 expected := [][]string{
335 {"/* 1a */", "/* 1b */", "/* 1c */", "// 1d"},
336 {"/* 2a\n*/", "// 2b"},
337 {"/* 3a */", "// 3b", "/* 3c */"},
338 {"// Example from go.dev/issue/3139"},
339 {"// before & after each rune"},
340 {"// Output:", "// 3", "// 5"},
342 if len(f.Comments) != len(expected) {
343 t.Fatalf("got %d comment groups; expected %d", len(f.Comments), len(expected))
345 for i, exp := range expected {
346 got := f.Comments[i].List
347 if len(got) != len(exp) {
348 t.Errorf("got %d comments in group %d; expected %d", len(got), i, len(exp))
351 for j, exp := range exp {
354 t.Errorf("got %q in group %d; expected %q", got, i, exp)
360 func getField(file *ast.File, fieldname string) *ast.Field {
361 parts := strings.Split(fieldname, ".")
362 for _, d := range file.Decls {
363 if d, ok := d.(*ast.GenDecl); ok && d.Tok == token.TYPE {
364 for _, s := range d.Specs {
365 if s, ok := s.(*ast.TypeSpec); ok && s.Name.Name == parts[0] {
366 if s, ok := s.Type.(*ast.StructType); ok {
367 for _, f := range s.Fields.List {
368 for _, name := range f.Names {
369 if name.Name == parts[1] {
382 // Don't use ast.CommentGroup.Text() - we want to see exact comment text.
383 func commentText(c *ast.CommentGroup) string {
384 var buf strings.Builder
386 for _, c := range c.List {
387 buf.WriteString(c.Text)
393 func checkFieldComments(t *testing.T, file *ast.File, fieldname, lead, line string) {
394 f := getField(file, fieldname)
396 t.Fatalf("field not found: %s", fieldname)
398 if got := commentText(f.Doc); got != lead {
399 t.Errorf("got lead comment %q; expected %q", got, lead)
401 if got := commentText(f.Comment); got != line {
402 t.Errorf("got line comment %q; expected %q", got, line)
406 func TestLeadAndLineComments(t *testing.T) {
407 f, err := ParseFile(token.NewFileSet(), "", `
410 /* F1 lead comment */
412 F1 int /* F1 */ // line comment
415 F2 int // F2 line comment
417 f3 int // f3 line comment
419 f4 int /* not a line comment */ ;
420 f5 int ; // f5 line comment
421 f6 int ; /* f6 line comment */
422 f7 int ; /*f7a*/ /*f7b*/ //f7c
428 checkFieldComments(t, f, "T.F1", "/* F1 lead comment *///", "/* F1 */// line comment")
429 checkFieldComments(t, f, "T.F2", "// F2 lead// comment", "// F2 line comment")
430 checkFieldComments(t, f, "T.f3", "// f3 lead comment", "// f3 line comment")
431 checkFieldComments(t, f, "T.f4", "", "")
432 checkFieldComments(t, f, "T.f5", "", "// f5 line comment")
433 checkFieldComments(t, f, "T.f6", "", "/* f6 line comment */")
434 checkFieldComments(t, f, "T.f7", "", "/*f7a*//*f7b*///f7c")
437 checkFieldComments(t, f, "T.F1", "/* F1 lead comment *///", "/* F1 */// line comment")
438 checkFieldComments(t, f, "T.F2", "// F2 lead// comment", "// F2 line comment")
439 if getField(f, "T.f3") != nil {
440 t.Error("not expected to find T.f3")
444 // TestIssue9979 verifies that empty statements are contained within their enclosing blocks.
445 func TestIssue9979(t *testing.T) {
446 for _, src := range []string{
447 "package p; func f() {;}",
448 "package p; func f() {L:}",
449 "package p; func f() {L:;}",
450 "package p; func f() {L:\n}",
451 "package p; func f() {L:\n;}",
452 "package p; func f() { ; }",
453 "package p; func f() { L: }",
454 "package p; func f() { L: ; }",
455 "package p; func f() { L: \n}",
456 "package p; func f() { L: \n; }",
458 fset := token.NewFileSet()
459 f, err := ParseFile(fset, "", src, 0)
464 var pos, end token.Pos
465 ast.Inspect(f, func(x ast.Node) bool {
466 switch s := x.(type) {
468 pos, end = s.Pos()+1, s.End()-1 // exclude "{", "}"
469 case *ast.LabeledStmt:
470 pos, end = s.Pos()+2, s.End() // exclude "L:"
473 if s.Pos() < pos || s.End() > end {
474 t.Errorf("%s: %T[%d, %d] not inside [%d, %d]", src, s, s.Pos(), s.End(), pos, end)
477 offs := fset.Position(s.Pos()).Offset
478 if ch := src[offs]; ch != ';' != s.Implicit {
481 want = "but ';' is implicit"
483 t.Errorf("%s: found %q at offset %d; %s", src, ch, offs, want)
491 func TestFileStartEndPos(t *testing.T) {
492 const src = `// Copyright
496 // Package p doc comment.
503 fset := token.NewFileSet()
504 f, err := ParseFile(fset, "file.go", src, 0)
509 // File{Start,End} spans the entire file, not just the declarations.
510 if got, want := fset.Position(f.FileStart).String(), "file.go:1:1"; got != want {
511 t.Errorf("for File.FileStart, got %s, want %s", got, want)
513 // The end position is the newline at the end of the /* end of file */ line.
514 if got, want := fset.Position(f.FileEnd).String(), "file.go:10:19"; got != want {
515 t.Errorf("for File.FileEnd, got %s, want %s", got, want)
519 // TestIncompleteSelection ensures that an incomplete selector
520 // expression is parsed as a (blank) *ast.SelectorExpr, not a
522 func TestIncompleteSelection(t *testing.T) {
523 for _, src := range []string{
524 "package p; var _ = fmt.", // at EOF
525 "package p; var _ = fmt.\ntype X int", // not at EOF
527 fset := token.NewFileSet()
528 f, err := ParseFile(fset, "", src, 0)
530 t.Errorf("ParseFile(%s) succeeded unexpectedly", src)
534 const wantErr = "expected selector or type assertion"
535 if !strings.Contains(err.Error(), wantErr) {
536 t.Errorf("ParseFile returned wrong error %q, want %q", err, wantErr)
539 var sel *ast.SelectorExpr
540 ast.Inspect(f, func(n ast.Node) bool {
541 if n, ok := n.(*ast.SelectorExpr); ok {
547 t.Error("found no *ast.SelectorExpr")
550 const wantSel = "&{fmt _}"
551 if fmt.Sprint(sel) != wantSel {
552 t.Errorf("found selector %s, want %s", sel, wantSel)
558 func TestLastLineComment(t *testing.T) {
559 const src = `package main
560 type x int // comment
562 fset := token.NewFileSet()
563 f, err := ParseFile(fset, "", src, ParseComments)
567 comment := f.Decls[0].(*ast.GenDecl).Specs[0].(*ast.TypeSpec).Comment.List[0].Text
568 if comment != "// comment" {
569 t.Errorf("got %q, want %q", comment, "// comment")
573 var parseDepthTests = []struct {
576 // multiplier is used when a single statement may result in more than one
577 // change in the depth level, for instance "1+(..." produces a BinaryExpr
578 // followed by a UnaryExpr, which increments the depth twice. The test
579 // case comment explains which nodes are triggering the multiple depth
582 // scope is true if we should also test the statement for the resolver scope
585 // scopeMultiplier does the same as parseMultiplier, but for the scope
589 // The format expands the part inside « » many times.
590 // A second set of brackets nested inside the first stops the repetition,
591 // so that for example «(«1»)» expands to (((...((((1))))...))).
592 {name: "array", format: "package main; var x «[1]»int"},
593 {name: "slice", format: "package main; var x «[]»int"},
594 {name: "struct", format: "package main; var x «struct { X «int» }»", scope: true},
595 {name: "pointer", format: "package main; var x «*»int"},
596 {name: "func", format: "package main; var x «func()»int", scope: true},
597 {name: "chan", format: "package main; var x «chan »int"},
598 {name: "chan2", format: "package main; var x «<-chan »int"},
599 {name: "interface", format: "package main; var x «interface { M() «int» }»", scope: true, scopeMultiplier: 2}, // Scopes: InterfaceType, FuncType
600 {name: "map", format: "package main; var x «map[int]»int"},
601 {name: "slicelit", format: "package main; var x = «[]any{«»}»", parseMultiplier: 2}, // Parser nodes: UnaryExpr, CompositeLit
602 {name: "arraylit", format: "package main; var x = «[1]any{«nil»}»", parseMultiplier: 2}, // Parser nodes: UnaryExpr, CompositeLit
603 {name: "structlit", format: "package main; var x = «struct{x any}{«nil»}»", parseMultiplier: 2}, // Parser nodes: UnaryExpr, CompositeLit
604 {name: "maplit", format: "package main; var x = «map[int]any{1:«nil»}»", parseMultiplier: 2}, // Parser nodes: CompositeLit, KeyValueExpr
605 {name: "dot", format: "package main; var x = «x.»x"},
606 {name: "index", format: "package main; var x = x«[1]»"},
607 {name: "slice", format: "package main; var x = x«[1:2]»"},
608 {name: "slice3", format: "package main; var x = x«[1:2:3]»"},
609 {name: "dottype", format: "package main; var x = x«.(any)»"},
610 {name: "callseq", format: "package main; var x = x«()»"},
611 {name: "methseq", format: "package main; var x = x«.m()»", parseMultiplier: 2}, // Parser nodes: SelectorExpr, CallExpr
612 {name: "binary", format: "package main; var x = «1+»1"},
613 {name: "binaryparen", format: "package main; var x = «1+(«1»)»", parseMultiplier: 2}, // Parser nodes: BinaryExpr, ParenExpr
614 {name: "unary", format: "package main; var x = «^»1"},
615 {name: "addr", format: "package main; var x = «& »x"},
616 {name: "star", format: "package main; var x = «*»x"},
617 {name: "recv", format: "package main; var x = «<-»x"},
618 {name: "call", format: "package main; var x = «f(«1»)»", parseMultiplier: 2}, // Parser nodes: Ident, CallExpr
619 {name: "conv", format: "package main; var x = «(*T)(«1»)»", parseMultiplier: 2}, // Parser nodes: ParenExpr, CallExpr
620 {name: "label", format: "package main; func main() { «Label:» }"},
621 {name: "if", format: "package main; func main() { «if true { «» }»}", parseMultiplier: 2, scope: true, scopeMultiplier: 2}, // Parser nodes: IfStmt, BlockStmt. Scopes: IfStmt, BlockStmt
622 {name: "ifelse", format: "package main; func main() { «if true {} else » {} }", scope: true},
623 {name: "switch", format: "package main; func main() { «switch { default: «» }»}", scope: true, scopeMultiplier: 2}, // Scopes: TypeSwitchStmt, CaseClause
624 {name: "typeswitch", format: "package main; func main() { «switch x.(type) { default: «» }» }", scope: true, scopeMultiplier: 2}, // Scopes: TypeSwitchStmt, CaseClause
625 {name: "for0", format: "package main; func main() { «for { «» }» }", scope: true, scopeMultiplier: 2}, // Scopes: ForStmt, BlockStmt
626 {name: "for1", format: "package main; func main() { «for x { «» }» }", scope: true, scopeMultiplier: 2}, // Scopes: ForStmt, BlockStmt
627 {name: "for3", format: "package main; func main() { «for f(); g(); h() { «» }» }", scope: true, scopeMultiplier: 2}, // Scopes: ForStmt, BlockStmt
628 {name: "forrange0", format: "package main; func main() { «for range x { «» }» }", scope: true, scopeMultiplier: 2}, // Scopes: RangeStmt, BlockStmt
629 {name: "forrange1", format: "package main; func main() { «for x = range z { «» }» }", scope: true, scopeMultiplier: 2}, // Scopes: RangeStmt, BlockStmt
630 {name: "forrange2", format: "package main; func main() { «for x, y = range z { «» }» }", scope: true, scopeMultiplier: 2}, // Scopes: RangeStmt, BlockStmt
631 {name: "go", format: "package main; func main() { «go func() { «» }()» }", parseMultiplier: 2, scope: true}, // Parser nodes: GoStmt, FuncLit
632 {name: "defer", format: "package main; func main() { «defer func() { «» }()» }", parseMultiplier: 2, scope: true}, // Parser nodes: DeferStmt, FuncLit
633 {name: "select", format: "package main; func main() { «select { default: «» }» }", scope: true},
636 // split splits pre«mid»post into pre, mid, post.
637 // If the string does not have that form, split returns x, "", "".
638 func split(x string) (pre, mid, post string) {
639 start, end := strings.Index(x, "«"), strings.LastIndex(x, "»")
640 if start < 0 || end < 0 {
643 return x[:start], x[start+len("«") : end], x[end+len("»"):]
646 func TestParseDepthLimit(t *testing.T) {
648 t.Skip("test requires significant memory")
650 for _, tt := range parseDepthTests {
651 for _, size := range []string{"small", "big"} {
652 t.Run(tt.name+"/"+size, func(t *testing.T) {
654 if tt.parseMultiplier > 0 {
655 n /= tt.parseMultiplier
658 // Decrease the number of statements by 10, in order to check
659 // that we do not fail when under the limit. 10 is used to
660 // provide some wiggle room for cases where the surrounding
661 // scaffolding syntax adds some noise to the depth that changes
662 // on a per testcase basis.
666 pre, mid, post := split(tt.format)
667 if strings.Contains(mid, "«") {
668 left, base, right := split(mid)
669 mid = strings.Repeat(left, n) + base + strings.Repeat(right, n)
671 mid = strings.Repeat(mid, n)
673 input := pre + mid + post
675 fset := token.NewFileSet()
676 _, err := ParseFile(fset, "", input, ParseComments|SkipObjectResolution)
679 t.Errorf("ParseFile(...): %v (want success)", err)
682 expected := "exceeded max nesting depth"
683 if err == nil || !strings.HasSuffix(err.Error(), expected) {
684 t.Errorf("ParseFile(...) = _, %v, want %q", err, expected)
692 func TestScopeDepthLimit(t *testing.T) {
693 for _, tt := range parseDepthTests {
697 for _, size := range []string{"small", "big"} {
698 t.Run(tt.name+"/"+size, func(t *testing.T) {
699 n := maxScopeDepth + 1
700 if tt.scopeMultiplier > 0 {
701 n /= tt.scopeMultiplier
704 // Decrease the number of statements by 10, in order to check
705 // that we do not fail when under the limit. 10 is used to
706 // provide some wiggle room for cases where the surrounding
707 // scaffolding syntax adds some noise to the depth that changes
708 // on a per testcase basis.
712 pre, mid, post := split(tt.format)
713 if strings.Contains(mid, "«") {
714 left, base, right := split(mid)
715 mid = strings.Repeat(left, n) + base + strings.Repeat(right, n)
717 mid = strings.Repeat(mid, n)
719 input := pre + mid + post
721 fset := token.NewFileSet()
722 _, err := ParseFile(fset, "", input, DeclarationErrors)
725 t.Errorf("ParseFile(...): %v (want success)", err)
728 expected := "exceeded max scope depth during object resolution"
729 if err == nil || !strings.HasSuffix(err.Error(), expected) {
730 t.Errorf("ParseFile(...) = _, %v, want %q", err, expected)
738 // proposal go.dev/issue/50429
739 func TestRangePos(t *testing.T) {
740 testcases := []string{
741 "package p; func _() { for range x {} }",
742 "package p; func _() { for i = range x {} }",
743 "package p; func _() { for i := range x {} }",
744 "package p; func _() { for k, v = range x {} }",
745 "package p; func _() { for k, v := range x {} }",
748 for _, src := range testcases {
749 fset := token.NewFileSet()
750 f, err := ParseFile(fset, src, src, 0)
755 ast.Inspect(f, func(x ast.Node) bool {
756 switch s := x.(type) {
758 pos := fset.Position(s.Range)
759 if pos.Offset != strings.Index(src, "range") {
760 t.Errorf("%s: got offset %v, want %v", src, pos.Offset, strings.Index(src, "range"))
768 // TestIssue59180 tests that line number overflow doesn't cause an infinite loop.
769 func TestIssue59180(t *testing.T) {
770 testcases := []string{
771 "package p\n//line :9223372036854775806\n\n//",
772 "package p\n//line :1:9223372036854775806\n\n//",
773 "package p\n//line file:9223372036854775806\n\n//",
776 for _, src := range testcases {
777 _, err := ParseFile(token.NewFileSet(), "", src, ParseComments)
779 t.Errorf("ParseFile(%s) succeeded unexpectedly", src)
784 func TestGoVersion(t *testing.T) {
785 fset := token.NewFileSet()
786 pkgs, err := ParseDir(fset, "./testdata/goversion", nil, 0)
791 for _, p := range pkgs {
792 want := strings.ReplaceAll(p.Name, "_", ".")
796 for _, f := range p.Files {
797 if f.GoVersion != want {
798 t.Errorf("%s: GoVersion = %q, want %q", fset.Position(f.Pos()), f.GoVersion, want)