Skip to content

Commit 3b01da3

Browse files
committed
Revert "Validate the special layout restriction on DynMetadata"
This reverts commit d83f3ca.
1 parent 21e6de7 commit 3b01da3

File tree

2 files changed

+8
-28
lines changed

2 files changed

+8
-28
lines changed

compiler/rustc_mir_transform/src/validate.rs

-9
Original file line numberDiff line numberDiff line change
@@ -685,15 +685,6 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
685685
check_equal(self, location, *f_ty);
686686
}
687687
ty::Adt(adt_def, args) => {
688-
// see <https://github.com/rust-lang/rust/blob/7601adcc764d42c9f2984082b49948af652df986/compiler/rustc_middle/src/ty/layout.rs#L861-L864>
689-
if Some(adt_def.did()) == self.tcx.lang_items().dyn_metadata() {
690-
self.fail(
691-
location,
692-
format!("You can't project to field {f:?} of `DynMetadata` because \
693-
layout is weird and thinks it doesn't have fields."),
694-
);
695-
}
696-
697688
let var = parent_ty.variant_index.unwrap_or(FIRST_VARIANT);
698689
let Some(field) = adt_def.variant(var).fields.get(f) else {
699690
fail_out_of_bounds(self, location);

library/core/src/ptr/metadata.rs

+8-19
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,8 @@ impl<T: ?Sized> Clone for PtrComponents<T> {
178178
/// compare equal (since identical vtables can be deduplicated within a codegen unit).
179179
#[lang = "dyn_metadata"]
180180
pub struct DynMetadata<Dyn: ?Sized> {
181-
_vtable_ptr: &'static VTable,
182-
_phantom: crate::marker::PhantomData<Dyn>,
181+
vtable_ptr: &'static VTable,
182+
phantom: crate::marker::PhantomData<Dyn>,
183183
}
184184

185185
extern "C" {
@@ -191,17 +191,6 @@ extern "C" {
191191
}
192192

193193
impl<Dyn: ?Sized> DynMetadata<Dyn> {
194-
/// One of the things that rustc_middle does with this being a lang item is
195-
/// give it `FieldsShape::Primitive`, which means that as far as codegen can
196-
/// tell, it *is* a reference, and thus doesn't have any fields.
197-
/// That means we can't use field access, and have to transmute it instead.
198-
#[inline]
199-
fn vtable_ptr(self) -> *const VTable {
200-
// SAFETY: this layout assumption is hard-coded into the compiler.
201-
// If it's somehow not a size match, the transmute will error.
202-
unsafe { crate::mem::transmute::<Self, &'static VTable>(self) }
203-
}
204-
205194
/// Returns the size of the type associated with this vtable.
206195
#[inline]
207196
pub fn size_of(self) -> usize {
@@ -210,7 +199,7 @@ impl<Dyn: ?Sized> DynMetadata<Dyn> {
210199
// `Send` part!
211200
// SAFETY: DynMetadata always contains a valid vtable pointer
212201
return unsafe {
213-
crate::intrinsics::vtable_size(self.vtable_ptr() as *const ())
202+
crate::intrinsics::vtable_size(self.vtable_ptr as *const VTable as *const ())
214203
};
215204
}
216205

@@ -219,7 +208,7 @@ impl<Dyn: ?Sized> DynMetadata<Dyn> {
219208
pub fn align_of(self) -> usize {
220209
// SAFETY: DynMetadata always contains a valid vtable pointer
221210
return unsafe {
222-
crate::intrinsics::vtable_align(self.vtable_ptr() as *const ())
211+
crate::intrinsics::vtable_align(self.vtable_ptr as *const VTable as *const ())
223212
};
224213
}
225214

@@ -237,7 +226,7 @@ unsafe impl<Dyn: ?Sized> Sync for DynMetadata<Dyn> {}
237226

238227
impl<Dyn: ?Sized> fmt::Debug for DynMetadata<Dyn> {
239228
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
240-
f.debug_tuple("DynMetadata").field(&self.vtable_ptr()).finish()
229+
f.debug_tuple("DynMetadata").field(&(self.vtable_ptr as *const VTable)).finish()
241230
}
242231
}
243232

@@ -259,15 +248,15 @@ impl<Dyn: ?Sized> Eq for DynMetadata<Dyn> {}
259248
impl<Dyn: ?Sized> PartialEq for DynMetadata<Dyn> {
260249
#[inline]
261250
fn eq(&self, other: &Self) -> bool {
262-
crate::ptr::eq::<VTable>(self.vtable_ptr(), other.vtable_ptr())
251+
crate::ptr::eq::<VTable>(self.vtable_ptr, other.vtable_ptr)
263252
}
264253
}
265254

266255
impl<Dyn: ?Sized> Ord for DynMetadata<Dyn> {
267256
#[inline]
268257
#[allow(ambiguous_wide_pointer_comparisons)]
269258
fn cmp(&self, other: &Self) -> crate::cmp::Ordering {
270-
<*const VTable>::cmp(&self.vtable_ptr(), &other.vtable_ptr())
259+
(self.vtable_ptr as *const VTable).cmp(&(other.vtable_ptr as *const VTable))
271260
}
272261
}
273262

@@ -281,6 +270,6 @@ impl<Dyn: ?Sized> PartialOrd for DynMetadata<Dyn> {
281270
impl<Dyn: ?Sized> Hash for DynMetadata<Dyn> {
282271
#[inline]
283272
fn hash<H: Hasher>(&self, hasher: &mut H) {
284-
crate::ptr::hash::<VTable, _>(self.vtable_ptr(), hasher)
273+
crate::ptr::hash::<VTable, _>(self.vtable_ptr, hasher)
285274
}
286275
}

0 commit comments

Comments
 (0)