Skip to content

gopls/completion: validate ident source range before use as replacement#637

Open
abhay1999 wants to merge 2 commits intogolang:masterfrom
abhay1999:gopls-completion-invalid-struct-fix
Open

gopls/completion: validate ident source range before use as replacement#637
abhay1999 wants to merge 2 commits intogolang:masterfrom
abhay1999:gopls-completion-invalid-struct-fix

Conversation

@abhay1999
Copy link
Copy Markdown
Contributor

@abhay1999 abhay1999 commented Apr 13, 2026

The Go parser's error recovery can produce ast.Ident nodes whose Name
field spans more source text than the actual identifier token — for
example, by merging adjacent tokens while reconstructing a syntactically
invalid file. When gopls passes such a node to setSurrounding, the
resulting completion replacement range is too wide and silently deletes
valid surrounding code (e.g. adjacent struct fields).

Fix this by adding a source-validation guard in setSurrounding: before
accepting an ident's Pos/End as the replacement range, verify that the
source bytes at ident.Pos() exactly match ident.Name. If they do not,
the node is a product of error recovery and must not be used as a range;
return early so that getSurrounding falls back to a zero-width insertion
at the cursor position instead.

A regression test is added in completion/bad.txt: completing an
identifier inside a struct whose field type is undefined must not delete
the adjacent valid struct fields.

Fixes golang/go#77481

When a func literal is implicitly converted to a named function type
(e.g. fs.WalkDirFunc), hovering over its "func" keyword now shows the
documentation and signature of that named type, with a link to pkg.go.dev.

Previously, hovering over "func" in:

    filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
        ...
    })

showed only the anonymous signature "func(path string, d fs.DirEntry,
err error) error". Now it shows the docs for fs.WalkDirFunc, which is
often the information the user actually wants.

Implementation:
- Detect the case in the hover switch: when cur.Node() is *ast.FuncType
  and the cursor is at the "func" token and the parent is a *ast.FuncLit.
- Use typesutil.TypesFromContext to find the named function type the
  literal must satisfy.
- Load the declaring package and build a hoverResult with the named
  type's documentation, signature, and pkg.go.dev link.
- If no named type is found, fall back to the existing anonymous-type
  hover behavior.

Add a marker test in hover/funclittype.txt covering both cases.

Fixes golang/go#76191
In invalid Go code, the parser's error recovery can produce *ast.Ident
nodes whose Name spans more source text than the actual identifier
token. When setSurrounding uses such a node's Pos/End as the completion
replacement range, the resulting text edit silently deletes valid
surrounding code.

Guard against this by verifying, in setSurrounding, that the source
bytes at ident.Pos() exactly match ident.Name. If they don't, the AST
node is malformed and using it as a replacement range would cause data
loss; return early so that getSurrounding falls back to a zero-width
insertion at the cursor.

Fixes golang/go#77481

Signed-off-by: abhay1999 <abhaychaurasiya19@gmail.com>
@gopherbot
Copy link
Copy Markdown
Contributor

This PR (HEAD: cb9d241) has been imported to Gerrit for code review.

Please visit Gerrit at https://go-review.googlesource.com/c/tools/+/766220.

Important tips:

  • Don't comment on this PR. All discussion takes place in Gerrit.
  • You need a Gmail or other Google account to log in to Gerrit.
  • To change your code in response to feedback:
    • Push a new commit to the branch used by your GitHub PR.
    • A new "patch set" will then appear in Gerrit.
    • Respond to each comment by marking as Done in Gerrit if implemented as suggested. You can alternatively write a reply.
    • Critical: you must click the blue Reply button near the top to publish your Gerrit responses.
    • Multiple commits in the PR will be squashed by GerritBot.
  • The title and description of the GitHub PR are used to construct the final commit message.
    • Edit these as needed via the GitHub web interface (not via Gerrit or git).
    • You should word wrap the PR description at ~76 characters unless you need longer lines (e.g., for tables or URLs).
  • See the Sending a change via GitHub and Reviews sections of the Contribution Guide as well as the FAQ for details.

@gopherbot
Copy link
Copy Markdown
Contributor

Message from Gopher Robot:

Patch Set 1:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/766220.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Copy Markdown
Contributor

Message from Abhay Chaurasiya:

Patch Set 1:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/766220.
After addressing review feedback, remember to publish your drafts!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

x/tools/gopls: accepting autocomplete suggestion within invalid code results in unwanted code deletion

2 participants