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

go/types: spurious imported and not used #67962

Closed
rsc opened this issue Jun 13, 2024 · 3 comments
Closed

go/types: spurious imported and not used #67962

rsc opened this issue Jun 13, 2024 · 3 comments
Assignees
Labels
NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@rsc
Copy link
Contributor

rsc commented Jun 13, 2024

This bug is shared between go/types and types2 and therefore between the compiler and gopls.

% cat x.go
package p

import "fmt"

func f() {
	x := 1
	for e := range x.Method() {
		switch m := e.(type) {
		case int:
			fmt.Printf("%d\n", m)
		}
	}
}
% go build x.go
# command-line-arguments
./x.go:3:8: "fmt" imported and not used   <<< WRONG
./x.go:7:19: x.Method undefined (type int has no field or method Method)
% gopls check x.go
Log: Loading packages...
Info: Finished loading packages.
Log: Running...
Info: completed
/tmp/x.go: found packages p (x.go) and main (y.go) in /tmp
/tmp/x.go:3:8-13: "fmt" imported and not used   <<< WRONG
/tmp/x.go:7:19-25: x.Method undefined (type int has no field or method Method)
%

The problem (I assume) is that the switch is operating on a value e of unknown type, so the cases are not type-checked at all. The use of fmt is not seen, leading to the incorrect imported and not used error.

I feel like we've had other bugs related to not checking inside switch cases in this situation before.

/cc @griesemer @rfindley

@rsc rsc added the NeedsFix The path to resolution is known, but the work has not been done. label Jun 13, 2024
@rsc rsc added this to the Go1.24 milestone Jun 13, 2024
@gabyhelp
Copy link

Similar Issues

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

@rfindley
Copy link

@findleyr

@griesemer griesemer self-assigned this Jun 13, 2024
@griesemer griesemer modified the milestones: Go1.24, Go1.23 Jun 13, 2024
@gopherbot
Copy link
Contributor

Change https://go.dev/cl/592555 mentions this issue: go/types, types2: typecheck cases even if switch expression is invalid

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

5 participants