Skip to content

Commit 6a24ff2

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 1fb01b2 commit 6a24ff2

File tree

26 files changed

+520
-209
lines changed

26 files changed

+520
-209
lines changed

compiler/rustc_data_structures/src/aligned.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::marker::PointeeSized;
12
use std::ptr::Alignment;
23

34
/// Returns the ABI-required minimum alignment of a type in bytes.
@@ -19,7 +20,7 @@ pub const fn align_of<T: ?Sized + Aligned>() -> Alignment {
1920
/// example `[T]` has alignment of `T`.
2021
///
2122
/// [`mem::align_of<Self>()`]: std::mem::align_of
22-
pub unsafe trait Aligned {
23+
pub unsafe trait Aligned: PointeeSized {
2324
/// Alignment of `Self`.
2425
const ALIGN: Alignment;
2526
}

compiler/rustc_data_structures/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#![feature(ptr_alignment_type)]
3434
#![feature(rustc_attrs)]
3535
#![feature(rustdoc_internals)]
36+
#![feature(sized_hierarchy)]
3637
#![feature(test)]
3738
#![feature(thread_id_value)]
3839
#![feature(type_alias_impl_trait)]

compiler/rustc_data_structures/src/marker.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::marker::PointeeSized;
2+
13
#[rustc_on_unimplemented(message = "`{Self}` doesn't implement `DynSend`. \
24
Add it to `rustc_data_structures::marker` or use `IntoDynSyncSend` if it's already `Send`")]
35
// This is an auto trait for types which can be sent across threads if `sync::is_dyn_thread_safe()`
@@ -13,7 +15,7 @@ pub unsafe auto trait DynSend {}
1315
pub unsafe auto trait DynSync {}
1416

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

1820
macro_rules! impls_dyn_send_neg {
1921
($([$t1: ty $(where $($generics1: tt)*)?])*) => {
@@ -25,9 +27,9 @@ macro_rules! impls_dyn_send_neg {
2527
impls_dyn_send_neg!(
2628
[std::env::Args]
2729
[std::env::ArgsOs]
28-
[*const T where T: ?Sized]
29-
[*mut T where T: ?Sized]
30-
[std::ptr::NonNull<T> where T: ?Sized]
30+
[*const T where T: ?Sized + PointeeSized]
31+
[*mut T where T: ?Sized + PointeeSized]
32+
[std::ptr::NonNull<T> where T: ?Sized + PointeeSized]
3133
[std::rc::Rc<T> where T: ?Sized]
3234
[std::rc::Weak<T> where T: ?Sized]
3335
[std::sync::MutexGuard<'_, T> where T: ?Sized]
@@ -90,12 +92,12 @@ macro_rules! impls_dyn_sync_neg {
9092
impls_dyn_sync_neg!(
9193
[std::env::Args]
9294
[std::env::ArgsOs]
93-
[*const T where T: ?Sized]
94-
[*mut T where T: ?Sized]
95+
[*const T where T: ?Sized + PointeeSized]
96+
[*mut T where T: ?Sized + PointeeSized]
9597
[std::cell::Cell<T> where T: ?Sized]
9698
[std::cell::RefCell<T> where T: ?Sized]
9799
[std::cell::UnsafeCell<T> where T: ?Sized]
98-
[std::ptr::NonNull<T> where T: ?Sized]
100+
[std::ptr::NonNull<T> where T: ?Sized + PointeeSized]
99101
[std::rc::Rc<T> where T: ?Sized]
100102
[std::rc::Weak<T> where T: ?Sized]
101103
[std::cell::OnceCell<T> where T]
@@ -157,10 +159,10 @@ impl_dyn_sync!(
157159
[thin_vec::ThinVec<T> where T: DynSync]
158160
);
159161

160-
pub fn assert_dyn_sync<T: ?Sized + DynSync>() {}
161-
pub fn assert_dyn_send<T: ?Sized + DynSend>() {}
162-
pub fn assert_dyn_send_val<T: ?Sized + DynSend>(_t: &T) {}
163-
pub fn assert_dyn_send_sync_val<T: ?Sized + DynSync + DynSend>(_t: &T) {}
162+
pub fn assert_dyn_sync<T: ?Sized + PointeeSized + DynSync>() {}
163+
pub fn assert_dyn_send<T: ?Sized + PointeeSized + DynSend>() {}
164+
pub fn assert_dyn_send_val<T: ?Sized + PointeeSized + DynSend>(_t: &T) {}
165+
pub fn assert_dyn_send_sync_val<T: ?Sized + PointeeSized + DynSync + DynSend>(_t: &T) {}
164166

165167
#[derive(Copy, Clone)]
166168
pub struct FromDyn<T>(T);
@@ -200,10 +202,10 @@ impl<T> std::ops::Deref for FromDyn<T> {
200202
// an instance of `DynSend` and `DynSync`, since the compiler cannot infer
201203
// it automatically in some cases. (e.g. Box<dyn Send / Sync>)
202204
#[derive(Copy, Clone)]
203-
pub struct IntoDynSyncSend<T: ?Sized>(pub T);
205+
pub struct IntoDynSyncSend<T: ?Sized + PointeeSized>(pub T);
204206

205-
unsafe impl<T: ?Sized + Send> DynSend for IntoDynSyncSend<T> {}
206-
unsafe impl<T: ?Sized + Sync> DynSync for IntoDynSyncSend<T> {}
207+
unsafe impl<T: ?Sized + PointeeSized + Send> DynSend for IntoDynSyncSend<T> {}
208+
unsafe impl<T: ?Sized + PointeeSized + Sync> DynSync for IntoDynSyncSend<T> {}
207209

208210
impl<T> std::ops::Deref for IntoDynSyncSend<T> {
209211
type Target = T;

compiler/rustc_middle/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
#![feature(ptr_alignment_type)]
5656
#![feature(rustc_attrs)]
5757
#![feature(rustdoc_internals)]
58+
#![feature(sized_hierarchy)]
5859
#![feature(trusted_len)]
5960
#![feature(try_blocks)]
6061
#![feature(try_trait_v2)]

compiler/rustc_middle/src/ty/codec.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
99
use std::hash::Hash;
1010
use std::intrinsics;
11-
use std::marker::DiscriminantKind;
11+
use std::marker::{DiscriminantKind, PointeeSized};
1212

1313
use rustc_abi::{FieldIdx, VariantIdx};
1414
use rustc_data_structures::fx::FxHashMap;
@@ -65,7 +65,7 @@ impl<'tcx, E: TyEncoder<I = TyCtxt<'tcx>>> EncodableWithShorthand<E> for ty::Pre
6565
///
6666
/// `Decodable` can still be implemented in cases where `Decodable` is required
6767
/// by a trait bound.
68-
pub trait RefDecodable<'tcx, D: TyDecoder<I = TyCtxt<'tcx>>> {
68+
pub trait RefDecodable<'tcx, D: TyDecoder<I = TyCtxt<'tcx>>>: PointeeSized {
6969
fn decode(d: &mut D) -> &'tcx Self;
7070
}
7171

compiler/rustc_middle/src/ty/context.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::assert_matches::{assert_matches, debug_assert_matches};
88
use std::borrow::Borrow;
99
use std::cmp::Ordering;
1010
use std::hash::{Hash, Hasher};
11-
use std::marker::PhantomData;
11+
use std::marker::{PhantomData, PointeeSized};
1212
use std::ops::{Bound, Deref};
1313
use std::sync::{Arc, OnceLock};
1414
use std::{fmt, iter, mem};
@@ -2421,17 +2421,17 @@ impl<'tcx> TyCtxt<'tcx> {
24212421
// this type just holds a pointer to it, but it still effectively owns it. It
24222422
// impls `Borrow` so that it can be looked up using the original
24232423
// (non-arena-memory-owning) types.
2424-
struct InternedInSet<'tcx, T: ?Sized>(&'tcx T);
2424+
struct InternedInSet<'tcx, T: ?Sized + PointeeSized>(&'tcx T);
24252425

2426-
impl<'tcx, T: 'tcx + ?Sized> Clone for InternedInSet<'tcx, T> {
2426+
impl<'tcx, T: 'tcx + ?Sized + PointeeSized> Clone for InternedInSet<'tcx, T> {
24272427
fn clone(&self) -> Self {
24282428
InternedInSet(self.0)
24292429
}
24302430
}
24312431

2432-
impl<'tcx, T: 'tcx + ?Sized> Copy for InternedInSet<'tcx, T> {}
2432+
impl<'tcx, T: 'tcx + ?Sized + PointeeSized> Copy for InternedInSet<'tcx, T> {}
24332433

2434-
impl<'tcx, T: 'tcx + ?Sized> IntoPointer for InternedInSet<'tcx, T> {
2434+
impl<'tcx, T: 'tcx + ?Sized + PointeeSized> IntoPointer for InternedInSet<'tcx, T> {
24352435
fn into_pointer(&self) -> *const () {
24362436
self.0 as *const _ as *const ()
24372437
}

compiler/rustc_serialize/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#![feature(min_specialization)]
1515
#![feature(never_type)]
1616
#![feature(rustdoc_internals)]
17+
#![feature(sized_hierarchy)]
1718
#![warn(unreachable_pub)]
1819
// tidy-alphabetical-end
1920

compiler/rustc_serialize/src/serialize.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::borrow::Cow;
44
use std::cell::{Cell, RefCell};
55
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet, VecDeque};
66
use std::hash::{BuildHasher, Hash};
7-
use std::marker::PhantomData;
7+
use std::marker::{PhantomData, PointeeSized};
88
use std::num::NonZero;
99
use std::path;
1010
use std::rc::Rc;
@@ -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>: 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 + 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
@@ -15,6 +15,7 @@
1515
)]
1616
#![doc(rust_logo)]
1717
#![feature(rustdoc_internals)]
18+
#![feature(sized_hierarchy)]
1819
#![warn(unreachable_pub)]
1920
// tidy-alphabetical-end
2021

compiler/rustc_smir/src/rustc_smir/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//!
88
//! For now, we are developing everything inside `rustc`, thus, we keep this module private.
99
10+
use std::marker::PointeeSized;
1011
use std::ops::RangeInclusive;
1112

1213
use rustc_hir::def::DefKind;
@@ -157,7 +158,7 @@ pub(crate) fn new_item_kind(kind: DefKind) -> ItemKind {
157158
}
158159

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

0 commit comments

Comments
 (0)