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/stdversion: language vs toolchain version #68246

Open
dominikh opened this issue Jun 28, 2024 · 3 comments
Open

x/tools/go/analysis/passes/stdversion: language vs toolchain version #68246

dominikh opened this issue Jun 28, 2024 · 3 comments
Labels
Analysis Issues related to static analysis (vet, x/tools/go/analysis) NeedsFix The path to resolution is known, but the work has not been done. Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@dominikh
Copy link
Member

In this example

-- go.mod --
module example.com

go 1.23
-- foo.go --
//go:build go1.21

package pkg

import "reflect"

func main() {
	_ = reflect.TypeFor[int]
}

stdversion reports ./foo.go:8:14: reflect.TypeFor requires go1.22 or later (file is go1.21)

I believe that this might be overly strict. Even though the //go:build directive downgrades the language version available in the file, it doesn't change the fact that this file will only be built by a toolchain that is at least verison 1.23, because the module as a whole will only be built by a toolchain that satisfies the go dependency in go.mod.

I thus believe that the standard library available to a file with a build tag is max(file's version, module's version), and not min.

/cc @adonovan

@gopherbot gopherbot added the Tools This label describes issues relating to any tools in the x/tools repository. label Jun 28, 2024
@gopherbot gopherbot added this to the Unreleased milestone Jun 28, 2024
@thanm thanm added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jul 1, 2024
@dominikh
Copy link
Member Author

@adonovan ping, thoughts?

@adonovan
Copy link
Member

adonovan commented Jan 24, 2025

Hi @dominikh, sorry for the delay. After a long discussion during which both @findleyr and I reversed ourselves, we agree that this is a bug. (Versioning is complex... @timothy-king wrote a talk laying out many aspects of it and how they interact, and I have long been meaning to distill these rules into a handy--or perhaps billboard-sized--flowchart for quick reference.)

The intent of the go1.21 build directive within a go1.23 module is to say: "the language features in this file should be downgraded to the semantics as they were in the go1.21 spec", but of course it will still be compiled and linked against the standard library of go1.23 or later. So this should disable go1.22 for-loop semantics (which could in theory be problematic for existing code), but should not prevent the file from accessing symbols of the go1.23+ library such as reflect.TypeFor.

In principle it may be impossible to use certain features of the go1.Y std library from a file tagged go1.X, where X < Y; for example, calling a generic library function from a source file that is not allowed to use generics. (So don't do that.)

I thus believe that the standard library available to a file with a build tag is max(file's version, module's version), and not min.

Agreed.

@rsc: Have we misremembered something from the long discussion we had when developing stdversion?

@adonovan adonovan added NeedsFix The path to resolution is known, but the work has not been done. Analysis Issues related to static analysis (vet, x/tools/go/analysis) and removed NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels Jan 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Analysis Issues related to static analysis (vet, x/tools/go/analysis) NeedsFix The path to resolution is known, but the work has not been done. Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

5 participants