golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
124.23k stars 17.7k forks source link

x/tools/gopls: Hover: panic in lookupObjectByName #69616

Open adonovan opened 1 month ago

adonovan commented 1 month ago
#!stacks
"runtime.sigpanic" && "golang.lookupObjectByName:+4"

Issue created by stacks.

func lookupObjectByName(pkg *cache.Package, pgf *parsego.File, name string) types.Object {
    scope := pkg.Types().Scope()
    fileScope := pkg.TypesInfo().Scopes[pgf.File]
    pkgName, suffix, _ := strings.Cut(name, ".")
    obj, ok := fileScope.Lookup(pkgName).(*types.PkgName) // <--- panic

This stack l9BGAQ was reported by telemetry:

gabyhelp commented 1 month ago

Related Issues and Documentation

(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)

adonovan commented 1 week ago

This is mysterious. The crash is in go/types.(*Scope).Lookup on this line:

func (s *Scope) Lookup(name string) Object {
    obj := resolve(name, s.elems[name])
    if obj == universeAnyAlias && !aliasAny() { // <---------- panic
        return universeAnyNoAlias
    }
    return obj
}

but the only way that line can panic (short of a compiler error that causes the wrong line to be reported) is if obj is a types.Object value of the same concrete type as universeAnyAlias and that concrete type does not support == comparison. However, universeAnyAlias clearly has type *TypeName, a pointer, which supports == comparison. (And presumably resolve also returns only pointers to various struct types).

findleyr commented 1 week ago

I do not understand how this crash is possible, and so suggest that we move it to the next milestone and see if it reoccurs in v0.17.0.

adonovan commented 1 week ago

@dr2chase Got any ideas how this crash could possibly occur?