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

cmd/compile: infinite recursion in types2 #50782

danscales opened this issue Jan 24, 2022 · 2 comments

cmd/compile: infinite recursion in types2 #50782

danscales opened this issue Jan 24, 2022 · 2 comments
NeedsInvestigation release-blocker


Copy link

@danscales danscales commented Jan 24, 2022

This code below causes an infinite recursion in types2. This may already be fixed by some changes in flight (such as for issue #48962 ), but I wanted to point out this simple example as another test case, if nothing else:

package p

type Numeric interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64

// numericAbs matches numeric types with an Abs method.
type numericAbs[T Numeric] interface {
	~struct{ Value T }
	Abs() T

// AbsDifference computes the absolute value of the difference of
// a and b, where the absolute value is determined by the Abs method.
func absDifference[T numericAbs[T]](a, b T) T {
	d := a.Value - b.Value
	return d.Abs()

This should not be valid code - in absDifference, we are constraining T to be also ~struct { Value T }, and also there is nothing that requires that T satisfies Numeric. But would be good if the type checker doesn't have an infinite recursion.

@griesemer @findleyr

@danscales danscales added this to the Go1.18 milestone Jan 24, 2022
@danscales danscales added the NeedsInvestigation label Jan 24, 2022
Copy link

@griesemer griesemer commented Jan 24, 2022

This doesn't seem related to #48962. Simpler reproducer:

package p

type T[P int] struct{ f P }

func _[P T[P]]() {}

Copy link

@gopherbot gopherbot commented Jan 24, 2022

Change mentions this issue: go/types, types2: pass the seen map through _TypeSet.IsComparable

@findleyr findleyr assigned findleyr and unassigned griesemer Jan 24, 2022
jproberts pushed a commit to jproberts/go that referenced this issue Jun 21, 2022
While checking comparability of type parameters, we recurse through
_TypeSet.IsComparable, but do not pass the cycle-tracking seen map,
resulting in infinite recursion in some cases.

Refactor to pass the seen map through this recursion.

Fixes golang#50782

Change-Id: I2c2bcfed3398c11eb9aa0c871da59e348bfba5f7
Trust: Robert Findley <>
Run-TryBot: Robert Findley <>
Reviewed-by: Robert Griesemer <>
TryBot-Result: Gopher Robot <>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
NeedsInvestigation release-blocker
None yet

No branches or pull requests

4 participants