diff --git a/src/librustdoc/html/markdown.rs b/src/librustdoc/html/markdown.rs index cabe4b49e98..b642f27c0b4 100644 --- a/src/librustdoc/html/markdown.rs +++ b/src/librustdoc/html/markdown.rs @@ -1127,7 +1127,7 @@ crate fn plain_text_summary(md: &str) -> String { s } -crate fn markdown_links(md: &str) -> Vec<(String, Option>)> { +crate fn markdown_links(md: &str) -> Vec<(String, Range)> { if md.is_empty() { return vec![]; } @@ -1135,7 +1135,7 @@ crate fn markdown_links(md: &str) -> Vec<(String, Option>)> { let mut links = vec![]; let mut shortcut_links = vec![]; - let locate = |s: &str| unsafe { + let locate = |s: &str, fallback: Range| unsafe { let s_start = s.as_ptr(); let s_end = s_start.add(s.len()); let md_start = md.as_ptr(); @@ -1143,16 +1143,16 @@ crate fn markdown_links(md: &str) -> Vec<(String, Option>)> { if md_start <= s_start && s_end <= md_end { let start = s_start.offset_from(md_start) as usize; let end = s_end.offset_from(md_start) as usize; - Some(start..end) + start..end } else { - None + fallback } }; let mut push = |link: BrokenLink<'_>| { // FIXME: use `link.span` instead of `locate` // (doing it now includes the `[]` as well as the text) - shortcut_links.push((link.reference.to_owned(), locate(link.reference))); + shortcut_links.push((link.reference.to_owned(), locate(link.reference, link.span))); None }; let p = Parser::new_with_broken_link_callback(md, opts(), Some(&mut push)).into_offset_iter(); @@ -1166,8 +1166,8 @@ crate fn markdown_links(md: &str) -> Vec<(String, Option>)> { if let Event::Start(Tag::Link(_, dest, _)) = ev.0 { debug!("found link: {}", dest); links.push(match dest { - CowStr::Borrowed(s) => (s.to_owned(), locate(s)), - s @ (CowStr::Boxed(..) | CowStr::Inlined(..)) => (s.into_string(), None), + CowStr::Borrowed(s) => (s.to_owned(), locate(s, ev.1)), + s @ (CowStr::Boxed(..) | CowStr::Inlined(..)) => (s.into_string(), ev.1), }); } } diff --git a/src/librustdoc/passes/collect_intra_doc_links.rs b/src/librustdoc/passes/collect_intra_doc_links.rs index 63cb02af3bc..3fc102f2fd2 100644 --- a/src/librustdoc/passes/collect_intra_doc_links.rs +++ b/src/librustdoc/passes/collect_intra_doc_links.rs @@ -245,7 +245,7 @@ struct DiagnosticInfo<'a> { item: &'a Item, dox: &'a str, ori_link: &'a str, - link_range: Option>, + link_range: Range, } #[derive(Clone, Debug, Hash)] @@ -982,7 +982,7 @@ impl LinkCollector<'_, '_> { parent_node: Option, krate: CrateNum, ori_link: String, - link_range: Option>, + link_range: Range, ) -> Option { trace!("considering link '{}'", ori_link); @@ -1628,7 +1628,7 @@ fn report_diagnostic( msg: &str, item: &Item, dox: &str, - link_range: &Option>, + link_range: &Range, decorate: impl FnOnce(&mut DiagnosticBuilder<'_>, Option), ) { let hir_id = match cx.as_local_hir_id(item.def_id) { @@ -1646,31 +1646,27 @@ fn report_diagnostic( cx.tcx.struct_span_lint_hir(lint, hir_id, sp, |lint| { let mut diag = lint.build(msg); - let span = link_range - .as_ref() - .and_then(|range| super::source_span_for_markdown_range(cx, dox, range, attrs)); + let span = super::source_span_for_markdown_range(cx, dox, link_range, attrs); - if let Some(link_range) = link_range { - if let Some(sp) = span { - diag.set_span(sp); - } else { - // blah blah blah\nblah\nblah [blah] blah blah\nblah blah - // ^ ~~~~ - // | link_range - // last_new_line_offset - let last_new_line_offset = dox[..link_range.start].rfind('\n').map_or(0, |n| n + 1); - let line = dox[last_new_line_offset..].lines().next().unwrap_or(""); + if let Some(sp) = span { + diag.set_span(sp); + } else { + // blah blah blah\nblah\nblah [blah] blah blah\nblah blah + // ^ ~~~~ + // | link_range + // last_new_line_offset + let last_new_line_offset = dox[..link_range.start].rfind('\n').map_or(0, |n| n + 1); + let line = dox[last_new_line_offset..].lines().next().unwrap_or(""); - // Print the line containing the `link_range` and manually mark it with '^'s. - diag.note(&format!( - "the link appears in this line:\n\n{line}\n\ + // Print the line containing the `link_range` and manually mark it with '^'s. + diag.note(&format!( + "the link appears in this line:\n\n{line}\n\ {indicator: , dox: &str, - link_range: Option>, + link_range: Range, kinds: SmallVec<[ResolutionFailure<'_>; 3]>, ) { let tcx = collector.cx.tcx; @@ -1914,7 +1910,7 @@ fn anchor_failure( item: &Item, path_str: &str, dox: &str, - link_range: Option>, + link_range: Range, failure: AnchorFailure, ) { let msg = match failure { @@ -1939,7 +1935,7 @@ fn ambiguity_error( item: &Item, path_str: &str, dox: &str, - link_range: Option>, + link_range: Range, candidates: Vec, ) { let mut msg = format!("`{}` is ", path_str); @@ -1988,13 +1984,12 @@ fn suggest_disambiguator( path_str: &str, dox: &str, sp: Option, - link_range: &Option>, + link_range: &Range, ) { let suggestion = disambiguator.suggestion(); let help = format!("to link to the {}, {}", disambiguator.descr(), suggestion.descr()); if let Some(sp) = sp { - let link_range = link_range.as_ref().expect("must have a link range if we have a span"); let msg = if dox.bytes().nth(link_range.start) == Some(b'`') { format!("`{}`", suggestion.as_help(path_str)) } else { @@ -2013,7 +2008,7 @@ fn privacy_error( item: &Item, path_str: &str, dox: &str, - link_range: Option>, + link_range: Range, ) { let sym; let item_name = match item.name {