Skip to content

Commit 884d0e0

Browse files
committed
library/compiler: add PointeeSized bounds
As core uses an extern type (`ptr::VTable`), the default `?Sized` to `MetaSized` migration isn't sufficient, and some code that previously accepted `VTable` needs relaxed to continue to accept extern types. Similarly, the compiler uses many extern types in `rustc_codegen_llvm` and in the `rustc_middle::ty::List` implementation (`OpaqueListContents`) some bounds must be relaxed to continue to accept these types. Unfortunately, due to the current inability to relax `Deref::Target`, some of the bounds in the standard library are forced to be stricter than they ideally would be.
1 parent 322cc31 commit 884d0e0

File tree

29 files changed

+310
-219
lines changed

29 files changed

+310
-219
lines changed

compiler/rustc_data_structures/src/aligned.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use std::ptr::Alignment;
22

3+
use rustc_serialize::PointeeSized;
4+
35
/// Returns the ABI-required minimum alignment of a type in bytes.
46
///
57
/// This is equivalent to [`align_of`], but also works for some unsized
@@ -17,7 +19,7 @@ pub const fn align_of<T: ?Sized + Aligned>() -> Alignment {
1719
/// example `[T]` has alignment of `T`.
1820
///
1921
/// [`align_of::<Self>()`]: align_of
20-
pub unsafe trait Aligned {
22+
pub unsafe trait Aligned: PointeeSized {
2123
/// Alignment of `Self`.
2224
const ALIGN: Alignment;
2325
}

compiler/rustc_data_structures/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#![allow(rustc::potential_query_instability)]
1313
#![cfg_attr(bootstrap, feature(cfg_match))]
1414
#![cfg_attr(not(bootstrap), feature(cfg_select))]
15+
#![cfg_attr(not(bootstrap), feature(sized_hierarchy))]
1516
#![deny(unsafe_op_in_unsafe_fn)]
1617
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
1718
#![doc(rust_logo)]
@@ -43,6 +44,9 @@ use std::fmt;
4344
pub use atomic_ref::AtomicRef;
4445
pub use ena::{snapshot_vec, undo_log, unify};
4546
pub use rustc_index::static_assert_size;
47+
// re-exported for `rustc_smir`
48+
// FIXME(sized_hierarchy): remove with `cfg(bootstrap)`, see `rustc_serialize/src/lib.rs`
49+
pub use rustc_serialize::PointeeSized;
4650

4751
pub mod aligned;
4852
pub mod base_n;

compiler/rustc_data_structures/src/marker.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use std::alloc::Allocator;
22

3+
use rustc_serialize::PointeeSized;
4+
35
#[diagnostic::on_unimplemented(message = "`{Self}` doesn't implement `DynSend`. \
46
Add it to `rustc_data_structures::marker` or use `IntoDynSyncSend` if it's already `Send`")]
57
// This is an auto trait for types which can be sent across threads if `sync::is_dyn_thread_safe()`
@@ -15,7 +17,7 @@ pub unsafe auto trait DynSend {}
1517
pub unsafe auto trait DynSync {}
1618

1719
// Same with `Sync` and `Send`.
18-
unsafe impl<T: DynSync + ?Sized> DynSend for &T {}
20+
unsafe impl<T: DynSync + ?Sized + PointeeSized> DynSend for &T {}
1921

2022
macro_rules! impls_dyn_send_neg {
2123
($([$t1: ty $(where $($generics1: tt)*)?])*) => {
@@ -27,9 +29,9 @@ macro_rules! impls_dyn_send_neg {
2729
impls_dyn_send_neg!(
2830
[std::env::Args]
2931
[std::env::ArgsOs]
30-
[*const T where T: ?Sized]
31-
[*mut T where T: ?Sized]
32-
[std::ptr::NonNull<T> where T: ?Sized]
32+
[*const T where T: ?Sized + PointeeSized]
33+
[*mut T where T: ?Sized + PointeeSized]
34+
[std::ptr::NonNull<T> where T: ?Sized + PointeeSized]
3335
[std::rc::Rc<T, A> where T: ?Sized, A: Allocator]
3436
[std::rc::Weak<T, A> where T: ?Sized, A: Allocator]
3537
[std::sync::MutexGuard<'_, T> where T: ?Sized]
@@ -100,12 +102,12 @@ macro_rules! impls_dyn_sync_neg {
100102
impls_dyn_sync_neg!(
101103
[std::env::Args]
102104
[std::env::ArgsOs]
103-
[*const T where T: ?Sized]
104-
[*mut T where T: ?Sized]
105+
[*const T where T: ?Sized + PointeeSized]
106+
[*mut T where T: ?Sized + PointeeSized]
105107
[std::cell::Cell<T> where T: ?Sized]
106108
[std::cell::RefCell<T> where T: ?Sized]
107109
[std::cell::UnsafeCell<T> where T: ?Sized]
108-
[std::ptr::NonNull<T> where T: ?Sized]
110+
[std::ptr::NonNull<T> where T: ?Sized + PointeeSized]
109111
[std::rc::Rc<T, A> where T: ?Sized, A: Allocator]
110112
[std::rc::Weak<T, A> where T: ?Sized, A: Allocator]
111113
[std::cell::OnceCell<T> where T]
@@ -175,10 +177,10 @@ impl_dyn_sync!(
175177
[thin_vec::ThinVec<T> where T: DynSync]
176178
);
177179

178-
pub fn assert_dyn_sync<T: ?Sized + DynSync>() {}
179-
pub fn assert_dyn_send<T: ?Sized + DynSend>() {}
180-
pub fn assert_dyn_send_val<T: ?Sized + DynSend>(_t: &T) {}
181-
pub fn assert_dyn_send_sync_val<T: ?Sized + DynSync + DynSend>(_t: &T) {}
180+
pub fn assert_dyn_sync<T: ?Sized + PointeeSized + DynSync>() {}
181+
pub fn assert_dyn_send<T: ?Sized + PointeeSized + DynSend>() {}
182+
pub fn assert_dyn_send_val<T: ?Sized + PointeeSized + DynSend>(_t: &T) {}
183+
pub fn assert_dyn_send_sync_val<T: ?Sized + PointeeSized + DynSync + DynSend>(_t: &T) {}
182184

183185
#[derive(Copy, Clone)]
184186
pub struct FromDyn<T>(T);
@@ -231,10 +233,10 @@ impl<T> std::ops::DerefMut for FromDyn<T> {
231233
// an instance of `DynSend` and `DynSync`, since the compiler cannot infer
232234
// it automatically in some cases. (e.g. Box<dyn Send / Sync>)
233235
#[derive(Copy, Clone)]
234-
pub struct IntoDynSyncSend<T: ?Sized>(pub T);
236+
pub struct IntoDynSyncSend<T: ?Sized + PointeeSized>(pub T);
235237

236-
unsafe impl<T: ?Sized + Send> DynSend for IntoDynSyncSend<T> {}
237-
unsafe impl<T: ?Sized + Sync> DynSync for IntoDynSyncSend<T> {}
238+
unsafe impl<T: ?Sized + PointeeSized + Send> DynSend for IntoDynSyncSend<T> {}
239+
unsafe impl<T: ?Sized + PointeeSized + Sync> DynSync for IntoDynSyncSend<T> {}
238240

239241
impl<T> std::ops::Deref for IntoDynSyncSend<T> {
240242
type Target = T;

compiler/rustc_middle/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#![allow(internal_features)]
2929
#![allow(rustc::diagnostic_outside_of_impl)]
3030
#![allow(rustc::untranslatable_diagnostic)]
31+
#![cfg_attr(not(bootstrap), feature(sized_hierarchy))]
3132
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
3233
#![doc(rust_logo)]
3334
#![feature(allocator_api)]

compiler/rustc_middle/src/ty/codec.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use std::marker::DiscriminantKind;
1313
use rustc_abi::{FieldIdx, VariantIdx};
1414
use rustc_data_structures::fx::FxHashMap;
1515
use rustc_hir::def_id::LocalDefId;
16-
use rustc_serialize::{Decodable, Encodable};
16+
use rustc_serialize::{Decodable, Encodable, PointeeSized};
1717
use rustc_span::source_map::Spanned;
1818
use rustc_span::{Span, SpanDecoder, SpanEncoder};
1919

@@ -96,7 +96,7 @@ impl<'tcx, E: TyEncoder<'tcx>> EncodableWithShorthand<'tcx, E> for ty::Predicate
9696
///
9797
/// `Decodable` can still be implemented in cases where `Decodable` is required
9898
/// by a trait bound.
99-
pub trait RefDecodable<'tcx, D: TyDecoder<'tcx>> {
99+
pub trait RefDecodable<'tcx, D: TyDecoder<'tcx>>: PointeeSized {
100100
fn decode(d: &mut D) -> &'tcx Self;
101101
}
102102

compiler/rustc_middle/src/ty/context.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ use rustc_macros::{HashStable, TyDecodable, TyEncodable};
4343
use rustc_query_system::cache::WithDepNode;
4444
use rustc_query_system::dep_graph::DepNodeIndex;
4545
use rustc_query_system::ich::StableHashingContext;
46+
use rustc_serialize::PointeeSized;
4647
use rustc_serialize::opaque::{FileEncodeResult, FileEncoder};
4748
use rustc_session::config::CrateType;
4849
use rustc_session::cstore::{CrateStoreDyn, Untracked};
@@ -2540,17 +2541,17 @@ impl<'tcx> TyCtxt<'tcx> {
25402541
// this type just holds a pointer to it, but it still effectively owns it. It
25412542
// impls `Borrow` so that it can be looked up using the original
25422543
// (non-arena-memory-owning) types.
2543-
struct InternedInSet<'tcx, T: ?Sized>(&'tcx T);
2544+
struct InternedInSet<'tcx, T: ?Sized + PointeeSized>(&'tcx T);
25442545

2545-
impl<'tcx, T: 'tcx + ?Sized> Clone for InternedInSet<'tcx, T> {
2546+
impl<'tcx, T: 'tcx + ?Sized + PointeeSized> Clone for InternedInSet<'tcx, T> {
25462547
fn clone(&self) -> Self {
25472548
InternedInSet(self.0)
25482549
}
25492550
}
25502551

2551-
impl<'tcx, T: 'tcx + ?Sized> Copy for InternedInSet<'tcx, T> {}
2552+
impl<'tcx, T: 'tcx + ?Sized + PointeeSized> Copy for InternedInSet<'tcx, T> {}
25522553

2553-
impl<'tcx, T: 'tcx + ?Sized> IntoPointer for InternedInSet<'tcx, T> {
2554+
impl<'tcx, T: 'tcx + ?Sized + PointeeSized> IntoPointer for InternedInSet<'tcx, T> {
25542555
fn into_pointer(&self) -> *const () {
25552556
self.0 as *const _ as *const ()
25562557
}

compiler/rustc_serialize/src/lib.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// tidy-alphabetical-start
44
#![allow(internal_features)]
55
#![allow(rustc::internal)]
6+
#![cfg_attr(not(bootstrap), feature(sized_hierarchy))]
67
#![cfg_attr(test, feature(test))]
78
#![doc(
89
html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/",
@@ -27,3 +28,19 @@ mod serialize;
2728
pub mod int_overflow;
2829
pub mod leb128;
2930
pub mod opaque;
31+
32+
// This has nothing to do with `rustc_serialize` but it is convenient to define it in one place
33+
// for the rest of the compiler so that `cfg(bootstrap)` doesn't need to be littered throughout
34+
// the compiler wherever `PointeeSized` would be used. `rustc_serialize` happens to be the deepest
35+
// crate in the crate graph which uses `PointeeSized`.
36+
//
37+
// When bootstrap bumps, remove both the `cfg(not(bootstrap))` and `cfg(bootstrap)` lines below
38+
// and just import `std::marker::PointeeSized` whereever this item was used.
39+
40+
#[cfg(not(bootstrap))]
41+
pub use std::marker::PointeeSized;
42+
43+
#[cfg(bootstrap)]
44+
pub trait PointeeSized {}
45+
#[cfg(bootstrap)]
46+
impl<T: ?Sized> PointeeSized for T {}

compiler/rustc_serialize/src/serialize.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ pub trait Decoder {
142142
/// `rustc_metadata::rmeta::Lazy`.
143143
/// * `TyEncodable` should be used for types that are only serialized in crate
144144
/// metadata or the incremental cache. This is most types in `rustc_middle`.
145-
pub trait Encodable<S: Encoder> {
145+
pub trait Encodable<S: Encoder>: crate::PointeeSized {
146146
fn encode(&self, s: &mut S);
147147
}
148148

@@ -198,7 +198,7 @@ direct_serialize_impls! {
198198
char emit_char read_char
199199
}
200200

201-
impl<S: Encoder, T: ?Sized> Encodable<S> for &T
201+
impl<S: Encoder, T: ?Sized + crate::PointeeSized> Encodable<S> for &T
202202
where
203203
T: Encodable<S>,
204204
{

compiler/rustc_smir/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// tidy-alphabetical-start
1010
#![allow(internal_features)]
1111
#![allow(rustc::usage_of_ty_tykind)]
12+
#![cfg_attr(not(bootstrap), feature(sized_hierarchy))]
1213
#![doc(
1314
html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/",
1415
test(attr(allow(unused_variables), deny(warnings)))

compiler/rustc_smir/src/rustc_smir/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
1010
use std::ops::RangeInclusive;
1111

12+
use rustc_data_structures::PointeeSized;
1213
use rustc_hir::def::DefKind;
1314
use rustc_middle::mir;
1415
use rustc_middle::mir::interpret::AllocId;
@@ -158,7 +159,7 @@ pub(crate) fn new_item_kind(kind: DefKind) -> ItemKind {
158159
}
159160

160161
/// Trait used to convert between an internal MIR type to a Stable MIR type.
161-
pub trait Stable<'cx> {
162+
pub trait Stable<'cx>: PointeeSized {
162163
/// The stable representation of the type implementing Stable.
163164
type T;
164165
/// Converts an object to the equivalent Stable MIR representation.

0 commit comments

Comments
 (0)