Skip to content

Commit cb4317d

Browse files
bugadaniJoshua Nelson
and
Joshua Nelson
committed
Always provide a range
Co-authored-by: Joshua Nelson <[email protected]>
1 parent 2b70da1 commit cb4317d

File tree

2 files changed

+34
-39
lines changed

2 files changed

+34
-39
lines changed

src/librustdoc/html/markdown.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -1127,32 +1127,32 @@ crate fn plain_text_summary(md: &str) -> String {
11271127
s
11281128
}
11291129

1130-
crate fn markdown_links(md: &str) -> Vec<(String, Option<Range<usize>>)> {
1130+
crate fn markdown_links(md: &str) -> Vec<(String, Range<usize>)> {
11311131
if md.is_empty() {
11321132
return vec![];
11331133
}
11341134

11351135
let mut links = vec![];
11361136
let mut shortcut_links = vec![];
11371137

1138-
let locate = |s: &str| unsafe {
1138+
let locate = |s: &str, fallback: Range<usize>| unsafe {
11391139
let s_start = s.as_ptr();
11401140
let s_end = s_start.add(s.len());
11411141
let md_start = md.as_ptr();
11421142
let md_end = md_start.add(md.len());
11431143
if md_start <= s_start && s_end <= md_end {
11441144
let start = s_start.offset_from(md_start) as usize;
11451145
let end = s_end.offset_from(md_start) as usize;
1146-
Some(start..end)
1146+
start..end
11471147
} else {
1148-
None
1148+
fallback
11491149
}
11501150
};
11511151

11521152
let mut push = |link: BrokenLink<'_>| {
11531153
// FIXME: use `link.span` instead of `locate`
11541154
// (doing it now includes the `[]` as well as the text)
1155-
shortcut_links.push((link.reference.to_owned(), locate(link.reference)));
1155+
shortcut_links.push((link.reference.to_owned(), locate(link.reference, link.span)));
11561156
None
11571157
};
11581158
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<Range<usize>>)> {
11661166
if let Event::Start(Tag::Link(_, dest, _)) = ev.0 {
11671167
debug!("found link: {}", dest);
11681168
links.push(match dest {
1169-
CowStr::Borrowed(s) => (s.to_owned(), locate(s)),
1170-
s @ (CowStr::Boxed(..) | CowStr::Inlined(..)) => (s.into_string(), None),
1169+
CowStr::Borrowed(s) => (s.to_owned(), locate(s, ev.1)),
1170+
s @ (CowStr::Boxed(..) | CowStr::Inlined(..)) => (s.into_string(), ev.1),
11711171
});
11721172
}
11731173
}

src/librustdoc/passes/collect_intra_doc_links.rs

+27-32
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ struct DiagnosticInfo<'a> {
245245
item: &'a Item,
246246
dox: &'a str,
247247
ori_link: &'a str,
248-
link_range: Option<Range<usize>>,
248+
link_range: Range<usize>,
249249
}
250250

