@@ -37,7 +37,7 @@ use rustc_session::config::{CrateType, OptLevel};
37
37
use rustc_session:: cstore:: { ForeignModule , LinkagePreference , NativeLib } ;
38
38
use rustc_span:: hygiene:: { ExpnIndex , HygieneEncodeContext , MacroKind } ;
39
39
use 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 } ;
41
41
use std:: borrow:: Borrow ;
42
42
use std:: collections:: hash_map:: Entry ;
43
43
use std:: hash:: Hash ;
@@ -53,6 +53,7 @@ pub(super) struct EncodeContext<'a, 'tcx> {
53
53
tables : TableBuilders ,
54
54
55
55
lazy_state : LazyState ,
56
+ span_shorthands : FxHashMap < Span , usize > ,
56
57
type_shorthands : FxHashMap < Ty < ' tcx > , usize > ,
57
58
predicate_shorthands : FxHashMap < ty:: PredicateKind < ' tcx > , usize > ,
58
59
@@ -177,8 +178,20 @@ impl<'a, 'tcx> Encodable<EncodeContext<'a, 'tcx>> for ExpnId {
177
178
178
179
impl < ' a , ' tcx > Encodable < EncodeContext < ' a , ' tcx > > for Span {
179
180
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
+ }
181
192
193
+ impl < ' a , ' tcx > Encodable < EncodeContext < ' a , ' tcx > > for SpanData {
194
+ fn encode ( & self , s : & mut EncodeContext < ' a , ' tcx > ) {
182
195
// Don't serialize any `SyntaxContext`s from a proc-macro crate,
183
196
// since we don't load proc-macro dependencies during serialization.
184
197
// This means that any hygiene information from macros used *within*
@@ -213,26 +226,26 @@ impl<'a, 'tcx> Encodable<EncodeContext<'a, 'tcx>> for Span {
213
226
if s. is_proc_macro {
214
227
SyntaxContext :: root ( ) . encode ( s) ;
215
228
} else {
216
- span . ctxt . encode ( s) ;
229
+ self . ctxt . encode ( s) ;
217
230
}
218
231
219
232
if self . is_dummy ( ) {
220
233
return TAG_PARTIAL_SPAN . encode ( s) ;
221
234
}
222
235
223
236
// The Span infrastructure should make sure that this invariant holds:
224
- debug_assert ! ( span . lo <= span . hi) ;
237
+ debug_assert ! ( self . lo <= self . hi) ;
225
238
226
- if !s. source_file_cache . 0 . contains ( span . lo ) {
239
+ if !s. source_file_cache . 0 . contains ( self . lo ) {
227
240
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 ) ;
229
242
s. source_file_cache =
230
243
( source_map. files ( ) [ source_file_index] . clone ( ) , source_file_index) ;
231
244
}
232
245
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) ) ;
234
247
235
- if !source_file. contains ( span . hi ) {
248
+ if !source_file. contains ( self . hi ) {
236
249
// Unfortunately, macro expansion still sometimes generates Spans
237
250
// that malformed in this way.
238
251
return TAG_PARTIAL_SPAN . encode ( s) ;
@@ -286,11 +299,11 @@ impl<'a, 'tcx> Encodable<EncodeContext<'a, 'tcx>> for Span {
286
299
287
300
// Encode the start position relative to the file start, so we profit more from the
288
301
// variable-length integer encoding.
289
- let lo = span . lo - source_file. start_pos ;
302
+ let lo = self . lo - source_file. start_pos ;
290
303
291
304
// Encode length which is usually less than span.hi and profits more
292
305
// from the variable-length integer encoding that we use.
293
- let len = span . hi - span . lo ;
306
+ let len = self . hi - self . lo ;
294
307
295
308
tag. encode ( s) ;
296
309
lo. encode ( s) ;
@@ -2182,6 +2195,7 @@ fn encode_metadata_impl(tcx: TyCtxt<'_>, path: &Path) {
2182
2195
feat : tcx. features ( ) ,
2183
2196
tables : Default :: default ( ) ,
2184
2197
lazy_state : LazyState :: NoNode ,
2198
+ span_shorthands : Default :: default ( ) ,
2185
2199
type_shorthands : Default :: default ( ) ,
2186
2200
predicate_shorthands : Default :: default ( ) ,
2187
2201
source_file_cache,
0 commit comments