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: spurious "declared but not used" errors #50493

Closed
rsc opened this issue Jan 7, 2022 · 4 comments
Closed

cmd/compile: spurious "declared but not used" errors #50493

rsc opened this issue Jan 7, 2022 · 4 comments
Labels
NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@rsc
Copy link
Contributor

rsc commented Jan 7, 2022

Not a release blocker but good to fix if easy.

package p

func f() {
	x := 1
	v := 2
	switch v.(type) {
	case int:
		println(x)
	}
}
% go tool compile /tmp/x.go
/tmp/x.go:4:2: x declared but not used
/tmp/x.go:6:9: v (variable of type int) is not an interface

Looks like because the switch header couldn't type-check, the bodies were not considered. And since the bodies were not considered, it looks like x was never used. The compiler should print the real error on line 6 and not print about line 4.

@rsc rsc added the NeedsFix The path to resolution is known, but the work has not been done. label Jan 7, 2022
@rsc rsc added this to the Go1.18 milestone Jan 7, 2022
@gopherbot
Copy link

gopherbot commented Jan 15, 2022

Change https://golang.org/cl/378774 mentions this issue: cmd/compile: spurious "declared but not used" errors

@gopherbot
Copy link

gopherbot commented Jan 16, 2022

Change https://golang.org/cl/378834 mentions this issue: cmd/compile/internal/types2: avoid declared but not used error if type switch stmt failed

@XiaodongLoong
Copy link
Contributor

XiaodongLoong commented Jan 16, 2022

@rsc
I try to fix this issue, but I found a similar issue(#50639).

@gopherbot
Copy link

gopherbot commented Jan 18, 2022

Change https://golang.org/cl/379117 mentions this issue: cmd/compile, go/types: fix checking of bad type switch

jproberts pushed a commit to jproberts/go that referenced this issue Jun 21, 2022
Consider the following program:

	package p

	func f() {
		x := 1
		v := 2
		switch v.(type) {
		case int:
			println(x)
			println(x / 0)
		case 1:
		}
	}

Before this CL, the compiler prints:

	x.go:4:2: x declared but not used
	x.go:6:9: v (variable of type int) is not an interface

x is in fact used, and other errors in the switch go undiagnosed.

This commit fixes that problem by processing the switch statement
even when the 'not an interface' error is reported.

Now the compiler drops the spurious 'declared but not used'
and adds two previously undiagnosed problems:

	x.go:6:9: v (variable of type int) is not an interface
	x.go:9:15: invalid operation: division by zero
	x.go:10:7: 1 is not a type

go/types was printing roughly the same thing the compiler did before,
and now still prints roughly the same thing the compiler does after.
(The only differences are in the exact reported columns.)

Fixes golang#50493.

Change-Id: I317883f29077b1b4bbd0e8793617fd3bb31aa0f8
Reviewed-on: https://go-review.googlesource.com/c/go/+/379117
Trust: Russ Cox <rsc@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
@rsc rsc removed their assignment Jun 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

3 participants