From 041540841e49bdbe86475109784c970b70d7cdfe Mon Sep 17 00:00:00 2001 From: Oscar Smith Date: Tue, 23 Jul 2024 13:41:47 -0400 Subject: [PATCH 1/6] add `unsafe_convert` for Reshaped, ReinterpretArray --- base/reshapedarray.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/base/reshapedarray.jl b/base/reshapedarray.jl index 4173ef1d3f598..85308ab47f427 100644 --- a/base/reshapedarray.jl +++ b/base/reshapedarray.jl @@ -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}, cconvert(Ptr{T}, a)) # Add a few handy specializations to further speed up views of reshaped ranges const ReshapedUnitRange{T,N,A<:AbstractUnitRange} = ReshapedArray{T,N,A,Tuple{}} From c00c374737ab15e4e45074370810b6c97f08c075 Mon Sep 17 00:00:00 2001 From: Oscar Smith Date: Tue, 23 Jul 2024 13:43:09 -0400 Subject: [PATCH 2/6] unsafe_convert_reinterpret --- base/reinterpretarray.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/base/reinterpretarray.jl b/base/reinterpretarray.jl index d74a043293a3a..0d3b2aad6b7b3 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} = unsafe_convert(Ptr{S}, cconvert(Ptr{T}, a)) @propagate_inbounds function getindex(a::NonReshapedReinterpretArray{T,0,S}) where {T,S} if isprimitivetype(T) && isprimitivetype(S) From c330749db84c7d4124805775e836fde63a0d10ed Mon Sep 17 00:00:00 2001 From: Oscar Smith Date: Tue, 23 Jul 2024 13:47:22 -0400 Subject: [PATCH 3/6] fix --- base/reshapedarray.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/reshapedarray.jl b/base/reshapedarray.jl index 85308ab47f427..019f1d30a25c2 100644 --- a/base/reshapedarray.jl +++ b/base/reshapedarray.jl @@ -324,7 +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}, cconvert(Ptr{T}, 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{}} From 4083f109af48a7fa2be7330780be00a9e7c125fd Mon Sep 17 00:00:00 2001 From: Oscar Smith Date: Tue, 23 Jul 2024 13:50:32 -0400 Subject: [PATCH 4/6] fix --- base/reinterpretarray.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/reinterpretarray.jl b/base/reinterpretarray.jl index 0d3b2aad6b7b3..94c91686a316f 100644 --- a/base/reinterpretarray.jl +++ b/base/reinterpretarray.jl @@ -373,7 +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} = unsafe_convert(Ptr{S}, cconvert(Ptr{T}, a)) +unsafe_convert(::Type{Ptr{T}}, a::ReinterpretArray{T}) = unsafe_convert(Ptr{T}, a.parent) @propagate_inbounds function getindex(a::NonReshapedReinterpretArray{T,0,S}) where {T,S} if isprimitivetype(T) && isprimitivetype(S) From 7b3989828bd840b0ae061862e99888f59ab133a9 Mon Sep 17 00:00:00 2001 From: Oscar Smith Date: Tue, 30 Jul 2024 08:24:24 -0400 Subject: [PATCH 5/6] fix --- base/reinterpretarray.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/reinterpretarray.jl b/base/reinterpretarray.jl index 94c91686a316f..d31f3ebb5dd2d 100644 --- a/base/reinterpretarray.jl +++ b/base/reinterpretarray.jl @@ -373,7 +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}) = unsafe_convert(Ptr{T}, 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) From 0dacc6958e5328b411695e9800a9b786fd7752d0 Mon Sep 17 00:00:00 2001 From: oscarddssmith Date: Wed, 7 Aug 2024 14:34:28 -0400 Subject: [PATCH 6/6] add test --- test/ccall.jl | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test/ccall.jl b/test/ccall.jl index a406af46f0c34..b10504de21abc 100644 --- a/test/ccall.jl +++ b/test/ccall.jl @@ -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)