Skip to content

Commit 9901861

Browse files
committed
Code structure edits.
1 parent 74d37ac commit 9901861

File tree

1 file changed

+61
-57
lines changed

1 file changed

+61
-57
lines changed

src/librustc_trans_utils/symbol_names.rs

Lines changed: 61 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -97,19 +97,19 @@
9797
//! virtually impossible. Thus, symbol hash generation exclusively relies on
9898
//! DefPaths which are much more robust in the face of changes to the code base.
9999
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};
104100
use rustc::hir::def_id::{DefId, LOCAL_CRATE};
105101
use rustc::hir::map as hir_map;
102+
use rustc::hir::map::definitions::DefPathData;
106103
use rustc::ich::NodeIdHashingMode;
107-
use rustc::ty::{self, Ty, TyCtxt, TypeFoldable};
104+
use rustc::middle::weak_lang_items;
108105
use rustc::ty::item_path::{self, ItemPathBuffer, RootMode};
109106
use rustc::ty::maps::Providers;
110107
use rustc::ty::subst::Substs;
111-
use rustc::hir::map::definitions::DefPathData;
108+
use rustc::ty::{self, Ty, TyCtxt, TypeFoldable};
112109
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;
113113

114114
use syntax::attr;
115115
use syntax_pos::symbol::Symbol;
@@ -125,25 +125,29 @@ pub fn provide(providers: &mut Providers) {
125125
};
126126
}
127127

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>,
129130

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,
132133

133-
// instance this name will be for
134-
instance: Instance<'tcx>,
134+
// instance this name will be for
135+
instance: Instance<'tcx>,
135136

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>,
141142

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+
);
147151

148152
let mut hasher = StableHasher::<u64>::new();
149153
let mut hcx = tcx.create_stable_hashing_context();
@@ -193,20 +197,20 @@ fn get_symbol_hash<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
193197
if !def_id.is_local() && tcx.share_generics() {
194198
// If we are re-using a monomorphization from another crate,
195199
// 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);
198201

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)
202205
} else {
203206
LOCAL_CRATE
204207
}
205208
} else {
206209
LOCAL_CRATE
207210
};
208211

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);
210214
(&tcx.crate_disambiguator(instantiating_crate)).hash_stable(&mut hcx, &mut hasher);
211215
}
212216
});
@@ -215,30 +219,25 @@ fn get_symbol_hash<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
215219
hasher.finish()
216220
}
217221

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 {
221223
let mut buffer = SymbolPathBuffer::new();
222224
item_path::with_forced_absolute_paths(|| {
223225
tcx.push_item_path(&mut buffer, def_id);
224226
});
225227
buffer.into_interned()
226228
}
227229

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+
}
232234
}
233235

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 {
237237
let def_id = instance.def_id();
238238
let substs = instance.substs;
239239

240-
debug!("symbol_name(def_id={:?}, substs={:?})",
241-
def_id, substs);
240+
debug!("symbol_name(def_id={:?}, substs={:?})", def_id, substs);
242241

243242
let node_id = tcx.hir.as_local_node_id(def_id);
244243

@@ -258,7 +257,7 @@ fn compute_symbol_name<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, instance: Instance
258257
let is_foreign = if let Some(id) = node_id {
259258
match tcx.hir.get(id) {
260259
hir_map::NodeForeignItem(_) => true,
261-
_ => false
260+
_ => false,
262261
}
263262
} else {
264263
tcx.is_foreign_item(def_id)
@@ -295,8 +294,7 @@ fn compute_symbol_name<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, instance: Instance
295294
loop {
296295
let key = tcx.def_key(ty_def_id);
297296
match key.disambiguated_data.data {
298-
DefPathData::TypeNs(_) |
299-
DefPathData::ValueNs(_) => {
297+
DefPathData::TypeNs(_) | DefPathData::ValueNs(_) => {
300298
instance_ty = tcx.type_of(ty_def_id);
301299
break;
302300
}
@@ -305,8 +303,12 @@ fn compute_symbol_name<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, instance: Instance
305303
// to be a value or type-def or something in there
306304
// *somewhere*
307305
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+
);
310312
});
311313
}
312314
}
@@ -336,14 +338,14 @@ fn compute_symbol_name<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, instance: Instance
336338
// use C++ name-mangling.
337339
struct SymbolPathBuffer {
338340
result: String,
339-
temp_buf: String
341+
temp_buf: String,
340342
}
341343

342344
impl SymbolPathBuffer {
343345
fn new() -> Self {
344346
let mut result = SymbolPathBuffer {
345347
result: String::with_capacity(64),
346-
temp_buf: String::with_capacity(16)
348+
temp_buf: String::with_capacity(16),
347349
};
348350
result.result.push_str("_ZN"); // _Z == Begin name-sequence, N == nested
349351
result
@@ -352,14 +354,16 @@ impl SymbolPathBuffer {
352354
fn from_interned(symbol: ty::SymbolName) -> Self {
353355
let mut result = SymbolPathBuffer {
354356
result: String::with_capacity(64),
355-
temp_buf: String::with_capacity(16)
357+
temp_buf: String::with_capacity(16),
356358
};
357359
result.result.push_str(&symbol.name.as_str());
358360
result
359361
}
360362

361363
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+
}
363367
}
364368

365369
fn finish(mut self, hash: u64) -> String {
@@ -378,7 +382,11 @@ impl ItemPathBuffer for SymbolPathBuffer {
378382
fn push(&mut self, text: &str) {
379383
self.temp_buf.clear();
380384
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+
);
382390
if need_underscore {
383391
self.result.push('_');
384392
}
@@ -409,16 +417,13 @@ pub fn sanitize(result: &mut String, s: &str) -> bool {
409417
'-' | ':' => result.push('.'),
410418

411419
// 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),
416421

417422
_ => {
418423
result.push('$');
419424
for c in c.escape_unicode().skip(1) {
420425
match c {
421-
'{' => {},
426+
'{' => {}
422427
'}' => result.push('$'),
423428
c => result.push(c),
424429
}
@@ -428,7 +433,6 @@ pub fn sanitize(result: &mut String, s: &str) -> bool {
428433
}
429434

430435
// 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()
434438
}

0 commit comments

Comments
 (0)