Skip to content

Commit bc15dd8

Browse files
adonovangopherbot
authored andcommitted
gopls/internal/analysis/fillstruct: use package name (not path) in UI
Also, factor out three instances of the type qualifier as typesinternal.NameRelativeTo. Change-Id: I22c1e8f89e77a7164c178cf335253a097b9850be Reviewed-on: https://go-review.googlesource.com/c/tools/+/595119 Reviewed-by: Robert Findley <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Auto-Submit: Alan Donovan <[email protected]>
1 parent 72edac2 commit bc15dd8

File tree

5 files changed

+24
-20
lines changed

5 files changed

+24
-20
lines changed

gopls/internal/analysis/fillstruct/fillstruct.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"golang.org/x/tools/internal/aliases"
3030
"golang.org/x/tools/internal/analysisinternal"
3131
"golang.org/x/tools/internal/typeparams"
32+
"golang.org/x/tools/internal/typesinternal"
3233
)
3334

3435
// Diagnose computes diagnostics for fillable struct literals overlapping with
@@ -89,7 +90,7 @@ func Diagnose(f *ast.File, start, end token.Pos, pkg *types.Package, info *types
8990
var name string
9091
if typ != tStruct {
9192
// named struct type (e.g. pkg.S[T])
92-
name = types.TypeString(typ, types.RelativeTo(pkg))
93+
name = types.TypeString(typ, typesinternal.NameRelativeTo(pkg))
9394
} else {
9495
// anonymous struct type
9596
totalFields := len(fillableFields)
@@ -159,7 +160,7 @@ func SuggestedFix(fset *token.FileSet, start, end token.Pos, content []byte, fil
159160
tStruct, ok := typ.Underlying().(*types.Struct)
160161
if !ok {
161162
return nil, nil, fmt.Errorf("%s is not a (pointer to) struct type",
162-
types.TypeString(typ, types.RelativeTo(pkg)))
163+
types.TypeString(typ, typesinternal.NameRelativeTo(pkg)))
163164
}
164165
// Inv: typ is the possibly-named struct type.
165166

gopls/internal/analysis/fillstruct/testdata/src/a/a.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ type nestedStruct struct {
3939

4040
var _ = nestedStruct{} // want `nestedStruct literal has missing fields`
4141

42-
var _ = data.B{} // want `b.B literal has missing fields`
42+
var _ = data.B{} // want `fillstruct.B literal has missing fields`
4343

4444
type typedStruct struct {
4545
m map[string]int
@@ -100,10 +100,10 @@ type pointerBuiltinStruct struct {
100100
var _ = pointerBuiltinStruct{} // want `pointerBuiltinStruct literal has missing fields`
101101

102102
var _ = []ast.BasicLit{
103-
{}, // want `go/ast.BasicLit literal has missing fields`
103+
{}, // want `ast.BasicLit literal has missing fields`
104104
}
105105

106-
var _ = []ast.BasicLit{{}} // want "go/ast.BasicLit literal has missing fields"
106+
var _ = []ast.BasicLit{{}} // want "ast.BasicLit literal has missing fields"
107107

108108
type unsafeStruct struct {
109109
foo unsafe.Pointer

gopls/internal/golang/freesymbols.go

+2-8
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"golang.org/x/tools/gopls/internal/util/maps"
2424
"golang.org/x/tools/gopls/internal/util/safetoken"
2525
"golang.org/x/tools/gopls/internal/util/slices"
26+
"golang.org/x/tools/internal/typesinternal"
2627
)
2728

2829
// FreeSymbolsHTML returns an HTML document containing the report of
@@ -49,14 +50,7 @@ func FreeSymbolsHTML(viewID string, pkg *cache.Package, pgf *parsego.File, start
4950
Local []Symbol
5051
}
5152

52-
// TODO(adonovan): factor with RenderPackageDoc.
53-
qualifier := func(other *types.Package) string {
54-
// (like types.RelativeTo but using Package.Name)
55-
if other == pkg.Types() {
56-
return "" // same package; unqualified
57-
}
58-
return other.Name()
59-
}
53+
qualifier := typesinternal.NameRelativeTo(pkg.Types())
6054

6155
// Populate model.
6256
{

gopls/internal/golang/pkgdoc.go

+1-7
Original file line numberDiff line numberDiff line change
@@ -639,13 +639,7 @@ window.addEventListener('load', function() {
639639
// since it is unreachable for the methods in go/doc.
640640
// - elides parameters after the first three: f(a, b, c, ...).
641641
fnString := func(fn *types.Func) string {
642-
// pkgRelative qualifies types by package name alone
643-
pkgRelative := func(other *types.Package) string {
644-
if pkg.Types() == other {
645-
return "" // same package; unqualified
646-
}
647-
return other.Name()
648-
}
642+
pkgRelative := typesinternal.NameRelativeTo(pkg.Types())
649643

650644
sig := fn.Type().(*types.Signature)
651645

internal/typesinternal/types.go

+15
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,18 @@ func ReadGo116ErrorData(err types.Error) (code ErrorCode, start, end token.Pos,
4848
}
4949
return ErrorCode(data[0]), token.Pos(data[1]), token.Pos(data[2]), true
5050
}
51+
52+
// NameRelativeTo returns a types.Qualifier that qualifies members of
53+
// all packages other than pkg, using only the package name.
54+
// (By contrast, [types.RelativeTo] uses the complete package path,
55+
// which is often excessive.)
56+
//
57+
// If pkg is nil, it is equivalent to [*types.Package.Name].
58+
func NameRelativeTo(pkg *types.Package) types.Qualifier {
59+
return func(other *types.Package) string {
60+
if pkg != nil && pkg == other {
61+
return "" // same package; unqualified
62+
}
63+
return other.Name()
64+
}
65+
}

0 commit comments

Comments
 (0)