Skip to content

Commit 3b7e654

Browse files
committed
Use more appropriate return type for resolve_associated_item
Previously, the types looked like this: - None means this is not an associated item (but may be a variant field) - Some(Err) means this is known to be an error. I think the only way that can happen is if it resolved and but you had your own anchor. - Some(Ok(_, None)) was impossible. Now, this returns a nested Option and does the error handling and fiddling with the side channel in the caller. As a side-effect, it also removes duplicate error handling. This has one small change in behavior, which is that `resolve_primitive_associated_item` now goes through `variant_field` if it fails to resolve something. This is not ideal, but since it will be quickly rejected anyway, I think the performance hit is worth the cleanup. This also fixes a bug where struct fields would forget to set the side channel, adds a test for the bug, and ignores `private_intra_doc_links` in rustc_resolve (since it's always documented with --document-private-items).
1 parent ac04dbd commit 3b7e654

File tree

6 files changed

+103
-98
lines changed

6 files changed

+103
-98
lines changed

compiler/rustc_resolve/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#![feature(nll)]
1919
#![cfg_attr(bootstrap, feature(or_patterns))]
2020
#![recursion_limit = "256"]
21+
#![allow(rustdoc::private_intra_doc_links)]
2122

2223
pub use rustc_hir::def::{Namespace, PerNS};
2324

src/librustdoc/passes/collect_intra_doc_links.rs

+56-83
Original file line numberDiff line numberDiff line change
@@ -368,54 +368,28 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
368368
}
369369

370370
/// Given a primitive type, try to resolve an associated item.
371-
///
372-
/// HACK(jynelson): `item_str` is passed in instead of derived from `item_name` so the
373-
/// lifetimes on `&'path` will work.
374371
fn resolve_primitive_associated_item(
375372
&self,
376373
prim_ty: PrimitiveType,
377374
ns: Namespace,
378-
module_id: DefId,
379375
item_name: Symbol,
380-
) -> Result<(Res, Option<String>), ErrorKind<'path>> {
376+
) -> Option<(Res, String, Option<(DefKind, DefId)>)> {
381377
let tcx = self.cx.tcx;
382378

383-
prim_ty
384-
.impls(tcx)
385-
.into_iter()
386-
.find_map(|&impl_| {
387-
tcx.associated_items(impl_)
388-
.find_by_name_and_namespace(tcx, Ident::with_dummy_span(item_name), ns, impl_)
389-
.map(|item| {
390-
let kind = item.kind;
391-
self.kind_side_channel.set(Some((kind.as_def_kind(), item.def_id)));
392-
match kind {
393-
ty::AssocKind::Fn => "method",
394-
ty::AssocKind::Const => "associatedconstant",
395-
ty::AssocKind::Type => "associatedtype",
396-
}
397-
})
398-
.map(|out| {
399-
(
400-
Res::Primitive(prim_ty),
401-
Some(format!("{}#{}.{}", prim_ty.as_str(), out, item_name)),
402-
)
403-
})
404-
})
405-
.ok_or_else(|| {
406-
debug!(
407-
"returning primitive error for {}::{} in {} namespace",
408-
prim_ty.as_str(),
409-
item_name,
410-
ns.descr()
411-
);
412-
ResolutionFailure::NotResolved {
413-
module_id,
414-
partial_res: Some(Res::Primitive(prim_ty)),
415-
unresolved: item_name.to_string().into(),
416-
}
417-
.into()
418-
})
379+
prim_ty.impls(tcx).into_iter().find_map(|&impl_| {
380+
tcx.associated_items(impl_)
381+
.find_by_name_and_namespace(tcx, Ident::with_dummy_span(item_name), ns, impl_)
382+
.map(|item| {
383+
let kind = item.kind;
384+
let out = match kind {
385+
ty::AssocKind::Fn => "method",
386+
ty::AssocKind::Const => "associatedconstant",
387+
ty::AssocKind::Type => "associatedtype",
388+
};
389+
let fragment = format!("{}#{}.{}", prim_ty.as_str(), out, item_name);
390+
(Res::Primitive(prim_ty), fragment, Some((kind.as_def_kind(), item.def_id)))
391+
})
392+
})
419393
}
420394

