@@ -273,25 +273,23 @@ impl<'a, 'tcx> SpecializedDecoder<Span> for DecodeContext<'a, 'tcx> {
273
273
let lo = BytePos :: decode ( self ) ?;
274
274
let hi = BytePos :: decode ( self ) ?;
275
275
276
+ if lo == BytePos ( 0 ) && hi == BytePos ( 0 ) {
277
+ // Don't try to rebase DUMMY_SP. Otherwise it will look like a valid
278
+ // Span again.
279
+ return Ok ( DUMMY_SP )
280
+ }
281
+
282
+ if hi < lo {
283
+ // Consistently map invalid spans to DUMMY_SP.
284
+ return Ok ( DUMMY_SP )
285
+ }
286
+
276
287
let sess = if let Some ( sess) = self . sess {
277
288
sess
278
289
} else {
279
290
bug ! ( "Cannot decode Span without Session." )
280
291
} ;
281
292
282
- let ( lo, hi) = if lo > hi {
283
- // Currently macro expansion sometimes produces invalid Span values
284
- // where lo > hi. In order not to crash the compiler when trying to
285
- // translate these values, let's transform them into something we
286
- // can handle (and which will produce useful debug locations at
287
- // least some of the time).
288
- // This workaround is only necessary as long as macro expansion is
289
- // not fixed. FIXME(#23480)
290
- ( lo, lo)
291
- } else {
292
- ( lo, hi)
293
- } ;
294
-
295
293
let imported_filemaps = self . cdata ( ) . imported_filemaps ( & sess. codemap ( ) ) ;
296
294
let filemap = {
297
295
// Optimize for the case that most spans within a translated item
@@ -321,6 +319,16 @@ impl<'a, 'tcx> SpecializedDecoder<Span> for DecodeContext<'a, 'tcx> {
321
319
}
322
320
} ;
323
321
322
+ // Make sure our binary search above is correct.
323
+ debug_assert ! ( lo >= filemap. original_start_pos &&
324
+ lo <= filemap. original_end_pos) ;
325
+
326
+ if hi < filemap. original_start_pos || hi > filemap. original_end_pos {
327
+ // `hi` points to a different FileMap than `lo` which is invalid.
328
+ // Again, map invalid Spans to DUMMY_SP.
329
+ return Ok ( DUMMY_SP )
330
+ }
331
+
324
332
let lo = ( lo + filemap. translated_filemap . start_pos ) - filemap. original_start_pos ;
325
333
let hi = ( hi + filemap. translated_filemap . start_pos ) - filemap. original_start_pos ;
326
334
0 commit comments