@@ -1668,13 +1668,8 @@ impl PyObjectRef {
16681668 /// another downcast can be attempted without unnecessary cloning.
16691669 #[ inline]
16701670 pub fn downcast_exact < T : PyPayload > ( self , vm : & VirtualMachine ) -> Result < PyRefExact < T > , Self > {
1671- if self . class ( ) . is ( T :: class ( & vm. ctx ) ) {
1672- // TODO: is this always true?
1673- assert ! (
1674- self . downcastable:: <T >( ) ,
1675- "obj.__class__ is T::class() but payload is not T"
1676- ) ;
1677- // SAFETY: just asserted that downcastable::<T>()
1671+ if self . class ( ) . is ( T :: class ( & vm. ctx ) ) && self . downcastable :: < T > ( ) {
1672+ // SAFETY: the class and allocation both match T.
16781673 Ok ( unsafe { PyRefExact :: new_unchecked ( PyRef :: from_obj_unchecked ( self ) ) } )
16791674 } else {
16801675 Err ( self )
@@ -1779,7 +1774,13 @@ impl PyObject {
17791774 #[ deprecated( note = "use downcastable instead" ) ]
17801775 #[ inline( always) ]
17811776 pub fn payload_is < T : PyPayload > ( & self ) -> bool {
1782- self . 0 . vtable . typeid == T :: PAYLOAD_TYPE_ID
1777+ self . downcastable :: < T > ( )
1778+ }
1779+
1780+ #[ doc( hidden) ]
1781+ #[ must_use]
1782+ pub fn supports_native_layout ( & self , layout : core:: any:: TypeId ) -> bool {
1783+ ( self . 0 . vtable . supports_native_layout ) ( layout)
17831784 }
17841785
17851786 /// Force to return payload as T.
@@ -1936,7 +1937,8 @@ impl PyObject {
19361937 pub fn downcast_ref_if_exact < T : PyPayload > ( & self , vm : & VirtualMachine ) -> Option < & Py < T > > {
19371938 self . class ( )
19381939 . is ( T :: class ( & vm. ctx ) )
1939- . then ( || unsafe { self . downcast_unchecked_ref :: < T > ( ) } )
1940+ . then ( || self . downcast_ref :: < T > ( ) )
1941+ . flatten ( )
19401942 }
19411943
19421944 /// # Safety
@@ -2875,32 +2877,27 @@ where
28752877 #[ inline]
28762878 #[ must_use]
28772879 pub fn into_base ( self ) -> PyRef < T :: Base > {
2878- let obj : PyObjectRef = self . into ( ) ;
2879- match obj . downcast ( ) {
2880- Ok ( base_ref ) => base_ref ,
2881- Err ( _ ) => unsafe { core:: hint :: unreachable_unchecked ( ) } ,
2882- }
2880+ // PySubclass establishes the physical prefix; Python may have changed
2881+ // the object's class or MRO since this typed reference was constructed.
2882+ let ptr = self . ptr . cast ( ) ;
2883+ core:: mem :: forget ( self ) ;
2884+ PyRef { ptr }
28832885 }
28842886 #[ inline]
28852887 #[ must_use]
28862888 pub fn upcast < U : PyPayload + StaticType > ( self ) -> PyRef < U >
28872889 where
28882890 T : StaticType ,
28892891 {
2890- debug_assert ! ( T :: static_type( ) . is_subtype( U :: static_type( ) ) ) ;
28912892 let obj: PyObjectRef = self . into ( ) ;
2892- match obj. downcast :: < U > ( ) {
2893- Ok ( upcast_ref) => upcast_ref,
2894- Err ( _) => unsafe { core:: hint:: unreachable_unchecked ( ) } ,
2895- }
2893+ obj. downcast :: < U > ( ) . expect ( "invalid native upcast" )
28962894 }
28972895}
28982896
28992897impl < T : crate :: class:: PySubclass > Py < T > {
29002898 /// Converts `&Py<T>` to `&Py<T::Base>`.
29012899 #[ inline]
29022900 pub fn to_base ( & self ) -> & Py < T :: Base > {
2903- debug_assert ! ( self . as_object( ) . downcast_ref:: <T :: Base >( ) . is_some( ) ) ;
29042901 // SAFETY: T is #[repr(transparent)] over T::Base,
29052902 // so Py<T> and Py<T::Base> have the same layout.
29062903 unsafe { & * ( self as * const Self as * const Py < T :: Base > ) }
@@ -2912,9 +2909,9 @@ impl<T: crate::class::PySubclass> Py<T> {
29122909 where
29132910 T : StaticType ,
29142911 {
2915- debug_assert ! ( T :: static_type ( ) . is_subtype ( U :: static_type ( ) ) ) ;
2916- // SAFETY: T is a subtype of U, so Py<T> can be viewed as Py<U>.
2917- unsafe { & * ( self as * const Self as * const Py < U > ) }
2912+ self . as_object ( )
2913+ . downcast_ref :: < U > ( )
2914+ . expect ( "invalid native upcast" )
29182915 }
29192916}
29202917
@@ -3248,6 +3245,104 @@ pub(crate) fn init_type_hierarchy() -> BootstrapTypeHierarchy {
32483245mod tests {
32493246 use super :: * ;
32503247
3248+ #[ test]
3249+ fn native_downcasts_validate_the_allocation ( ) {
3250+ use crate :: {
3251+ builtins:: builtin_func:: { PyNativeFunction , PyNativeMethod } ,
3252+ class:: PyClassImpl ,
3253+ function:: { ItemDoc , PyMethodDef , PyMethodFlags } ,
3254+ } ;
3255+
3256+ #[ pyclass( module = false , name = "AllocationBase" ) ]
3257+ #[ derive( Debug , PyPayload ) ]
3258+ struct AllocationBase {
3259+ value : usize ,
3260+ }
3261+
3262+ #[ pyclass( flags( BASETYPE ) ) ]
3263+ impl AllocationBase { }
3264+
3265+ #[ pyclass( module = false , name = "AllocationDerived" , base = AllocationBase ) ]
3266+ #[ derive( Debug ) ]
3267+ struct AllocationDerived {
3268+ base : AllocationBase ,
3269+ value : usize ,
3270+ }
3271+
3272+ #[ pyclass]
3273+ impl AllocationDerived { }
3274+
3275+ crate :: Interpreter :: without_stdlib ( Default :: default ( ) ) . enter ( |vm| {
3276+ let _ = AllocationBase :: make_static_type ( ) ;
3277+ let derived = AllocationDerived :: make_static_type ( ) ;
3278+ // A class reference does not enlarge the allocated Rust payload.
3279+ let base = PyRef :: new_ref ( AllocationBase { value : 5 } , derived. clone ( ) , None ) ;
3280+ assert ! ( !base. as_object( ) . downcastable:: <AllocationDerived >( ) ) ;
3281+ assert ! (
3282+ base. as_object( )
3283+ . downcast_ref_if_exact:: <AllocationDerived >( vm)
3284+ . is_none( )
3285+ ) ;
3286+ assert ! (
3287+ base. as_object( )
3288+ . to_owned( )
3289+ . downcast_exact:: <AllocationDerived >( vm)
3290+ . is_err( )
3291+ ) ;
3292+ let actual = PyRef :: new_ref (
3293+ AllocationDerived {
3294+ base : AllocationBase { value : 5 } ,
3295+ value : 17 ,
3296+ } ,
3297+ derived,
3298+ None ,
3299+ ) ;
3300+ assert_eq ! (
3301+ actual
3302+ . as_object( )
3303+ . downcast_ref:: <AllocationDerived >( )
3304+ . unwrap( )
3305+ . value,
3306+ 17
3307+ ) ;
3308+ assert_eq ! (
3309+ actual
3310+ . as_object( )
3311+ . downcast_ref:: <AllocationBase >( )
3312+ . unwrap( )
3313+ . value,
3314+ 5
3315+ ) ;
3316+
3317+ static METHOD : PyMethodDef = PyMethodDef :: new_const (
3318+ "identity" ,
3319+ |value : PyObjectRef | value,
3320+ PyMethodFlags :: METHOD ,
3321+ ItemDoc :: NONE ,
3322+ ) ;
3323+ let function = METHOD . build_function ( & vm. ctx ) ;
3324+ // CFunction and CMethod share a Python class, but not a Rust layout.
3325+ assert ! ( !function. as_object( ) . downcastable:: <PyNativeMethod >( ) ) ;
3326+ assert ! (
3327+ function
3328+ . as_object( )
3329+ . downcast_ref_if_exact:: <PyNativeMethod >( vm)
3330+ . is_none( )
3331+ ) ;
3332+ assert ! (
3333+ function
3334+ . as_object( )
3335+ . to_owned( )
3336+ . downcast_exact:: <PyNativeMethod >( vm)
3337+ . is_err( )
3338+ ) ;
3339+ let method =
3340+ METHOD . build_bound_method ( & vm. ctx , vm. ctx . none ( ) , vm. ctx . types . object_type ) ;
3341+ assert ! ( method. as_object( ) . downcastable:: <PyNativeMethod >( ) ) ;
3342+ assert ! ( method. as_object( ) . downcastable:: <PyNativeFunction >( ) ) ;
3343+ } ) ;
3344+ }
3345+
32513346 #[ test]
32523347 fn native_type_basicsize_includes_payload_padding ( ) {
32533348 use crate :: class:: PyClassDef ;
0 commit comments