Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/derive-impl/src/pyclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ pub(crate) fn impl_pyclass(attr: PunctuatedNestedMeta, item: Item) -> Result<Tok
const PAYLOAD_TYPE_ID: ::core::any::TypeId = <#base_type as ::rustpython_vm::PyPayload>::PAYLOAD_TYPE_ID;

#[inline]
fn validate_downcastable_from(obj: &::rustpython_vm::PyObject) -> bool {
unsafe fn validate_downcastable_from(obj: &::rustpython_vm::PyObject) -> bool {
<Self as ::rustpython_vm::class::PyClassDef>::BASICSIZE <= obj.class().slots.basicsize && obj.class().fast_issubclass(<Self as ::rustpython_vm::class::StaticType>::static_type())
}

Expand Down
2 changes: 1 addition & 1 deletion crates/derive-impl/src/pystructseq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ pub(crate) fn impl_pystruct_sequence(
const PAYLOAD_TYPE_ID: ::core::any::TypeId = <::rustpython_vm::builtins::PyTuple as ::rustpython_vm::PyPayload>::PAYLOAD_TYPE_ID;

#[inline]
fn validate_downcastable_from(obj: &::rustpython_vm::PyObject) -> bool {
unsafe fn validate_downcastable_from(obj: &::rustpython_vm::PyObject) -> bool {
obj.class().fast_issubclass(<Self as ::rustpython_vm::class::StaticType>::static_type())
}

Expand Down
2 changes: 1 addition & 1 deletion crates/vm/src/builtins/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1944,7 +1944,7 @@ impl PyPayload for PyUtf8Str {

const PAYLOAD_TYPE_ID: core::any::TypeId = core::any::TypeId::of::<PyStr>();

fn validate_downcastable_from(obj: &PyObject) -> bool {
unsafe fn validate_downcastable_from(obj: &PyObject) -> bool {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

huh, every other parts are safe but this one was unsafe

// SAFETY: we know the object is a PyStr in this context
let wtf8 = unsafe { obj.downcast_unchecked_ref::<PyStr>() };
wtf8.is_utf8()
Expand Down
2 changes: 1 addition & 1 deletion crates/vm/src/object/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,7 @@ impl PyObject {
/// Check if this object can be downcast to T.
#[inline(always)]
pub fn downcastable<T: PyPayload>(&self) -> bool {
T::downcastable_from(self)
self.typeid() == T::PAYLOAD_TYPE_ID && unsafe { T::validate_downcastable_from(self) }
}

/// Attempt to downcast this reference to a subclass.
Expand Down
12 changes: 4 additions & 8 deletions crates/vm/src/object/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,15 @@ pub(crate) fn cold_downcast_type_error(
pub trait PyPayload: MaybeTraverse + PyThreadingConstraint + Sized + 'static {
const PAYLOAD_TYPE_ID: core::any::TypeId = core::any::TypeId::of::<Self>();

/// # Safety: this function should only be called if `payload_type_id` matches the type of `obj`.
/// # Safety
/// This function should only be called if `payload_type_id` matches the type of `obj`.
#[inline]
fn downcastable_from(obj: &PyObject) -> bool {
obj.typeid() == Self::PAYLOAD_TYPE_ID && Self::validate_downcastable_from(obj)
}

#[inline]
fn validate_downcastable_from(_obj: &PyObject) -> bool {
unsafe fn validate_downcastable_from(_obj: &PyObject) -> bool {
true
}

fn try_downcast_from(obj: &PyObject, vm: &VirtualMachine) -> PyResult<()> {
if Self::downcastable_from(obj) {
if obj.downcastable::<Self>() {
return Ok(());
}

Expand Down
Loading