rustdoc: Cleanup broken link callbacks

This commit is contained in:
Vadim Petrochenkov 2023-02-18 14:10:38 +04:00
parent 34ba77d260
commit ccdb598d1b
3 changed files with 43 additions and 58 deletions

View File

@ -5,7 +5,6 @@ use rustc_data_structures::fx::FxHashMap;
use rustc_span::def_id::DefId;
use rustc_span::symbol::{kw, Symbol};
use rustc_span::Span;
use std::cell::RefCell;
use std::{cmp, mem};
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
@ -354,16 +353,14 @@ pub(crate) fn attrs_to_preprocessed_links(attrs: &[ast::Attribute]) -> Vec<Strin
let (doc_fragments, _) = attrs_to_doc_fragments(attrs.iter().map(|attr| (attr, None)), true);
let doc = prepare_to_doc_link_resolution(&doc_fragments).into_values().next().unwrap();
let links = RefCell::new(Vec::new());
let mut callback = |link: BrokenLink<'_>| {
links.borrow_mut().push(preprocess_link(&link.reference));
None
};
for event in Parser::new_with_broken_link_callback(&doc, main_body_opts(), Some(&mut callback))
{
if let Event::Start(Tag::Link(_, dest, _)) = event {
links.borrow_mut().push(preprocess_link(&dest));
}
}
links.into_inner()
Parser::new_with_broken_link_callback(
&doc,
main_body_opts(),
Some(&mut |link: BrokenLink<'_>| Some((link.reference, "".into()))),
)
.filter_map(|event| match event {
Event::Start(Tag::Link(_, dest, _)) => Some(preprocess_link(&dest)),
_ => None,
})
.collect()
}

View File

@ -34,7 +34,6 @@ use rustc_span::{Span, Symbol};
use once_cell::sync::Lazy;
use std::borrow::Cow;
use std::cell::RefCell;
use std::collections::VecDeque;
use std::default::Default;
use std::fmt::Write;
@ -1226,14 +1225,12 @@ pub(crate) struct MarkdownLink {
pub(crate) fn markdown_links<R>(
md: &str,
filter_map: impl Fn(MarkdownLink) -> Option<R>,
preprocess_link: impl Fn(MarkdownLink) -> Option<R>,
) -> Vec<R> {
if md.is_empty() {
return vec![];
}
let links = RefCell::new(vec![]);
// FIXME: remove this function once pulldown_cmark can provide spans for link definitions.
let locate = |s: &str, fallback: Range<usize>| unsafe {
let s_start = s.as_ptr();
@ -1265,21 +1262,14 @@ pub(crate) fn markdown_links<R>(
}
};
let mut push = |link: BrokenLink<'_>| {
let span = span_for_link(&link.reference, link.span);
filter_map(MarkdownLink {
kind: LinkType::ShortcutUnknown,
link: link.reference.to_string(),
range: span,
})
.map(|link| links.borrow_mut().push(link));
None
};
for ev in Parser::new_with_broken_link_callback(md, main_body_opts(), Some(&mut push))
.into_offset_iter()
{
if let Event::Start(Tag::Link(
Parser::new_with_broken_link_callback(
md,
main_body_opts(),
Some(&mut |link: BrokenLink<'_>| Some((link.reference, "".into()))),
)
.into_offset_iter()
.filter_map(|(event, span)| match event {
Event::Start(Tag::Link(
// `<>` links cannot be intra-doc links so we skip them.
kind @ (LinkType::Inline
| LinkType::Reference
@ -1290,16 +1280,14 @@ pub(crate) fn markdown_links<R>(
| LinkType::ShortcutUnknown),
dest,
_,
)) = ev.0
{
debug!("found link: {dest}");
let span = span_for_link(&dest, ev.1);
filter_map(MarkdownLink { kind, link: dest.into_string(), range: span })
.map(|link| links.borrow_mut().push(link));
}
}
links.into_inner()
)) => preprocess_link(MarkdownLink {
kind,
range: span_for_link(&dest, span),
link: dest.into_string(),
}),
_ => None,
})
.collect()
}
#[derive(Debug)]

View File

@ -20,22 +20,6 @@ LL | //! Linking to [foo@banana] and [`bar@banana!()`].
|
= note: see https://doc.rust-lang.org/$CHANNEL/rustdoc/write-documentation/linking-to-items-by-name.html#namespaces-and-disambiguators for more info about disambiguators
error: unknown disambiguator `foo`
--> $DIR/unknown-disambiguator.rs:10:34
|
LL | //! And with weird backticks: [``foo@hello``] [foo`@`hello].
| ^^^
|
= note: see https://doc.rust-lang.org/$CHANNEL/rustdoc/write-documentation/linking-to-items-by-name.html#namespaces-and-disambiguators for more info about disambiguators
error: unknown disambiguator `foo`
--> $DIR/unknown-disambiguator.rs:10:48
|
LL | //! And with weird backticks: [``foo@hello``] [foo`@`hello].
| ^^^
|
= note: see https://doc.rust-lang.org/$CHANNEL/rustdoc/write-documentation/linking-to-items-by-name.html#namespaces-and-disambiguators for more info about disambiguators
error: unknown disambiguator ``
--> $DIR/unknown-disambiguator.rs:7:31
|
@ -52,5 +36,21 @@ LL | //! And to [no disambiguator](@nectarine) and [another](@apricot!()).
|
= note: see https://doc.rust-lang.org/$CHANNEL/rustdoc/write-documentation/linking-to-items-by-name.html#namespaces-and-disambiguators for more info about disambiguators
error: unknown disambiguator `foo`
--> $DIR/unknown-disambiguator.rs:10:34
|
LL | //! And with weird backticks: [``foo@hello``] [foo`@`hello].
| ^^^
|
= note: see https://doc.rust-lang.org/$CHANNEL/rustdoc/write-documentation/linking-to-items-by-name.html#namespaces-and-disambiguators for more info about disambiguators
error: unknown disambiguator `foo`
--> $DIR/unknown-disambiguator.rs:10:48
|
LL | //! And with weird backticks: [``foo@hello``] [foo`@`hello].
| ^^^
|
= note: see https://doc.rust-lang.org/$CHANNEL/rustdoc/write-documentation/linking-to-items-by-name.html#namespaces-and-disambiguators for more info about disambiguators
error: aborting due to 6 previous errors