@@ -12,10 +12,14 @@ use rustc_data_structures::fx::FxIndexMap;
12
12
use rustc_data_structures:: unord:: UnordSet ;
13
13
use rustc_middle:: ty:: TyCtxt ;
14
14
use rustc_span:: def_id:: DefId ;
15
+ use rustc_span:: source_map:: SourceMap ;
15
16
use rustc_span:: { DUMMY_SP , InnerSpan , Span , Symbol , sym} ;
16
17
use thin_vec:: ThinVec ;
17
18
use tracing:: { debug, trace} ;
18
19
20
+ #[ cfg( test) ]
21
+ mod tests;
22
+
19
23
#[ derive( Clone , Copy , PartialEq , Eq , Debug ) ]
20
24
pub enum DocFragmentKind {
21
25
/// A doc fragment created from a `///` or `//!` doc comment.
@@ -531,10 +535,20 @@ pub fn source_span_for_markdown_range(
531
535
markdown : & str ,
532
536
md_range : & Range < usize > ,
533
537
fragments : & [ DocFragment ] ,
538
+ ) -> Option < Span > {
539
+ let map = tcx. sess . source_map ( ) ;
540
+ source_span_for_markdown_range_inner ( map, markdown, md_range, fragments)
541
+ }
542
+
543
+ // inner function used for unit testing
544
+ pub fn source_span_for_markdown_range_inner (
545
+ map : & SourceMap ,
546
+ markdown : & str ,
547
+ md_range : & Range < usize > ,
548
+ fragments : & [ DocFragment ] ,
534
549
) -> Option < Span > {
535
550
use rustc_span:: BytePos ;
536
551
537
- let map = tcx. sess . source_map ( ) ;
538
552
if let & [ fragment] = & fragments
539
553
&& fragment. kind == DocFragmentKind :: RawDoc
540
554
&& let Ok ( snippet) = map. span_to_snippet ( fragment. span )
@@ -570,7 +584,13 @@ pub fn source_span_for_markdown_range(
570
584
{
571
585
// If there is either a match in a previous fragment, or
572
586
// multiple matches in this fragment, there is ambiguity.
573
- if match_data. is_none ( ) && !snippet[ match_start + 1 ..] . contains ( pat) {
587
+ // the snippet cannot be zero-sized, because it matches
588
+ // the pattern, which is checked to not be zero sized.
589
+ if match_data. is_none ( )
590
+ && !snippet. as_bytes ( ) [ match_start + 1 ..]
591
+ . windows ( pat. len ( ) )
592
+ . any ( |s| s == pat. as_bytes ( ) )
593
+ {
574
594
match_data = Some ( ( i, match_start) ) ;
575
595
} else {
576
596
// Heirustic produced ambiguity, return nothing.
0 commit comments