Skip to content

Commit 35d8b1b

Browse files
committed
gopls/internal/golang: don't bug.Report repaired ASTs in hover
Updates golang/go#64241 Change-Id: If799f3454f628421f03f055eb003442bb90fe656 Reviewed-on: https://go-review.googlesource.com/c/tools/+/558255 Reviewed-by: Robert Findley <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 8174727 commit 35d8b1b

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

Diff for: gopls/internal/golang/hover.go

+11-8
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ func hover(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, pp pro
200200
if err != nil {
201201
return protocol.Range{}, nil, fmt.Errorf("re-parsing declaration of %s: %v", obj.Name(), err)
202202
}
203-
decl, spec, field := findDeclInfo([]*ast.File{declPGF.File}, declPos)
203+
decl, spec, field := findDeclInfo([]*ast.File{declPGF.File}, declPos) // may be nil^3
204204
comment := chooseDocComment(decl, spec, field)
205205
docText := comment.Text()
206206

@@ -219,19 +219,22 @@ func hover(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, pp pro
219219
// objectString is insufficient:
220220
// (1) large structs are formatted poorly, with no newlines
221221
// (2) we lose inline comments
222-
//
223222
// Furthermore, we include a summary of their method set.
224-
//
225-
// TODO(rfindley): this should use FormatVarType to get proper qualification
226-
// of identifiers, and we should revisit the formatting of method set.
227-
//
228-
// TODO(adonovan): this logic belongs in objectString.
229223
_, isTypeName := obj.(*types.TypeName)
230224
_, isTypeParam := obj.Type().(*types.TypeParam)
231225
if isTypeName && !isTypeParam {
232226
spec, ok := spec.(*ast.TypeSpec)
233227
if !ok {
234-
return protocol.Range{}, nil, bug.Errorf("type name %q without type spec", obj.Name())
228+
// We cannot find a TypeSpec for this type or alias declaration
229+
// (that is not a type parameter or a built-in).
230+
// This should be impossible even for ill-formed trees;
231+
// we suspect that AST repair may be creating inconsistent
232+
// positions. Don't report a bug in that case. (#64241)
233+
errorf := fmt.Errorf
234+
if !declPGF.Fixed() {
235+
errorf = bug.Errorf
236+
}
237+
return protocol.Range{}, nil, errorf("type name %q without type spec", obj.Name())
235238
}
236239
spec2 := *spec
237240
// Don't duplicate comments when formatting type specs.

0 commit comments

Comments
 (0)