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

The documentation "Equal is called even if x or y is nil" seems incorrect. #363

Open
your-diary opened this issue Sep 3, 2024 · 0 comments

Comments

@your-diary
Copy link

The documentation of Equal() (introduced in #63) seems incorrect (emphasis mine):

If the values have an Equal method of the form "(T) Equal(T) bool" or "(T) Equal(I) bool" where T is assignable to I, then use the result of x.Equal(y) even if x or y is nil. Otherwise, no such method exists and evaluation proceeds to the next rule.

As far as I tested, x.Equal(y) is NOT called if x == nil or/and y == nil.

playground

package main

import (
	"fmt"

	"github.com/google/go-cmp/cmp"
)

type S struct {
	V *T
}

type T struct {
}

func (t1 T) Equal(t2 T) bool {
	fmt.Println("hello")
	return true
}

func main() {
	{
		s1 := S{
			V: &T{},
		}
		s2 := S{
			V: &T{},
		}
		fmt.Println(cmp.Equal(s1, s2)) //=> hello, hello, true (as expected)
	}

	{
		s1 := S{
			V: nil,
		}
		s2 := S{
			V: &T{},
		}
		// s1.V.Equal(*s2.V) //panics
		fmt.Println(cmp.Equal(s1, s2)) //=> false (doesn't panic)
	}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant