Skip to content

Commit 02fd870

Browse files
committed
Fix infinite loop when diffing non-equatable recursive objects
Fixes #136.
1 parent 5d132f7 commit 02fd870

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

Sources/CustomDump/Internal/Mirror.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ extension Mirror {
2727
}
2828

2929
func isMirrorEqual(_ lhs: Any, _ rhs: Any) -> Bool {
30+
let lhsType = type(of: lhs)
31+
if lhsType is AnyClass, lhsType == type(of: rhs), lhs as AnyObject === rhs as AnyObject {
32+
return true
33+
}
3034
guard let lhs = lhs as? any Equatable else {
3135
let lhsMirror = Mirror(customDumpReflecting: lhs)
3236
let rhsMirror = Mirror(customDumpReflecting: rhs)

Tests/CustomDumpTests/DiffTests.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1290,6 +1290,19 @@ final class DiffTests: XCTestCase {
12901290
"""
12911291
)
12921292
}
1293+
1294+
func testRecursion() {
1295+
class Object {
1296+
var child: Object?
1297+
}
1298+
let object = Object()
1299+
object.child = object
1300+
expectNoDifference(
1301+
diff(object, object),
1302+
"""
1303+
"""
1304+
)
1305+
}
12931306
}
12941307

12951308
private class Shared: _CustomDiffObject, Equatable {

0 commit comments

Comments
 (0)