diff --git a/gopls/internal/analysis/fillstruct/fillstruct.go b/gopls/internal/analysis/fillstruct/fillstruct.go index 1428b84ce10..b42b8232f22 100644 --- a/gopls/internal/analysis/fillstruct/fillstruct.go +++ b/gopls/internal/analysis/fillstruct/fillstruct.go @@ -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 @@ -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) @@ -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. diff --git a/gopls/internal/analysis/fillstruct/testdata/src/a/a.go b/gopls/internal/analysis/fillstruct/testdata/src/a/a.go index 79c51d209c1..4a16a803379 100644 --- a/gopls/internal/analysis/fillstruct/testdata/src/a/a.go +++ b/gopls/internal/analysis/fillstruct/testdata/src/a/a.go @@ -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 @@ -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 diff --git a/gopls/internal/golang/freesymbols.go b/gopls/internal/golang/freesymbols.go index edf69382331..f09975d759a 100644 --- a/gopls/internal/golang/freesymbols.go +++ b/gopls/internal/golang/freesymbols.go @@ -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 @@ -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. { diff --git a/gopls/internal/golang/pkgdoc.go b/gopls/internal/golang/pkgdoc.go index 84aee26a9c0..d0e9ffc8040 100644 --- a/gopls/internal/golang/pkgdoc.go +++ b/gopls/internal/golang/pkgdoc.go @@ -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) diff --git a/internal/typesinternal/types.go b/internal/typesinternal/types.go index 7c77c2fbc03..83923286120 100644 --- a/internal/typesinternal/types.go +++ b/internal/typesinternal/types.go @@ -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() + } +}