@@ -27,6 +27,7 @@ use rustc_middle::mir::interpret::{AllocDecodingSession, AllocDecodingState};
27
27
use rustc_middle:: mir:: { self , Body , Promoted } ;
28
28
use rustc_middle:: thir;
29
29
use rustc_middle:: ty:: codec:: TyDecoder ;
30
+ use rustc_middle:: ty:: fast_reject:: SimplifiedType ;
30
31
use rustc_middle:: ty:: { self , Ty , TyCtxt , Visibility } ;
31
32
use rustc_serialize:: { opaque, Decodable , Decoder } ;
32
33
use rustc_session:: cstore:: {
@@ -92,8 +93,7 @@ crate struct CrateMetadata {
92
93
/// Trait impl data.
93
94
/// FIXME: Used only from queries and can use query cache,
94
95
/// so pre-decoding can probably be avoided.
95
- trait_impls :
96
- FxHashMap < ( u32 , DefIndex ) , Lazy < [ ( DefIndex , Option < ty:: fast_reject:: SimplifiedType > ) ] > > ,
96
+ trait_impls : FxHashMap < ( u32 , DefIndex ) , Lazy < [ ( DefIndex , Option < SimplifiedType > ) ] > > ,
97
97
/// Proc macro descriptions for this crate, if it's a proc macro crate.
98
98
raw_proc_macros : Option < & ' static [ ProcMacro ] > ,
99
99
/// Source maps for code from the crate.
@@ -722,25 +722,24 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
722
722
& self . raw_proc_macros . unwrap ( ) [ pos]
723
723
}
724
724
725
- fn try_item_ident ( & self , item_index : DefIndex , sess : & Session ) -> Result < Ident , String > {
726
- let name = self
727
- . def_key ( item_index)
728
- . disambiguated_data
729
- . data
730
- . get_opt_name ( )
731
- . ok_or_else ( || format ! ( "Missing opt name for {:?}" , item_index) ) ?;
732
- let span = self
733
- . root
734
- . tables
735
- . ident_span
736
- . get ( self , item_index)
737
- . ok_or_else ( || format ! ( "Missing ident span for {:?} ({:?})" , name, item_index) ) ?
738
- . decode ( ( self , sess) ) ;
739
- Ok ( Ident :: new ( name, span) )
725
+ fn opt_item_ident ( & self , item_index : DefIndex , sess : & Session ) -> Option < Ident > {
726
+ let name = self . def_key ( item_index) . disambiguated_data . data . get_opt_name ( ) ?;
727
+ let span = match self . root . tables . ident_span . get ( self , item_index) {
728
+ Some ( lazy_span) => lazy_span. decode ( ( self , sess) ) ,
729
+ None => {
730
+ // FIXME: this weird case of a name with no span is specific to `extern crate`
731
+ // items, which are supposed to be treated like `use` items and only be encoded
732
+ // to metadata as `Export`s, return `None` because that's what all the callers
733
+ // expect in this case.
734
+ assert_eq ! ( self . def_kind( item_index) , DefKind :: ExternCrate ) ;
735
+ return None ;
736
+ }
737
+ } ;
738
+ Some ( Ident :: new ( name, span) )
740
739
}
741
740
742
741
fn item_ident ( & self , item_index : DefIndex , sess : & Session ) -> Ident {
743
- self . try_item_ident ( item_index, sess) . unwrap ( )
742
+ self . opt_item_ident ( item_index, sess) . expect ( "no encoded ident for item" )
744
743
}
745
744
746
745
fn maybe_kind ( & self , item_id : DefIndex ) -> Option < EntryKind > {
@@ -1102,27 +1101,19 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
1102
1101
// Iterate over all children.
1103
1102
if let Some ( children) = self . root . tables . children . get ( self , id) {
1104
1103
for child_index in children. decode ( ( self , sess) ) {
1105
- // FIXME: Merge with the logic below.
1106
- if let None | Some ( EntryKind :: ForeignMod | EntryKind :: Impl ( _) ) =
1107
- self . maybe_kind ( child_index)
1108
- {
1109
- continue ;
1110
- }
1111
-
1112
- let def_key = self . def_key ( child_index) ;
1113
- if def_key. disambiguated_data . data . get_opt_name ( ) . is_some ( ) {
1114
- let span = self . get_span ( child_index, sess) ;
1104
+ if let Some ( ident) = self . opt_item_ident ( child_index, sess) {
1115
1105
let kind = self . def_kind ( child_index) ;
1116
- let ident = self . item_ident ( child_index, sess) ;
1117
- let vis = self . get_visibility ( child_index) ;
1106
+ if matches ! ( kind, DefKind :: Macro ( ..) ) {
1107
+ // FIXME: Macros are currently encoded twice, once as items and once as
1108
+ // reexports. We ignore the items here and only use the reexports.
1109
+ continue ;
1110
+ }
1118
1111
let def_id = self . local_def_id ( child_index) ;
1119
1112
let res = Res :: Def ( kind, def_id) ;
1113
+ let vis = self . get_visibility ( child_index) ;
1114
+ let span = self . get_span ( child_index, sess) ;
1120
1115
1121
- // FIXME: Macros are currently encoded twice, once as items and once as
1122
- // reexports. We ignore the items here and only use the reexports.
1123
- if !matches ! ( kind, DefKind :: Macro ( ..) ) {
1124
- callback ( Export { res, ident, vis, span } ) ;
1125
- }
1116
+ callback ( Export { ident, res, vis, span } ) ;
1126
1117
1127
1118
// For non-re-export structs and variants add their constructors to children.
1128
1119
// Re-export lists automatically contain constructors when necessary.
@@ -1309,24 +1300,26 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
1309
1300
1310
1301
fn get_item_attrs (
1311
1302
& ' a self ,
1312
- node_id : DefIndex ,
1303
+ id : DefIndex ,
1313
1304
sess : & ' a Session ,
1314
1305
) -> impl Iterator < Item = ast:: Attribute > + ' a {
1315
- // The attributes for a tuple struct/variant are attached to the definition, not the ctor;
1316
- // we assume that someone passing in a tuple struct ctor is actually wanting to
1317
- // look at the definition
1318
- let def_key = self . def_key ( node_id) ;
1319
- let item_id = if def_key. disambiguated_data . data == DefPathData :: Ctor {
1320
- def_key. parent . unwrap ( )
1321
- } else {
1322
- node_id
1323
- } ;
1324
-
1325
1306
self . root
1326
1307
. tables
1327
1308
. attributes
1328
- . get ( self , item_id)
1329
- . unwrap_or_else ( Lazy :: empty)
1309
+ . get ( self , id)
1310
+ . unwrap_or_else ( || {
1311
+ // Structure and variant constructors don't have any attributes encoded for them,
1312
+ // but we assume that someone passing a constructor ID actually wants to look at
1313
+ // the attributes on the corresponding struct or variant.
1314
+ let def_key = self . def_key ( id) ;
1315
+ assert_eq ! ( def_key. disambiguated_data. data, DefPathData :: Ctor ) ;
1316
+ let parent_id = def_key. parent . expect ( "no parent for a constructor" ) ;
1317
+ self . root
1318
+ . tables
1319
+ . attributes
1320
+ . get ( self , parent_id)
1321
+ . expect ( "no encoded attributes for a structure or variant" )
1322
+ } )
1330
1323
. decode ( ( self , sess) )
1331
1324
}
1332
1325
@@ -1372,39 +1365,39 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
1372
1365
self . root . traits . decode ( self ) . map ( |index| self . local_def_id ( index) )
1373
1366
}
1374
1367
1375
- fn get_implementations_for_trait (
1368
+ fn get_trait_impls ( & ' a self ) -> impl Iterator < Item = ( DefId , Option < SimplifiedType > ) > + ' a {
1369
+ self . trait_impls . values ( ) . flat_map ( move |impls| {
1370
+ impls
1371
+ . decode ( self )
1372
+ . map ( |( idx, simplified_self_ty) | ( self . local_def_id ( idx) , simplified_self_ty) )
1373
+ } )
1374
+ }
1375
+
1376
+ fn get_implementations_of_trait (
1376
1377
& self ,
1377
1378
tcx : TyCtxt < ' tcx > ,
1378
- filter : Option < DefId > ,
1379
- ) -> & ' tcx [ ( DefId , Option < ty :: fast_reject :: SimplifiedType > ) ] {
1379
+ trait_def_id : DefId ,
1380
+ ) -> & ' tcx [ ( DefId , Option < SimplifiedType > ) ] {
1380
1381
if self . root . is_proc_macro_crate ( ) {
1381
1382
// proc-macro crates export no trait impls.
1382
1383
return & [ ] ;
1383
1384
}
1384
1385
1385
- if let Some ( def_id) = filter {
1386
- // Do a reverse lookup beforehand to avoid touching the crate_num
1387
- // hash map in the loop below.
1388
- let filter = match self . reverse_translate_def_id ( def_id) {
1389
- Some ( def_id) => ( def_id. krate . as_u32 ( ) , def_id. index ) ,
1390
- None => return & [ ] ,
1391
- } ;
1386
+ // Do a reverse lookup beforehand to avoid touching the crate_num
1387
+ // hash map in the loop below.
1388
+ let key = match self . reverse_translate_def_id ( trait_def_id) {
1389
+ Some ( def_id) => ( def_id. krate . as_u32 ( ) , def_id. index ) ,
1390
+ None => return & [ ] ,
1391
+ } ;
1392
1392
1393
- if let Some ( impls) = self . trait_impls . get ( & filter) {
1394
- tcx. arena . alloc_from_iter (
1395
- impls. decode ( self ) . map ( |( idx, simplified_self_ty) | {
1396
- ( self . local_def_id ( idx) , simplified_self_ty)
1397
- } ) ,
1398
- )
1399
- } else {
1400
- & [ ]
1401
- }
1402
- } else {
1403
- tcx. arena . alloc_from_iter ( self . trait_impls . values ( ) . flat_map ( |impls| {
1393
+ if let Some ( impls) = self . trait_impls . get ( & key) {
1394
+ tcx. arena . alloc_from_iter (
1404
1395
impls
1405
1396
. decode ( self )
1406
- . map ( |( idx, simplified_self_ty) | ( self . local_def_id ( idx) , simplified_self_ty) )
1407
- } ) )
1397
+ . map ( |( idx, simplified_self_ty) | ( self . local_def_id ( idx) , simplified_self_ty) ) ,
1398
+ )
1399
+ } else {
1400
+ & [ ]
1408
1401
}
1409
1402
}
1410
1403
0 commit comments