Skip to content

Document how to cast / recast / change type of pointer inside Ptr docs #58641

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

freemin7
Copy link
Contributor

@freemin7 freemin7 commented Jun 4, 2025

It took me a while to figure out how to do this and wished it would be documented here or in a similar place.

freemin7 added 3 commits June 4, 2025 23:49
It took me a while to figure out how to do this and wished it would be documented here or in a similar place.
Copy link

github-actions bot commented Jun 4, 2025

Hello! I am a bot.

Thank you for your pull request!

I have assigned @LilithHafner to this pull request.

@LilithHafner can either choose to review this pull request themselves, or they can choose to find someone else to review this pull request.

Note: If you are a Julia committer, please make sure that your organization membership is public.

@freemin7
Copy link
Contributor Author

freemin7 commented Jun 4, 2025

This is a pure change to documentation making it easier for people to mess or observe via Julias memory.
This is only possible through other unsafe methods in the same file. While using reinterpret makes perfect sense
to change the type of pointers i did not consider it but i checked the docs and didn't find it either. Finally i found an issue which had this information and wanted to save others the same hunt.

@LilithHafner LilithHafner added the docs This change adds or pertains to documentation label Jun 4, 2025
@LilithHafner
Copy link
Member

LilithHafner commented Jun 4, 2025

Thanks! Would you please format this as a see also section? You can grep for "see also" in the julia repo to see examples.

It doesn't necessarily have to be a "see also", but this note should come after the rest of the docstring as it is not core functionality of Ptr.

@freemin7
Copy link
Contributor Author

freemin7 commented Jun 5, 2025

I took the syntax from an See also section but decided to add additional information as the documentation of reinterpret does not have a subsection on pointers. It is not necessarily obvious that Ptr is just a type and falls under the generic reinterpret(::Type{Out}, x::In) from reading that doc string to people comming from other languages and it wasn't to me despite me being quite aware of that function. I feel like downgrading this to a "See also" would require adding info the docs of reinterpret to contain the same information (albeit with one more look up).

I also just noticed convert(::Type{Ptr{T}}, p::Ptr) where {T} = bitcast(Ptr{T}, p)::Ptr{T} does the same as reinterpret.

However https://github.com/freemin7/julia/blob/6c425515e68e29944c06852906e2f8754aca2268/base/pointer.jl#L21 is ambigous who deep it refers to.

This turned a bit more complicated then i thought.

I see 3 top level options i have no preference between:

  1. Undo changes to Ptr, add see also to convert and possibly cconvert. Write doc string showing how to use convert with pointers and document the property: convert(In, convert(Out, x)) === x and a warning that no exceptions will be raised by it unlike in the base case of convert and that a pointer of non isbits type is ambigous (for reference see below). Check with someone core Julia developer that convert between pointers is not meant to be deprectated and remove/update the ToDo based on their statement.
  2. Leave as is.
  3. Undo changes to Ptr, add see also to reinterpret. Modify doc string for reinterpret(::Type{Out}, x::In). I am not sure how i would choose to do that. My ideas so far are:
    3.1 Add example on pointers to reinterpret doc string. This would not fit so well with the other examples since it would involve a lot of other functions to explain. A mininal example would be:
unsafe_wrap(Vector{UInt64},reinterpret(Ptr{UInt64},pointer(rand(Float32,6))),3;own=true)

There are smaller minimal examples which involve pointer to integer conversion.

3.2 Add note to reinterpret doc string which explains something special about a pointers
3.3 A warning to reinterpret doc string which explains something special about pointers such as documenting the fact that.

The fundamental issue is that Ptr of non bitstype is ambiguious.
from #30276 (comment)

Any choice {1, 2, 3.1, 3.2, 3.3} is fine by me but also open to hear other ideas.

@LilithHafner
Copy link
Member

  1. is not ideal because conversion is much more widely used than Ptr, so offering this method to people who query ?convert is less precise than offering it to people who query ?Ptr. 3 suffers from a similar issue.

Perhaps a variant of 2 would be good: adding a sentence after the existing Ptr docstring rather than in the middle of it.

This information is based on a conversation in the Julia Slack internals channel.
@freemin7
Copy link
Contributor Author

freemin7 commented Jun 6, 2025

I added a more sections which elaborates on the role the type T plays in pointers. I think this information is best collected in a central place. It also makes the reinterpret sentence stand out less.

Copy link
Member

@LilithHafner LilithHafner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this is much better :)

memory is actually valid, or that it actually represents data of the specified type.
Some operations like [`unsafe_load`](@ref) are only supported when T is an [`isbitstype`](@ref)
while [`unsafe_wrap`](@ref) also supports abstract types.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unsafe_wrap is a good see also, but it is not an alternative to Ptr in many cases so this is not appropriate as a "while" clause.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not meant as an alternative to pointers. unsafe_wrap takes a pointer as an argument and if it is a Ptr{T} where T is an abstract type it will still worked, while unsafe_load will throw an error if given such a pointer.


!!! warn
When T is an [`Union`](@ref) type, the pointer does not contain the necessary information
to reconstruct what elements in the array had which type. Such pointers can't be use to
Copy link
Member

@LilithHafner LilithHafner Jun 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pointers are not just for arrays. For example, you can get a pointer to a field in an mutable struct.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/immutable/mutable I assume

Copy link
Contributor Author

@freemin7 freemin7 Jun 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"what data type it pointed to." would that be better?

!!! warn
When T is an [`Union`](@ref) type, the pointer does not contain the necessary information
to reconstruct what elements in the array had which type. Such pointers can't be use to
retrieve any Julia objects.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's still possible to retrieve an object, you just have to know what type you're looking for.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well then you would manually de-unionize the pointer and it no longer the "same" pointer.

I will think of a different phrasing.

freemin7 and others added 3 commits June 6, 2025 20:31
Co-authored-by: Lilith Orion Hafner <[email protected]>
Co-authored-by: Lilith Orion Hafner <[email protected]>
Co-authored-by: Lilith Orion Hafner <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs This change adds or pertains to documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants