Skip to content

Commit 52b7d1c

Browse files
ANAS727189gopherbot
authored andcommitted
gopls/internal/mcp: fix panic in symbolReferencesHandler on nil types.Object
resolveSymbol might be nil for unresolved names or members, which caused ObjectLocation to panic. Added proper nil checks and returned an error instead of nil to prevent crashes. Fixes golang/go#74904 Change-Id: I85e5584a2fb8f61baf03e9057089ab8818d07ab4 Reviewed-on: https://go-review.googlesource.com/c/tools/+/697315 Reviewed-by: Robert Findley <[email protected]> Auto-Submit: Robert Findley <[email protected]> Reviewed-by: Hongxiang Jiang <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 477e0ab commit 52b7d1c

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

gopls/internal/mcp/symbol_references.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,15 +140,25 @@ func resolveSymbol(path []string, pkg *cache.Package, pgf *parsego.File) (types.
140140
switch len(path) {
141141
case 1:
142142
_, target := fileScope.LookupParent(path[0], token.NoPos)
143+
if target == nil {
144+
return nil, fmt.Errorf("failed to resolve name %q", path[0])
145+
}
143146
return target, nil
144147
case 2:
145148
switch _, obj := fileScope.LookupParent(path[0], token.NoPos); obj := obj.(type) {
146149
case *types.PkgName:
147-
return obj.Imported().Scope().Lookup(path[1]), nil
150+
target := obj.Imported().Scope().Lookup(path[1])
151+
if target == nil {
152+
return nil, fmt.Errorf("failed to resolve member %q of %q", path[1], path[0])
153+
}
154+
return target, nil
148155
case nil:
149156
return nil, fmt.Errorf("failed to resolve name %q", path[0])
150157
default:
151158
target, _, _ := types.LookupFieldOrMethod(obj.Type(), true, pkg.Types(), path[1])
159+
if target == nil {
160+
return nil, fmt.Errorf("failed to resolve member %q of %q", path[1], path[0])
161+
}
152162
return target, nil
153163
}
154164
case 3:
@@ -163,6 +173,9 @@ func resolveSymbol(path []string, pkg *cache.Package, pgf *parsego.File) (types.
163173
return nil, fmt.Errorf("invalid qualified symbol: could not find %q in package %q", path[1], path[0])
164174
}
165175
target, _, _ := types.LookupFieldOrMethod(recv.Type(), true, pkg.Types(), path[2])
176+
if target == nil {
177+
return nil, fmt.Errorf("failed to resolve member %q of %q", path[2], path[1])
178+
}
166179
return target, nil
167180
}
168181
panic("unreachable")

0 commit comments

Comments
 (0)