97
97
//! virtually impossible. Thus, symbol hash generation exclusively relies on
98
98
//! DefPaths which are much more robust in the face of changes to the code base.
99
99
100
- use rustc:: middle:: weak_lang_items;
101
- use rustc_data_structures:: stable_hasher:: { HashStable , StableHasher } ;
102
- use rustc_mir:: monomorphize:: Instance ;
103
- use rustc_mir:: monomorphize:: item:: { MonoItem , MonoItemExt , InstantiationMode } ;
104
100
use rustc:: hir:: def_id:: { DefId , LOCAL_CRATE } ;
105
101
use rustc:: hir:: map as hir_map;
102
+ use rustc:: hir:: map:: definitions:: DefPathData ;
106
103
use rustc:: ich:: NodeIdHashingMode ;
107
- use rustc:: ty :: { self , Ty , TyCtxt , TypeFoldable } ;
104
+ use rustc:: middle :: weak_lang_items ;
108
105
use rustc:: ty:: item_path:: { self , ItemPathBuffer , RootMode } ;
109
106
use rustc:: ty:: maps:: Providers ;
110
107
use rustc:: ty:: subst:: Substs ;
111
- use rustc:: hir :: map :: definitions :: DefPathData ;
108
+ use rustc:: ty :: { self , Ty , TyCtxt , TypeFoldable } ;
112
109
use rustc:: util:: common:: record_time;
110
+ use rustc_data_structures:: stable_hasher:: { HashStable , StableHasher } ;
111
+ use rustc_mir:: monomorphize:: item:: { InstantiationMode , MonoItem , MonoItemExt } ;
112
+ use rustc_mir:: monomorphize:: Instance ;
113
113
114
114
use syntax:: attr;
115
115
use syntax_pos:: symbol:: Symbol ;
@@ -125,25 +125,29 @@ pub fn provide(providers: &mut Providers) {
125
125
} ;
126
126
}
127
127
128
- fn get_symbol_hash < ' a , ' tcx > ( tcx : TyCtxt < ' a , ' tcx , ' tcx > ,
128
+ fn get_symbol_hash < ' a , ' tcx > (
129
+ tcx : TyCtxt < ' a , ' tcx , ' tcx > ,
129
130
130
- // the DefId of the item this name is for
131
- def_id : DefId ,
131
+ // the DefId of the item this name is for
132
+ def_id : DefId ,
132
133
133
- // instance this name will be for
134
- instance : Instance < ' tcx > ,
134
+ // instance this name will be for
135
+ instance : Instance < ' tcx > ,
135
136
136
- // type of the item, without any generic
137
- // parameters substituted; this is
138
- // included in the hash as a kind of
139
- // safeguard.
140
- item_type : Ty < ' tcx > ,
137
+ // type of the item, without any generic
138
+ // parameters substituted; this is
139
+ // included in the hash as a kind of
140
+ // safeguard.
141
+ item_type : Ty < ' tcx > ,
141
142
142
- // values for generic type parameters,
143
- // if any.
144
- substs : & ' tcx Substs < ' tcx > )
145
- -> u64 {
146
- debug ! ( "get_symbol_hash(def_id={:?}, parameters={:?})" , def_id, substs) ;
143
+ // values for generic type parameters,
144
+ // if any.
145
+ substs : & ' tcx Substs < ' tcx > ,
146
+ ) -> u64 {
147
+ debug ! (
148
+ "get_symbol_hash(def_id={:?}, parameters={:?})" ,
149
+ def_id, substs
150
+ ) ;
147
151
148
152
let mut hasher = StableHasher :: < u64 > :: new ( ) ;
149
153
let mut hcx = tcx. create_stable_hashing_context ( ) ;
@@ -193,20 +197,20 @@ fn get_symbol_hash<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
193
197
if !def_id. is_local ( ) && tcx. share_generics ( ) {
194
198
// If we are re-using a monomorphization from another crate,
195
199
// we have to compute the symbol hash accordingly.
196
- let upstream_monomorphizations =
197
- tcx. upstream_monomorphizations_for ( def_id) ;
200
+ let upstream_monomorphizations = tcx. upstream_monomorphizations_for ( def_id) ;
198
201
199
- upstream_monomorphizations. and_then ( |monos| monos . get ( & substs )
200
- . cloned ( ) )
201
- . unwrap_or ( LOCAL_CRATE )
202
+ upstream_monomorphizations
203
+ . and_then ( |monos| monos . get ( & substs ) . cloned ( ) )
204
+ . unwrap_or ( LOCAL_CRATE )
202
205
} else {
203
206
LOCAL_CRATE
204
207
}
205
208
} else {
206
209
LOCAL_CRATE
207
210
} ;
208
211
209
- ( & tcx. original_crate_name ( instantiating_crate) . as_str ( ) [ ..] ) . hash_stable ( & mut hcx, & mut hasher) ;
212
+ ( & tcx. original_crate_name ( instantiating_crate) . as_str ( ) [ ..] )
213
+ . hash_stable ( & mut hcx, & mut hasher) ;
210
214
( & tcx. crate_disambiguator ( instantiating_crate) ) . hash_stable ( & mut hcx, & mut hasher) ;
211
215
}
212
216
} ) ;
@@ -215,30 +219,25 @@ fn get_symbol_hash<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
215
219
hasher. finish ( )
216
220
}
217
221
218
- fn def_symbol_name < ' a , ' tcx > ( tcx : TyCtxt < ' a , ' tcx , ' tcx > , def_id : DefId )
219
- -> ty:: SymbolName
220
- {
222
+ fn def_symbol_name < ' a , ' tcx > ( tcx : TyCtxt < ' a , ' tcx , ' tcx > , def_id : DefId ) -> ty:: SymbolName {
221
223
let mut buffer = SymbolPathBuffer :: new ( ) ;
222
224
item_path:: with_forced_absolute_paths ( || {
223
225
tcx. push_item_path ( & mut buffer, def_id) ;
224
226
} ) ;
225
227
buffer. into_interned ( )
226
228
}
227
229
228
- fn symbol_name < ' a , ' tcx > ( tcx : TyCtxt < ' a , ' tcx , ' tcx > , instance : Instance < ' tcx > )
229
- -> ty:: SymbolName
230
- {
231
- ty :: SymbolName { name : Symbol :: intern ( & compute_symbol_name ( tcx , instance ) ) . as_interned_str ( ) }
230
+ fn symbol_name < ' a , ' tcx > ( tcx : TyCtxt < ' a , ' tcx , ' tcx > , instance : Instance < ' tcx > ) -> ty :: SymbolName {
231
+ ty:: SymbolName {
232
+ name : Symbol :: intern ( & compute_symbol_name ( tcx , instance ) ) . as_interned_str ( ) ,
233
+ }
232
234
}
233
235
234
- fn compute_symbol_name < ' a , ' tcx > ( tcx : TyCtxt < ' a , ' tcx , ' tcx > , instance : Instance < ' tcx > )
235
- -> String
236
- {
236
+ fn compute_symbol_name < ' a , ' tcx > ( tcx : TyCtxt < ' a , ' tcx , ' tcx > , instance : Instance < ' tcx > ) -> String {
237
237
let def_id = instance. def_id ( ) ;
238
238
let substs = instance. substs ;
239
239
240
- debug ! ( "symbol_name(def_id={:?}, substs={:?})" ,
241
- def_id, substs) ;
240
+ debug ! ( "symbol_name(def_id={:?}, substs={:?})" , def_id, substs) ;
242
241
243
242
let node_id = tcx. hir . as_local_node_id ( def_id) ;
244
243
@@ -258,7 +257,7 @@ fn compute_symbol_name<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, instance: Instance
258
257
let is_foreign = if let Some ( id) = node_id {
259
258
match tcx. hir . get ( id) {
260
259
hir_map:: NodeForeignItem ( _) => true ,
261
- _ => false
260
+ _ => false ,
262
261
}
263
262
} else {
264
263
tcx. is_foreign_item ( def_id)
@@ -295,8 +294,7 @@ fn compute_symbol_name<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, instance: Instance
295
294
loop {
296
295
let key = tcx. def_key ( ty_def_id) ;
297
296
match key. disambiguated_data . data {
298
- DefPathData :: TypeNs ( _) |
299
- DefPathData :: ValueNs ( _) => {
297
+ DefPathData :: TypeNs ( _) | DefPathData :: ValueNs ( _) => {
300
298
instance_ty = tcx. type_of ( ty_def_id) ;
301
299
break ;
302
300
}
@@ -305,8 +303,12 @@ fn compute_symbol_name<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, instance: Instance
305
303
// to be a value or type-def or something in there
306
304
// *somewhere*
307
305
ty_def_id. index = key. parent . unwrap_or_else ( || {
308
- bug ! ( "finding type for {:?}, encountered def-id {:?} with no \
309
- parent", def_id, ty_def_id) ;
306
+ bug ! (
307
+ "finding type for {:?}, encountered def-id {:?} with no \
308
+ parent",
309
+ def_id,
310
+ ty_def_id
311
+ ) ;
310
312
} ) ;
311
313
}
312
314
}
@@ -336,14 +338,14 @@ fn compute_symbol_name<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, instance: Instance
336
338
// use C++ name-mangling.
337
339
struct SymbolPathBuffer {
338
340
result : String ,
339
- temp_buf : String
341
+ temp_buf : String ,
340
342
}
341
343
342
344
impl SymbolPathBuffer {
343
345
fn new ( ) -> Self {
344
346
let mut result = SymbolPathBuffer {
345
347
result : String :: with_capacity ( 64 ) ,
346
- temp_buf : String :: with_capacity ( 16 )
348
+ temp_buf : String :: with_capacity ( 16 ) ,
347
349
} ;
348
350
result. result . push_str ( "_ZN" ) ; // _Z == Begin name-sequence, N == nested
349
351
result
@@ -352,14 +354,16 @@ impl SymbolPathBuffer {
352
354
fn from_interned ( symbol : ty:: SymbolName ) -> Self {
353
355
let mut result = SymbolPathBuffer {
354
356
result : String :: with_capacity ( 64 ) ,
355
- temp_buf : String :: with_capacity ( 16 )
357
+ temp_buf : String :: with_capacity ( 16 ) ,
356
358
} ;
357
359
result. result . push_str ( & symbol. name . as_str ( ) ) ;
358
360
result
359
361
}
360
362
361
363
fn into_interned ( self ) -> ty:: SymbolName {
362
- ty:: SymbolName { name : Symbol :: intern ( & self . result ) . as_interned_str ( ) }
364
+ ty:: SymbolName {
365
+ name : Symbol :: intern ( & self . result ) . as_interned_str ( ) ,
366
+ }
363
367
}
364
368
365
369
fn finish ( mut self , hash : u64 ) -> String {
@@ -378,7 +382,11 @@ impl ItemPathBuffer for SymbolPathBuffer {
378
382
fn push ( & mut self , text : & str ) {
379
383
self . temp_buf . clear ( ) ;
380
384
let need_underscore = sanitize ( & mut self . temp_buf , text) ;
381
- let _ = write ! ( self . result, "{}" , self . temp_buf. len( ) + ( need_underscore as usize ) ) ;
385
+ let _ = write ! (
386
+ self . result,
387
+ "{}" ,
388
+ self . temp_buf. len( ) + ( need_underscore as usize )
389
+ ) ;
382
390
if need_underscore {
383
391
self . result . push ( '_' ) ;
384
392
}
@@ -409,16 +417,13 @@ pub fn sanitize(result: &mut String, s: &str) -> bool {
409
417
'-' | ':' => result. push ( '.' ) ,
410
418
411
419
// These are legal symbols
412
- 'a' ... 'z'
413
- | 'A' ... 'Z'
414
- | '0' ... '9'
415
- | '_' | '.' | '$' => result. push ( c) ,
420
+ 'a' ...'z' | 'A' ...'Z' | '0' ...'9' | '_' | '.' | '$' => result. push ( c) ,
416
421
417
422
_ => {
418
423
result. push ( '$' ) ;
419
424
for c in c. escape_unicode ( ) . skip ( 1 ) {
420
425
match c {
421
- '{' => { } ,
426
+ '{' => { }
422
427
'}' => result. push ( '$' ) ,
423
428
c => result. push ( c) ,
424
429
}
@@ -428,7 +433,6 @@ pub fn sanitize(result: &mut String, s: &str) -> bool {
428
433
}
429
434
430
435
// Underscore-qualify anything that didn't start as an ident.
431
- !result. is_empty ( ) &&
432
- result. as_bytes ( ) [ 0 ] != '_' as u8 &&
433
- ! ( result. as_bytes ( ) [ 0 ] as char ) . is_xid_start ( )
436
+ !result. is_empty ( ) && result. as_bytes ( ) [ 0 ] != '_' as u8
437
+ && !( result. as_bytes ( ) [ 0 ] as char ) . is_xid_start ( )
434
438
}
0 commit comments