Skip to content

Commit

Permalink
Stop conflating Hashable and Equatable. Closes #647
Browse files Browse the repository at this point in the history
  • Loading branch information
ileitch committed Oct 31, 2023
1 parent 705d9e0 commit 94265ec
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
8 changes: 4 additions & 4 deletions Sources/PeripheryKit/Indexer/Declaration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ final class Declaration {
var isImplicit: Bool = false
var isObjcAccessible: Bool = false

private let identifier: Int
private let hashValueCache: Int

var ancestralDeclarations: Set<Declaration> {
var maybeParent = parent
Expand Down Expand Up @@ -251,7 +251,7 @@ final class Declaration {
self.kind = kind
self.usrs = usrs
self.location = location
self.identifier = usrs.hashValue
self.hashValueCache = usrs.hashValue
}

func isDeclaredInExtension(kind: Declaration.Kind) -> Bool {
Expand All @@ -262,13 +262,13 @@ final class Declaration {

extension Declaration: Hashable {
func hash(into hasher: inout Hasher) {
hasher.combine(identifier)
hasher.combine(hashValueCache)
}
}

extension Declaration: Equatable {
static func == (lhs: Declaration, rhs: Declaration) -> Bool {
lhs.identifier == rhs.identifier
lhs.usrs == rhs.usrs
}
}

Expand Down
8 changes: 4 additions & 4 deletions Sources/PeripheryKit/Indexer/Reference.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ final class Reference {
let usr: String
var role: Role = .unknown

private let identifier: Int
private let hashValueCache: Int

init(kind: Declaration.Kind, usr: String, location: SourceLocation, isRelated: Bool = false) {
self.kind = kind
self.usr = usr
self.isRelated = isRelated
self.location = location
self.identifier = [usr.hashValue, location.hashValue, isRelated.hashValue].hashValue
self.hashValueCache = [usr.hashValue, location.hashValue, isRelated.hashValue].hashValue
}

var descendentReferences: Set<Reference> {
Expand All @@ -47,13 +47,13 @@ final class Reference {

extension Reference: Hashable {
func hash(into hasher: inout Hasher) {
hasher.combine(identifier)
hasher.combine(hashValueCache)
}
}

extension Reference: Equatable {
static func == (lhs: Reference, rhs: Reference) -> Bool {
lhs.identifier == rhs.identifier
lhs.usr == rhs.usr && lhs.location == rhs.location && lhs.isRelated == rhs.isRelated
}
}

Expand Down
9 changes: 5 additions & 4 deletions Sources/PeripheryKit/Indexer/SourceLocation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ class SourceLocation {
let line: Int
let column: Int

private let identifier: Int
private let hashValueCache: Int

init(file: SourceFile, line: Int, column: Int) {
self.file = file
self.line = line
self.column = column
self.identifier = [file.hashValue, line, column].hashValue
self.hashValueCache = [file.hashValue, line, column].hashValue
}

// MARK: - Private
Expand All @@ -31,13 +31,14 @@ class SourceLocation {

extension SourceLocation: Equatable {
static func == (lhs: SourceLocation, rhs: SourceLocation) -> Bool {
lhs.identifier == rhs.identifier
lhs.file == rhs.file && lhs.line == rhs.line && lhs.column == rhs.column
}
}

extension SourceLocation: Hashable {
func hash(into hasher: inout Hasher) {
hasher.combine(identifier)

hasher.combine(hashValueCache)
}
}

Expand Down

0 comments on commit 94265ec

Please sign in to comment.