@@ -11,6 +11,8 @@ use core::cell::RefCell;
1111use core:: cmp;
1212use core:: fmt:: { self , Debug , Display , Write } ;
1313use core:: mem:: ManuallyDrop ;
14+ #[ cfg( span_locations) ]
15+ use core:: ops:: Range ;
1416use core:: ops:: RangeBounds ;
1517use core:: ptr;
1618use core:: str:: FromStr ;
@@ -372,7 +374,7 @@ impl FileInfo {
372374 span. lo >= self . span . lo && span. hi <= self . span . hi
373375 }
374376
375- fn source_text ( & mut self , span : Span ) -> String {
377+ fn byte_range ( & mut self , span : Span ) -> Range < usize > {
376378 let lo_char = ( span. lo - self . span . lo ) as usize ;
377379
378380 // Look up offset of the largest already-computed char index that is
@@ -401,11 +403,15 @@ impl FileInfo {
401403
402404 let trunc_lo = & self . source_text [ lo_byte..] ;
403405 let char_len = ( span. hi - span. lo ) as usize ;
404- let source_text = match trunc_lo. char_indices ( ) . nth ( char_len) {
405- Some ( ( offset, _ch) ) => & trunc_lo[ ..offset] ,
406- None => trunc_lo,
407- } ;
408- source_text. to_owned ( )
406+ lo_byte..match trunc_lo. char_indices ( ) . nth ( char_len) {
407+ Some ( ( offset, _ch) ) => lo_byte + offset,
408+ None => self . source_text . len ( ) ,
409+ }
410+ }
411+
412+ fn source_text ( & mut self , span : Span ) -> String {
413+ let byte_range = self . byte_range ( span) ;
414+ self . source_text [ byte_range] . to_owned ( )
409415 }
410416}
411417
@@ -547,6 +553,21 @@ impl Span {
547553 } )
548554 }
549555
556+ #[ cfg( span_locations) ]
557+ pub fn byte_range ( & self ) -> Range < usize > {
558+ #[ cfg( fuzzing) ]
559+ return 0 ..0 ;
560+
561+ #[ cfg( not( fuzzing) ) ]
562+ {
563+ if self . is_call_site ( ) {
564+ 0 ..0
565+ } else {
566+ SOURCE_MAP . with ( |sm| sm. borrow_mut ( ) . fileinfo_mut ( * self ) . byte_range ( * self ) )
567+ }
568+ }
569+ }
570+
550571 #[ cfg( span_locations) ]
551572 pub fn start ( & self ) -> LineColumn {
552573 #[ cfg( fuzzing) ]
0 commit comments