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

conversion-gen: support recursive types #49747

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,12 @@ func (e equalMemoryTypes) Skip(a, b *types.Type) {
}

func (e equalMemoryTypes) Equal(a, b *types.Type) bool {
// alreadyVisitedTypes holds all the types that have already been checked in the structural type recursion.
alreadyVisitedTypes := make(map[*types.Type]bool)
return e.cachingEqual(a, b, alreadyVisitedTypes)
}

func (e equalMemoryTypes) cachingEqual(a, b *types.Type, alreadyVisitedTypes map[*types.Type]bool) bool {
if a == b {
return true
}
Expand All @@ -338,36 +344,42 @@ func (e equalMemoryTypes) Equal(a, b *types.Type) bool {
if equal, ok := e[conversionPair{b, a}]; ok {
return equal
}
result := e.equal(a, b)
result := e.equal(a, b, alreadyVisitedTypes)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering, do we need the parameter inEqual at all? Can't we create the empty map here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't do that because we need the populated map in line 366, 372, 374 and 376 below. If we create an empty map here and those lines are executed, they will see the type as "not visited" - which will defeat our purpose. :)

Also, tried it out. Gives the goroutine stack exceeds 1000000000-byte limit error.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, didn't realize the recursion goes through Equal and not equal.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But, everything here in Equal can be moved to equal and then we can recursively call equal instead of Equal, can't it? Then my suggestion should work.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't make the code easier to read though. Let's leave it as it is.

e[conversionPair{a, b}] = result
e[conversionPair{b, a}] = result
return result
}

func (e equalMemoryTypes) equal(a, b *types.Type) bool {
func (e equalMemoryTypes) equal(a, b *types.Type, alreadyVisitedTypes map[*types.Type]bool) bool {
in, out := unwrapAlias(a), unwrapAlias(b)
switch {
case in == out:
return true
case in.Kind == out.Kind:
// if the type exists already, return early to avoid recursion
if alreadyVisitedTypes[in] {
return true
}
alreadyVisitedTypes[in] = true

switch in.Kind {
case types.Struct:
if len(in.Members) != len(out.Members) {
return false
}
for i, inMember := range in.Members {
outMember := out.Members[i]
if !e.Equal(inMember.Type, outMember.Type) {
if !e.cachingEqual(inMember.Type, outMember.Type, alreadyVisitedTypes) {
return false
}
}
return true
case types.Pointer:
return e.Equal(in.Elem, out.Elem)
return e.cachingEqual(in.Elem, out.Elem, alreadyVisitedTypes)
case types.Map:
return e.Equal(in.Key, out.Key) && e.Equal(in.Elem, out.Elem)
return e.cachingEqual(in.Key, out.Key, alreadyVisitedTypes) && e.cachingEqual(in.Elem, out.Elem, alreadyVisitedTypes)
case types.Slice:
return e.Equal(in.Elem, out.Elem)
return e.cachingEqual(in.Elem, out.Elem, alreadyVisitedTypes)
case types.Interface:
// TODO: determine whether the interfaces are actually equivalent - for now, they must have the
// same type.
Expand Down