diff --git a/Cargo.toml b/Cargo.toml index 1ef31f825f7..f99a58500ea 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -131,7 +131,7 @@ members = [ version = "0.4.0" authors = ["RustPython Team"] edition = "2024" -rust-version = "1.89.0" +rust-version = "1.91.0" repository = "https://github.com/RustPython/RustPython" license = "MIT" diff --git a/crates/derive-impl/src/pyclass.rs b/crates/derive-impl/src/pyclass.rs index 028d3d7c292..a81a7bacbad 100644 --- a/crates/derive-impl/src/pyclass.rs +++ b/crates/derive-impl/src/pyclass.rs @@ -635,10 +635,7 @@ pub(crate) fn impl_pyclass(attr: PunctuatedNestedMeta, item: Item) -> Result() <= std::mem::size_of::<#ident>()); impl ::rustpython_vm::PyPayload for #ident { - #[inline] - fn payload_type_id() -> ::std::any::TypeId { - <#base_type as ::rustpython_vm::PyPayload>::payload_type_id() - } + 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 { diff --git a/crates/derive-impl/src/pystructseq.rs b/crates/derive-impl/src/pystructseq.rs index 32b603fe478..0ee0c2c2e3c 100644 --- a/crates/derive-impl/src/pystructseq.rs +++ b/crates/derive-impl/src/pystructseq.rs @@ -592,10 +592,7 @@ pub(crate) fn impl_pystruct_sequence( // Subtype uses base type's payload_type_id impl ::rustpython_vm::PyPayload for #pytype_ident { - #[inline] - fn payload_type_id() -> ::std::any::TypeId { - <::rustpython_vm::builtins::PyTuple as ::rustpython_vm::PyPayload>::payload_type_id() - } + 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 { diff --git a/crates/vm/src/builtins/str.rs b/crates/vm/src/builtins/str.rs index 4931a748198..640778c8cb9 100644 --- a/crates/vm/src/builtins/str.rs +++ b/crates/vm/src/builtins/str.rs @@ -1935,9 +1935,7 @@ impl PyPayload for PyUtf8Str { ctx.types.str_type } - fn payload_type_id() -> core::any::TypeId { - core::any::TypeId::of::() - } + const PAYLOAD_TYPE_ID: core::any::TypeId = core::any::TypeId::of::(); fn validate_downcastable_from(obj: &PyObject) -> bool { // SAFETY: we know the object is a PyStr in this context diff --git a/crates/vm/src/object/core.rs b/crates/vm/src/object/core.rs index e904117b06a..271a9cc34d2 100644 --- a/crates/vm/src/object/core.rs +++ b/crates/vm/src/object/core.rs @@ -105,8 +105,6 @@ pub(super) unsafe fn try_trace_obj(x: &PyObject, tracer_fn: &mut T #[repr(C)] pub(super) struct PyInner { pub(super) ref_count: RefCount, - // TODO: move typeid into vtable once TypeId::of is const - pub(super) typeid: TypeId, pub(super) vtable: &'static PyObjVTable, pub(super) typ: PyAtomicRef, // __class__ member @@ -449,7 +447,6 @@ impl PyInner { let member_count = typ.slots.member_count; Box::new(Self { ref_count: RefCount::new(), - typeid: T::payload_type_id(), vtable: PyObjVTable::of::(), typ: PyAtomicRef::from(typ), dict: dict.map(InstanceDict::new), @@ -639,7 +636,7 @@ impl PyObject { #[deprecated(note = "use downcastable instead")] #[inline(always)] pub fn payload_is(&self) -> bool { - self.0.typeid == T::payload_type_id() + self.0.vtable.typeid == T::PAYLOAD_TYPE_ID } /// Force to return payload as T. @@ -722,7 +719,7 @@ impl PyObject { #[inline] pub(crate) fn typeid(&self) -> TypeId { - self.0.typeid + self.0.vtable.typeid } /// Check if this object can be downcast to T. @@ -1276,7 +1273,6 @@ pub(crate) fn init_type_hierarchy() -> (PyTypeRef, PyTypeRef, PyTypeRef) { let type_type_ptr = Box::into_raw(Box::new(partially_init!( PyInner:: { ref_count: RefCount::new(), - typeid: TypeId::of::(), vtable: PyObjVTable::of::(), dict: None, weak_list: WeakRefList::new(), @@ -1288,7 +1284,6 @@ pub(crate) fn init_type_hierarchy() -> (PyTypeRef, PyTypeRef, PyTypeRef) { let object_type_ptr = Box::into_raw(Box::new(partially_init!( PyInner:: { ref_count: RefCount::new(), - typeid: TypeId::of::(), vtable: PyObjVTable::of::(), dict: None, weak_list: WeakRefList::new(), diff --git a/crates/vm/src/object/payload.rs b/crates/vm/src/object/payload.rs index 3a2f42675f7..143033ee642 100644 --- a/crates/vm/src/object/payload.rs +++ b/crates/vm/src/object/payload.rs @@ -26,15 +26,12 @@ pub(crate) fn cold_downcast_type_error( } pub trait PyPayload: MaybeTraverse + PyThreadingConstraint + Sized + 'static { - #[inline] - fn payload_type_id() -> core::any::TypeId { - core::any::TypeId::of::() - } + const PAYLOAD_TYPE_ID: core::any::TypeId = core::any::TypeId::of::(); /// # 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) + obj.typeid() == Self::PAYLOAD_TYPE_ID && Self::validate_downcastable_from(obj) } #[inline] diff --git a/crates/vm/src/object/traverse_object.rs b/crates/vm/src/object/traverse_object.rs index 075ce5b9513..7a66f0b35f0 100644 --- a/crates/vm/src/object/traverse_object.rs +++ b/crates/vm/src/object/traverse_object.rs @@ -1,4 +1,5 @@ use alloc::fmt; +use core::any::TypeId; use crate::{ PyObject, @@ -11,6 +12,7 @@ use crate::{ use super::{Traverse, TraverseFn}; pub(in crate::object) struct PyObjVTable { + pub(in crate::object) typeid: TypeId, pub(in crate::object) drop_dealloc: unsafe fn(*mut PyObject), pub(in crate::object) debug: unsafe fn(&PyObject, &mut fmt::Formatter<'_>) -> fmt::Result, pub(in crate::object) trace: Option)>, @@ -19,6 +21,7 @@ pub(in crate::object) struct PyObjVTable { impl PyObjVTable { pub const fn of() -> &'static Self { &Self { + typeid: T::PAYLOAD_TYPE_ID, drop_dealloc: drop_dealloc_obj::, debug: debug_obj::, trace: const {