diff --git a/flake.nix b/flake.nix index f9013df..dccf7a4 100644 --- a/flake.nix +++ b/flake.nix @@ -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 diff --git a/internal/testdata/snapshots/input/pr261/README.md b/internal/testdata/snapshots/input/pr261/README.md new file mode 100644 index 0000000..1570ea3 --- /dev/null +++ b/internal/testdata/snapshots/input/pr261/README.md @@ -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. diff --git a/internal/testdata/snapshots/input/pr261/bare_pkg.go b/internal/testdata/snapshots/input/pr261/bare_pkg.go new file mode 100644 index 0000000..ca1895c --- /dev/null +++ b/internal/testdata/snapshots/input/pr261/bare_pkg.go @@ -0,0 +1,5 @@ +package pr261 + +import "sort" + +var _ = sort diff --git a/internal/testdata/snapshots/input/pr261/go.mod b/internal/testdata/snapshots/input/pr261/go.mod new file mode 100644 index 0000000..2bba4a2 --- /dev/null +++ b/internal/testdata/snapshots/input/pr261/go.mod @@ -0,0 +1,3 @@ +module sg/pr261 + +go 1.23 diff --git a/internal/testdata/snapshots/output/pr261/bare_pkg.go b/internal/testdata/snapshots/output/pr261/bare_pkg.go new file mode 100755 index 0000000..e5147a9 --- /dev/null +++ b/internal/testdata/snapshots/output/pr261/bare_pkg.go @@ -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 + diff --git a/internal/visitors/visitor_file.go b/internal/visitors/visitor_file.go index 08c440a..36d9e87 100644 --- a/internal/visitors/visitor_file.go +++ b/internal/visitors/visitor_file.go @@ -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