@@ -37,7 +37,7 @@ use rustc_session::config::{CrateType, OptLevel};
3737use rustc_session:: cstore:: { ForeignModule , LinkagePreference , NativeLib } ;
3838use rustc_span:: hygiene:: { ExpnIndex , HygieneEncodeContext , MacroKind } ;
3939use rustc_span:: symbol:: { sym, Symbol } ;
40- use rustc_span:: { self , ExternalSource , FileName , SourceFile , Span , SyntaxContext } ;
40+ use rustc_span:: { self , ExternalSource , FileName , SourceFile , Span , SpanData , SyntaxContext } ;
4141use std:: borrow:: Borrow ;
4242use std:: collections:: hash_map:: Entry ;
4343use std:: hash:: Hash ;
@@ -53,6 +53,7 @@ pub(super) struct EncodeContext<'a, 'tcx> {
5353 tables : TableBuilders ,
5454
5555 lazy_state : LazyState ,
56+ span_shorthands : FxHashMap < Span , usize > ,
5657 type_shorthands : FxHashMap < Ty < ' tcx > , usize > ,
5758 predicate_shorthands : FxHashMap < ty:: PredicateKind < ' tcx > , usize > ,
5859
@@ -177,8 +178,20 @@ impl<'a, 'tcx> Encodable<EncodeContext<'a, 'tcx>> for ExpnId {
177178
178179impl < ' a , ' tcx > Encodable < EncodeContext < ' a , ' tcx > > for Span {
179180 fn encode ( & self , s : & mut EncodeContext < ' a , ' tcx > ) {
180- let span = self . data ( ) ;
181+ match s. span_shorthands . entry ( * self ) {
182+ Entry :: Occupied ( o) => SpanEncodingMode :: Shorthand ( * o. get ( ) ) . encode ( s) ,
183+ Entry :: Vacant ( v) => {
184+ let position = s. opaque . position ( ) ;
185+ v. insert ( position) ;
186+ SpanEncodingMode :: Direct . encode ( s) ;
187+ self . data ( ) . encode ( s) ;
188+ }
189+ }
190+ }
191+ }
181192
193+ impl < ' a , ' tcx > Encodable < EncodeContext < ' a , ' tcx > > for SpanData {
194+ fn encode ( & self , s : & mut EncodeContext < ' a , ' tcx > ) {
182195 // Don't serialize any `SyntaxContext`s from a proc-macro crate,
183196 // since we don't load proc-macro dependencies during serialization.
184197 // This means that any hygiene information from macros used *within*
@@ -213,26 +226,26 @@ impl<'a, 'tcx> Encodable<EncodeContext<'a, 'tcx>> for Span {
213226 if s. is_proc_macro {
214227 SyntaxContext :: root ( ) . encode ( s) ;
215228 } else {
216- span . ctxt . encode ( s) ;
229+ self . ctxt . encode ( s) ;
217230 }
218231
219232 if self . is_dummy ( ) {
220233 return TAG_PARTIAL_SPAN . encode ( s) ;
221234 }
222235
223236 // The Span infrastructure should make sure that this invariant holds:
224- debug_assert ! ( span . lo <= span . hi) ;
237+ debug_assert ! ( self . lo <= self . hi) ;
225238
226- if !s. source_file_cache . 0 . contains ( span . lo ) {
239+ if !s. source_file_cache . 0 . contains ( self . lo ) {
227240 let source_map = s. tcx . sess . source_map ( ) ;
228- let source_file_index = source_map. lookup_source_file_idx ( span . lo ) ;
241+ let source_file_index = source_map. lookup_source_file_idx ( self . lo ) ;
229242 s. source_file_cache =
230243 ( source_map. files ( ) [ source_file_index] . clone ( ) , source_file_index) ;
231244 }
232245 let ( ref source_file, source_file_index) = s. source_file_cache ;
233- debug_assert ! ( source_file. contains( span . lo) ) ;
246+ debug_assert ! ( source_file. contains( self . lo) ) ;
234247
235- if !source_file. contains ( span . hi ) {
248+ if !source_file. contains ( self . hi ) {
236249 // Unfortunately, macro expansion still sometimes generates Spans
237250 // that malformed in this way.
238251 return TAG_PARTIAL_SPAN . encode ( s) ;
@@ -286,11 +299,11 @@ impl<'a, 'tcx> Encodable<EncodeContext<'a, 'tcx>> for Span {
286299
287300 // Encode the start position relative to the file start, so we profit more from the
288301 // variable-length integer encoding.
289- let lo = span . lo - source_file. start_pos ;
302+ let lo = self . lo - source_file. start_pos ;
290303
291304 // Encode length which is usually less than span.hi and profits more
292305 // from the variable-length integer encoding that we use.
293- let len = span . hi - span . lo ;
306+ let len = self . hi - self . lo ;
294307
295308 tag. encode ( s) ;
296309 lo. encode ( s) ;
@@ -2182,6 +2195,7 @@ fn encode_metadata_impl(tcx: TyCtxt<'_>, path: &Path) {
21822195 feat : tcx. features ( ) ,
21832196 tables : Default :: default ( ) ,
21842197 lazy_state : LazyState :: NoNode ,
2198+ span_shorthands : Default :: default ( ) ,
21852199 type_shorthands : Default :: default ( ) ,
21862200 predicate_shorthands : Default :: default ( ) ,
21872201 source_file_cache,
0 commit comments