golang.org/x/tools/gopls@v0.15.3/internal/cache/parsego/parse_test.go (about)

     1  // Copyright 2023 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.
     4  
     5  package parsego_test
     6  
     7  import (
     8  	"context"
     9  	"go/ast"
    10  	"go/token"
    11  	"testing"
    12  
    13  	"golang.org/x/tools/gopls/internal/cache/parsego"
    14  	"golang.org/x/tools/gopls/internal/util/safetoken"
    15  	"golang.org/x/tools/internal/tokeninternal"
    16  )
    17  
    18  // TODO(golang/go#64335): we should have many more tests for fixed syntax.
    19  
    20  func TestFixPosition_Issue64488(t *testing.T) {
    21  	// This test reproduces the conditions of golang/go#64488, where a type error
    22  	// on fixed syntax overflows the token.File.
    23  	const src = `
    24  package foo
    25  
    26  func _() {
    27  	type myThing struct{}
    28  	var foo []myThing
    29  	for ${1:}, ${2:} := range foo {
    30  	$0
    31  }
    32  }
    33  `
    34  
    35  	pgf, _ := parsego.Parse(context.Background(), token.NewFileSet(), "file://foo.go", []byte(src), parsego.ParseFull, false)
    36  	fset := tokeninternal.FileSetFor(pgf.Tok)
    37  	ast.Inspect(pgf.File, func(n ast.Node) bool {
    38  		if n != nil {
    39  			posn := safetoken.StartPosition(fset, n.Pos())
    40  			if !posn.IsValid() {
    41  				t.Fatalf("invalid position for %T (%v): %v not in [%d, %d]", n, n, n.Pos(), pgf.Tok.Base(), pgf.Tok.Base()+pgf.Tok.Size())
    42  			}
    43  		}
    44  		return true
    45  	})
    46  }