Skip to content

Commit 98a7664

Browse files
authored
Rollup merge of rust-lang#112806 - kadiwa4:collect_intra_doc_links, r=notriddle
Small code improvements in `collect_intra_doc_links.rs` Makes some of the code more readable by shortening it, and removes some unnecessary bounds checks.
2 parents dfe53af + d2af7da commit 98a7664

File tree

1 file changed

+39
-64
lines changed

1 file changed

+39
-64
lines changed

src/librustdoc/passes/collect_intra_doc_links.rs

+39-64
Original file line numberDiff line numberDiff line change
@@ -108,15 +108,13 @@ impl Res {
108108
Res::Primitive(_) => return Suggestion::Prefix("prim"),
109109
Res::Def(kind, _) => kind,
110110
};
111-
if kind == DefKind::Macro(MacroKind::Bang) {
112-
return Suggestion::Macro;
113-
} else if kind == DefKind::Fn || kind == DefKind::AssocFn {
114-
return Suggestion::Function;
115-
} else if kind == DefKind::Field {
116-
return Suggestion::RemoveDisambiguator;
117-
}
118111

119112
let prefix = match kind {
113+
DefKind::Fn | DefKind::AssocFn => return Suggestion::Function,
114+
DefKind::Field => return Suggestion::RemoveDisambiguator,
115+
DefKind::Macro(MacroKind::Bang) => return Suggestion::Macro,
116+
117+
DefKind::Macro(MacroKind::Derive) => "derive",
120118
DefKind::Struct => "struct",
121119
DefKind::Enum => "enum",
122120
DefKind::Trait => "trait",
@@ -126,7 +124,6 @@ impl Res {
126124
"const"
127125
}
128126
DefKind::Static(_) => "static",
129-
DefKind::Macro(MacroKind::Derive) => "derive",
130127
// Now handle things that don't have a specific disambiguator
131128
_ => match kind
132129
.ns()
@@ -283,20 +280,15 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
283280

284281
debug!("looking for enum variant {path_str}");
285282
let mut split = path_str.rsplitn(3, "::");
286-
let variant_field_name = split
287-
.next()
288-
.map(|f| Symbol::intern(f))
289-
.expect("fold_item should ensure link is non-empty");
290-
let variant_name =
291-
// we're not sure this is a variant at all, so use the full string
292-
// If there's no second component, the link looks like `[path]`.
293-
// So there's no partial res and we should say the whole link failed to resolve.
294-
split.next().map(|f| Symbol::intern(f)).ok_or_else(no_res)?;
295-
let path = split
296-
.next()
297-
// If there's no third component, we saw `[a::b]` before and it failed to resolve.
298-
// So there's no partial res.
299-
.ok_or_else(no_res)?;
283+
let variant_field_name = Symbol::intern(split.next().unwrap());
284+
// We're not sure this is a variant at all, so use the full string.
285+
// If there's no second component, the link looks like `[path]`.
286+
// So there's no partial res and we should say the whole link failed to resolve.
287+
let variant_name = Symbol::intern(split.next().ok_or_else(no_res)?);
288+
289+
// If there's no third component, we saw `[a::b]` before and it failed to resolve.
290+
// So there's no partial res.
291+
let path = split.next().ok_or_else(no_res)?;
300292
let ty_res = self.resolve_path(&path, TypeNS, item_id, module_id).ok_or_else(no_res)?;
301293

302294
match ty_res {
@@ -447,41 +439,29 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
447439
}
448440

449441
// Try looking for methods and associated items.
450-
let mut split = path_str.rsplitn(2, "::");
451-
// NB: `split`'s first element is always defined, even if the delimiter was not present.
452-
// NB: `item_str` could be empty when resolving in the root namespace (e.g. `::std`).
453-
let item_str = split.next().unwrap();
454-
let item_name = Symbol::intern(item_str);
455-
let path_root = split
456-
.next()
442+
// NB: `path_root` could be empty when resolving in the root namespace (e.g. `::std`).
443+
let (path_root, item_str) = path_str.rsplit_once("::").ok_or_else(|| {
457444
// If there's no `::`, it's not an associated item.
458445
// So we can be sure that `rustc_resolve` was accurate when it said it wasn't resolved.
459-
.ok_or_else(|| {
460-
debug!("found no `::`, assuming {item_name} was correctly not in scope");
461-
UnresolvedPath {
462-
item_id,
463-
module_id,
464-
partial_res: None,
465-
unresolved: item_str.into(),
466-
}
467-
})?;
446+
debug!("found no `::`, assuming {path_str} was correctly not in scope");
447+
UnresolvedPath { item_id, module_id, partial_res: None, unresolved: path_str.into() }
448+
})?;
449+
let item_name = Symbol::intern(item_str);
468450

469451
// FIXME(#83862): this arbitrarily gives precedence to primitives over modules to support
470452
// links to primitives when `#[rustc_doc_primitive]` is present. It should give an ambiguity
471453
// error instead and special case *only* modules with `#[rustc_doc_primitive]`, not all
472454
// primitives.
473-
match resolve_primitive(&path_root, TypeNS)
474-
.or_else(|| self.resolve_path(&path_root, TypeNS, item_id, module_id))
475-
.and_then(|ty_res| {
476-
let candidates = self
477-
.resolve_associated_item(ty_res, item_name, ns, module_id)
455+
match resolve_primitive(path_root, TypeNS)
456+
.or_else(|| self.resolve_path(path_root, TypeNS, item_id, module_id))
457+
.map(|ty_res| {
458+
self.resolve_associated_item(ty_res, item_name, ns, module_id)
478459
.into_iter()
479460
.map(|(res, def_id)| (res, Some(def_id)))
480-
.collect::<Vec<_>>();
481-
if !candidates.is_empty() { Some(candidates) } else { None }
461+
.collect::<Vec<_>>()
482462
}) {
483-
Some(r) => Ok(r),
484-
None => {
463+
Some(r) if !r.is_empty() => Ok(r),
464+
_ => {
485465
if ns == Namespace::ValueNS {
486466
self.variant_field(path_str, item_id, module_id)
487467
.map(|(res, def_id)| vec![(res, Some(def_id))])
@@ -1263,16 +1243,18 @@ impl LinkCollector<'_, '_> {
12631243
self.report_rawptr_assoc_feature_gate(diag.dox, &diag.link_range, diag.item);
12641244
return None;
12651245
} else {
1266-
candidates = vec![candidates[0]];
1246+
candidates = vec![*candidate];
12671247
}
12681248
}
12691249

12701250
// If there are multiple items with the same "kind" (for example, both "associated types")
12711251
// and after removing duplicated kinds, only one remains, the `ambiguity_error` function
12721252
// won't emit an error. So at this point, we can just take the first candidate as it was
12731253
// the first retrieved and use it to generate the link.
1274-
if candidates.len() > 1 && !ambiguity_error(self.cx, &diag, &key.path_str, &candidates) {
1275-
candidates = vec![candidates[0]];
1254+
if let [candidate, _candidate2, ..] = *candidates
1255+
&& !ambiguity_error(self.cx, &diag, &key.path_str, &candidates)
1256+
{
1257+
candidates = vec![candidate];
12761258
}
12771259

12781260
if let &[(res, def_id)] = candidates.as_slice() {
@@ -1322,12 +1304,11 @@ impl LinkCollector<'_, '_> {
13221304
let mut err = ResolutionFailure::NotResolved(err);
13231305
for other_ns in [TypeNS, ValueNS, MacroNS] {
13241306
if other_ns != expected_ns {
1325-
if let Ok(res) =
1326-
self.resolve(path_str, other_ns, item_id, module_id)
1327-
&& !res.is_empty()
1307+
if let Ok(&[res, ..]) =
1308+
self.resolve(path_str, other_ns, item_id, module_id).as_deref()
13281309
{
13291310
err = ResolutionFailure::WrongNamespace {
1330-
res: full_res(self.cx.tcx, res[0]),
1311+
res: full_res(self.cx.tcx, res),
13311312
expected_ns,
13321313
};
13331314
break;
@@ -1748,7 +1729,6 @@ fn report_diagnostic(
17481729
lint.note(format!(
17491730
"the link appears in this line:\n\n{line}\n\
17501731
{indicator: <before$}{indicator:^<found$}",
1751-
line = line,
17521732
indicator = "",
17531733
before = md_range.start - last_new_line_offset,
17541734
found = md_range.len(),
@@ -1807,18 +1787,13 @@ fn resolution_failure(
18071787

18081788
let item_id = *item_id;
18091789
let module_id = *module_id;
1810-
// FIXME(jynelson): this might conflict with my `Self` fix in #76467
1811-
// FIXME: maybe use itertools `collect_tuple` instead?
1812-
fn split(path: &str) -> Option<(&str, &str)> {
1813-
let mut splitter = path.rsplitn(2, "::");
1814-
splitter.next().and_then(|right| splitter.next().map(|left| (left, right)))
1815-
}
18161790

18171791
// Check if _any_ parent of the path gets resolved.
18181792
// If so, report it and say the first which failed; if not, say the first path segment didn't resolve.
18191793
let mut name = path_str;
18201794
'outer: loop {
1821-
let Some((start, end)) = split(name) else {
1795+
// FIXME(jynelson): this might conflict with my `Self` fix in #76467
1796+
let Some((start, end)) = name.rsplit_once("::") else {
18221797
// avoid bug that marked [Quux::Z] as missing Z, not Quux
18231798
if partial_res.is_none() {
18241799
*unresolved = name.into();
@@ -1829,8 +1804,8 @@ fn resolution_failure(
18291804
for ns in [TypeNS, ValueNS, MacroNS] {
18301805
if let Ok(v_res) = collector.resolve(start, ns, item_id, module_id) {
18311806
debug!("found partial_res={v_res:?}");
1832-
if !v_res.is_empty() {
1833-
*partial_res = Some(full_res(tcx, v_res[0]));
1807+
if let Some(&res) = v_res.first() {
1808+
*partial_res = Some(full_res(tcx, res));
18341809
*unresolved = end.into();
18351810
break 'outer;
18361811
}

0 commit comments

Comments
 (0)