@@ -2,25 +2,23 @@ use crate::hir;
22use crate :: hir:: def_id:: { DefId , DefIndex } ;
33use crate :: hir:: map:: DefPathHash ;
44use crate :: hir:: map:: definitions:: Definitions ;
5- use crate :: ich:: { self , CachingSourceMapView , Fingerprint } ;
5+ use crate :: ich:: { self , CachingSourceMapView } ;
66use crate :: middle:: cstore:: CrateStore ;
77use crate :: ty:: { TyCtxt , fast_reject} ;
88use crate :: session:: Session ;
99
1010use std:: cmp:: Ord ;
11- use std:: hash as std_hash;
12- use std:: cell:: RefCell ;
1311
1412use syntax:: ast;
1513use syntax:: source_map:: SourceMap ;
1614use syntax:: symbol:: Symbol ;
17- use syntax_pos:: { Span , DUMMY_SP } ;
18- use syntax_pos:: hygiene:: { self , SyntaxContext } ;
15+ use syntax_pos:: { SourceFile , BytePos } ;
1916
2017use rustc_data_structures:: stable_hasher:: {
2118 HashStable , StableHasher , ToStableHashKey ,
2219} ;
2320use rustc_data_structures:: fx:: { FxHashSet , FxHashMap } ;
21+ use rustc_data_structures:: sync:: Lrc ;
2422use smallvec:: SmallVec ;
2523
2624fn compute_ignored_attr_names ( ) -> FxHashSet < Symbol > {
@@ -281,85 +279,14 @@ impl<'a> ToStableHashKey<StableHashingContext<'a>> for ast::NodeId {
281279}
282280
283281impl < ' a > syntax_pos:: HashStableContext for StableHashingContext < ' a > {
284- /// Hashes a span in a stable way. We can't directly hash the span's `BytePos`
285- /// fields (that would be similar to hashing pointers, since those are just
286- /// offsets into the `SourceMap`). Instead, we hash the (file name, line, column)
287- /// triple, which stays the same even if the containing `SourceFile` has moved
288- /// within the `SourceMap`.
289- /// Also note that we are hashing byte offsets for the column, not unicode
290- /// codepoint offsets. For the purpose of the hash that's sufficient.
291- /// Also, hashing filenames is expensive so we avoid doing it twice when the
292- /// span starts and ends in the same file, which is almost always the case.
293- fn hash_stable_span ( & mut self , span : & Span , hasher : & mut StableHasher ) {
294- const TAG_VALID_SPAN : u8 = 0 ;
295- const TAG_INVALID_SPAN : u8 = 1 ;
296- const TAG_EXPANSION : u8 = 0 ;
297- const TAG_NO_EXPANSION : u8 = 1 ;
298-
299- if !self . hash_spans {
300- return
301- }
302-
303- if * span == DUMMY_SP {
304- return std_hash:: Hash :: hash ( & TAG_INVALID_SPAN , hasher) ;
305- }
306-
307- // If this is not an empty or invalid span, we want to hash the last
308- // position that belongs to it, as opposed to hashing the first
309- // position past it.
310- let span = span. data ( ) ;
311- let ( file_lo, line_lo, col_lo) = match self . source_map ( )
312- . byte_pos_to_line_and_col ( span. lo ) {
313- Some ( pos) => pos,
314- None => {
315- return std_hash:: Hash :: hash ( & TAG_INVALID_SPAN , hasher) ;
316- }
317- } ;
318-
319- if !file_lo. contains ( span. hi ) {
320- return std_hash:: Hash :: hash ( & TAG_INVALID_SPAN , hasher) ;
321- }
322-
323- std_hash:: Hash :: hash ( & TAG_VALID_SPAN , hasher) ;
324- // We truncate the stable ID hash and line and column numbers. The chances
325- // of causing a collision this way should be minimal.
326- std_hash:: Hash :: hash ( & ( file_lo. name_hash as u64 ) , hasher) ;
327-
328- let col = ( col_lo. 0 as u64 ) & 0xFF ;
329- let line = ( ( line_lo as u64 ) & 0xFF_FF_FF ) << 8 ;
330- let len = ( ( span. hi - span. lo ) . 0 as u64 ) << 32 ;
331- let line_col_len = col | line | len;
332- std_hash:: Hash :: hash ( & line_col_len, hasher) ;
333-
334- if span. ctxt == SyntaxContext :: root ( ) {
335- TAG_NO_EXPANSION . hash_stable ( self , hasher) ;
336- } else {
337- TAG_EXPANSION . hash_stable ( self , hasher) ;
338-
339- // Since the same expansion context is usually referenced many
340- // times, we cache a stable hash of it and hash that instead of
341- // recursing every time.
342- thread_local ! {
343- static CACHE : RefCell <FxHashMap <hygiene:: ExpnId , u64 >> = Default :: default ( ) ;
344- }
345-
346- let sub_hash: u64 = CACHE . with ( |cache| {
347- let expn_id = span. ctxt . outer_expn ( ) ;
348-
349- if let Some ( & sub_hash) = cache. borrow ( ) . get ( & expn_id) {
350- return sub_hash;
351- }
352-
353- let mut hasher = StableHasher :: new ( ) ;
354- expn_id. expn_data ( ) . hash_stable ( self , & mut hasher) ;
355- let sub_hash: Fingerprint = hasher. finish ( ) ;
356- let sub_hash = sub_hash. to_smaller_hash ( ) ;
357- cache. borrow_mut ( ) . insert ( expn_id, sub_hash) ;
358- sub_hash
359- } ) ;
282+ fn hash_spans ( & self ) -> bool {
283+ self . hash_spans
284+ }
360285
361- sub_hash. hash_stable ( self , hasher) ;
362- }
286+ fn byte_pos_to_line_and_col ( & mut self , byte : BytePos )
287+ -> Option < ( Lrc < SourceFile > , usize , BytePos ) >
288+ {
289+ self . source_map ( ) . byte_pos_to_line_and_col ( byte)
363290 }
364291}
365292
0 commit comments