@@ -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,9 +554,9 @@ impl SourceMap {
554
554
}
555
555
556
556
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) ;
558
558
} 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) ;
560
560
} else {
561
561
return Err ( SpanSnippetError :: SourceNotAvailable {
562
562
filename : local_begin. sf . name . clone ( )
@@ -567,8 +567,9 @@ impl SourceMap {
567
567
568
568
/// Returns the source snippet as `String` corresponding to the given `Span`
569
569
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) ) )
572
573
}
573
574
574
575
pub fn span_to_margin ( & self , sp : Span ) -> Option < usize > {
@@ -582,7 +583,9 @@ impl SourceMap {
582
583
583
584
/// Returns the source snippet as `String` before the given `Span`
584
585
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) ) )
586
589
}
587
590
588
591
/// Extend the given `Span` to just after the previous occurrence of `c`. Return the same span
0 commit comments