421395
/// Resolves a string as a macro.
@@ -538,7 +512,21 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
538512
resolve_primitive(&path_root, TypeNS)
539513
.or_else(|| self.resolve_path(&path_root, TypeNS, module_id))
540514
.and_then(|ty_res| {
541-
self.resolve_associated_item(ty_res, item_name, ns, module_id, extra_fragment)
515+
let (res, fragment, side_channel) =
516+
self.resolve_associated_item(ty_res, item_name, ns, module_id)?;
517+
let result = if extra_fragment.is_some() {
518+
let diag_res = side_channel.map_or(res, |(k, r)| Res::Def(k, r));
519+
Err(ErrorKind::AnchorFailure(AnchorFailure::RustdocAnchorConflict(diag_res)))
520+
} else {
521+
// HACK(jynelson): `clean` expects the type, not the associated item
522+
// but the disambiguator logic expects the associated item.
523+
// Store the kind in a side channel so that only the disambiguator logic looks at it.
524+
if let Some((kind, id)) = side_channel {
525+
self.kind_side_channel.set(Some((kind, id)));
526+
}
527+
Ok((res, Some(fragment)))
528+
};
529+
Some(result)
542530
})
543531
.unwrap_or_else(|| {
544532
if ns == Namespace::ValueNS {
@@ -554,21 +542,21 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
554542
})
555543
}
556544

545+
/// Returns:
546+
/// - None if no associated item was found
547+
/// - Some((_, _, Some(_))) if an item was found and should go through a side channel
548+
/// - Some((_, _, None)) otherwise
557549
fn resolve_associated_item(
558550
&mut self,
559551
root_res: Res,
560552
item_name: Symbol,
561553
ns: Namespace,
562554
module_id: DefId,
563-
extra_fragment: &Option<String>,
564-
// lol this is so bad
565-
) -> Option<Result<(Res, Option<String>), ErrorKind<'static>>> {
555+
) -> Option<(Res, String, Option<(DefKind, DefId)>)> {
566556
let tcx = self.cx.tcx;
567557

568558
match root_res {
569-
Res::Primitive(prim) => {
570-
Some(self.resolve_primitive_associated_item(prim, ns, module_id, item_name))
571-
}
559+
Res::Primitive(prim) => self.resolve_primitive_associated_item(prim, ns, item_name),
572560
Res::Def(
573561
DefKind::Struct
574562
| DefKind::Union
@@ -611,17 +599,14 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
611599
ty::AssocKind::Const => "associatedconstant",
612600
ty::AssocKind::Type => "associatedtype",
613601
};
614-
return Some(if extra_fragment.is_some() {
615-
Err(ErrorKind::AnchorFailure(AnchorFailure::RustdocAnchorConflict(
616-
root_res,
617-
)))
618-
} else {
619-
// HACK(jynelson): `clean` expects the type, not the associated item
620-
// but the disambiguator logic expects the associated item.
621-
// Store the kind in a side channel so that only the disambiguator logic looks at it.
622-
self.kind_side_channel.set(Some((kind.as_def_kind(), id)));
623-
Ok((root_res, Some(format!("{}.{}", out, item_name))))
624-
});
602+
// HACK(jynelson): `clean` expects the type, not the associated item
603+
// but the disambiguator logic expects the associated item.
604+
// Store the kind in a side channel so that only the disambiguator logic looks at it.
605+
return Some((
606+
root_res,
607+
format!("{}.{}", out, item_name),
608+
Some((kind.as_def_kind(), id)),
609+
));
625610
}
626611

627612
if ns != Namespace::ValueNS {
@@ -640,22 +625,16 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
640625
} else {
641626
def.non_enum_variant().fields.iter().find(|item| item.ident.name == item_name)
642627
}?;
643-
Some(if extra_fragment.is_some() {
644-
let res = Res::Def(
645-
if def.is_enum() { DefKind::Variant } else { DefKind::Field },
646-
field.did,
647-
);
648-
Err(ErrorKind::AnchorFailure(AnchorFailure::RustdocAnchorConflict(res)))
649-
} else {
650-
Ok((
651-
root_res,
652-
Some(format!(
653-
"{}.{}",
654-
if def.is_enum() { "variant" } else { "structfield" },
655-
field.ident
656-
)),
657-
))
658-
})
628+
let kind = if def.is_enum() { DefKind::Variant } else { DefKind::Field };
629+
Some((
630+
root_res,
631+
format!(
632+
"{}.{}",
633+
if def.is_enum() { "variant" } else { "structfield" },
634+
field.ident
635+
),
636+
Some((kind, field.did)),
637+
))
659638
}
660639
Res::Def(DefKind::Trait, did) => tcx
661640
.associated_items(did)
@@ -673,14 +652,8 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
673652
}
674653
};
675654

676-
if extra_fragment.is_some() {
677-
Err(ErrorKind::AnchorFailure(AnchorFailure::RustdocAnchorConflict(
678-
root_res,
679-
)))
680-
} else {
681-
let res = Res::Def(item.kind.as_def_kind(), item.def_id);
682-
Ok((res, Some(format!("{}.{}", kind, item_name))))
683-
}
655+
let res = Res::Def(item.kind.as_def_kind(), item.def_id);
656+
(res, format!("{}.{}", kind, item_name), None)
684657
}),
685658
_ => None,
686659
}
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
11
warning: public documentation for `DocMe` links to private item `DontDocMe`
2-
--> $DIR/private.rs:5:11
2+
--> $DIR/private.rs:7:11
33
|
4-
LL | /// docs [DontDocMe] [DontDocMe::f]
4+
LL | /// docs [DontDocMe] [DontDocMe::f] [DontDocMe::x]
55
| ^^^^^^^^^ this item is private
66
|
77
= note: `#[warn(rustdoc::private_intra_doc_links)]` on by default
88
= note: this link resolves only because you passed `--document-private-items`, but will break without
99

