From 894454c294120c4bfc3b228e38dae8c9da85199d Mon Sep 17 00:00:00 2001 From: Oscar Smith Date: Thu, 8 Aug 2024 10:51:48 -0400 Subject: [PATCH] re-add `unsafe_convert` for Reinterpret and Reshaped array (#55226) Fxes https://github.com/JuliaLang/julia/issues/54725 (cherry picked from commit f0a2a7a0a9438433eb1ac824bd09627ab8fd0586) --- base/reinterpretarray.jl | 1 + base/reshapedarray.jl | 1 + test/ccall.jl | 5 +++++ 3 files changed, 7 insertions(+) diff --git a/base/reinterpretarray.jl b/base/reinterpretarray.jl index d74a043293a3a..d31f3ebb5dd2d 100644 --- a/base/reinterpretarray.jl +++ b/base/reinterpretarray.jl @@ -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) diff --git a/base/reshapedarray.jl b/base/reshapedarray.jl index c7ae1e3a25cd1..fb5a930bd1c32 100644 --- a/base/reshapedarray.jl +++ b/base/reshapedarray.jl @@ -307,6 +307,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{}} diff --git a/test/ccall.jl b/test/ccall.jl index 8b1fefdfc66e4..8fe3c8cbefe03 100644 --- a/test/ccall.jl +++ b/test/ccall.jl @@ -1937,3 +1937,8 @@ 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