@@ -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,15 +554,9 @@ impl SourceMap {
554554 }
555555
556556 if let Some ( ref src) = local_begin. sf . src {
557- if !src. is_char_boundary ( start_index) || !src. is_char_boundary ( end_index) {
558- return Err ( SpanSnippetError :: IllFormedSpan ( sp) ) ;
559- }
560- return Ok ( extract_source ( src, start_index, end_index) ) ;
557+ return extract_source ( src, start_index, end_index) ;
561558 } else if let Some ( src) = local_begin. sf . external_src . borrow ( ) . get_source ( ) {
562- if !src. is_char_boundary ( start_index) || !src. is_char_boundary ( end_index) {
563- return Err ( SpanSnippetError :: IllFormedSpan ( sp) ) ;
564- }
565- return Ok ( extract_source ( src, start_index, end_index) ) ;
559+ return extract_source ( src, start_index, end_index) ;
566560 } else {
567561 return Err ( SpanSnippetError :: SourceNotAvailable {
568562 filename : local_begin. sf . name . clone ( )
@@ -573,8 +567,9 @@ impl SourceMap {
573567
574568 /// Returns the source snippet as `String` corresponding to the given `Span`
575569 pub fn span_to_snippet ( & self , sp : Span ) -> Result < String , SpanSnippetError > {
576- self . span_to_source ( sp, |src, start_index, end_index| src[ start_index..end_index]
577- . 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) ) )
578573 }
579574
580575 pub fn span_to_margin ( & self , sp : Span ) -> Option < usize > {
@@ -588,7 +583,9 @@ impl SourceMap {
588583
589584 /// Returns the source snippet as `String` before the given `Span`
590585 pub fn span_to_prev_source ( & self , sp : Span ) -> Result < String , SpanSnippetError > {
591- 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) ) )
592589 }
593590
594591 /// Extend the given `Span` to just after the previous occurrence of `c`. Return the same span
0 commit comments