@@ -519,7 +519,7 @@ impl SourceMap {
519519 /// extract function takes three arguments: a string slice containing the source, an index in
520520 /// the slice for the beginning of the span and an index in the slice for the end of the span.
521521 fn span_to_source < F > ( & self , sp : Span , extract_source : F ) -> Result < String , SpanSnippetError >
522- where F : Fn ( & str , usize , usize ) -> String
522+ where F : Fn ( & str , usize , usize ) -> Result < String , SpanSnippetError >
523523 {
524524 if sp. lo ( ) > sp. hi ( ) {
525525 return Err ( SpanSnippetError :: IllFormedSpan ( sp) ) ;
@@ -554,9 +554,9 @@ impl SourceMap {
554554 }
555555
556556 if let Some ( ref src) = local_begin. sf . src {
557- return Ok ( extract_source ( src, start_index, end_index) ) ;
557+ return extract_source ( src, start_index, end_index) ;
558558 } else if let Some ( src) = local_begin. sf . external_src . borrow ( ) . get_source ( ) {
559- return Ok ( extract_source ( src, start_index, end_index) ) ;
559+ return extract_source ( src, start_index, end_index) ;
560560 } else {
561561 return Err ( SpanSnippetError :: SourceNotAvailable {
562562 filename : local_begin. sf . name . clone ( )
@@ -567,8 +567,9 @@ impl SourceMap {
567567
568568 /// Returns the source snippet as `String` corresponding to the given `Span`
569569 pub fn span_to_snippet ( & self , sp : Span ) -> Result < String , SpanSnippetError > {
570- self . span_to_source ( sp, |src, start_index, end_index| src[ start_index..end_index]
571- . to_string ( ) )
570+ self . span_to_source ( sp, |src, start_index, end_index| src. get ( start_index..end_index)
571+ . map ( |s| s. to_string ( ) )
572+ . ok_or_else ( || SpanSnippetError :: IllFormedSpan ( sp) ) )
572573 }
573574
574575 pub fn span_to_margin ( & self , sp : Span ) -> Option < usize > {
@@ -582,7 +583,9 @@ impl SourceMap {
582583
583584 /// Returns the source snippet as `String` before the given `Span`
584585 pub fn span_to_prev_source ( & self , sp : Span ) -> Result < String , SpanSnippetError > {
585- self . span_to_source ( sp, |src, start_index, _| src[ ..start_index] . to_string ( ) )
586+ self . span_to_source ( sp, |src, start_index, _| src. get ( ..start_index)
587+ . map ( |s| s. to_string ( ) )
588+ . ok_or_else ( || SpanSnippetError :: IllFormedSpan ( sp) ) )
586589 }
587590
588591 /// Extend the given `Span` to just after the previous occurrence of `c`. Return the same span
0 commit comments