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

cmd/compile: consistently report "declared but not used" errors - needs release notes #49214

Closed
griesemer opened this issue Oct 28, 2021 · 4 comments
Milestone

Comments

@griesemer
Copy link
Contributor

griesemer commented Oct 28, 2021

Problem

Per the spec:

A compiler may make it illegal to declare a variable inside a function body if the variable is never used.

a compiler may report errors for unused variables. The pre-1.18 compiler fails to do so if a variable is never used but set inside a function literal. Example:

package main
func main() {
        p := true
        func() {
                p = true
        }()
}

There is no use of p in this program but until now the compiler didn't report an error. See also issues #8560 and #46004.

The new 1.18 compiler front-end uses a go/types-based type checker (types2) which correctly reports an error in cases like these; so does go/types, and so does go/vet.

Proposal

The compiler should consistently report such errors from now on. This does not affect existing code that passes go/vet. There is a chance that code that used to compile (such as the example above) won't compile anymore; it is also likely that such code was not correct in the first place.

For existing code that won't compile anymore there are several work-arounds:

  1. Fix the code if it was in fact incorrect (this may be as simple as removing the unused variable, or using the correct variable).
  2. If the code was correct, address the error by introducing a use if the variable is still desired (which is as simple as _ = x for an unused variable x).
  3. Compile using the 1.17 compiler by setting the -G=0 compiler flag. This doesn't require code changes.

Implementation

If the proposal is accepted, nothing needs to be done; this is already the new behavior of the compiler.
If the proposal is not accepted, the compiler needs to be adjusted such that any access of a variable in a function literal is considered a "use".

@rsc
Copy link
Contributor

rsc commented Nov 3, 2021

This is spec-compatible, and it is also compatible with gccgo, as well as go vet.
(Neither gccgo nor go vet would have succeeded on code that is newly rejected here.)

Since there is no spec change, we don't really need a proposal here,
but we should be ready to implement the old behavior
if there are significant problems during the beta/rc period.

@rsc rsc added NeedsFix The path to resolution is known, but the work has not been done. and removed Proposal labels Nov 3, 2021
@rsc rsc changed the title proposal: cmd/compile: consistently report "declared but not used" errors cmd/compile: consistently report "declared but not used" errors Nov 3, 2021
@rsc
Copy link
Contributor

rsc commented Nov 3, 2021

Leaving open to add text to release notes.

@rsc rsc removed this from Incoming in Proposals (old) Nov 3, 2021
@griesemer griesemer added Documentation and removed NeedsFix The path to resolution is known, but the work has not been done. release-blocker labels Nov 3, 2021
@griesemer griesemer changed the title cmd/compile: consistently report "declared but not used" errors cmd/compile: consistently report "declared but not used" errors - needs release notes Nov 3, 2021
@griesemer griesemer self-assigned this Nov 3, 2021
@gopherbot
Copy link

Change https://golang.org/cl/366274 mentions this issue: doc: document compiler change for "declared but not used" errors

@chuangyeshuo

This comment was marked as outdated.

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

No branches or pull requests

4 participants