Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x/tools/go/analysis/passes/fieldalignment: mutates AST #68735

Open
adonovan opened this issue Aug 5, 2024 · 1 comment
Open

x/tools/go/analysis/passes/fieldalignment: mutates AST #68735

adonovan opened this issue Aug 5, 2024 · 1 comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@adonovan
Copy link
Member

adonovan commented Aug 5, 2024

Noticed in passing: the fieldalignment analyzer (which as of gopls/v0.17.0 is now used in x/tools only by its tests) potentially mutates the AST it is passed by the analysis framework:

x/tools/go/analysis/passes/fieldalignment/fieldalignment.go:

func fieldalignment(pass *analysis.Pass, node *ast.StructType, typ *types.Struct) {
	...
	for _, f := range node.Fields.List {
		// TODO: Preserve comment, for now get rid of them.
		//       See https://github.com/golang/go/issues/20744
		f.Comment = nil
		f.Doc = nil

No analyzer should do that. Similar mutations in other analyzers could be a cause of a variety of bugs, inconsistencies, and crashes in tools such as gopls.

We should consider developing the deepHash approach used in x/tools/internal/refactor/inline/inline_test.go so that we can dynamically check for unwanted AST mutations around certain operations such as Analyzer.Run. (Of course, it is not cheap, and is only as good as the test coverage.)

@gopherbot gopherbot added the Tools This label describes issues relating to any tools in the x/tools repository. label Aug 5, 2024
@gopherbot gopherbot added this to the Unreleased milestone Aug 5, 2024
@gabyhelp
Copy link

gabyhelp commented Aug 5, 2024

Related Issues and Documentation

(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)

@dr2chase dr2chase added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Aug 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

4 participants