1717//! [crates.io](https://crates.io).
1818
1919use std:: fmt:: Debug ;
20+ use std:: marker:: PhantomData ;
2021use std:: { fmt, io} ;
2122
2223pub ( crate ) use rustc_public_bridge:: IndexedVal ;
@@ -33,7 +34,10 @@ pub use crate::crate_def::{CrateDef, CrateDefItems, CrateDefType, DefId};
3334pub use crate :: error:: * ;
3435use crate :: mir:: mono:: StaticDef ;
3536use crate :: mir:: { Body , Mutability } ;
36- use crate :: ty:: { AssocItem , FnDef , ForeignModuleDef , ImplDef , ProvenanceMap , Span , TraitDef , Ty } ;
37+ use crate :: ty:: {
38+ AssocItem , FnDef , ForeignModuleDef , ImplDef , ProvenanceMap , Span , TraitDef , Ty ,
39+ serialize_index_impl,
40+ } ;
3741use crate :: unstable:: Stable ;
3842
3943pub mod abi;
@@ -46,31 +50,25 @@ pub mod compiler_interface;
4650pub mod error;
4751pub mod mir;
4852pub mod target;
53+ #[ cfg( test) ]
54+ mod tests;
4955pub mod ty;
5056pub mod visitor;
5157
5258/// Use String for now but we should replace it.
5359pub type Symbol = String ;
5460
5561/// The number that identifies a crate.
56- pub type CrateNum = usize ;
62+ #[ derive( Clone , Copy , PartialEq , Eq , Debug ) ]
63+ pub struct CrateNum ( pub ( crate ) usize , ThreadLocalIndex ) ;
64+ serialize_index_impl ! ( CrateNum ) ;
5765
5866impl Debug for DefId {
5967 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
6068 f. debug_struct ( "DefId" ) . field ( "id" , & self . 0 ) . field ( "name" , & self . name ( ) ) . finish ( )
6169 }
6270}
6371
64- impl IndexedVal for DefId {
65- fn to_val ( index : usize ) -> Self {
66- DefId ( index)
67- }
68-
69- fn to_index ( & self ) -> usize {
70- self . 0
71- }
72- }
73-
7472/// A list of crate items.
7573pub type CrateItems = Vec < CrateItem > ;
7674
@@ -297,3 +295,25 @@ impl rustc_public_bridge::bridge::Allocation<compiler_interface::BridgeTys>
297295 }
298296 }
299297}
298+
299+ #[ derive( Clone , Copy , Hash , PartialEq , Eq , Default ) ]
300+ /// Marker type for indexes into thread local structures.
301+ ///
302+ /// Makes things `!Send`/`!Sync`, so users don't move `rustc_public` types to
303+ /// thread with no (or worse, different) `rustc_public` pointer.
304+ ///
305+ /// Note. This doesn't make it impossible to confuse TLS. You could return a
306+ /// `DefId` from one `run!` invocation, and then use it inside a different
307+ /// `run!` invocation with different tables.
308+ pub ( crate ) struct ThreadLocalIndex {
309+ _phantom : PhantomData < * const ( ) > ,
310+ }
311+ #[ expect( non_upper_case_globals) ]
312+ /// Emulating unit struct `struct ThreadLocalIndex`;
313+ pub ( crate ) const ThreadLocalIndex : ThreadLocalIndex = ThreadLocalIndex { _phantom : PhantomData } ;
314+
315+ impl fmt:: Debug for ThreadLocalIndex {
316+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
317+ f. debug_tuple ( "ThreadLocalIndex" ) . finish ( )
318+ }
319+ }
0 commit comments