Skip to content

Base.unsafe_convert(Ptr, ::Vector} started throwing on master #51996

@Drvi

Description

@Drvi

Hey folks,

This used to work prior to 1.11:

julia> Base.unsafe_convert(Ptr{Float64}, Int[0]) 
Ptr{Float64} @0x00000002992d5d00

julia> Base.unsafe_convert(Ptr{Int}, Int[0])
Ptr{Int64} @0x00000002998d5b40

But now it throws:

julia> Base.unsafe_convert(Ptr{Float64}, Int[0])
ERROR: conversion to pointer not defined for Vector{Int64}

julia> Base.unsafe_convert(Ptr{Int}, Int[0])
ERROR: conversion to pointer not defined for Vector{Int64}

To give more context, we do something like:

struct W{Int}
    x::Int
end

SRC_T = Tuple{String,W{Int}}
DST_T = Tuple{String,Int}

x = SRC_T[("a", W(1)), ("b", W(2))]
ptr = Base.unsafe_convert(Ptr{DST_T}, x)
Base.unsafe_wrap(Vector{DST_T}, ptr, length(x))

Which would on 1.9 / 1.10 yield:

julia> ptr = Base.unsafe_convert(Ptr{DST_T}, x)
Ptr{Tuple{String, Int64}} @0x0000000160d55060

julia> Base.unsafe_wrap(Vector{DST_T}, ptr, length(x))
2-element Vector{Tuple{String, Int64}}:
 ("a", 1)
 ("b", 2)

But throws in the unsafe_convert step on master.

Is this expected? This broke our master-tracking CI.

Would a reasonable fix for this be rewriting theunsafe_convert step to

ptr = Base.unsafe_convert(Ptr{DST_T}, pointer(x))

(adding the pointer(...) call)?

Is this fundamentally impossible to make safe and there is a better idiom for this kind of unsafe conversion?

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    arrays[a, r, r, a, y, s]

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions