Skip to content

Commit

Permalink
Add pointer comparison in compareAny
Browse files Browse the repository at this point in the history
When comparing pointers in compareAny, if the pointers are pointing on the
same object we report that they are equal.

Fixes google#80
  • Loading branch information
posener committed Mar 20, 2018
1 parent 0c93777 commit e36f839
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
7 changes: 7 additions & 0 deletions cmp/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,13 @@ func (s *state) compareAny(vx, vy reflect.Value) {
s.report(vx.IsNil() && vy.IsNil(), vx, vy)
return
}

// If pointers are pointing on the same thing, we can report them as equal
if vx.Pointer() == vy.Pointer() {
s.report(true, vx, vy)
return
}

s.curPath.push(&indirect{pathStep{t.Elem()}})
defer s.curPath.pop()
s.compareAny(vx.Elem(), vy.Elem())
Expand Down
4 changes: 4 additions & 0 deletions cmp/compare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,10 @@ root[0]["hr"]:
root[1]["hr"]:
-: int(63)
+: float64(63)`,
}, {
label: label + "/types",
x: reflect.TypeOf(1),
y: reflect.TypeOf(2),
}}
}

Expand Down

0 comments on commit e36f839

Please sign in to comment.