cmd/go: per-file Go versions via //go:build do not play well with goexperiment #66399
Labels
NeedsInvestigation
Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Go version
go version devel go1.23-8496060870 2024-03-19 07:29:47 +0000 linux/amd64
Output of
go env
in your module/workspace:What did you do?
Testscript below, run via https://pkg.go.dev/github.com/rogpeppe/go-internal@v1.12.0/cmd/testscript:
What did you see happen?
What did you expect to see?
Both should work the same.
Note that this is technically working as expected given #59033. https://pkg.go.dev/go/build/constraint#GoVersion on
go1.23
givesgo1.23
, but forgo1.23 || goexperiment.rangefunc
gives no result, so cmd/go falls back togo 1.22
from go.mod.I reckon this is a sharp edge that we should polish; cmd/go should be able to figure out that, since either build tag must be true with
go1.23 || goexperiment.rangefunc
, the range-over-func language feature should be enabled. I think the simplest way to solve this would be to have thego1.23
build tag imply thegoexperiment.rangefunc
build tag, alongside a default ofGOEXPERIMENT=rangefunc
being set. In the same setup, using//go:build goexperiment.rangefunc
withGOEXPERIMENT=rangefunc go run
works, after all.The reason I ran into this is because we have a library that was written in a way to be compatible with iterator functions, i.e. Go funcs and methods return yield funcs today, even though we only support Go 1.21 and 1.22, where range-over-func isn't available yet. But we want to double check that range-over-func will work with those APIs once it's live, so we use a build-tagged file with
go1.23 || goexperiment.rangefunc
to briefly test that the range-over-func would compile and run as expected. Having the experiment build tag there is useful so it can be tested on Go 1.22 by enabling the experiment, and not just by installing Go 1.23 from master.I reckon this is fairly niche and perhaps low priority, but given that there likely will be multiple more language changes implemented in a similar way with GOEXPERIMENT (#57001), I think polishing this sharp edge would make it a lot easier to trial the new features before they are stabilized.
The text was updated successfully, but these errors were encountered: