gopls/completion: validate ident source range before use as replacement#637
Open
abhay1999 wants to merge 2 commits intogolang:masterfrom
Open
gopls/completion: validate ident source range before use as replacement#637abhay1999 wants to merge 2 commits intogolang:masterfrom
abhay1999 wants to merge 2 commits intogolang:masterfrom
Conversation
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>
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:
|
Contributor
|
Message from Gopher Robot: Patch Set 1: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/766220. |
Contributor
|
Message from Abhay Chaurasiya: Patch Set 1: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/766220. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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