@@ -12,10 +12,14 @@ use rustc_data_structures::fx::FxIndexMap;
1212use rustc_data_structures:: unord:: UnordSet ;
1313use rustc_middle:: ty:: TyCtxt ;
1414use rustc_span:: def_id:: DefId ;
15+ use rustc_span:: source_map:: SourceMap ;
1516use rustc_span:: { DUMMY_SP , InnerSpan , Span , Symbol , sym} ;
1617use thin_vec:: ThinVec ;
1718use tracing:: { debug, trace} ;
1819
20+ #[ cfg( test) ]
21+ mod tests;
22+
1923#[ derive( Clone , Copy , PartialEq , Eq , Debug ) ]
2024pub enum DocFragmentKind {
2125 /// A doc fragment created from a `///` or `//!` doc comment.
@@ -531,10 +535,20 @@ pub fn source_span_for_markdown_range(
531535 markdown : & str ,
532536 md_range : & Range < usize > ,
533537 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 ] ,
534549) -> Option < Span > {
535550 use rustc_span:: BytePos ;
536551
537- let map = tcx. sess . source_map ( ) ;
538552 if let & [ fragment] = & fragments
539553 && fragment. kind == DocFragmentKind :: RawDoc
540554 && let Ok ( snippet) = map. span_to_snippet ( fragment. span )
@@ -570,7 +584,13 @@ pub fn source_span_for_markdown_range(
570584 {
571585 // If there is either a match in a previous fragment, or
572586 // 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+ {
574594 match_data = Some ( ( i, match_start) ) ;
575595 } else {
576596 // Heirustic produced ambiguity, return nothing.
0 commit comments