@@ -180,10 +180,20 @@ pub struct DynMetadata<Dyn: ?Sized> {
180180 phantom : crate :: marker:: PhantomData < Dyn > ,
181181}
182182
183+ /// Opaque type for accessing vtables.
184+ ///
185+ /// Private implementation detail of `DynMetadata::size_of` etc.
186+ /// Must be zero-sized since there is conceptually not actually any Abstract Machine memory behind this pointer.
187+ /// However, we can require pointer alignment.
188+ #[ repr( C ) ]
189+ #[ cfg( not( bootstrap) ) ]
190+ struct VTable ( [ usize ; 0 ] ) ;
191+
183192/// The common prefix of all vtables. It is followed by function pointers for trait methods.
184193///
185194/// Private implementation detail of `DynMetadata::size_of` etc.
186195#[ repr( C ) ]
196+ #[ cfg( bootstrap) ]
187197struct VTable {
188198 drop_in_place : fn ( * mut ( ) ) ,
189199 size_of : usize ,
@@ -194,13 +204,25 @@ impl<Dyn: ?Sized> DynMetadata<Dyn> {
194204 /// Returns the size of the type associated with this vtable.
195205 #[ inline]
196206 pub fn size_of ( self ) -> usize {
197- self . vtable_ptr . size_of
207+ #[ cfg( bootstrap) ]
208+ return self . vtable_ptr . size_of ;
209+ #[ cfg( not( bootstrap) ) ]
210+ // SAFETY: DynMetadata always contains a valid vtable pointer
211+ return unsafe {
212+ crate :: intrinsics:: vtable_size ( self . vtable_ptr as * const VTable as * const ( ) )
213+ } ;
198214 }
199215
200216 /// Returns the alignment of the type associated with this vtable.
201217 #[ inline]
202218 pub fn align_of ( self ) -> usize {
203- self . vtable_ptr . align_of
219+ #[ cfg( bootstrap) ]
220+ return self . vtable_ptr . align_of ;
221+ #[ cfg( not( bootstrap) ) ]
222+ // SAFETY: DynMetadata always contains a valid vtable pointer
223+ return unsafe {
224+ crate :: intrinsics:: vtable_align ( self . vtable_ptr as * const VTable as * const ( ) )
225+ } ;
204226 }
205227
206228 /// Returns the size and alignment together as a `Layout`
0 commit comments