Skip to content

Commit bb8e102

Browse files
committed
all: merge master (3fca6a0) into gopls-release-branch.0.7
Merge List: + 2021-12-08 3fca6a0 cmd/gorename: log 'go build' output on failure + 2021-12-08 cadd57e docs: updated nvim-lspconfig link + 2021-12-07 fd2bfb7 go/analysis/passes/stdmethods: recognize any as alias for interface{}, for errors.As check + 2021-12-07 68cbf41 internal/lsp/template: add missed hover cases + 2021-12-07 d3358c1 go/internal/gcimporter: fix test for Go 1.18 any + 2021-12-06 feb39d0 internal/lsp/source: don't format generated files + 2021-12-03 c882a49 gopls/doc: fix rendering of example for the infertypeargs analyzer + 2021-12-03 f64c0f4 internal/lsp/analysis/fillreturns: update fillreturns for new type errs + 2021-12-02 e212aff internal/memoize: do not allow (*Generation).Acquire to fail + 2021-12-02 2ac48c6 go/types/typeutil: add support for mapping generic types + 2021-12-01 df48029 go/internal/gcimporter: allow reusing empty interfaces on the RHS of type decls + 2021-12-01 d99d6fa internal/lsp/protocol: fix whitespace in comments + 2021-12-01 3c63f30 gopls/internal/regtest/misc: temporarily skip TestGenerateProgress + 2021-12-01 615f9a6 internal/lsp/protocol: bring the LSP stubs up to date + 2021-11-30 1fd30d2 refactor/importgraph: set env from packagestest.Export and check errors from Build + 2021-11-30 2c9b078 internal/memoize: record the caller of Destroy + 2021-11-29 6e52f51 x/tools: temporarily skip a couple of tests + 2021-11-29 a618923 internal/lsp/template: fix error that causes crashes + 2021-11-24 cb80a01 cmd/godoc: remove extra // characters from deprecation notice + 2021-11-23 7cf1f38 go/ssa: remove deprecated FindTests and CreateTestMainPackage + 2021-11-23 1e71a25 gopls: template suffix flags and documentation + 2021-11-22 c2c92fd go/callgraph/vta/internal/trie: fix build with go1.12 + 2021-11-19 d0c7211 internal/lsp/cache: fix resolution of the go directive in multi-module workspaces Change-Id: I35eaffd1d914065a619cb71a96f6ce3cc2815888
2 parents f7f495a + 3fca6a0 commit bb8e102

File tree

50 files changed

+1365
-728
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+1365
-728
lines changed

cmd/godoc/doc.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ see https://golang.org/pkg/testing/#hdr-Examples for the conventions.
108108
See "Godoc: documenting Go code" for how to write good comments for godoc:
109109
https://golang.org/doc/articles/godoc_documenting_go_code.html
110110
111-
// Deprecated: godoc cannot select what version of a package is displayed.
112-
// Instead, use golang.org/x/pkgsite/cmd/pkgsite.
111+
Deprecated: godoc cannot select what version of a package is displayed.
112+
Instead, use golang.org/x/pkgsite/cmd/pkgsite.
113113
*/
114114
package main // import "golang.org/x/tools/cmd/godoc"

cmd/gorename/gorename_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,8 +331,8 @@ func buildGorename(t *testing.T) (tmp, bin string, cleanup func()) {
331331
bin += ".exe"
332332
}
333333
cmd := exec.Command("go", "build", "-o", bin)
334-
if err := cmd.Run(); err != nil {
335-
t.Fatalf("Building gorename: %v", err)
334+
if out, err := cmd.CombinedOutput(); err != nil {
335+
t.Fatalf("Building gorename: %v\n%s", err, out)
336336
}
337337
return tmp, bin, func() { os.RemoveAll(tmp) }
338338
}

