Skip to content

Commit a721d4c

Browse files
committed
internal/typesinternal: factor out IsPackageLevel
Define IsPackageLevel in one place instead of three. Replace all calls to the new function. Delete the old ones. Change-Id: I30e0a41908e72ef4a5c0715489672a32209c55b1 Reviewed-on: https://go-review.googlesource.com/c/tools/+/645696 Reviewed-by: Alan Donovan <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent f0ddc4b commit a721d4c

File tree

9 files changed

+18
-32
lines changed

9 files changed

+18
-32
lines changed

gopls/internal/golang/freesymbols.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ func freeRefs(pkg *types.Package, info *types.Info, file *ast.File, start, end t
297297

298298
// Compute dotted path.
299299
objects := append(suffix, obj)
300-
if obj.Pkg() != nil && obj.Pkg() != pkg && isPackageLevel(obj) { // dot import
300+
if obj.Pkg() != nil && obj.Pkg() != pkg && typesinternal.IsPackageLevel(obj) { // dot import
301301
// Synthesize the implicit PkgName.
302302
pkgName := types.NewPkgName(token.NoPos, pkg, obj.Pkg().Name(), obj.Pkg())
303303
parent = fileScope

gopls/internal/golang/hover.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -587,13 +587,13 @@ func hover(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, pp pro
587587
pkg := obj.Pkg()
588588
if recv != nil {
589589
linkName = fmt.Sprintf("(%s.%s).%s", pkg.Name(), recv.Name(), obj.Name())
590-
if obj.Exported() && recv.Exported() && isPackageLevel(recv) {
590+
if obj.Exported() && recv.Exported() && typesinternal.IsPackageLevel(recv) {
591591
linkPath = pkg.Path()
592592
anchor = fmt.Sprintf("%s.%s", recv.Name(), obj.Name())
593593
}
594594
} else {
595595
linkName = fmt.Sprintf("%s.%s", pkg.Name(), obj.Name())
596-
if obj.Exported() && isPackageLevel(obj) {
596+
if obj.Exported() && typesinternal.IsPackageLevel(obj) {
597597
linkPath = pkg.Path()
598598
anchor = obj.Name()
599599
}
@@ -1333,7 +1333,7 @@ func StdSymbolOf(obj types.Object) *stdlib.Symbol {
13331333
}
13341334

13351335
// Handle Function, Type, Const & Var.
1336-
if isPackageLevel(obj) {
1336+
if obj != nil && typesinternal.IsPackageLevel(obj) {
13371337
for _, s := range symbols {
13381338
if s.Kind == stdlib.Method || s.Kind == stdlib.Field {
13391339
continue
@@ -1348,7 +1348,7 @@ func StdSymbolOf(obj types.Object) *stdlib.Symbol {
13481348
// Handle Method.
13491349
if fn, _ := obj.(*types.Func); fn != nil {
13501350
isPtr, named := typesinternal.ReceiverNamed(fn.Signature().Recv())
1351-
if named != nil && isPackageLevel(named.Obj()) {
1351+
if named != nil && typesinternal.IsPackageLevel(named.Obj()) {
13521352
for _, s := range symbols {
13531353
if s.Kind != stdlib.Method {
13541354
continue

gopls/internal/golang/pkgdoc.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ func DocFragment(pkg *cache.Package, pgf *parsego.File, start, end token.Pos) (p
140140
}
141141

142142
// package-level symbol?
143-
if isPackageLevel(sym) {
143+
if typesinternal.IsPackageLevel(sym) {
144144
return pkgpath, sym.Name(), makeTitle(objectKind(sym), sym.Pkg(), sym.Name())
145145
}
146146

gopls/internal/golang/rename.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ func renameOrdinary(ctx context.Context, snapshot *cache.Snapshot, f file.Handle
555555
// objectpath, the classifies them as local vars, but as
556556
// they came from export data they lack syntax and the
557557
// correct scope tree (issue #61294).
558-
if !obj.(*types.Var).IsField() && !isPackageLevel(obj) {
558+
if !obj.(*types.Var).IsField() && !typesinternal.IsPackageLevel(obj) {
559559
goto skipObjectPath
560560
}
561561
}
@@ -1345,7 +1345,7 @@ func (r *renamer) updateCommentDocLinks() (map[protocol.DocumentURI][]diff.Edit,
13451345
recvName := ""
13461346
// Doc links can reference only exported package-level objects
13471347
// and methods of exported package-level named types.
1348-
if !isPackageLevel(obj) {
1348+
if !typesinternal.IsPackageLevel(obj) {
13491349
obj, isFunc := obj.(*types.Func)
13501350
if !isFunc {
13511351
continue
@@ -1363,7 +1363,7 @@ func (r *renamer) updateCommentDocLinks() (map[protocol.DocumentURI][]diff.Edit,
13631363
continue
13641364
}
13651365
name := named.Origin().Obj()
1366-
if !name.Exported() || !isPackageLevel(name) {
1366+
if !name.Exported() || !typesinternal.IsPackageLevel(name) {
13671367
continue
13681368
}
13691369
recvName = name.Name()

gopls/internal/golang/rename_check.go

+1-8
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func (r *renamer) check(from types.Object) {
100100
r.checkInFileBlock(from_)
101101
} else if from_, ok := from.(*types.Label); ok {
102102
r.checkLabel(from_)
103-
} else if isPackageLevel(from) {
103+
} else if typesinternal.IsPackageLevel(from) {
104104
r.checkInPackageBlock(from)
105105
} else if v, ok := from.(*types.Var); ok && v.IsField() {
106106
r.checkStructField(v)
@@ -949,13 +949,6 @@ func isLocal(obj types.Object) bool {
949949
return depth >= 4
950950
}
951951

952-
func isPackageLevel(obj types.Object) bool {
953-
if obj == nil {
954-
return false
955-
}
956-
return obj.Pkg().Scope().Lookup(obj.Name()) == obj
957-
}
958-
959952
// -- Plundered from go/scanner: ---------------------------------------
960953

961954
func isLetter(ch rune) bool {

internal/analysisinternal/analysis.go

+2-10
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ func IsTypeNamed(t types.Type, pkgPath string, names ...string) bool {
299299
if named, ok := types.Unalias(t).(*types.Named); ok {
300300
tname := named.Obj()
301301
return tname != nil &&
302-
isPackageLevel(tname) &&
302+
typesinternal.IsPackageLevel(tname) &&
303303
tname.Pkg().Path() == pkgPath &&
304304
slices.Contains(names, tname.Name())
305305
}
@@ -326,7 +326,7 @@ func IsPointerToNamed(t types.Type, pkgPath string, names ...string) bool {
326326
func IsFunctionNamed(obj types.Object, pkgPath string, names ...string) bool {
327327
f, ok := obj.(*types.Func)
328328
return ok &&
329-
isPackageLevel(obj) &&
329+
typesinternal.IsPackageLevel(obj) &&
330330
f.Pkg().Path() == pkgPath &&
331331
f.Type().(*types.Signature).Recv() == nil &&
332332
slices.Contains(names, f.Name())
@@ -350,14 +350,6 @@ func IsMethodNamed(obj types.Object, pkgPath string, typeName string, names ...s
350350
return false
351351
}
352352

353-
// isPackageLevel reports whether obj is a package-level symbol.
354-
//
355-
// TODO(adonovan): publish in typesinternal and factor with
356-
// gopls/internal/golang/rename_check.go, refactor/rename/util.go.
357-
func isPackageLevel(obj types.Object) bool {
358-
return obj.Pkg() != nil && obj.Parent() == obj.Pkg().Scope()
359-
}
360-
361353
// ValidateFixes validates the set of fixes for a single diagnostic.
362354
// Any error indicates a bug in the originating analyzer.
363355
//

internal/typesinternal/types.go

+5
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,8 @@ func Origin(t NamedOrAlias) NamedOrAlias {
120120
}
121121
return t
122122
}
123+
124+
// IsPackageLevel reports whether obj is a package-level symbol.
125+
func IsPackageLevel(obj types.Object) bool {
126+
return obj.Pkg() != nil && obj.Parent() == obj.Pkg().Scope()
127+
}

refactor/rename/check.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func (r *renamer) check(from types.Object) {
3636
r.checkInFileBlock(from_)
3737
} else if from_, ok := from.(*types.Label); ok {
3838
r.checkLabel(from_)
39-
} else if isPackageLevel(from) {
39+
} else if typesinternal.IsPackageLevel(from) {
4040
r.checkInPackageBlock(from)
4141
} else if v, ok := from.(*types.Var); ok && v.IsField() {
4242
r.checkStructField(v)

refactor/rename/util.go

-4
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,6 @@ func isLocal(obj types.Object) bool {
6161
return depth >= 4
6262
}
6363

64-
func isPackageLevel(obj types.Object) bool {
65-
return obj.Pkg().Scope().Lookup(obj.Name()) == obj
66-
}
67-
6864
// -- Plundered from go/scanner: ---------------------------------------
6965

7066
func isLetter(ch rune) bool {

0 commit comments

Comments
 (0)