Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,11 @@
bad=$(
# Snapshot outputs are generated with modified
# indentation for alignment with annotations.
# pr261's input intentionally keeps an "unused" import
# to reproduce a panic on a bare PkgName ident.
find . -name '*.go' \
-not -path '*/testdata/snapshots/output/*' \
-not -path '*/testdata/snapshots/input/pr261/*' \
-exec ${pkgs.gotools}/bin/goimports -l {} +
)
if [ -n "$bad" ]; then
Expand Down
12 changes: 12 additions & 0 deletions internal/testdata/snapshots/input/pr261/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Bare `PkgName` identifier

`var _ = sort` is syntactically valid Go but ill-typed; the compiler rejects it
with `use of package sort not in selector`. The parser still accepts it and the
type checker, run in best-effort mode by `packages.Load`, still records
`info.Uses[sort] = *types.PkgName`.

Well-typed code routes such uses through the `*ast.SelectorExpr` handler in
`visitor_file.go`; a bare ident slips past it into the generic reference path
and previously hit the `panic("should never lookup PkgName ...")` assertion in
`lookup.go`. The visitor now skips bare `*types.PkgName` references instead of
crashing.
5 changes: 5 additions & 0 deletions internal/testdata/snapshots/input/pr261/bare_pkg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package pr261

import "sort"

var _ = sort
3 changes: 3 additions & 0 deletions internal/testdata/snapshots/input/pr261/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module sg/pr261

go 1.23
12 changes: 12 additions & 0 deletions internal/testdata/snapshots/output/pr261/bare_pkg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package pr261
// ^^^^^ definition 0.1.test `sg/pr261`/
// kind Package
// display_name pr261
// signature_documentation
// > package pr261

import "sort"
// ^^^^ reference github.com/golang/go/src go1.22 sort/

var _ = sort

4 changes: 4 additions & 0 deletions internal/visitors/visitor_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,10 @@ func (v *fileVisitor) Visit(n ast.Node) ast.Visitor {
// Emit Reference
ref := info.Uses[node]
if ref != nil {
if _, ok := ref.(*types.PkgName); ok {
return v
}

var (
symbol string
deprecated bool
Expand Down
Loading