LazyStaticType::get_or_init returns an *mut instead of a & ref#994
LazyStaticType::get_or_init returns an *mut instead of a & ref#994kngwyu merged 1 commit intoPyO3:masterfrom
LazyStaticType::get_or_init returns an *mut instead of a & ref#994Conversation
davidhewitt
left a comment
There was a problem hiding this comment.
This seems like a sensible idea to me!
|
I wonder about a possible further refactoring: |
Since |
|
Thanks! |
Yeah, the details of our reference rules are pretty fuzzy, and Stacked Borrows is a promising next step but it is not an official model yet. However, this page is clear at least about some things. It is UB to do the following:
|
Previously, we were returning a
&ffi::PyTypeObject. However, we were often casting this immutable reference to a*mut ffi::PyTypeObjectand then passing that pointer to FFI functions.But we don't know whether the FFI functions mutate the
PyTypeObjectbehind our back or not, and if they do, I believe that this is Undefined Behavior because the pointer comes from a shared reference.Unfortunately, these things are very badly documented: I'm only aware of this because I followed many Unsafe Code Guideline discussions. Best source that I could get is the following comment from @RalfJung, who is very knowledgeable on the subject. The details are probably written in the Stacked Borrows paper, to be fair.
Even if it's not UB right now because of ill-specification or because of the compiler not taking advantage of it, we'd better be safe.