Skip to content

Commit

Permalink
all: merge master (5ab57de) into gopls-release-branch.0.14
Browse files Browse the repository at this point in the history
Also add back the replace directive.

For golang/go#63220

Merge List:

+ 2023-10-24 5ab57de go/packages: ensure that types.Sizes is correct
+ 2023-10-23 02048e6 go/packages: document that types.Sizes may be nil
+ 2023-10-23 5185da1 internal/refactor/inline: avoid redundant import names added by inlining
+ 2023-10-23 6360c0b gopls/internal/lsp/source: find linkname directives without parsing
+ 2023-10-19 71f6a46 cmd/bundle: drop old +build lines
+ 2023-10-19 cdf1b5e cmd/present: drop NaCl reference from docs
+ 2023-10-18 99bbd3c go/callgraph/vta: use core type for struct fields
+ 2023-10-17 8ed1113 go/ssa: add support for range-over-int
+ 2023-10-16 7df9d5f gopls/internal/lsp: fix signature crash on error.Error

Change-Id: I80b8c0040d9e3ec207446d07212ad671f60c4e7d
  • Loading branch information
findleyr committed Oct 24, 2023
2 parents 83d08c9 + 5ab57de commit 35a647a
Show file tree
Hide file tree
Showing 29 changed files with 663 additions and 202 deletions.
1 change: 0 additions & 1 deletion cmd/bundle/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,6 @@ func bundle(src, dst, dstpkg, prefix, buildTags string) ([]byte, error) {
var out bytes.Buffer
if buildTags != "" {
fmt.Fprintf(&out, "//go:build %s\n", buildTags)
fmt.Fprintf(&out, "// +build %s\n\n", buildTags)
}

fmt.Fprintf(&out, "// Code generated by golang.org/x/tools/cmd/bundle. DO NOT EDIT.\n")
Expand Down
1 change: 0 additions & 1 deletion cmd/bundle/testdata/out.golden
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build tag
// +build tag

// Code generated by golang.org/x/tools/cmd/bundle. DO NOT EDIT.
// $ bundle
Expand Down
3 changes: 0 additions & 3 deletions cmd/present/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ presents slide and article files from the current directory.
It may be run as a stand-alone command or an App Engine app.
The setup of the Go version of NaCl is documented at:
https://golang.org/wiki/NativeClient
To use with App Engine, copy the files in the tools/cmd/present directory to the
root of your application and create an app.yaml file similar to this:
Expand Down
6 changes: 3 additions & 3 deletions go/callgraph/vta/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,12 @@ type field struct {
}

func (f field) Type() types.Type {
s := f.StructType.Underlying().(*types.Struct)
s := typeparams.CoreType(f.StructType).(*types.Struct)
return s.Field(f.index).Type()
}

func (f field) String() string {
s := f.StructType.Underlying().(*types.Struct)
s := typeparams.CoreType(f.StructType).(*types.Struct)
return fmt.Sprintf("Field(%v:%s)", f.StructType, s.Field(f.index).Name())
}

Expand Down Expand Up @@ -434,7 +434,7 @@ func (b *builder) field(f *ssa.Field) {
}

func (b *builder) fieldAddr(f *ssa.FieldAddr) {
t := f.X.Type().Underlying().(*types.Pointer).Elem()
t := typeparams.CoreType(f.X.Type()).(*types.Pointer).Elem()

// Since we are getting pointer to a field, make a bidirectional edge.
fnode := field{StructType: t, index: f.Field}
Expand Down
26 changes: 26 additions & 0 deletions go/callgraph/vta/testdata/src/issue63146.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package test

type embedded struct{}

type S struct{ embedded }

func (_ S) M() {}

type C interface {
M()
S
}

func G[T C]() {
t := T{embedded{}}
t.M()
}

func F() {
G[S]()
}

// WANT:
// F: G[testdata.S]() -> G[testdata.S]
// G[testdata.S]: (S).M(t2) -> S.M
// S.M: (testdata.S).M(t1) -> S.M
1 change: 1 addition & 0 deletions go/callgraph/vta/vta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ func TestVTACallGraphGenerics(t *testing.T) {
files := []string{
"testdata/src/arrays_generics.go",
"testdata/src/callgraph_generics.go",
"testdata/src/issue63146.go",
}
for _, file := range files {
t.Run(file, func(t *testing.T) {
Expand Down
81 changes: 44 additions & 37 deletions go/packages/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,13 +258,21 @@ type driverResponse struct {
// proceeding with further analysis. The PrintErrors function is
// provided for convenient display of all errors.
func Load(cfg *Config, patterns ...string) ([]*Package, error) {
l := newLoader(cfg)
response, err := defaultDriver(&l.Config, patterns...)
ld := newLoader(cfg)
response, err := defaultDriver(&ld.Config, patterns...)
if err != nil {
return nil, err
}
l.sizes = types.SizesFor(response.Compiler, response.Arch)
return l.refine(response)

// If type size information is needed but unavailable.
// reject the whole Load since the error is the same for every package.
ld.sizes = types.SizesFor(response.Compiler, response.Arch)
if ld.sizes == nil && ld.Config.Mode&(NeedTypes|NeedTypesSizes|NeedTypesInfo) != 0 {
return nil, fmt.Errorf("can't determine type sizes for compiler %q on GOARCH %q",
response.Compiler, response.Arch)
}

return ld.refine(response)
}

// defaultDriver is a driver that implements go/packages' fallback behavior.
Expand Down Expand Up @@ -553,7 +561,7 @@ type loaderPackage struct {
type loader struct {
pkgs map[string]*loaderPackage
Config
sizes types.Sizes
sizes types.Sizes // non-nil if needed by mode
parseCache map[string]*parseValue
parseCacheMu sync.Mutex
exportMu sync.Mutex // enforces mutual exclusion of exportdata operations
Expand Down Expand Up @@ -678,7 +686,7 @@ func (ld *loader) refine(response *driverResponse) ([]*Package, error) {
}
}

// Materialize the import graph.
// Materialize the import graph (if NeedImports).

const (
white = 0 // new
Expand All @@ -696,9 +704,8 @@ func (ld *loader) refine(response *driverResponse) ([]*Package, error) {
// visit returns whether the package needs src or has a transitive
// dependency on a package that does. These are the only packages
// for which we load source code.
var stack []*loaderPackage
var stack, srcPkgs []*loaderPackage
var visit func(lpkg *loaderPackage) bool
var srcPkgs []*loaderPackage
visit = func(lpkg *loaderPackage) bool {
switch lpkg.color {
case black:
Expand All @@ -709,35 +716,34 @@ func (ld *loader) refine(response *driverResponse) ([]*Package, error) {
lpkg.color = grey
stack = append(stack, lpkg) // push
stubs := lpkg.Imports // the structure form has only stubs with the ID in the Imports
// If NeedImports isn't set, the imports fields will all be zeroed out.
if ld.Mode&NeedImports != 0 {
lpkg.Imports = make(map[string]*Package, len(stubs))
for importPath, ipkg := range stubs {
var importErr error
imp := ld.pkgs[ipkg.ID]
if imp == nil {
// (includes package "C" when DisableCgo)
importErr = fmt.Errorf("missing package: %q", ipkg.ID)
} else if imp.color == grey {
importErr = fmt.Errorf("import cycle: %s", stack)
}
if importErr != nil {
if lpkg.importErrors == nil {
lpkg.importErrors = make(map[string]error)
}
lpkg.importErrors[importPath] = importErr
continue
lpkg.Imports = make(map[string]*Package, len(stubs))
for importPath, ipkg := range stubs {
var importErr error
imp := ld.pkgs[ipkg.ID]
if imp == nil {
// (includes package "C" when DisableCgo)
importErr = fmt.Errorf("missing package: %q", ipkg.ID)
} else if imp.color == grey {
importErr = fmt.Errorf("import cycle: %s", stack)
}
if importErr != nil {
if lpkg.importErrors == nil {
lpkg.importErrors = make(map[string]error)
}
lpkg.importErrors[importPath] = importErr
continue
}

if visit(imp) {
lpkg.needsrc = true
}
lpkg.Imports[importPath] = imp.Package
if visit(imp) {
lpkg.needsrc = true
}
lpkg.Imports[importPath] = imp.Package
}
if lpkg.needsrc {
srcPkgs = append(srcPkgs, lpkg)
}
// NeedTypeSizes causes TypeSizes to be set even
// on packages for which types aren't needed.
if ld.Mode&NeedTypesSizes != 0 {
lpkg.TypesSizes = ld.sizes
}
Expand All @@ -757,17 +763,18 @@ func (ld *loader) refine(response *driverResponse) ([]*Package, error) {
for _, lpkg := range initial {
visit(lpkg)
}
}
if ld.Mode&NeedImports != 0 && ld.Mode&NeedTypes != 0 {
for _, lpkg := range srcPkgs {

if ld.Mode&NeedTypes != 0 {
// Complete type information is required for the
// immediate dependencies of each source package.
for _, ipkg := range lpkg.Imports {
imp := ld.pkgs[ipkg.ID]
imp.needtypes = true
for _, lpkg := range srcPkgs {
for _, ipkg := range lpkg.Imports {
ld.pkgs[ipkg.ID].needtypes = true
}
}
}
}

// Load type data and syntax if needed, starting at
// the initial packages (roots of the import DAG).
if ld.Mode&NeedTypes != 0 || ld.Mode&NeedSyntax != 0 {
Expand Down Expand Up @@ -1042,7 +1049,7 @@ func (ld *loader) loadPackage(lpkg *loaderPackage) {
IgnoreFuncBodies: ld.Mode&NeedDeps == 0 && !lpkg.initial,

Error: appendError,
Sizes: ld.sizes,
Sizes: ld.sizes, // may be nil
}
if lpkg.Module != nil && lpkg.Module.GoVersion != "" {
typesinternal.SetGoVersion(tc, "go"+lpkg.Module.GoVersion)
Expand Down
28 changes: 28 additions & 0 deletions go/packages/packages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1217,6 +1217,34 @@ func testSizes(t *testing.T, exporter packagestest.Exporter) {
}
}

// This is a regression test for the root cause of
// github.com/golang/vscode-go/issues/3021.
// If types are needed (any of NeedTypes{,Info,Sizes}
// and the types.Sizes cannot be obtained (e.g. due to a bad GOARCH)
// then the Load operation must fail. It must not return a nil
// TypesSizes, or use the default (wrong) size.
//
// We use a file=... query because it suppresses the bad-GOARCH check
// that the go command would otherwise perform eagerly.
// (Gopls relies on this as a fallback.)
func TestNeedTypeSizesWithBadGOARCH(t *testing.T) {
testAllOrModulesParallel(t, func(t *testing.T, exporter packagestest.Exporter) {
exported := packagestest.Export(t, exporter, []packagestest.Module{{
Name: "testdata",
Files: map[string]interface{}{"a/a.go": `package a`}}})
defer exported.Cleanup()

exported.Config.Mode = packages.NeedTypesSizes // or {,Info,Sizes}
exported.Config.Env = append(exported.Config.Env, "GOARCH=286")
_, err := packages.Load(exported.Config, "file=./a/a.go")
got := fmt.Sprint(err)
want := "can't determine type sizes"
if !strings.Contains(got, want) {
t.Errorf("Load error %q does not contain substring %q", got, want)
}
})
}

// TestContainsFallbackSticks ensures that when there are both contains and non-contains queries
// the decision whether to fallback to the pre-1.11 go list sticks across both sets of calls to
// go list.
Expand Down
Loading

0 comments on commit 35a647a

Please sign in to comment.