2020//! [crates.io](https://crates.io).
2121
2222use std:: fmt:: Debug ;
23+ use std:: marker:: PhantomData ;
2324use std:: { fmt, io} ;
2425
2526pub ( crate ) use rustc_public_bridge:: IndexedVal ;
@@ -36,7 +37,10 @@ pub use crate::crate_def::{CrateDef, CrateDefItems, CrateDefType, DefId};
3637pub use crate :: error:: * ;
3738use crate :: mir:: mono:: StaticDef ;
3839use crate :: mir:: { Body , Mutability } ;
39- use crate :: ty:: { AssocItem , FnDef , ForeignModuleDef , ImplDef , ProvenanceMap , Span , TraitDef , Ty } ;
40+ use crate :: ty:: {
41+ AssocItem , FnDef , ForeignModuleDef , ImplDef , ProvenanceMap , Span , TraitDef , Ty ,
42+ serialize_index_impl,
43+ } ;
4044use crate :: unstable:: Stable ;
4145
4246pub mod abi;
@@ -49,31 +53,25 @@ pub mod compiler_interface;
4953pub mod error;
5054pub mod mir;
5155pub mod target;
56+ #[ cfg( test) ]
57+ mod tests;
5258pub mod ty;
5359pub mod visitor;
5460
5561/// Use String for now but we should replace it.
5662pub type Symbol = String ;
5763
5864/// The number that identifies a crate.
59- pub type CrateNum = usize ;
65+ #[ derive( Clone , Copy , PartialEq , Eq , Debug ) ]
66+ pub struct CrateNum ( pub ( crate ) usize , ThreadLocalIndex ) ;
67+ serialize_index_impl ! ( CrateNum ) ;
6068
6169impl Debug for DefId {
6270 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
6371 f. debug_struct ( "DefId" ) . field ( "id" , & self . 0 ) . field ( "name" , & self . name ( ) ) . finish ( )
6472 }
6573}
6674
67- impl IndexedVal for DefId {
68- fn to_val ( index : usize ) -> Self {
69- DefId ( index)
70- }
71-
72- fn to_index ( & self ) -> usize {
73- self . 0
74- }
75- }
76-
7775/// A list of crate items.
7876pub type CrateItems = Vec < CrateItem > ;
7977
@@ -300,3 +298,25 @@ impl rustc_public_bridge::bridge::Allocation<compiler_interface::BridgeTys>
300298 }
301299 }
302300}
301+
302+ #[ derive( Clone , Copy , Hash , PartialEq , Eq , Default ) ]
303+ /// Marker type for indexes into thread local structures.
304+ ///
305+ /// Makes things `!Send`/`!Sync`, so users don't move `rustc_public` types to
306+ /// thread with no (or worse, different) `rustc_public` pointer.
307+ ///
308+ /// Note. This doesn't make it impossible to confuse TLS. You could return a
309+ /// `DefId` from one `run!` invocation, and then use it inside a different
310+ /// `run!` invocation with different tables.
311+ pub ( crate ) struct ThreadLocalIndex {
312+ _phantom : PhantomData < * const ( ) > ,
313+ }
314+ #[ expect( non_upper_case_globals) ]
315+ /// Emulating unit struct `struct ThreadLocalIndex`;
316+ pub ( crate ) const ThreadLocalIndex : ThreadLocalIndex = ThreadLocalIndex { _phantom : PhantomData } ;
317+
318+ impl fmt:: Debug for ThreadLocalIndex {
319+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
320+ f. debug_tuple ( "ThreadLocalIndex" ) . finish ( )
321+ }
322+ }
0 commit comments