1010
warning: public documentation for `DocMe` links to private item `DontDocMe::f`
11-
--> $DIR/private.rs:5:23
11+
--> $DIR/private.rs:7:23
1212
|
13-
LL | /// docs [DontDocMe] [DontDocMe::f]
13+
LL | /// docs [DontDocMe] [DontDocMe::f] [DontDocMe::x]
1414
| ^^^^^^^^^^^^ this item is private
1515
|
1616
= note: this link resolves only because you passed `--document-private-items`, but will break without
1717

18-
warning: 2 warnings emitted
18+
warning: public documentation for `DocMe` links to private item `DontDocMe::x`
19+
--> $DIR/private.rs:7:38
20+
|
21+
LL | /// docs [DontDocMe] [DontDocMe::f] [DontDocMe::x]
22+
| ^^^^^^^^^^^^ this item is private
23+
|
24+
= note: this link resolves only because you passed `--document-private-items`, but will break without
25+
26+
warning: 3 warnings emitted
1927

Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
11
warning: public documentation for `DocMe` links to private item `DontDocMe`
2-
--> $DIR/private.rs:5:11
2+
--> $DIR/private.rs:7:11
33
|
4-
LL | /// docs [DontDocMe] [DontDocMe::f]
4+
LL | /// docs [DontDocMe] [DontDocMe::f] [DontDocMe::x]
55
| ^^^^^^^^^ this item is private
66
|
77
= note: `#[warn(rustdoc::private_intra_doc_links)]` on by default
88
= note: this link will resolve properly if you pass `--document-private-items`
99

1010
warning: public documentation for `DocMe` links to private item `DontDocMe::f`
11-
--> $DIR/private.rs:5:23
11+
--> $DIR/private.rs:7:23
1212
|
13-
LL | /// docs [DontDocMe] [DontDocMe::f]
13+
LL | /// docs [DontDocMe] [DontDocMe::f] [DontDocMe::x]
1414
| ^^^^^^^^^^^^ this item is private
1515
|
1616
= note: this link will resolve properly if you pass `--document-private-items`
1717

18-
warning: 2 warnings emitted
18+
warning: public documentation for `DocMe` links to private item `DontDocMe::x`
19+
--> $DIR/private.rs:7:38
20+
|
21+
LL | /// docs [DontDocMe] [DontDocMe::f] [DontDocMe::x]
22+
| ^^^^^^^^^^^^ this item is private
23+
|
24+
= note: this link will resolve properly if you pass `--document-private-items`
25+
26+
warning: 3 warnings emitted
1927

src/test/rustdoc-ui/intra-doc/private.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22
// revisions: public private
33
// [private]compile-flags: --document-private-items
44

5-
/// docs [DontDocMe] [DontDocMe::f]
5+
// make sure to update `rustdoc/intra-doc/private.rs` if you update this file
6+
7+
/// docs [DontDocMe] [DontDocMe::f] [DontDocMe::x]
68
//~^ WARNING public documentation for `DocMe` links to private item `DontDocMe`
9+
//~| WARNING public documentation for `DocMe` links to private item `DontDocMe::x`
710
//~| WARNING public documentation for `DocMe` links to private item `DontDocMe::f`
8-
// FIXME: for [private] we should also make sure the link was actually generated
911
pub struct DocMe;
10-
struct DontDocMe;
12+
struct DontDocMe {
13+
x: usize,
14+
}
1115

1216
impl DontDocMe {
1317
fn f() {}

src/test/rustdoc/intra-doc/private.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
#![crate_name = "private"]
22
// compile-flags: --document-private-items
3-
/// docs [DontDocMe]
3+
4+
// make sure to update `rustdoc-ui/intra-doc/private.rs` if you update this file
5+
6+
/// docs [DontDocMe] [DontDocMe::f] [DontDocMe::x]
47
// @has private/struct.DocMe.html '//*a[@href="../private/struct.DontDocMe.html"]' 'DontDocMe'
8+
// @has private/struct.DocMe.html '//*a[@href="../private/struct.DontDocMe.html#method.f"]' 'DontDocMe::f'
9+
// @has private/struct.DocMe.html '//*a[@href="../private/struct.DontDocMe.html#structfield.x"]' 'DontDocMe::x'
510
pub struct DocMe;
6-
struct DontDocMe;
11+
struct DontDocMe {
12+
x: usize,
13+
}
14+
15+
impl DontDocMe {
16+
fn f() {}
17+
}

0 commit comments

Comments
 (0)