| 1 | // Copyright 2009 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE-go file. |
| 4 | |
| 5 | // Deep equality test via reflection |
| 6 | |
| 7 | package deepequal |
| 8 | |
| 9 | import ( |
| 10 | "bytes" |
| 11 | "reflect" |
| 12 | "unsafe" |
| 13 | ) |
| 14 | |
| 15 | // During deepValueEqual, must keep track of checks that are |
| 16 | // in progress. The comparison algorithm assumes that all |
| 17 | // checks in progress are true when it reencounters them. |
| 18 | // Visited comparisons are stored in a map indexed by visit. |
| 19 | type visit struct { |
| 20 | a1 unsafe.Pointer |
| 21 | a2 unsafe.Pointer |
| 22 | typ reflect.Type |
| 23 | } |
| 24 | |
| 25 | // Tests for deep equality using reflected types. The map argument tracks |
| 26 | // comparisons that have already been seen, which allows short circuiting on |
| 27 | // recursive types. |
| 28 | func deepValueEqual(v1, v2 reflect.Value, visited map[visit]bool) bool { |
| 29 | if !v1.IsValid() || !v2.IsValid() { |
| 30 | return v1.IsValid() == v2.IsValid() |
| 31 | } |
| 32 | if v1.Type() != v2.Type() { |
| 33 | return false |
| 34 | } |
| 35 | |
| 36 | // We want to avoid putting more in the visited map than we need to. |
| 37 | // For any possible reference cycle that might be encountered, |
| 38 | // hard(v1, v2) needs to return true for at least one of the types in the cycle, |
| 39 | // and it's safe and valid to get Value's internal pointer. |
| 40 | hard := func(v1, v2 reflect.Value) bool { |
| 41 | switch v1.Kind() { |
| 42 | case reflect.Pointer: |
| 43 | if !hasPointers(v1) { |
| 44 | // not-in-heap pointers can't be cyclic. |
| 45 | // At least, all of our current uses of internal/runtime/sys.NotInHeap |
| 46 | // have that property. The runtime ones aren't cyclic (and we don't use |
| 47 | // DeepEqual on them anyway), and the cgo-generated ones are |
| 48 | // all empty structs. |
| 49 | return false |
| 50 | } |
| 51 | fallthrough |
| 52 | case reflect.Map, reflect.Slice, reflect.Interface: |
| 53 | // Nil pointers cannot be cyclic. Avoid putting them in the visited map. |
| 54 | return !v1.IsNil() && !v2.IsNil() |
| 55 | } |
| 56 | return false |
| 57 | } |
| 58 | |
| 59 | if hard(v1, v2) { |
| 60 | // For a Pointer or Map value, we need to check flagIndir, |
| 61 | // which we do by calling the pointer method. |
| 62 | // For Slice or Interface, flagIndir is always set, |
| 63 | // and using v.ptr suffices. |
| 64 | ptrval := func(v reflect.Value) unsafe.Pointer { |
| 65 | switch v.Kind() { |
| 66 | case reflect.Pointer, reflect.Map: |
| 67 | return unsafe.Pointer(v.Pointer()) //nolint:gosec // Audit. |
| 68 | default: |
| 69 | vRef := (*value)(unsafe.Pointer(&v)) //nolint:gosec // Audit. |
| 70 | return vRef.ptr |
| 71 | } |
| 72 | } |
| 73 | addr1 := ptrval(v1) |
| 74 | addr2 := ptrval(v2) |
| 75 | if uintptr(addr1) > uintptr(addr2) { |
| 76 | // Canonicalize order to reduce number of entries in visited. |
| 77 | // Assumes non-moving garbage collector. |
| 78 | addr1, addr2 = addr2, addr1 |
| 79 | } |
| 80 | |
| 81 | // Short circuit if references are already seen. |
| 82 | typ := v1.Type() |
| 83 | v := visit{addr1, addr2, typ} |
| 84 | if visited[v] { |
| 85 | return true |
| 86 | } |
| 87 | |
| 88 | // Remember for later. |
| 89 | visited[v] = true |
| 90 | } |
| 91 | |
| 92 | if equal, ok := equalFunc(v1); ok { |
| 93 | return call(equal, []reflect.Value{v2})[0].Bool() |
| 94 | } |
| 95 | |
| 96 | switch v1.Kind() { |
| 97 | case reflect.Array: |
| 98 | for i := 0; i < v1.Len(); i++ { |
| 99 | if !deepValueEqual(v1.Index(i), v2.Index(i), visited) { |
| 100 | return false |
| 101 | } |
| 102 | } |
| 103 | return true |
| 104 | case reflect.Slice: |
| 105 | if v1.IsNil() != v2.IsNil() { |
| 106 | return false |
| 107 | } |
| 108 | if v1.Len() != v2.Len() { |
| 109 | return false |
| 110 | } |
| 111 | if v1.UnsafePointer() == v2.UnsafePointer() { |
| 112 | return true |
| 113 | } |
| 114 | // Special case for []byte, which is common. |
| 115 | if v1.Type().Elem().Kind() == reflect.Uint8 { |
| 116 | return bytes.Equal(v1.Bytes(), v2.Bytes()) |
| 117 | } |
| 118 | for i := 0; i < v1.Len(); i++ { |
| 119 | if !deepValueEqual(v1.Index(i), v2.Index(i), visited) { |
| 120 | return false |
| 121 | } |
| 122 | } |
| 123 | return true |
| 124 | case reflect.Interface: |
| 125 | if v1.IsNil() || v2.IsNil() { |
| 126 | return v1.IsNil() == v2.IsNil() |
| 127 | } |
| 128 | return deepValueEqual(v1.Elem(), v2.Elem(), visited) |
| 129 | case reflect.Pointer: |
| 130 | if v1.UnsafePointer() == v2.UnsafePointer() { |
| 131 | return true |
| 132 | } |
| 133 | return deepValueEqual(v1.Elem(), v2.Elem(), visited) |
| 134 | case reflect.Struct: |
| 135 | for i, n := 0, v1.NumField(); i < n; i++ { |
| 136 | if !deepValueEqual(v1.Field(i), v2.Field(i), visited) { |
| 137 | return false |
| 138 | } |
| 139 | } |
| 140 | return true |
| 141 | case reflect.Map: |
| 142 | if v1.IsNil() != v2.IsNil() { |
| 143 | return false |
| 144 | } |
| 145 | if v1.Len() != v2.Len() { |
| 146 | return false |
| 147 | } |
| 148 | if v1.UnsafePointer() == v2.UnsafePointer() { |
| 149 | return true |
| 150 | } |
| 151 | iter := v1.MapRange() |
| 152 | for iter.Next() { |
| 153 | val1 := iter.Value() |
| 154 | val2 := v2.MapIndex(iter.Key()) |
| 155 | if !val1.IsValid() || !val2.IsValid() || !deepValueEqual(val1, val2, visited) { |
| 156 | return false |
| 157 | } |
| 158 | } |
| 159 | return true |
| 160 | case reflect.Func: |
| 161 | if v1.IsNil() && v2.IsNil() { |
| 162 | return true |
| 163 | } |
| 164 | // Can't do better than this: |
| 165 | return false |
| 166 | case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: |
| 167 | return v1.Int() == v2.Int() |
| 168 | case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: |
| 169 | return v1.Uint() == v2.Uint() |
| 170 | case reflect.String: |
| 171 | return v1.String() == v2.String() |
| 172 | case reflect.Bool: |
| 173 | return v1.Bool() == v2.Bool() |
| 174 | case reflect.Float32, reflect.Float64: |
| 175 | return v1.Float() == v2.Float() |
| 176 | case reflect.Complex64, reflect.Complex128: |
| 177 | return v1.Complex() == v2.Complex() |
| 178 | default: |
| 179 | // Normal equality suffices |
| 180 | return valueInterface(v1) == valueInterface(v2) |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | // DeepEqual reports whether x and y are “deeply equal,” defined as follows. |
| 185 | // Two values of identical type are deeply equal if one of the following cases applies. |
| 186 | // Values of distinct types are never deeply equal. |
| 187 | // |
| 188 | // If x's type implements Equal method such as x.Equal(y) returns true then |
| 189 | // values are deeply equal. |
| 190 | // |
| 191 | // Array values are deeply equal when their corresponding elements are deeply equal. |
| 192 | // |
| 193 | // Struct values are deeply equal if their corresponding fields, |
| 194 | // both exported and unexported, are deeply equal. |
| 195 | // |
| 196 | // Func values are deeply equal if both are nil; otherwise they are not deeply equal. |
| 197 | // |
| 198 | // Interface values are deeply equal if they hold deeply equal concrete values. |
| 199 | // |
| 200 | // Map values are deeply equal when all of the following are true: |
| 201 | // they are both nil or both non-nil, they have the same length, |
| 202 | // and either they are the same map object or their corresponding keys |
| 203 | // (matched using Go equality) map to deeply equal values. |
| 204 | // |
| 205 | // Pointer values are deeply equal if they are equal using Go's == operator |
| 206 | // or if they point to deeply equal values. |
| 207 | // |
| 208 | // Slice values are deeply equal when all of the following are true: |
| 209 | // they are both nil or both non-nil, they have the same length, |
| 210 | // and either they point to the same initial entry of the same underlying array |
| 211 | // (that is, &x[0] == &y[0]) or their corresponding elements (up to length) are deeply equal. |
| 212 | // Note that a non-nil empty slice and a nil slice (for example, []byte{} and []byte(nil)) |
| 213 | // are not deeply equal. |
| 214 | // |
| 215 | // Other values - numbers, bools, strings, and channels - are deeply equal |
| 216 | // if they are equal using Go's == operator. |
| 217 | // |
| 218 | // In general DeepEqual is a recursive relaxation of Go's == operator. |
| 219 | // However, this idea is impossible to implement without some inconsistency. |
| 220 | // Specifically, it is possible for a value to be unequal to itself, |
| 221 | // either because it is of func type (uncomparable in general) |
| 222 | // or because it is a floating-point NaN value (not equal to itself in floating-point comparison), |
| 223 | // or because it is an array, struct, or interface containing |
| 224 | // such a value. |
| 225 | // On the other hand, pointer values are always equal to themselves, |
| 226 | // even if they point at or contain such problematic values, |
| 227 | // because they compare equal using Go's == operator, and that |
| 228 | // is a sufficient condition to be deeply equal, regardless of content. |
| 229 | // DeepEqual has been defined so that the same short-cut applies |
| 230 | // to slices and maps: if x and y are the same slice or the same map, |
| 231 | // they are deeply equal regardless of content. |
| 232 | // |
| 233 | // As DeepEqual traverses the data values it may find a cycle. The |
| 234 | // second and subsequent times that DeepEqual compares two pointer |
| 235 | // values that have been compared before, it treats the values as |
| 236 | // equal rather than examining the values to which they point. |
| 237 | // This ensures that DeepEqual terminates. |
| 238 | func DeepEqual(x, y any) bool { |
| 239 | if x == nil || y == nil { |
| 240 | return x == y |
| 241 | } |
| 242 | v1 := reflect.ValueOf(x) |
| 243 | v2 := reflect.ValueOf(y) |
| 244 | if v1.Type() != v2.Type() { |
| 245 | return false |
| 246 | } |
| 247 | return deepValueEqual(v1, v2, make(map[visit]bool)) |
| 248 | } |