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

Closed
danscales opened this issue Jan 24, 2022 · 2 comments
Closed

cmd/compile: infinite recursion in types2 #50782

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

Comments

@danscales
Copy link
Contributor

@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 https://go-review.googlesource.com/c/go/+/379414/ 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
@griesemer
Copy link
Contributor

@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]]() {}

@gopherbot
Copy link

@gopherbot gopherbot commented Jan 24, 2022

Change https://golang.org/cl/380504 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
Reviewed-on: https://go-review.googlesource.com/c/go/+/380504
Trust: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation release-blocker
Projects
None yet
Development

No branches or pull requests

4 participants