Skip to content

Commit 684cc2c

Browse files
committed
Introduce CovariantCopyTaggedPtr and use it in TyKind::TraitObject
1 parent 243d2ca commit 684cc2c

File tree

24 files changed

+479
-48
lines changed

24 files changed

+479
-48
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use rustc_data_structures::packed::Pu128;
2828
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
2929
use rustc_data_structures::stack::ensure_sufficient_stack;
3030
use rustc_data_structures::sync::Lrc;
31+
use rustc_data_structures::tagged_ptr::Tag;
3132
use rustc_macros::{Decodable, Encodable, HashStable_Generic};
3233
pub use rustc_span::AttrId;
3334
use rustc_span::source_map::{Spanned, respan};
@@ -2269,10 +2270,32 @@ impl TyKind {
22692270

22702271
/// Syntax used to declare a trait object.
22712272
#[derive(Clone, Copy, PartialEq, Encodable, Decodable, Debug, HashStable_Generic)]
2273+
#[repr(u8)]
22722274
pub enum TraitObjectSyntax {
2273-
Dyn,
2274-
DynStar,
2275-
None,
2275+
// SAFETY: When adding new variants make sure to update the `Tag` impl.
2276+
Dyn = 0,
2277+
DynStar = 1,
2278+
None = 2,
2279+
}
2280+
2281+
/// SAFETY: `TraitObjectSyntax` only has 3 data-less variants which means
2282+
/// it can be represented with a `u2`. We use `repr(u8)` to guarantee the
2283+
/// discriminants of the variants are no greater than `3`.
2284+
unsafe impl Tag for TraitObjectSyntax {
2285+
const BITS: u32 = 2;
2286+
2287+
fn into_usize(self) -> usize {
2288+
self as u8 as usize
2289+
}
2290+
2291+
unsafe fn from_usize(tag: usize) -> Self {
2292+
match tag {
2293+
0 => TraitObjectSyntax::Dyn,
2294+
1 => TraitObjectSyntax::DynStar,
2295+
2 => TraitObjectSyntax::None,
2296+
_ => unreachable!(),
2297+
}
2298+
}
22762299
}
22772300

22782301
#[derive(Clone, Encodable, Decodable, Debug)]

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ use rustc_data_structures::fingerprint::Fingerprint;
4747
use rustc_data_structures::sorted_map::SortedMap;
4848
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
4949
use rustc_data_structures::sync::Lrc;
50+
use rustc_data_structures::tagged_ptr::CovariantCopyTaggedPtr;
5051
use rustc_errors::{DiagArgFromDisplay, DiagCtxtHandle, StashKey};
5152
use rustc_hir::def::{DefKind, LifetimeRes, Namespace, PartialRes, PerNS, Res};
5253
use rustc_hir::def_id::{CRATE_DEF_ID, LOCAL_CRATE, LocalDefId};
@@ -1157,7 +1158,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
11571158
let lifetime_bound = this.elided_dyn_bound(t.span);
11581159
(bounds, lifetime_bound)
11591160
});
1160-
let kind = hir::TyKind::TraitObject(bounds, lifetime_bound, TraitObjectSyntax::None);
1161+
let kind = hir::TyKind::TraitObject(
1162+
bounds,
1163+
CovariantCopyTaggedPtr::new(lifetime_bound, TraitObjectSyntax::None),
1164+
);
11611165
return hir::Ty { kind, span: self.lower_span(t.span), hir_id: self.next_id() };
11621166
}
11631167

@@ -1308,7 +1312,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
13081312
lifetime_bound.unwrap_or_else(|| this.elided_dyn_bound(t.span));
13091313
(bounds, lifetime_bound)
13101314
});
1311-
hir::TyKind::TraitObject(bounds, lifetime_bound, *kind)
1315+
hir::TyKind::TraitObject(bounds, CovariantCopyTaggedPtr::new(lifetime_bound, *kind))
13121316
}
13131317
TyKind::ImplTrait(def_node_id, bounds) => {
13141318
let span = t.span;
@@ -2364,8 +2368,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
23642368
hir_id = self.next_id();
23652369
hir::TyKind::TraitObject(
23662370
arena_vec![self; principal],
2367-
self.elided_dyn_bound(span),
2368-
TraitObjectSyntax::None,
2371+
CovariantCopyTaggedPtr::new(
2372+
self.elided_dyn_bound(span),
2373+
TraitObjectSyntax::None,
2374+
),
23692375
)
23702376
}
23712377
_ => hir::TyKind::Path(hir::QPath::Resolved(None, path)),

compiler/rustc_borrowck/src/diagnostics/region_errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -893,7 +893,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
893893
if alias_ty.span.desugaring_kind().is_some() {
894894
// Skip `async` desugaring `impl Future`.
895895
}
896-
if let TyKind::TraitObject(_, lt, _) = alias_ty.kind {
896+
if let TyKind::TraitObject(_, lt) = alias_ty.kind {
897897
if lt.ident.name == kw::Empty {
898898
spans_suggs.push((lt.ident.span.shrink_to_hi(), " + 'a".to_string()));
899899
} else {

compiler/rustc_data_structures/src/marker.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ impl_dyn_send!(
7373
[Box<T, A> where T: ?Sized + DynSend, A: std::alloc::Allocator + DynSend]
7474
[crate::sync::RwLock<T> where T: DynSend]
7575
[crate::tagged_ptr::CopyTaggedPtr<P, T, CP> where P: Send + crate::tagged_ptr::Pointer, T: Send + crate::tagged_ptr::Tag, const CP: bool]
76+
[crate::tagged_ptr::CovariantCopyTaggedPtr<P, T, CP> where P: Send + crate::tagged_ptr::Pointer<Target: Sized>, T: Send + crate::tagged_ptr::Tag, const CP: bool]
7677
[rustc_arena::TypedArena<T> where T: DynSend]
7778
[indexmap::IndexSet<V, S> where V: DynSend, S: DynSend]
7879
[indexmap::IndexMap<K, V, S> where K: DynSend, V: DynSend, S: DynSend]
@@ -149,6 +150,7 @@ impl_dyn_sync!(
149150
[crate::sync::WorkerLocal<T> where T: DynSend]
150151
[crate::intern::Interned<'a, T> where 'a, T: DynSync]
151152
[crate::tagged_ptr::CopyTaggedPtr<P, T, CP> where P: Sync + crate::tagged_ptr::Pointer, T: Sync + crate::tagged_ptr::Tag, const CP: bool]
153+
[crate::tagged_ptr::CovariantCopyTaggedPtr<P, T, CP> where P: Sync + crate::tagged_ptr::Pointer<Target: Sized>, T: Sync + crate::tagged_ptr::Tag, const CP: bool]
152154
[parking_lot::lock_api::Mutex<R, T> where R: DynSync, T: ?Sized + DynSend]
153155
[parking_lot::lock_api::RwLock<R, T> where R: DynSync, T: ?Sized + DynSend + DynSync]
154156
[indexmap::IndexSet<V, S> where V: DynSync, S: DynSync]

compiler/rustc_data_structures/src/tagged_ptr.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@ use std::sync::Arc;
2323
use crate::aligned::Aligned;
2424

2525
mod copy;
26+
mod covariant;
2627
mod drop;
2728
mod impl_tag;
2829

2930
pub use copy::CopyTaggedPtr;
31+
pub use covariant::CovariantCopyTaggedPtr;
3032
pub use drop::TaggedPtr;
3133

3234
/// This describes the pointer type encapsulated by [`TaggedPtr`] and

0 commit comments

Comments
 (0)