Skip to content

x/tools/gopls: Hover: nil deref in typeDeclContent #74056

Closed
@adonovan

Description

@adonovan
#!stacks
"sigpanic" && "golang.typeDeclContent" && "findRhsTypeDecl"

Issue created by stacks.

// findRhsTypeDecl finds an alias's rhs type and returns its declaration.
// The rhs of an alias might be an alias as well, but we feel this is a rare case.
// It returns an empty string if the given obj is not an alias.
func findRhsTypeDecl(ctx context.Context, snapshot *cache.Snapshot, pkg *cache.Package, obj types.Object) (string, error) {
	if alias, ok := obj.Type().(*types.Alias); ok {
		// we choose Rhs instead of types.Unalias to make the connection between original alias
		// and the corresponding aliased type clearer.
		// types.Unalias brings confusion because it breaks the connection from A to C given
		// the alias chain like 'type ( A = B; B =C ; )' except we show all transitive alias
		// from start to the end. As it's rare, we don't do so.
		t := alias.Rhs()
		switch o := t.(type) {
		case *types.Named:
			obj = o.Obj()
			declPGF1, declPos1, _ := parseFull(ctx, snapshot, pkg.FileSet(), obj.Pos()) <------ missing error check
			realTypeDecl, _, err := typeDeclContent(declPGF1, declPos1, obj) <---- panics if declPGF1 is nil
			return realTypeDecl, err
		}
	}
	return "", nil
}

@xieyuschen

This stack 33hzqg was reported by telemetry:

golang.org/x/tools/gopls@v0.19.0-pre.2 go1.24.3 darwin/arm64 other (1)

Dups: mYeZxQ

Metadata

Metadata

Assignees

Labels

BugReportIssues describing a possible bug in the Go implementation.NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.ToolsThis label describes issues relating to any tools in the x/tools repository.goplsIssues related to the Go language server, gopls.gopls/telemetry-wins

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions