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/packages: IgnoreFuncBodies implementation and comment mismatch #34806

Closed
zhangyoufu opened this issue Oct 10, 2019 · 2 comments
Closed
Assignees
Labels
FrozenDueToAge 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

@zhangyoufu
Copy link
Contributor

zhangyoufu commented Oct 10, 2019

https://github.com/golang/tools/blob/567d1c21dc5f922038933aa862c0719a9768eb83/go/packages/packages.go#L827-L830

// Type-check bodies of functions only in non-initial packages.
// Example: for import graph A->B->C and initial packages {A,C},
// we can ignore function bodies in B.
IgnoreFuncBodies: ld.Mode&NeedDeps == 0 && !lpkg.initial,

The comment and the given example is self-contradictory. The implementation is actually type-check bodies of functions only in initial packages.


For my use case, I would appreciate if IgnoreFuncBodies could be somehow controlled by packages.Config.

I have a script (invoked by go generate) parsing source code, harvesting struct definition and generating serialization-related code for those structs. Due to lack of serialization-related code, the type-checking for function bodies always fail because those structs does not implement my serialization interface yet.

I don't have any assertions like var _ MySerializationInterface = MyStruct{}. So as long as I can turn IgnoreFuncBodies on, everything works for me.

@gopherbot gopherbot added this to the Unreleased milestone Oct 10, 2019
@gopherbot gopherbot added the Tools This label describes issues relating to any tools in the x/tools repository. label Oct 10, 2019
@andybons andybons added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Oct 10, 2019
@stamblerre stamblerre changed the title x/tools/packages: IgnoreFuncBodies implementation and comment mismatch x/tools/go/packages: IgnoreFuncBodies implementation and comment mismatch Nov 7, 2019
@gopherbot
Copy link

Change https://go.dev/cl/452578 mentions this issue: go/packages: fix doc typo

@adonovan
Copy link
Member

adonovan commented Nov 21, 2022

There isn't currently a way to control this field directly, but there are various ways to achieve what you want. The simplest is to supply a ParseFile function to the Config that invokes the parser as normal, and then destroys the function bodies:

        // Eliminate bodies of top-level functions, methods, inits.
        for _, decl := range file.Decls {
                if fn, ok := decl.(*ast.FuncDecl); ok {
                        fn.Body = nil
                }
        }

Another would be to ignore any type error that is located within a function body. Or to ignore certain kinds of error message.

You may need to take the last approach because there are likely to be declarations outside of function bodies that assign your struct values to interface types (e.g. var v I = newMyStruct()), so destroying function bodies may not be sufficient to suppress all errors.

https://go-review.git.corp.google.com/c/tools/+/452578 fixes the documentation. Thanks for pointing that out.

@adonovan adonovan self-assigned this Nov 21, 2022
@golang golang locked and limited conversation to collaborators Nov 21, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge 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