Remove the broken `pointer(x::AbstractArray)` fallback #63436
Ideally this fallback doesn't exist, but that probably is too breaking of a change.
maybe a silly question --- but I mean it genuinely --- if it's this "easy" to fix, why hasn't it been done already? I feel like this pointer fallback bug has been noticed and discussed several times over
Most uses of pointer have been replaced with cconvert and unsafe_convert, but needing to specify the pointer type and use two functions is more cumbersome, so pointer continues to be used in some cases.
Also fixing the underlying issue requires updating packages (see #62895) because really unsafe_convert should only be called on the output of cconvert, as mentioned in #51962
As an example https://github.com/JuliaLinearAlgebra/BlockBandedMatrices.jl is currently broken on 1.14 (and this simple PR doesn't fix that pointer conversion problem)
BlockBandedMatrices.jl in now fixed for 1.14 with https://github.com/JuliaLinearAlgebra/BlockBandedMatrices.jl/pull/251 but this PR would break it again. Due to incorrect use of the pointer function in https://github.com/JuliaLang/LinearAlgebra.jl/blob/15a797512d30db531415e176a387484927f6bcce/src/blas.jl#L784
Before this is merged all use of the pointer function in Base and the standard libraries should be replaced by calls to cconvert and unsafe_convert.
Could the fallback instead be unsafe_convert(Ptr{T}, x), i.e. it would succeed only in cases where cconvert is the identity and unsafe_convert is defined?
We could, but then what is the point of unsafe_convert? Won't it just be pointer under a different name?
The main difference is that with pointer the array chooses the pointer type, whereas with unsafe_convert the caller chooses the pointer type. The big question here is should pointer on an array wrapper call pointer of the parent, or is pointer never supposed to be overloaded, so unsafe_convert ends up being called on the parent.
The advantage of an unsafe_convert fallback is that a typical type needs to implement fewer methods, and the change is more likely to be backwards compatible (since types didn't need to implement both pointer and unsafe_convert in the past).
(A type that implements a nontrivial cconvert will still need a separate pointer method, though.)
Do we still need the cconvert methods too?
Again, it seems like we can get rid of the cconvert method?
It seems like a lot of types are defining cconvert where they were supposed to define unsafe_convert, since in my understanding the latter is what is supposed to be used for pointer conversions: https://github.com/JuliaLang/julia/blob/b3032e9d11177204bea506abd3bf69e921c4851c/base/essentials.jl#L741
Some array types only define cconvert and not unsafe_convert. Removing the cconvert method overloads from, for example, PermutedDimsArray would cause issues if the parent is a cconvert only array.
In general only cconvert needs to be defined since it can return something that already has a unsafe_convert method.
I thought that the unsafe_convert + cconvert design is that the "unsafe" part — converting a rooted object to a raw pointer, was supposed to happen in unsafe_convert.
What is unsafe about cconvert(::Type{Ptr{T}}, s::CodeUnits{T}) where {T} = cconvert(Ptr{T}, s.s) ? And this may be required because s.s may have a cconvert method but not a unsafe_convert method. For example https://github.com/JuliaStrings/StaticStrings.jl/blob/3be63a7d1432680ec71b1e7f8404c842b15580c3/src/convert.jl#L72-L74
If this is true for CodeUnits, then the same logic applies to all of the array wrapping types here.
nhz2
The generic
pointer(x::AbstractArray)returnedunsafe_convert(Ptr{T}, cconvert(Ptr{T}, x))since #51764. The object returned bycconvertis only guaranteed to be kept alive while it is rooted, as it is during accall, but callers ofpointeronly preservexitself. Whencconvertallocates a new object, the returned pointer can refer to memory that has already been freed.Remove the broken
pointer(x::AbstractArray)fallback, and give the array wrappers that previously relied oncconvertto reach their parent (PermutedDimsArray,CodeUnits,SharedArray, stridedSubArray,ReshapedArrayandReinterpretArray)unsafe_convertmethods that forward tounsafe_convertof the parent, so nested wrappers keep working.Fixes #51962, #59685, and #60408
CC: @jakobnissen @chriselrod @eschnett
Assisted-by: Claude Code (Opus 5.5)