Skip to content

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

Closed
@griesemer

Description

@griesemer

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.)

Metadata

Metadata

Assignees

Labels

FrozenDueToAgeNeedsDecisionFeedback is required from experts, contributors, and/or the community before a change can be made.

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions