@@ -519,7 +519,7 @@ impl SourceMap {
519
519
/// extract function takes three arguments: a string slice containing the source, an index in
520
520
/// the slice for the beginning of the span and an index in the slice for the end of the span.
521
521
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 >
523
523
{
524
524
if sp. lo ( ) > sp. hi ( ) {
525
525
return Err ( SpanSnippetError :: IllFormedSpan ( sp) ) ;
@@ -554,15 +554,9 @@ impl SourceMap {
554
554
}
555
555
556
556
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) ;
561
558
} 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) ;
566
560
} else {
567
561
return Err ( SpanSnippetError :: SourceNotAvailable {
568
562
filename : local_begin. sf . name . clone ( )
@@ -573,8 +567,9 @@ impl SourceMap {
573
567
574
568
/// Returns the source snippet as `String` corresponding to the given `Span`
575
569
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) ) )
578
573
}
579
574
580
575
pub fn span_to_margin ( & self , sp : Span ) -> Option < usize > {
@@ -588,7 +583,9 @@ impl SourceMap {
588
583
589
584
/// Returns the source snippet as `String` before the given `Span`
590
585
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) ) )
592
589
}
593
590
594
591
/// Extend the given `Span` to just after the previous occurrence of `c`. Return the same span
0 commit comments