-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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: error "type instantiation requires go1.18 or later" is unclear when go version comes from go.mod #52880
Comments
cc @ianlancetaylor @griesemer maybe? |
I'm not clear on exactly what you did to see the error, and I'm not clear on exactly what error you saw. If I set the version in go.mod to 1.17 and run
That seems clear enough to me and I don't think there is anything to change. But perhaps you are doing something different and seeing a different message. |
@ianlancetaylor Apologizes for it being unclear! The error I saw was When you set the version in go.mod to 1.17, where you still building with 1.18? I think I can simplify the explanation. And why I was so confused. Lets say you have a file like this, along with a go.mod that is version 1.16 or 1.17 (anything below the build flag you'll note at the top) If you build that file So the confusion comes in when I add generics, and it starts telling me I need to use 1.18 or later. But as just discussed
Therefore I had to be using 1.18. Why would the error tell me to use 1.18 when that is in fact what I was using. Again, the code worked fine without said language feature, it wasn't until I used a language feature after 1.16 did it fail. This is why I thought it was a bug at first (it builds with 1.18 sometimes but not others?). The file builds fine and is clearly being included as it should when I compile with 1.18 and the build flag is being honored (runtime.Version prints 1.18.2). But I use a feature and then it fails telling me to use 1.18.
I thought to myself. Until I hit that link above (this one, the relevant snippet is quoted in my OP) that says language features are gated by the version in the go.mod, not the version of Go I was building with, nor the version of Go in my build flag The fact that isn't mentioned in So to be clear, I don't want readers to think I simply had a module with 1.16 and I was arbitrarily using features introduced after the fact. I knew that wouldn't work. Said features were only in files with a 1.18 build flag, aka "please only include when go1.18 is being used". It was like Go was including my 1.18 only file, then telling me I need to use 1.18. This brings me back to my question (out of curiosity), now that I understand the problem I ran into. Thanks again! |
We don't want to change the compiler behavior based on the build tags of the file being compiled. Also, it doesn't seem likely to be a big use case. If a package is to support Go 1.16, it can't use generics. It seems unlikely that anybody would write the same functionality in two different ways, one with generics and one without. And we don't want to encourage a package providing a different API depending on the compiler version being used. That's not a grea user experience. I see the error in go/types; changing to a go/types bug report to provide a clearer error message for this case. CC @griesemer |
I believe I'm bumping into this restriction also, using the same A Google Cloud Function I'm developing is has For this particular case, the file containing the generics implementation has the build constraint Further errors that may be related to this issue.
|
Change https://go.dev/cl/407896 mentions this issue: |
…d any Use the existing versionErrorf mechanism to report use of undeclared any and comparable. Also, port versionErrorf mechanism to go/types and use it in this case as well. Adjust tests as needed. For #52880. Change-Id: I6a646f16a849692ee0cb57e362d5f3d77e2c25f9 Reviewed-on: https://go-review.googlesource.com/c/go/+/407896 TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Run-TryBot: Robert Griesemer <[email protected]> Reviewed-by: Robert Griesemer <[email protected]>
I think this still gets at the crux of my original issue; that it seems logical (at least to me) that you'd be able to use build flags/constraints to 'gate' language version feature/changes (otherwise the Go version build flags become less useful and its more difficult to write modules that transition to newer versions). Like my original post noted I don't remember this restriction existing before modules, but I could be wrong. It's not like changes on the scale of generics come to Go very often. |
This forces module authors to maintain two major versions of a module, or delaying adoption of generics, even for compatible changes like adding a function, whereas the standard library gets away with doing it in a point release, because it goes bundled with the compiler. |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
1.18.2 is the latest at the time of writing; I have not tried on tip/other branches or versions.
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
Working in a module that specifies 1.16 as minimum, tried adding a file with
//go:build go1.18
For a file that should only be built/included when go1.18 or newer is used.
What did you expect to see?
The build and my editor (vscode + gopls) to work and not fail
What did you see instead?
type instantiation requires go1.18 or later compilerUnsupportedFeature
Context
Well I had an entirely different bug report written until just before I submitted it when I realized what I think is a documentation issue.
The TLDR on the issue was I had a module with
go 1.16
and wanted to add some code/files that used generics guarded by a build flag such as mentioned:From cmd/go
With the above error from gopls/go build when I used generics, but not when I stopped using them (and left the 1.18 restriction) I thought this was a bug until I finally found a line in the module reference
I'm not sure why it isn't allowed if such a file is guarded with build flag/constraints (the module reference doesn't mention those at all). But I'm assuming this is the cause of what I'm seeing. I had looked at the 1.18 release notes, the generics proposal, go/build,
go help build
and various other places and didn't find anything mentioning the disabling of features like this, other than in the module ref above. It was obvious to me code using or expecting to work with 1.16 would not work with generics or any language feature introduced after, but if such code lives in a separate file that is guarded (just like you'd do for anything else say based on OS or ARCH) I didn't understand why that wasn't working.Perhaps more documentation can be added to go/build and cmd friends that mentions this? And maybe a better error if this comes up, something like:
While I'm at it, if the use of a language feature is guarded with flags why not allow its use? If I have a module that says 1.16 is the minimum but code in its own file that won't be included in a 1.16 build why not allow the (rest of the module) code to still build? Perhaps there was a good reason or maybe build flags based on Go version were over looked relative to this module (compiler?) restriction? I don't want to move the entire module to 1.18 for a relatively small bit of optional code, for the time being, so using a build flag I thought was the proper solution.
Thank you!
The text was updated successfully, but these errors were encountered: