1
1
//! Validates all used crates and extern libraries and loads their metadata
2
2
3
- use crate :: cstore:: { self , CStore , MetadataBlob } ;
4
- use crate :: locator:: { self , CratePaths } ;
5
- use crate :: rmeta:: { CrateRoot , CrateDep } ;
6
- use rustc_data_structures:: sync:: { Lock , Once , AtomicCell } ;
3
+ use crate :: cstore:: CStore ;
4
+ use crate :: locator:: { CrateLocator , CratePaths } ;
5
+ use crate :: rmeta:: { CrateMetadata , CrateNumMap , CrateRoot , CrateDep , MetadataBlob } ;
7
6
8
7
use rustc:: hir:: def_id:: CrateNum ;
9
8
use rustc_data_structures:: svh:: Svh ;
10
- use rustc:: dep_graph:: DepNodeIndex ;
11
9
use rustc:: middle:: cstore:: DepKind ;
12
- use rustc:: mir:: interpret:: AllocDecodingState ;
13
10
use rustc:: session:: { Session , CrateDisambiguator } ;
14
11
use rustc:: session:: config:: { Sanitizer , self } ;
15
12
use rustc_target:: spec:: { PanicStrategy , TargetTriple } ;
16
13
use rustc:: session:: search_paths:: PathKind ;
17
14
use rustc:: middle:: cstore:: { CrateSource , ExternCrate , ExternCrateSource , MetadataLoaderDyn } ;
18
- use rustc:: util:: common:: record_time;
19
15
use rustc:: util:: nodemap:: FxHashSet ;
20
16
use rustc:: hir:: map:: Definitions ;
21
17
use rustc:: hir:: def_id:: LOCAL_CRATE ;
@@ -50,9 +46,9 @@ pub struct CrateLoader<'a> {
50
46
51
47
fn dump_crates ( cstore : & CStore ) {
52
48
info ! ( "resolved crates:" ) ;
53
- cstore. iter_crate_data ( |_ , data| {
49
+ cstore. iter_crate_data ( |cnum , data| {
54
50
info ! ( " name: {}" , data. root. name) ;
55
- info ! ( " cnum: {}" , data . cnum) ;
51
+ info ! ( " cnum: {}" , cnum) ;
56
52
info ! ( " hash: {}" , data. root. hash) ;
57
53
info ! ( " reqd: {:?}" , * data. dep_kind. lock( ) ) ;
58
54
let CrateSource { dylib, rlib, rmeta } = data. source . clone ( ) ;
@@ -68,13 +64,13 @@ enum LoadResult {
68
64
}
69
65
70
66
enum LoadError < ' a > {
71
- LocatorError ( locator :: Context < ' a > ) ,
67
+ LocatorError ( CrateLocator < ' a > ) ,
72
68
}
73
69
74
70
impl < ' a > LoadError < ' a > {
75
71
fn report ( self ) -> ! {
76
72
match self {
77
- LoadError :: LocatorError ( locate_ctxt ) => locate_ctxt . report_errs ( ) ,
73
+ LoadError :: LocatorError ( locator ) => locator . report_errs ( ) ,
78
74
}
79
75
}
80
76
}
@@ -145,7 +141,7 @@ impl<'a> CrateLoader<'a> {
145
141
let prev_kind = source. dylib . as_ref ( ) . or ( source. rlib . as_ref ( ) )
146
142
. or ( source. rmeta . as_ref ( ) )
147
143
. expect ( "No sources for crate" ) . 1 ;
148
- if ret . is_none ( ) && ( prev_kind == kind || prev_kind == PathKind :: All ) {
144
+ if kind . matches ( prev_kind) {
149
145
ret = Some ( cnum) ;
150
146
}
151
147
} ) ;
@@ -211,71 +207,52 @@ impl<'a> CrateLoader<'a> {
211
207
let root = if let Some ( root) = root {
212
208
root
213
209
} else {
214
- crate_paths = CratePaths { name : crate_root. name , source : source . clone ( ) } ;
210
+ crate_paths = CratePaths :: new ( crate_root. name , source. clone ( ) ) ;
215
211
& crate_paths
216
212
} ;
217
213
218
214
let cnum_map = self . resolve_crate_deps ( root, & crate_root, & metadata, cnum, span, dep_kind) ;
219
215
220
- let dependencies: Vec < CrateNum > = cnum_map. iter ( ) . cloned ( ) . collect ( ) ;
221
-
222
- let raw_proc_macros = crate_root. proc_macro_data . map ( |_| {
216
+ let raw_proc_macros = if crate_root. is_proc_macro_crate ( ) {
223
217
let temp_root;
224
218
let ( dlsym_source, dlsym_root) = match & host_lib {
225
219
Some ( host_lib) =>
226
220
( & host_lib. source , { temp_root = host_lib. metadata . get_root ( ) ; & temp_root } ) ,
227
221
None => ( & source, & crate_root) ,
228
222
} ;
229
223
let dlsym_dylib = dlsym_source. dylib . as_ref ( ) . expect ( "no dylib for a proc-macro crate" ) ;
230
- self . dlsym_proc_macros ( & dlsym_dylib. 0 , dlsym_root. disambiguator , span)
231
- } ) ;
232
-
233
- let interpret_alloc_index: Vec < u32 > = crate_root. interpret_alloc_index
234
- . decode ( & metadata)
235
- . collect ( ) ;
236
- let trait_impls = crate_root
237
- . impls
238
- . decode ( ( & metadata, self . sess ) )
239
- . map ( |trait_impls| ( trait_impls. trait_id , trait_impls. impls ) )
240
- . collect ( ) ;
241
-
242
- let def_path_table = record_time ( & self . sess . perf_stats . decode_def_path_tables_time , || {
243
- crate_root. def_path_table . decode ( ( & metadata, self . sess ) )
244
- } ) ;
224
+ Some ( self . dlsym_proc_macros ( & dlsym_dylib. 0 , dlsym_root. disambiguator , span) )
225
+ } else {
226
+ None
227
+ } ;
245
228
246
- self . cstore . set_crate_data ( cnum, cstore:: CrateMetadata {
247
- extern_crate : Lock :: new ( None ) ,
248
- def_path_table,
249
- trait_impls,
250
- root : crate_root,
251
- host_hash,
252
- blob : metadata,
253
- cnum_map,
229
+ self . cstore . set_crate_data ( cnum, CrateMetadata :: new (
230
+ self . sess ,
231
+ metadata,
232
+ crate_root,
233
+ raw_proc_macros,
254
234
cnum,
255
- dependencies : Lock :: new ( dependencies) ,
256
- source_map_import_info : Once :: new ( ) ,
257
- alloc_decoding_state : AllocDecodingState :: new ( interpret_alloc_index) ,
258
- dep_kind : Lock :: new ( dep_kind) ,
235
+ cnum_map,
236
+ dep_kind,
259
237
source,
260
238
private_dep,
261
- raw_proc_macros,
262
- dep_node_index : AtomicCell :: new ( DepNodeIndex :: INVALID ) ,
263
- } ) ;
239
+ host_hash,
240
+ ) ) ;
264
241
265
242
cnum
266
243
}
267
244
268
245
fn load_proc_macro < ' b > (
269
246
& self ,
270
- locate_ctxt : & mut locator :: Context < ' b > ,
247
+ locator : & mut CrateLocator < ' b > ,
271
248
path_kind : PathKind ,
272
249
) -> Option < ( LoadResult , Option < Library > ) >
273
250
where
274
251
' a : ' b ,
275
252
{
276
- // Use a new locator Context so trying to load a proc macro doesn't affect the error
253
+ // Use a new crate locator so trying to load a proc macro doesn't affect the error
277
254
// message we emit
278
- let mut proc_macro_locator = locate_ctxt . clone ( ) ;
255
+ let mut proc_macro_locator = locator . clone ( ) ;
279
256
280
257
// Try to load a proc macro
281
258
proc_macro_locator. is_proc_macro = Some ( true ) ;
@@ -287,10 +264,10 @@ impl<'a> CrateLoader<'a> {
287
264
LoadResult :: Previous ( cnum) => return Some ( ( LoadResult :: Previous ( cnum) , None ) ) ,
288
265
LoadResult :: Loaded ( library) => Some ( LoadResult :: Loaded ( library) )
289
266
} ;
290
- locate_ctxt . hash = locate_ctxt . host_hash ;
291
- // Use the locate_ctxt when looking for the host proc macro crate, as that is required
267
+ locator . hash = locator . host_hash ;
268
+ // Use the locator when looking for the host proc macro crate, as that is required
292
269
// so we want it to affect the error message
293
- ( locate_ctxt , result)
270
+ ( locator , result)
294
271
} else {
295
272
( & mut proc_macro_locator, None )
296
273
} ;
@@ -350,37 +327,30 @@ impl<'a> CrateLoader<'a> {
350
327
( LoadResult :: Previous ( cnum) , None )
351
328
} else {
352
329
info ! ( "falling back to a load" ) ;
353
- let mut locate_ctxt = locator :: Context {
354
- sess : self . sess ,
355
- span ,
356
- crate_name : name,
330
+ let mut locator = CrateLocator :: new (
331
+ self . sess ,
332
+ self . metadata_loader ,
333
+ name,
357
334
hash,
358
335
host_hash,
359
336
extra_filename,
360
- filesearch : self . sess . target_filesearch ( path_kind ) ,
361
- target : & self . sess . target . target ,
362
- triple : self . sess . opts . target_triple . clone ( ) ,
337
+ false , // is_host
338
+ path_kind ,
339
+ span ,
363
340
root,
364
- rejected_via_hash : vec ! [ ] ,
365
- rejected_via_triple : vec ! [ ] ,
366
- rejected_via_kind : vec ! [ ] ,
367
- rejected_via_version : vec ! [ ] ,
368
- rejected_via_filename : vec ! [ ] ,
369
- should_match_name : true ,
370
- is_proc_macro : Some ( false ) ,
371
- metadata_loader : self . metadata_loader ,
372
- } ;
341
+ Some ( false ) , // is_proc_macro
342
+ ) ;
373
343
374
- self . load ( & mut locate_ctxt ) . map ( |r| ( r, None ) ) . or_else ( || {
344
+ self . load ( & mut locator ) . map ( |r| ( r, None ) ) . or_else ( || {
375
345
dep_kind = DepKind :: UnexportedMacrosOnly ;
376
- self . load_proc_macro ( & mut locate_ctxt , path_kind)
377
- } ) . ok_or_else ( move || LoadError :: LocatorError ( locate_ctxt ) ) ?
346
+ self . load_proc_macro ( & mut locator , path_kind)
347
+ } ) . ok_or_else ( move || LoadError :: LocatorError ( locator ) ) ?
378
348
} ;
379
349
380
350
match result {
381
351
( LoadResult :: Previous ( cnum) , None ) => {
382
352
let data = self . cstore . get_crate_data ( cnum) ;
383
- if data. root . proc_macro_data . is_some ( ) {
353
+ if data. root . is_proc_macro_crate ( ) {
384
354
dep_kind = DepKind :: UnexportedMacrosOnly ;
385
355
}
386
356
data. dep_kind . with_lock ( |data_dep_kind| {
@@ -395,8 +365,8 @@ impl<'a> CrateLoader<'a> {
395
365
}
396
366
}
397
367
398
- fn load ( & self , locate_ctxt : & mut locator :: Context < ' _ > ) -> Option < LoadResult > {
399
- let library = locate_ctxt . maybe_load_library_crate ( ) ?;
368
+ fn load ( & self , locator : & mut CrateLocator < ' _ > ) -> Option < LoadResult > {
369
+ let library = locator . maybe_load_library_crate ( ) ?;
400
370
401
371
// In the case that we're loading a crate, but not matching
402
372
// against a hash, we could load a crate which has the same hash
@@ -407,11 +377,11 @@ impl<'a> CrateLoader<'a> {
407
377
// don't want to match a host crate against an equivalent target one
408
378
// already loaded.
409
379
let root = library. metadata . get_root ( ) ;
410
- if locate_ctxt . triple == self . sess . opts . target_triple {
380
+ if locator . triple == self . sess . opts . target_triple {
411
381
let mut result = LoadResult :: Loaded ( library) ;
412
382
self . cstore . iter_crate_data ( |cnum, data| {
413
383
if data. root . name == root. name && root. hash == data. root . hash {
414
- assert ! ( locate_ctxt . hash. is_none( ) ) ;
384
+ assert ! ( locator . hash. is_none( ) ) ;
415
385
info ! ( "load success, going to previous cnum: {}" , cnum) ;
416
386
result = LoadResult :: Previous ( cnum) ;
417
387
}
@@ -471,16 +441,16 @@ impl<'a> CrateLoader<'a> {
471
441
krate : CrateNum ,
472
442
span : Span ,
473
443
dep_kind : DepKind )
474
- -> cstore :: CrateNumMap {
444
+ -> CrateNumMap {
475
445
debug ! ( "resolving deps of external crate" ) ;
476
- if crate_root. proc_macro_data . is_some ( ) {
477
- return cstore :: CrateNumMap :: new ( ) ;
446
+ if crate_root. is_proc_macro_crate ( ) {
447
+ return CrateNumMap :: new ( ) ;
478
448
}
479
449
480
450
// The map from crate numbers in the crate we're resolving to local crate numbers.
481
451
// We map 0 and all other holes in the map to our parent crate. The "additional"
482
452
// self-dependencies should be harmless.
483
- std:: iter:: once ( krate) . chain ( crate_root. crate_deps . decode ( metadata) . map ( |dep| {
453
+ std:: iter:: once ( krate) . chain ( crate_root. decode_crate_deps ( metadata) . map ( |dep| {
484
454
info ! ( "resolving dep crate {} hash: `{}` extra filename: `{}`" , dep. name, dep. hash,
485
455
dep. extra_filename) ;
486
456
if dep. kind == DepKind :: UnexportedMacrosOnly {
@@ -824,7 +794,7 @@ impl<'a> CrateLoader<'a> {
824
794
fn inject_dependency_if ( & self ,
825
795
krate : CrateNum ,
826
796
what : & str ,
827
- needs_dep : & dyn Fn ( & cstore :: CrateMetadata ) -> bool ) {
797
+ needs_dep : & dyn Fn ( & CrateMetadata ) -> bool ) {
828
798
// don't perform this validation if the session has errors, as one of
829
799
// those errors may indicate a circular dependency which could cause
830
800
// this to stack overflow.
0 commit comments