Skip to content

go/types, types2: improved constant handling guarded by go1.21 but other inference improvements are not #60566

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

Closed
griesemer opened this issue Jun 1, 2023 · 4 comments
Assignees
Labels
FrozenDueToAge NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.
Milestone

Comments

@griesemer
Copy link
Contributor

griesemer commented Jun 1, 2023

The Go 1.21 compiler implements significantly more powerful and precise type inference than Go 1.20. One of the improvements made is for untyped constants (#58671). That change was implemented based on the language version: -lang must be >= go1.21.

func f[P any](...P) {}

func main() {
	f(1, 2.0)
}

produces an error (default type float64 of 2.0 does not match inferred type int for P) with -lang=go1.20 but runs fine with -lang >= go1.21.

However, there are plenty of other inference improvements that that were made for 1.21 which are accessible even if -lang < go1.21. For instance:

package main

func f[T any](interface{ m(T) }) {}

type S struct{}

func (S) m(int) {}

func main() {
	f(S{})
}

In this case the change and error is not fundamentally different from above: in Go 1.20 we couldn't infer a type argument and now we can. In both these cases one can provide explicit type arguments and make the code compile. Also, all inference improvements are fully backwards-compatible.

I propose that we remove the -lang check for the more lenient constant handling for consistency with the other inference improvements. (Making all improvements -lang dependent is not practical.)

@griesemer griesemer added the NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. label Jun 1, 2023
@griesemer griesemer added this to the Go1.21 milestone Jun 1, 2023
@griesemer griesemer self-assigned this Jun 1, 2023
@griesemer
Copy link
Contributor Author

cc: @findleyr @ianlancetaylor for feedback

@griesemer griesemer changed the title go/types, types2: improved constant handling guarded by go1.21 but other inference imrovements are not go/types, types2: improved constant handling guarded by go1.21 but other inference improvements are not Jun 1, 2023
@gopherbot
Copy link
Contributor

Change https://go.dev/cl/499997 mentions this issue: go/types, types2: remove version check for more lenient constant handling in inference

@ianlancetaylor
Copy link
Contributor

I am OK with this.

@findleyr
Copy link
Member

findleyr commented Jun 2, 2023

I am fine with this too. If it proves to be a problem, we can always put back the -lang check: making -lang more compatible with the released Go version should always be OK.

@golang golang locked and limited conversation to collaborators Jun 1, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.
Projects
None yet
Development

No branches or pull requests

4 participants