Skip to content

Commit

Permalink
fix #26038, make isequal consistent with hash for Ptr
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffBezanson committed Apr 19, 2018
1 parent 5179455 commit d089a96
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
7 changes: 5 additions & 2 deletions base/pointer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,12 @@ end

## limited pointer arithmetic & comparison ##

isequal(x::Ptr, y::Ptr) = (x === y)
isless(x::Ptr{T}, y::Ptr{T}) where {T} = x < y

==(x::Ptr, y::Ptr) = UInt(x) == UInt(y)
isless(x::Ptr, y::Ptr) = isless(UInt(x), UInt(y))
-(x::Ptr, y::Ptr) = UInt(x) - UInt(y)
<(x::Ptr, y::Ptr) = UInt(x) < UInt(y)
-(x::Ptr, y::Ptr) = UInt(x) - UInt(y)

+(x::Ptr, y::Integer) = oftype(x, Intrinsics.add_ptr(UInt(x), (y % UInt) % UInt))
-(x::Ptr, y::Integer) = oftype(x, Intrinsics.sub_ptr(UInt(x), (y % UInt) % UInt))
Expand Down
15 changes: 15 additions & 0 deletions test/hashing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,18 @@ let vals_expr = :(Any[Vector, (Array{T,1} where T), 1, 2, Union{Int, String}, Un
@test (a === b) == (objectid(a) == objectid(b))
end
end

# issue #26038
let p1 = Ptr{Int8}(1), p2 = Ptr{Int32}(1), p3 = Ptr{Int8}(2)
@test p1 == p2
@test !isequal(p1, p2)
@test p1 != p3
@test hash(p1) != hash(p2)
@test hash(p1) != hash(p3)
@test hash(p1) == hash(Ptr{Int8}(1))

@test p1 < p3
@test !(p1 < p2)
@test isless(p1, p3)
@test_throws MethodError isless(p1, p2)
end

0 comments on commit d089a96

Please sign in to comment.