Skip to content

Commit

Permalink
gopls/internal/analysis/fillstruct: use package name (not path) in UI
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
adonovan authored and gopherbot committed Jun 27, 2024
1 parent 72edac2 commit bc15dd8
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 20 deletions.
5 changes: 3 additions & 2 deletions gopls/internal/analysis/fillstruct/fillstruct.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"golang.org/x/tools/internal/aliases"
"golang.org/x/tools/internal/analysisinternal"
"golang.org/x/tools/internal/typeparams"
"golang.org/x/tools/internal/typesinternal"
)

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

Expand Down
6 changes: 3 additions & 3 deletions gopls/internal/analysis/fillstruct/testdata/src/a/a.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type nestedStruct struct {

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

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

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

var _ = []ast.BasicLit{
{}, // want `go/ast.BasicLit literal has missing fields`
{}, // want `ast.BasicLit literal has missing fields`
}

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

type unsafeStruct struct {
foo unsafe.Pointer
Expand Down
10 changes: 2 additions & 8 deletions gopls/internal/golang/freesymbols.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"golang.org/x/tools/gopls/internal/util/maps"
"golang.org/x/tools/gopls/internal/util/safetoken"
"golang.org/x/tools/gopls/internal/util/slices"
"golang.org/x/tools/internal/typesinternal"
)

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

// TODO(adonovan): factor with RenderPackageDoc.
qualifier := func(other *types.Package) string {
// (like types.RelativeTo but using Package.Name)
if other == pkg.Types() {
return "" // same package; unqualified
}
return other.Name()
}
qualifier := typesinternal.NameRelativeTo(pkg.Types())

// Populate model.
{
Expand Down
8 changes: 1 addition & 7 deletions gopls/internal/golang/pkgdoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -639,13 +639,7 @@ window.addEventListener('load', function() {
// since it is unreachable for the methods in go/doc.
// - elides parameters after the first three: f(a, b, c, ...).
fnString := func(fn *types.Func) string {
// pkgRelative qualifies types by package name alone
pkgRelative := func(other *types.Package) string {
if pkg.Types() == other {
return "" // same package; unqualified
}
return other.Name()
}
pkgRelative := typesinternal.NameRelativeTo(pkg.Types())

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

Expand Down
15 changes: 15 additions & 0 deletions internal/typesinternal/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,18 @@ func ReadGo116ErrorData(err types.Error) (code ErrorCode, start, end token.Pos,
}
return ErrorCode(data[0]), token.Pos(data[1]), token.Pos(data[2]), true
}

// NameRelativeTo returns a types.Qualifier that qualifies members of
// all packages other than pkg, using only the package name.
// (By contrast, [types.RelativeTo] uses the complete package path,
// which is often excessive.)
//
// If pkg is nil, it is equivalent to [*types.Package.Name].
func NameRelativeTo(pkg *types.Package) types.Qualifier {
return func(other *types.Package) string {
if pkg != nil && pkg == other {
return "" // same package; unqualified
}
return other.Name()
}
}

0 comments on commit bc15dd8

Please sign in to comment.