Skip to content

Commit 0640701

Browse files
adonovangopherbot
authored andcommitted
gopls/internal: don't bug.Report when given an invalid position
It's possible that the client sent us a bad protocol.Range: that shouldn't trigger a bug report. (It's also possible that AST fixing is perturbing positions so as to cause a valid range not to match a parameter, but we can't easily distinguish that case.) Fixes golang/go#64544 Change-Id: I28f29716e8c98cf57d44cddfad5b46b1311808bb Reviewed-on: https://go-review.googlesource.com/c/tools/+/548738 Auto-Submit: Alan Donovan <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent 90c60a2 commit 0640701

File tree

6 files changed

+24
-22
lines changed

6 files changed

+24
-22
lines changed

gopls/internal/lsp/source/change_quote.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ import (
2727
func ConvertStringLiteral(pgf *ParsedGoFile, fh file.Handle, rng protocol.Range) (protocol.CodeAction, bool) {
2828
startPos, endPos, err := pgf.RangePos(rng)
2929
if err != nil {
30-
bug.Reportf("(file=%v).RangePos(%v) failed: %v", pgf.URI, rng, err)
31-
return protocol.CodeAction{}, false
30+
return protocol.CodeAction{}, false // e.g. invalid range
3231
}
3332
path, _ := astutil.PathEnclosingInterval(pgf.File, startPos, endPos)
3433
lit, ok := path[0].(*ast.BasicLit)

gopls/internal/lsp/source/change_signature.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ func RemoveUnusedParameter(ctx context.Context, fh file.Handle, rng protocol.Ran
5555
return nil, fmt.Errorf("can't change signatures for packages with parse or type errors: (e.g. %s)", sample)
5656
}
5757

58-
info := FindParam(pgf, rng)
59-
if info.Decl == nil {
60-
return nil, fmt.Errorf("failed to find declaration")
58+
info, err := FindParam(pgf, rng)
59+
if err != nil {
60+
return nil, err // e.g. invalid range
6161
}
6262
if info.Decl.Recv != nil {
6363
return nil, fmt.Errorf("can't change signature of methods (yet)")
@@ -242,20 +242,18 @@ func rewriteSignature(fset *token.FileSet, declIdx int, src0 []byte, newDecl *as
242242

243243
// ParamInfo records information about a param identified by a position.
244244
type ParamInfo struct {
245-
Decl *ast.FuncDecl // enclosing func decl, or nil
245+
Decl *ast.FuncDecl // enclosing func decl (non-nil)
246246
FieldIndex int // index of Field in Decl.Type.Params, or -1
247-
Field *ast.Field // enclosing field of Decl, or nil
247+
Field *ast.Field // enclosing field of Decl, or nil if range not among parameters
248248
NameIndex int // index of Name in Field.Names, or nil
249249
Name *ast.Ident // indicated name (either enclosing, or Field.Names[0] if len(Field.Names) == 1)
250250
}
251251

252252
// FindParam finds the parameter information spanned by the given range.
253-
func FindParam(pgf *ParsedGoFile, rng protocol.Range) ParamInfo {
254-
info := ParamInfo{FieldIndex: -1, NameIndex: -1}
253+
func FindParam(pgf *ParsedGoFile, rng protocol.Range) (*ParamInfo, error) {
255254
start, end, err := pgf.RangePos(rng)
256255
if err != nil {
257-
bug.Reportf("(file=%v).RangePos(%v) failed: %v", pgf.URI, rng, err)
258-
return info
256+
return nil, err
259257
}
260258

261259
path, _ := astutil.PathEnclosingInterval(pgf.File, start, end)
@@ -278,9 +276,13 @@ func FindParam(pgf *ParsedGoFile, rng protocol.Range) ParamInfo {
278276
}
279277
// Check the conditions described in the docstring.
280278
if decl == nil {
281-
return info
279+
return nil, fmt.Errorf("range is not within a function declaration")
280+
}
281+
info := &ParamInfo{
282+
FieldIndex: -1,
283+
NameIndex: -1,
284+
Decl: decl,
282285
}
283-
info.Decl = decl
284286
for fi, f := range decl.Type.Params.List {
285287
if f == field {
286288
info.FieldIndex = fi
@@ -299,7 +301,7 @@ func FindParam(pgf *ParsedGoFile, rng protocol.Range) ParamInfo {
299301
break
300302
}
301303
}
302-
return info
304+
return info, nil
303305
}
304306

305307
// signatureRewrite defines a rewritten function signature.

gopls/internal/lsp/source/fix.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ func addEmbedImport(ctx context.Context, snapshot *cache.Snapshot, fh file.Handl
165165
for _, e := range protoEdits {
166166
start, end, err := pgf.RangePos(e.Range)
167167
if err != nil {
168-
return nil, fmt.Errorf("map range: %w", err)
168+
return nil, err // e.g. invalid range
169169
}
170170
edits = append(edits, analysis.TextEdit{
171171
Pos: start,

gopls/internal/lsp/source/inline_all.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ func inlineAllCalls(ctx context.Context, logf func(string, ...any), snapshot *ca
113113
}
114114
start, end, err := pgf.RangePos(ref.Range)
115115
if err != nil {
116-
return nil, bug.Errorf("RangePos(ref): %v", err)
116+
return nil, err // e.g. invalid range
117117
}
118118

119119
// Look for the surrounding call expression.

gopls/internal/server/code_action.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -543,15 +543,16 @@ func refactorRewrite(snapshot *cache.Snapshot, pkg *cache.Package, pgf *source.P
543543
// - [start, end) is contained within an unused field or parameter name
544544
// - ... of a non-method function declaration.
545545
func canRemoveParameter(pkg *cache.Package, pgf *source.ParsedGoFile, rng protocol.Range) bool {
546-
info := source.FindParam(pgf, rng)
547-
if info.Decl == nil || info.Field == nil {
548-
return false
546+
info, err := source.FindParam(pgf, rng)
547+
if err != nil {
548+
return false // e.g. invalid range
549+
}
550+
if info.Field == nil {
551+
return false // range does not span a parameter
549552
}
550-
551553
if info.Decl.Body == nil {
552554
return false // external function
553555
}
554-
555556
if len(info.Field.Names) == 0 {
556557
return true // no names => field is unused
557558
}

gopls/internal/server/semantic.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func (s *server) semanticTokens(ctx context.Context, td protocol.TextDocumentIde
9797
var err error
9898
start, end, err = pgf.RangePos(*rng)
9999
if err != nil {
100-
return nil, fmt.Errorf("range span (%w) error for %s", err, pgf.File.Name)
100+
return nil, err // e.g. invalid range
101101
}
102102
} else {
103103
tok := pgf.Tok

0 commit comments

Comments
 (0)