Skip to content

Commit

Permalink
re-add unsafe_convert for Reinterpret and Reshaped array (#55226)
Browse files Browse the repository at this point in the history
Fxes #54725
  • Loading branch information
oscardssmith authored Aug 8, 2024
1 parent e439836 commit f0a2a7a
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 1 deletion.
1 change: 1 addition & 0 deletions base/reinterpretarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ has_offset_axes(a::ReinterpretArray) = has_offset_axes(a.parent)

elsize(::Type{<:ReinterpretArray{T}}) where {T} = sizeof(T)
cconvert(::Type{Ptr{T}}, a::ReinterpretArray{T,N,S} where N) where {T,S} = cconvert(Ptr{S}, a.parent)
unsafe_convert(::Type{Ptr{T}}, a::ReinterpretArray{T,N,S} where N) where {T,S} = Ptr{T}(unsafe_convert(Ptr{S},a.parent))

@propagate_inbounds function getindex(a::NonReshapedReinterpretArray{T,0,S}) where {T,S}
if isprimitivetype(T) && isprimitivetype(S)
Expand Down
1 change: 1 addition & 0 deletions base/reshapedarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ setindex!(A::ReshapedRange, val, index::ReshapedIndex) = _rs_setindex!_err()
@noinline _rs_setindex!_err() = error("indexed assignment fails for a reshaped range; consider calling collect")

cconvert(::Type{Ptr{T}}, a::ReshapedArray{T}) where {T} = cconvert(Ptr{T}, parent(a))
unsafe_convert(::Type{Ptr{T}}, a::ReshapedArray{T}) where {T} = unsafe_convert(Ptr{T}, a.parent)

# Add a few handy specializations to further speed up views of reshaped ranges
const ReshapedUnitRange{T,N,A<:AbstractUnitRange} = ReshapedArray{T,N,A,Tuple{}}
Expand Down
5 changes: 4 additions & 1 deletion test/ccall.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1937,7 +1937,10 @@ end

# issue #52025
@test Base.unsafe_convert(Ptr{Ptr{Cchar}}, Base.cconvert(Ptr{Ptr{Cchar}}, map(pointer, ["ab"]))) isa Ptr{Ptr{Cchar}}

#issue #54725
for A in (reinterpret(UInt, [0]), reshape([0, 0], 1, 2))
@test pointer(A) == Base.unsafe_convert(Ptr{Cvoid}, A) == Base.unsafe_convert(Ptr{Int}, A)
end
# Cglobal with non-static symbols doesn't error
function cglobal_non_static1()
sym = (:global_var, libccalltest)
Expand Down

0 comments on commit f0a2a7a

Please sign in to comment.