Skip to content

Commit 35a647a

Browse files
committed
all: merge master (5ab57de) into gopls-release-branch.0.14
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
2 parents 83d08c9 + 5ab57de commit 35a647a

File tree

29 files changed

+663
-202
lines changed

29 files changed

+663
-202
lines changed

Diff for: cmd/bundle/main.go

-1
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,6 @@ func bundle(src, dst, dstpkg, prefix, buildTags string) ([]byte, error) {
228228
var out bytes.Buffer
229229
if buildTags != "" {
230230
fmt.Fprintf(&out, "//go:build %s\n", buildTags)
231-
fmt.Fprintf(&out, "// +build %s\n\n", buildTags)
232231
}
233232

234233
fmt.Fprintf(&out, "// Code generated by golang.org/x/tools/cmd/bundle. DO NOT EDIT.\n")

Diff for: cmd/bundle/testdata/out.golden

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
//go:build tag
2-
// +build tag
32

43
// Code generated by golang.org/x/tools/cmd/bundle. DO NOT EDIT.
54
// $ bundle

Diff for: cmd/present/doc.go

-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ presents slide and article files from the current directory.
88
99
It may be run as a stand-alone command or an App Engine app.
1010
11-
The setup of the Go version of NaCl is documented at:
12-
https://golang.org/wiki/NativeClient
13-
1411
To use with App Engine, copy the files in the tools/cmd/present directory to the
1512
root of your application and create an app.yaml file similar to this:
1613

Diff for: go/callgraph/vta/graph.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,12 @@ type field struct {
106106
}
107107

108108
func (f field) Type() types.Type {
109-
s := f.StructType.Underlying().(*types.Struct)
109+
s := typeparams.CoreType(f.StructType).(*types.Struct)
110110
return s.Field(f.index).Type()
111111
}
112112

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

@@ -434,7 +434,7 @@ func (b *builder) field(f *ssa.Field) {
434434
}
435435

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

439439
// Since we are getting pointer to a field, make a bidirectional edge.
440440
fnode := field{StructType: t, index: f.Field}

Diff for: go/callgraph/vta/testdata/src/issue63146.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package test
2+
3+
type embedded struct{}
4+
5+
type S struct{ embedded }
6+
7+
func (_ S) M() {}
8+
9+
type C interface {
10+
M()
11+
S
12+
}
13+
14+
func G[T C]() {
15+
t := T{embedded{}}
16+
t.M()
17+
}
18+
19+
func F() {
20+
G[S]()
21+
}
22+
23+
// WANT:
24+
// F: G[testdata.S]() -> G[testdata.S]
25+
// G[testdata.S]: (S).M(t2) -> S.M
26+
// S.M: (testdata.S).M(t1) -> S.M

Diff for: go/callgraph/vta/vta_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ func TestVTACallGraphGenerics(t *testing.T) {
127127
files := []string{
128128
"testdata/src/arrays_generics.go",
129129
"testdata/src/callgraph_generics.go",
130+
"testdata/src/issue63146.go",
130131
}
131132
for _, file := range files {
132133
t.Run(file, func(t *testing.T) {

Diff for: go/packages/packages.go

+44-37
Original file line numberDiff line numberDiff line change
@@ -258,13 +258,21 @@ type driverResponse struct {
258258
// proceeding with further analysis. The PrintErrors function is
259259
// provided for convenient display of all errors.
260260
func Load(cfg *Config, patterns ...string) ([]*Package, error) {
261-
l := newLoader(cfg)
262-
response, err := defaultDriver(&l.Config, patterns...)
261+
ld := newLoader(cfg)
262+
response, err := defaultDriver(&ld.Config, patterns...)
263263
if err != nil {
264264
return nil, err
265265
}
266-
l.sizes = types.SizesFor(response.Compiler, response.Arch)
267-
return l.refine(response)
266+
267+
// If type size information is needed but unavailable.
268+
// reject the whole Load since the error is the same for every package.
269+
ld.sizes = types.SizesFor(response.Compiler, response.Arch)
270+
if ld.sizes == nil && ld.Config.Mode&(NeedTypes|NeedTypesSizes|NeedTypesInfo) != 0 {
271+
return nil, fmt.Errorf("can't determine type sizes for compiler %q on GOARCH %q",
272+
response.Compiler, response.Arch)
273+
}
274+
275+
return ld.refine(response)
268276
}
269277

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

681-
// Materialize the import graph.
689+
// Materialize the import graph (if NeedImports).
682690

683691
const (
684692
white = 0 // new
@@ -696,9 +704,8 @@ func (ld *loader) refine(response *driverResponse) ([]*Package, error) {
696704
// visit returns whether the package needs src or has a transitive
697705
// dependency on a package that does. These are the only packages
698706
// for which we load source code.
699-
var stack []*loaderPackage
707+
var stack, srcPkgs []*loaderPackage
700708
var visit func(lpkg *loaderPackage) bool
701-
var srcPkgs []*loaderPackage
702709
visit = func(lpkg *loaderPackage) bool {
703710
switch lpkg.color {
704711
case black:
@@ -709,35 +716,34 @@ func (ld *loader) refine(response *driverResponse) ([]*Package, error) {
709716
lpkg.color = grey
710717
stack = append(stack, lpkg) // push
711718
stubs := lpkg.Imports // the structure form has only stubs with the ID in the Imports
712-
// If NeedImports isn't set, the imports fields will all be zeroed out.
713-
if ld.Mode&NeedImports != 0 {
714-
lpkg.Imports = make(map[string]*Package, len(stubs))
715-
for importPath, ipkg := range stubs {
716-
var importErr error
717-
imp := ld.pkgs[ipkg.ID]
718-
if imp == nil {
719-
// (includes package "C" when DisableCgo)
720-
importErr = fmt.Errorf("missing package: %q", ipkg.ID)
721-
} else if imp.color == grey {
722-
importErr = fmt.Errorf("import cycle: %s", stack)
723-
}
724-
if importErr != nil {
725-
if lpkg.importErrors == nil {
726-
lpkg.importErrors = make(map[string]error)
727-
}
728-
lpkg.importErrors[importPath] = importErr
729-
continue
719+
lpkg.Imports = make(map[string]*Package, len(stubs))
720+
for importPath, ipkg := range stubs {
721+
var importErr error
722+
imp := ld.pkgs[ipkg.ID]
723+
if imp == nil {
724+
// (includes package "C" when DisableCgo)
725+
importErr = fmt.Errorf("missing package: %q", ipkg.ID)
726+
} else if imp.color == grey {
727+
importErr = fmt.Errorf("import cycle: %s", stack)
728+
}
729+
if importErr != nil {
730+
if lpkg.importErrors == nil {
731+
lpkg.importErrors = make(map[string]error)
730732
}
733+
lpkg.importErrors[importPath] = importErr
734+
continue
735+
}
731736

732-
if visit(imp) {
733-
lpkg.needsrc = true
734-
}
735-
lpkg.Imports[importPath] = imp.Package
737+
if visit(imp) {
738+
lpkg.needsrc = true
736739
}
740+
lpkg.Imports[importPath] = imp.Package
737741
}
738742
if lpkg.needsrc {
739743
srcPkgs = append(srcPkgs, lpkg)
740744
}
745+
// NeedTypeSizes causes TypeSizes to be set even
746+
// on packages for which types aren't needed.
741747
if ld.Mode&NeedTypesSizes != 0 {
742748
lpkg.TypesSizes = ld.sizes
743749
}
@@ -757,17 +763,18 @@ func (ld *loader) refine(response *driverResponse) ([]*Package, error) {
757763
for _, lpkg := range initial {
758764
visit(lpkg)
759765
}
760-
}
761-
if ld.Mode&NeedImports != 0 && ld.Mode&NeedTypes != 0 {
762-
for _, lpkg := range srcPkgs {
766+
767+
if ld.Mode&NeedTypes != 0 {
763768
// Complete type information is required for the
764769
// immediate dependencies of each source package.
765-
for _, ipkg := range lpkg.Imports {
766-
imp := ld.pkgs[ipkg.ID]
767-
imp.needtypes = true
770+
for _, lpkg := range srcPkgs {
771+
for _, ipkg := range lpkg.Imports {
772+
ld.pkgs[ipkg.ID].needtypes = true
773+
}
768774
}
769775
}
770776
}
777+
771778
// Load type data and syntax if needed, starting at
772779
// the initial packages (roots of the import DAG).
773780
if ld.Mode&NeedTypes != 0 || ld.Mode&NeedSyntax != 0 {
@@ -1042,7 +1049,7 @@ func (ld *loader) loadPackage(lpkg *loaderPackage) {
10421049
IgnoreFuncBodies: ld.Mode&NeedDeps == 0 && !lpkg.initial,
10431050

10441051
Error: appendError,
1045-
Sizes: ld.sizes,
1052+
Sizes: ld.sizes, // may be nil
10461053
}
10471054
if lpkg.Module != nil && lpkg.Module.GoVersion != "" {
10481055
typesinternal.SetGoVersion(tc, "go"+lpkg.Module.GoVersion)

Diff for: go/packages/packages_test.go

+28
Original file line numberDiff line numberDiff line change
@@ -1217,6 +1217,34 @@ func testSizes(t *testing.T, exporter packagestest.Exporter) {
12171217
}
12181218
}
12191219

1220+
// This is a regression test for the root cause of
1221+
// github.com/golang/vscode-go/issues/3021.
1222+
// If types are needed (any of NeedTypes{,Info,Sizes}
1223+
// and the types.Sizes cannot be obtained (e.g. due to a bad GOARCH)
1224+
// then the Load operation must fail. It must not return a nil
1225+
// TypesSizes, or use the default (wrong) size.
1226+
//
1227+
// We use a file=... query because it suppresses the bad-GOARCH check
1228+
// that the go command would otherwise perform eagerly.
1229+
// (Gopls relies on this as a fallback.)
1230+
func TestNeedTypeSizesWithBadGOARCH(t *testing.T) {
1231+
testAllOrModulesParallel(t, func(t *testing.T, exporter packagestest.Exporter) {
1232+
exported := packagestest.Export(t, exporter, []packagestest.Module{{
1233+
Name: "testdata",
1234+
Files: map[string]interface{}{"a/a.go": `package a`}}})
1235+
defer exported.Cleanup()
1236+
1237+
exported.Config.Mode = packages.NeedTypesSizes // or {,Info,Sizes}
1238+
exported.Config.Env = append(exported.Config.Env, "GOARCH=286")
1239+
_, err := packages.Load(exported.Config, "file=./a/a.go")
1240+
got := fmt.Sprint(err)
1241+
want := "can't determine type sizes"
1242+
if !strings.Contains(got, want) {
1243+
t.Errorf("Load error %q does not contain substring %q", got, want)
1244+
}
1245+
})
1246+
}
1247+
12201248
// TestContainsFallbackSticks ensures that when there are both contains and non-contains queries
12211249
// the decision whether to fallback to the pre-1.11 go list sticks across both sets of calls to
12221250
// go list.

0 commit comments

Comments
 (0)