@@ -6,11 +6,11 @@ use crate::rmeta::{CrateDep, CrateMetadata, CrateNumMap, CrateRoot, MetadataBlob
6
6
7
7
use rustc_ast:: expand:: allocator:: AllocatorKind ;
8
8
use rustc_ast:: { self as ast, * } ;
9
- use rustc_data_structures:: fx:: { FxHashMap , FxHashSet } ;
9
+ use rustc_data_structures:: fx:: FxHashSet ;
10
10
use rustc_data_structures:: svh:: Svh ;
11
11
use rustc_data_structures:: sync:: { MappedReadGuard , MappedWriteGuard , ReadGuard , WriteGuard } ;
12
12
use rustc_expand:: base:: SyntaxExtension ;
13
- use rustc_hir:: def_id:: { CrateNum , LocalDefId , StableCrateId , LOCAL_CRATE } ;
13
+ use rustc_hir:: def_id:: { CrateNum , LocalDefId , StableCrateId , StableCrateIdMap , LOCAL_CRATE } ;
14
14
use rustc_hir:: definitions:: Definitions ;
15
15
use rustc_index:: vec:: IndexVec ;
16
16
use rustc_middle:: ty:: TyCtxt ;
@@ -46,9 +46,8 @@ pub struct CStore {
46
46
/// This crate has a `#[alloc_error_handler]` item.
47
47
has_alloc_error_handler : bool ,
48
48
49
- /// This map is used to verify we get no hash conflicts between
50
- /// `StableCrateId` values.
51
- pub ( crate ) stable_crate_ids : FxHashMap < StableCrateId , CrateNum > ,
49
+ /// The interned [StableCrateId]s.
50
+ pub ( crate ) stable_crate_ids : StableCrateIdMap ,
52
51
53
52
/// Unused externs of the crate
54
53
unused_externs : Vec < Symbol > ,
@@ -144,9 +143,21 @@ impl CStore {
144
143
} )
145
144
}
146
145
147
- fn alloc_new_crate_num ( & mut self ) -> CrateNum {
148
- self . metas . push ( None ) ;
149
- CrateNum :: new ( self . metas . len ( ) - 1 )
146
+ fn intern_stable_crate_id ( & mut self , root : & CrateRoot ) -> Result < CrateNum , CrateError > {
147
+ assert_eq ! ( self . metas. len( ) , self . stable_crate_ids. len( ) ) ;
148
+ let num = CrateNum :: new ( self . stable_crate_ids . len ( ) ) ;
149
+ if let Some ( & existing) = self . stable_crate_ids . get ( & root. stable_crate_id ( ) ) {
150
+ let crate_name0 = root. name ( ) ;
151
+ if let Some ( crate_name1) = self . metas [ existing] . as_ref ( ) . map ( |data| data. name ( ) ) {
152
+ Err ( CrateError :: StableCrateIdCollision ( crate_name0, crate_name1) )
153
+ } else {
154
+ Err ( CrateError :: SymbolConflictsCurrent ( crate_name0) )
155
+ }
156
+ } else {
157
+ self . metas . push ( None ) ;
158
+ self . stable_crate_ids . insert ( root. stable_crate_id ( ) , num) ;
159
+ Ok ( num)
160
+ }
150
161
}
151
162
152
163
pub fn has_crate_data ( & self , cnum : CrateNum ) -> bool {
@@ -247,7 +258,7 @@ impl CStore {
247
258
}
248
259
249
260
pub fn new ( sess : & Session ) -> CStore {
250
- let mut stable_crate_ids = FxHashMap :: default ( ) ;
261
+ let mut stable_crate_ids = StableCrateIdMap :: default ( ) ;
251
262
stable_crate_ids. insert ( sess. local_stable_crate_id ( ) , LOCAL_CRATE ) ;
252
263
CStore {
253
264
// We add an empty entry for LOCAL_CRATE (which maps to zero) in
@@ -342,42 +353,6 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
342
353
None
343
354
}
344
355
345
- fn verify_no_symbol_conflicts ( & self , root : & CrateRoot ) -> Result < ( ) , CrateError > {
346
- // Check for (potential) conflicts with the local crate
347
- if self . sess . local_stable_crate_id ( ) == root. stable_crate_id ( ) {
348
- return Err ( CrateError :: SymbolConflictsCurrent ( root. name ( ) ) ) ;
349
- }
350
-
351
- // Check for conflicts with any crate loaded so far
352
- for ( _, other) in self . cstore . iter_crate_data ( ) {
353
- // Same stable crate id but different SVH
354
- if other. stable_crate_id ( ) == root. stable_crate_id ( ) && other. hash ( ) != root. hash ( ) {
355
- bug ! (
356
- "Previously returned E0523 here. \
357
- See https://github.com/rust-lang/rust/pull/100599 for additional discussion.\
358
- root.name() = {}.",
359
- root. name( )
360
- ) ;
361
- }
362
- }
363
-
364
- Ok ( ( ) )
365
- }
366
-
367
- fn verify_no_stable_crate_id_hash_conflicts (
368
- & mut self ,
369
- root : & CrateRoot ,
370
- cnum : CrateNum ,
371
- ) -> Result < ( ) , CrateError > {
372
- if let Some ( existing) = self . cstore . stable_crate_ids . insert ( root. stable_crate_id ( ) , cnum) {
373
- let crate_name0 = root. name ( ) ;
374
- let crate_name1 = self . cstore . get_crate_data ( existing) . name ( ) ;
375
- return Err ( CrateError :: StableCrateIdCollision ( crate_name0, crate_name1) ) ;
376
- }
377
-
378
- Ok ( ( ) )
379
- }
380
-
381
356
fn register_crate (
382
357
& mut self ,
383
358
host_lib : Option < Library > ,
@@ -396,7 +371,7 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
396
371
self . sess . opts . externs . get ( name. as_str ( ) ) . map_or ( false , |e| e. is_private_dep ) ;
397
372
398
373
// Claim this crate number and cache it
399
- let cnum = self . cstore . alloc_new_crate_num ( ) ;
374
+ let cnum = self . cstore . intern_stable_crate_id ( & crate_root ) ? ;
400
375
401
376
info ! (
402
377
"register crate `{}` (cnum = {}. private_dep = {})" ,
@@ -432,14 +407,6 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
432
407
None
433
408
} ;
434
409
435
- // Perform some verification *after* resolve_crate_deps() above is
436
- // known to have been successful. It seems that - in error cases - the
437
- // cstore can be in a temporarily invalid state between cnum allocation
438
- // and dependency resolution and the verification code would produce
439
- // ICEs in that case (see #83045).
440
- self . verify_no_symbol_conflicts ( & crate_root) ?;
441
- self . verify_no_stable_crate_id_hash_conflicts ( & crate_root, cnum) ?;
442
-
443
410
let crate_metadata = CrateMetadata :: new (
444
411
self . sess ,
445
412
& self . cstore ,
0 commit comments