Skip to content

Commit c6d7696

Browse files
committed
handle more edge cases
1 parent fc86385 commit c6d7696

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

compiler/rustc_resolve/src/rustdoc.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -538,11 +538,19 @@ pub fn source_span_for_markdown_range(
538538
// doc fragments, but the target range does not span across multiple
539539
// fragments.
540540
let mut match_data = None;
541+
let pat = &markdown[md_range.clone()];
542+
// this heirustic doesn't make sense with a zero-sized range.
543+
if pat.len() == 0 {
544+
return None;
545+
}
541546
for (i, fragment) in fragments.iter().enumerate() {
542547
if let Ok(snippet) = span_to_snippet(fragment.span)
543-
&& let Some(match_start) = snippet.find(&markdown[md_range.clone()])
548+
&& let Some(match_start) = snippet.find(pat)
544549
{
545-
if match_data.is_none() {
550+
// if there is either a match in a previous fragment,
551+
// or multiple matches in this fragment,
552+
// there is ambiguity.
553+
if match_data.is_none() && !snippet[match_start + 1..].contains(pat) {
546554
match_data = Some((i, match_start));
547555
} else {
548556
// heirustic produced ambiguity, return nothing.

tests/rustdoc-ui/unescaped_backticks.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -628,21 +628,21 @@ LL | /// or even to add a number `n` to 42 (`add(42, n)\`)!
628628
| +
629629

630630
error: unescaped backtick
631-
--> $DIR/unescaped_backticks.rs:108:9
631+
--> $DIR/unescaped_backticks.rs:108:10
632632
|
633633
LL | #[doc = "`"]
634-
| ^^^
634+
| ^
635635
|
636636
= help: the opening or closing backtick of an inline code may be missing
637637
= help: if you meant to use a literal backtick, escape it
638638
change: `
639639
to this: \`
640640

641641
error: unescaped backtick
642-
--> $DIR/unescaped_backticks.rs:115:9
642+
--> $DIR/unescaped_backticks.rs:115:26
643643
|
644644
LL | #[doc = concat!("\\", "`")]
645-
| ^^^^^^^^^^^^^^^^^^^^
645+
| ^
646646
|
647647
= help: the opening backtick of an inline code may be missing
648648
change: \`

0 commit comments

Comments
 (0)