Skip to content

Commit ccdb598

Browse files
committed
rustdoc: Cleanup broken link callbacks
1 parent 34ba77d commit ccdb598

File tree

3 files changed

+43
-58
lines changed

3 files changed

+43
-58
lines changed

compiler/rustc_resolve/src/rustdoc.rs

+10-13
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use rustc_data_structures::fx::FxHashMap;
55
use rustc_span::def_id::DefId;
66
use rustc_span::symbol::{kw, Symbol};
77
use rustc_span::Span;
8-
use std::cell::RefCell;
98
use std::{cmp, mem};
109

1110
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
@@ -354,16 +353,14 @@ pub(crate) fn attrs_to_preprocessed_links(attrs: &[ast::Attribute]) -> Vec<Strin
354353
let (doc_fragments, _) = attrs_to_doc_fragments(attrs.iter().map(|attr| (attr, None)), true);
355354
let doc = prepare_to_doc_link_resolution(&doc_fragments).into_values().next().unwrap();
356355

357-
let links = RefCell::new(Vec::new());
358-
let mut callback = |link: BrokenLink<'_>| {
359-
links.borrow_mut().push(preprocess_link(&link.reference));
360-
None
361-
};
362-
for event in Parser::new_with_broken_link_callback(&doc, main_body_opts(), Some(&mut callback))
363-
{
364-
if let Event::Start(Tag::Link(_, dest, _)) = event {
365-
links.borrow_mut().push(preprocess_link(&dest));
366-
}
367-
}
368-
links.into_inner()
356+
Parser::new_with_broken_link_callback(
357+
&doc,
358+
main_body_opts(),
359+
Some(&mut |link: BrokenLink<'_>| Some((link.reference, "".into()))),
360+
)
361+
.filter_map(|event| match event {
362+
Event::Start(Tag::Link(_, dest, _)) => Some(preprocess_link(&dest)),
363+
_ => None,
364+
})
365+
.collect()
369366
}

src/librustdoc/html/markdown.rs

+17-29
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ use rustc_span::{Span, Symbol};
3434

3535
use once_cell::sync::Lazy;
3636
use std::borrow::Cow;
37-
use std::cell::RefCell;
3837
use std::collections::VecDeque;
3938
use std::default::Default;
4039
use std::fmt::Write;
@@ -1226,14 +1225,12 @@ pub(crate) struct MarkdownLink {
12261225

12271226
pub(crate) fn markdown_links<R>(
12281227
md: &str,
1229-
filter_map: impl Fn(MarkdownLink) -> Option<R>,
1228+
preprocess_link: impl Fn(MarkdownLink) -> Option<R>,
12301229
) -> Vec<R> {
12311230
if md.is_empty() {
12321231
return vec![];
12331232
}
12341233

1235-
let links = RefCell::new(vec![]);
1236-
12371234
// FIXME: remove this function once pulldown_cmark can provide spans for link definitions.
12381235
let locate = |s: &str, fallback: Range<usize>| unsafe {
12391236
let s_start = s.as_ptr();
@@ -1265,21 +1262,14 @@ pub(crate) fn markdown_links<R>(
12651262
}
12661263
};
12671264

1268-
let mut push = |link: BrokenLink<'_>| {
1269-
let span = span_for_link(&link.reference, link.span);
1270-
filter_map(MarkdownLink {
1271-
kind: LinkType::ShortcutUnknown,
1272-
link: link.reference.to_string(),
1273-
range: span,
1274-
})
1275-
.map(|link| links.borrow_mut().push(link));
1276-
None
1277-
};
1278-
1279-
for ev in Parser::new_with_broken_link_callback(md, main_body_opts(), Some(&mut push))
1280-
.into_offset_iter()
1281-
{
1282-
if let Event::Start(Tag::Link(
1265+
Parser::new_with_broken_link_callback(
1266+
md,
1267+
main_body_opts(),
1268+
Some(&mut |link: BrokenLink<'_>| Some((link.reference, "".into()))),
1269+
)
1270+
.into_offset_iter()
1271+
.filter_map(|(event, span)| match event {
1272+
Event::Start(Tag::Link(
12831273
// `<>` links cannot be intra-doc links so we skip them.
12841274
kind @ (LinkType::Inline
12851275
| LinkType::Reference
@@ -1290,16 +1280,14 @@ pub(crate) fn markdown_links<R>(
12901280
| LinkType::ShortcutUnknown),
12911281
dest,
12921282
_,
1293-
)) = ev.0
1294-
{
1295-
debug!("found link: {dest}");
1296-
let span = span_for_link(&dest, ev.1);
1297-
filter_map(MarkdownLink { kind, link: dest.into_string(), range: span })
1298-
.map(|link| links.borrow_mut().push(link));
1299-
}
1300-
}
1301-
1302-
links.into_inner()
1283+
)) => preprocess_link(MarkdownLink {
1284+
kind,
1285+
range: span_for_link(&dest, span),
1286+
link: dest.into_string(),
1287+
}),
1288+
_ => None,
1289+
})
1290+
.collect()
13031291
}
13041292

13051293
#[derive(Debug)]

tests/rustdoc-ui/intra-doc/unknown-disambiguator.stderr

+16-16
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,6 @@ LL | //! Linking to [foo@banana] and [`bar@banana!()`].
2020
|
2121
= 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
2222

23-
error: unknown disambiguator `foo`
24-
--> $DIR/unknown-disambiguator.rs:10:34
25-
|
26-
LL | //! And with weird backticks: [``foo@hello``] [foo`@`hello].
27-
| ^^^
28-
|
29-
= 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
30-
31-
error: unknown disambiguator `foo`
32-
--> $DIR/unknown-disambiguator.rs:10:48
33-
|
34-
LL | //! And with weird backticks: [``foo@hello``] [foo`@`hello].
35-
| ^^^
36-
|
37-
= 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
38-
3923
error: unknown disambiguator ``
4024
--> $DIR/unknown-disambiguator.rs:7:31
4125
|
@@ -52,5 +36,21 @@ LL | //! And to [no disambiguator](@nectarine) and [another](@apricot!()).
5236
|
5337
= 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
5438

39+
error: unknown disambiguator `foo`
40+
--> $DIR/unknown-disambiguator.rs:10:34
41+
|
42+
LL | //! And with weird backticks: [``foo@hello``] [foo`@`hello].
43+
| ^^^
44+
|
45+
= 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
46+
47+
error: unknown disambiguator `foo`
48+
--> $DIR/unknown-disambiguator.rs:10:48
49+
|
50+
LL | //! And with weird backticks: [``foo@hello``] [foo`@`hello].
51+
| ^^^
52+
|
53+
= 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
54+
5555
error: aborting due to 6 previous errors
5656

0 commit comments

Comments
 (0)