251251
#[derive(Clone, Debug, Hash)]
@@ -982,7 +982,7 @@ impl LinkCollector<'_, '_> {
982982
parent_node: Option<DefId>,
983983
krate: CrateNum,
984984
ori_link: String,
985-
link_range: Option<Range<usize>>,
985+
link_range: Range<usize>,
986986
) -> Option<ItemLink> {
987987
trace!("considering link '{}'", ori_link);
988988

@@ -1628,7 +1628,7 @@ fn report_diagnostic(
16281628
msg: &str,
16291629
item: &Item,
16301630
dox: &str,
1631-
link_range: &Option<Range<usize>>,
1631+
link_range: &Range<usize>,
16321632
decorate: impl FnOnce(&mut DiagnosticBuilder<'_>, Option<rustc_span::Span>),
16331633
) {
16341634
let hir_id = match cx.as_local_hir_id(item.def_id) {
@@ -1646,31 +1646,27 @@ fn report_diagnostic(
16461646
cx.tcx.struct_span_lint_hir(lint, hir_id, sp, |lint| {
16471647
let mut diag = lint.build(msg);
16481648

1649-
let span = link_range
1650-
.as_ref()
1651-
.and_then(|range| super::source_span_for_markdown_range(cx, dox, range, attrs));
1649+
let span = super::source_span_for_markdown_range(cx, dox, link_range, attrs);
16521650

1653-
if let Some(link_range) = link_range {
1654-
if let Some(sp) = span {
1655-
diag.set_span(sp);
1656-
} else {
1657-
// blah blah blah\nblah\nblah [blah] blah blah\nblah blah
1658-
// ^ ~~~~
1659-
// | link_range
1660-
// last_new_line_offset
1661-
let last_new_line_offset = dox[..link_range.start].rfind('\n').map_or(0, |n| n + 1);
1662-
let line = dox[last_new_line_offset..].lines().next().unwrap_or("");
1663-
1664-
// Print the line containing the `link_range` and manually mark it with '^'s.
1665-
diag.note(&format!(
1666-
"the link appears in this line:\n\n{line}\n\
1651+
if let Some(sp) = span {
1652+
diag.set_span(sp);
1653+
} else {
1654+
// blah blah blah\nblah\nblah [blah] blah blah\nblah blah
1655+
// ^ ~~~~
1656+
// | link_range
1657+
// last_new_line_offset
1658+
let last_new_line_offset = dox[..link_range.start].rfind('\n').map_or(0, |n| n + 1);
1659+
let line = dox[last_new_line_offset..].lines().next().unwrap_or("");
1660+
1661+
// Print the line containing the `link_range` and manually mark it with '^'s.
1662+
diag.note(&format!(
1663+
"the link appears in this line:\n\n{line}\n\
16671664
{indicator: <before$}{indicator:^<found$}",
1668-
line = line,
1669-
indicator = "",
1670-
before = link_range.start - last_new_line_offset,
1671-
found = link_range.len(),
1672-
));
1673-
}
1665+
line = line,
1666+
indicator = "",
1667+
before = link_range.start - last_new_line_offset,
1668+
found = link_range.len(),
1669+
));
16741670
}
16751671

16761672
decorate(&mut diag, span);
@@ -1690,7 +1686,7 @@ fn resolution_failure(
16901686
path_str: &str,
16911687
disambiguator: Option<Disambiguator>,
16921688
dox: &str,
1693-
link_range: Option<Range<usize>>,
1689+
link_range: Range<usize>,
16941690
kinds: SmallVec<[ResolutionFailure<'_>; 3]>,
16951691
) {
16961692
let tcx = collector.cx.tcx;
@@ -1914,7 +1910,7 @@ fn anchor_failure(
19141910
item: &Item,
19151911
path_str: &str,
19161912
dox: &str,
1917-
link_range: Option<Range<usize>>,
1913+
link_range: Range<usize>,
19181914
failure: AnchorFailure,
19191915
) {
19201916
let msg = match failure {
@@ -1939,7 +1935,7 @@ fn ambiguity_error(
19391935
item: &Item,
19401936
path_str: &str,
19411937
dox: &str,
1942-
link_range: Option<Range<usize>>,
1938+
link_range: Range<usize>,
19431939
candidates: Vec<Res>,
19441940
) {
19451941
let mut msg = format!("`{}` is ", path_str);
@@ -1988,13 +1984,12 @@ fn suggest_disambiguator(
19881984
path_str: &str,
19891985
dox: &str,
19901986
sp: Option<rustc_span::Span>,
1991-
link_range: &Option<Range<usize>>,
1987+
link_range: &Range<usize>,
19921988
) {
19931989
let suggestion = disambiguator.suggestion();
19941990
let help = format!("to link to the {}, {}", disambiguator.descr(), suggestion.descr());
19951991

19961992
if let Some(sp) = sp {
1997-
let link_range = link_range.as_ref().expect("must have a link range if we have a span");
19981993
let msg = if dox.bytes().nth(link_range.start) == Some(b'`') {
19991994
format!("`{}`", suggestion.as_help(path_str))
20001995
} else {
@@ -2013,7 +2008,7 @@ fn privacy_error(
20132008
item: &Item,
20142009
path_str: &str,
20152010
dox: &str,
2016-
link_range: Option<Range<usize>>,
2011+
link_range: Range<usize>,
20172012
) {
20182013
let sym;
20192014
let item_name = match item.name {

0 commit comments

Comments
 (0)