go/analysis/passes/stdmethods/stdmethods.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ var Analyzer = &analysis.Analyzer{
6161
// we let it go. But if it does have a fmt.ScanState, then the
6262
// rest has to match.
6363
var canonicalMethods = map[string]struct{ args, results []string }{
64-
"As": {[]string{"interface{}"}, []string{"bool"}}, // errors.As
64+
"As": {[]string{"any"}, []string{"bool"}}, // errors.As
6565
// "Flush": {{}, {"error"}}, // http.Flusher and jpeg.writer conflict
6666
"Format": {[]string{"=fmt.State", "rune"}, []string{}}, // fmt.Formatter
6767
"GobDecode": {[]string{"[]byte"}, []string{"error"}}, // gob.GobDecoder
@@ -194,7 +194,9 @@ func matchParams(pass *analysis.Pass, expect []string, actual *types.Tuple, pref
194194
func matchParamType(expect string, actual types.Type) bool {
195195
expect = strings.TrimPrefix(expect, "=")
196196
// Overkill but easy.
197-
return typeString(actual) == expect
197+
t := typeString(actual)
198+
return t == expect ||
199+
(t == "any" || t == "interface{}") && (expect == "any" || expect == "interface{}")
198200
}
199201

200202
var errorType = types.Universe.Lookup("error").Type().Underlying().(*types.Interface)

go/analysis/passes/stdmethods/testdata/src/a/a.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,18 @@ type E int
4747

4848
func (E) Error() string { return "" } // E implements error.
4949

50-
func (E) As() {} // want `method As\(\) should have signature As\(interface{}\) bool`
50+
func (E) As() {} // want `method As\(\) should have signature As\((any|interface\{\})\) bool`
5151
func (E) Is() {} // want `method Is\(\) should have signature Is\(error\) bool`
5252
func (E) Unwrap() {} // want `method Unwrap\(\) should have signature Unwrap\(\) error`
5353

5454
type F int
5555

5656
func (F) Error() string { return "" } // Both F and *F implement error.
5757

58-
func (*F) As() {} // want `method As\(\) should have signature As\(interface{}\) bool`
58+
func (*F) As() {} // want `method As\(\) should have signature As\((any|interface\{\})\) bool`
5959
func (*F) Is() {} // want `method Is\(\) should have signature Is\(error\) bool`
6060
func (*F) Unwrap() {} // want `method Unwrap\(\) should have signature Unwrap\(\) error`
61+
62+
type G int
63+
64+
func (G) As(interface{}) bool // ok
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright 2021 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+
//go:build go1.18
6+
// +build go1.18
7+
8+
package a
9+
10+
type H int
11+
12+
func (H) As(any) bool // ok

go/analysis/passes/stdmethods/testdata/src/typeparams/typeparams.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ type E[P any] int
2828

2929
func (E[_]) Error() string { return "" } // E implements error.
3030

31-
func (E[P]) As() {} // want `method As\(\) should have signature As\(interface{}\) bool`
31+
func (E[P]) As() {} // want `method As\(\) should have signature As\((any|interface\{\})\) bool`
3232
func (E[_]) Is() {} // want `method Is\(\) should have signature Is\(error\) bool`
3333
func (E[_]) Unwrap() {} // want `method Unwrap\(\) should have signature Unwrap\(\) error`
3434

3535
type F[P any] int
3636

3737
func (F[_]) Error() string { return "" } // Both F and *F implement error.
3838

39-
func (*F[_]) As() {} // want `method As\(\) should have signature As\(interface{}\) bool`
39+
func (*F[_]) As() {} // want `method As\(\) should have signature As\((any|interface\{\})\) bool`
4040
func (*F[_]) Is() {} // want `method Is\(\) should have signature Is\(error\) bool`
4141
func (*F[_]) Unwrap() {} // want `method Unwrap\(\) should have signature Unwrap\(\) error`

go/callgraph/vta/internal/trie/bits.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func branchingBit(x, y prefix) bitpos {
4646
if p == 0 {
4747
return 0
4848
}
49-
return 1 << (bits.Len64(uint64(p)) - 1)
49+
return bitpos(1) << uint(bits.Len64(uint64(p))-1) // uint conversion needed for go1.12
5050
}
5151

5252
// zeroBit returns true if k has a 0 bit at position `b`.

go/callgraph/vta/internal/trie/bits_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5+
//go:build go1.13
6+
// +build go1.13
7+
58
package trie
69

710
import (

go/callgraph/vta/internal/trie/trie_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5+
//go:build go1.13
6+
// +build go1.13
7+
58
package trie
69

710
import (

go/internal/gcimporter/gcimporter_test.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ package gcimporter
1010
import (
1111
"bytes"
1212
"fmt"
13+
"go/build"
1314
"go/constant"
1415
"go/types"
1516
"io/ioutil"
@@ -255,7 +256,7 @@ var importedObjectTests = []struct {
255256
{"go/internal/gcimporter.FindPkg", "func FindPkg(path string, srcDir string) (filename string, id string)"},
256257

257258
// interfaces
258-
{"context.Context", "type Context interface{Deadline() (deadline time.Time, ok bool); Done() <-chan struct{}; Err() error; Value(key interface{}) interface{}}"},
259+
{"context.Context", "type Context interface{Deadline() (deadline time.Time, ok bool); Done() <-chan struct{}; Err() error; Value(key any) any}"},
259260
{"crypto.Decrypter", "type Decrypter interface{Decrypt(rand io.Reader, msg []byte, opts DecrypterOpts) (plaintext []byte, err error); Public() PublicKey}"},
260261
{"encoding.BinaryMarshaler", "type BinaryMarshaler interface{MarshalBinary() (data []byte, err error)}"},
261262
{"io.Reader", "type Reader interface{Read(p []byte) (n int, err error)}"},
@@ -264,6 +265,18 @@ var importedObjectTests = []struct {
264265
{"go/types.Type", "type Type interface{String() string; Underlying() Type}"},
265266
}
266267

268+
// TODO(rsc): Delete this init func after x/tools no longer needs to test successfully with Go 1.17.
269+
func init() {
270+
if build.Default.ReleaseTags[len(build.Default.ReleaseTags)-1] <= "go1.17" {
271+
for i := range importedObjectTests {
272+
if importedObjectTests[i].name == "context.Context" {
273+
// Expand any to interface{}.
274+
importedObjectTests[i].want = "type Context interface{Deadline() (deadline time.Time, ok bool); Done() <-chan struct{}; Err() error; Value(key interface{}) interface{}}"
275+
}
276+
}
277+
}
278+
}
279+
267280
func TestImportedTypes(t *testing.T) {
268281
testenv.NeedsGo1Point(t, 11)
269282
// This package only handles gc export data.
@@ -275,6 +288,12 @@ func TestImportedTypes(t *testing.T) {
275288
continue // error reported elsewhere
276289
}
277290
got := types.ObjectString(obj, types.RelativeTo(obj.Pkg()))
291+
292+
// TODO(rsc): Delete this block once go.dev/cl/368254 lands.
293+
if got != test.want && test.want == strings.ReplaceAll(got, "interface{}", "any") {
294+
got = test.want
295+
}
296+
278297
if got != test.want {
279298
t.Errorf("%s: got %q; want %q", test.name, got, test.want)
280299
}

go/internal/gcimporter/iexport_go118_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,13 @@ import (
2323
"golang.org/x/tools/go/internal/gcimporter"
2424
)
2525

26+
// TODO(rfindley): migrate this to testdata, as has been done in the standard library.
2627
func TestGenericExport(t *testing.T) {
2728
const src = `
2829
package generic
2930
31+
type Any any
32+
3033
type T[A, B any] struct { Left A; Right B }
3134
3235
var X T[int, string] = T[int, string]{1, "hi"}

go/internal/gcimporter/iimport.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ func (p *iimporter) pkgAt(off uint64) *types.Package {
331331
}
332332

333333
func (p *iimporter) typAt(off uint64, base *types.Named) types.Type {
334-
if t, ok := p.typCache[off]; ok && (base == nil || !isInterface(t)) {
334+
if t, ok := p.typCache[off]; ok && canReuse(base, t) {
335335
return t
336336
}
337337

@@ -343,12 +343,30 @@ func (p *iimporter) typAt(off uint64, base *types.Named) types.Type {
343343
r.declReader.Reset(p.declData[off-predeclReserved:])
344344
t := r.doType(base)
345345

346-
if base == nil || !isInterface(t) {
346+
if canReuse(base, t) {
347347
p.typCache[off] = t
348348
}
349349
return t
350350
}
351351

352+
// canReuse reports whether the type rhs on the RHS of the declaration for def
353+
// may be re-used.
354+
//
355+
// Specifically, if def is non-nil and rhs is an interface type with methods, it
356+
// may not be re-used because we have a convention of setting the receiver type
357+
// for interface methods to def.
358+
func canReuse(def *types.Named, rhs types.Type) bool {
359+
if def == nil {
360+
return true
361+
}
362+
iface, _ := rhs.(*types.Interface)
363+
if iface == nil {
364+
return true
365+
}
366+
// Don't use iface.Empty() here as iface may not be complete.
367+
return iface.NumEmbeddeds() == 0 && iface.NumExplicitMethods() == 0
368+
}
369+
352370
type importReader struct {
353371
p *iimporter
354372
declReader bytes.Reader

0 commit comments

Comments
 (0)