Skip to content

Commit 3903ca1

Browse files
committed
rustdoc: load the set of in-scope traits for modules with no docstring
Fixes #93428 This fix is a response to a couple of special cases related to the `module_id`, which is eventually used for trait candidates: * The module id is always set to the current crate, when checking `crate::`. Normally, the set of in-scope traits would be set in `load_links_in_attrs`, but if there are no doc comments, then that loop will never run. * the module id is set to the parent module, when resolving a module that is spelled like this: // Notice how we use an outlined doc comment here! // [`Test::my_fn`] mod something { } As with the above problem with `crate::`, we need to make sure the module gets its traits in scope resolved, even if it has no doc comments of its own.
1 parent 427eba2 commit 3903ca1

File tree

4 files changed

+41
-0
lines changed

4 files changed

+41
-0
lines changed

src/librustdoc/passes/collect_intra_doc_links.rs

+1
Original file line numberDiff line numberDiff line change
@@ -920,6 +920,7 @@ fn trait_assoc_to_impl_assoc_item<'tcx>(
920920
///
921921
/// NOTE: this cannot be a query because more traits could be available when more crates are compiled!
922922
/// So it is not stable to serialize cross-crate.
923+
#[instrument(level = "debug", skip(cx))]
923924
fn trait_impls_for<'a>(
924925
cx: &mut DocContext<'a>,
925926
ty: Ty<'a>,

src/librustdoc/passes/collect_intra_doc_links/early.rs

+10
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ crate fn early_resolve_intra_doc_links(
3232
all_trait_impls: Default::default(),
3333
};
3434

35+
// Because of the `crate::` prefix, any doc comment can reference
36+
// the crate root's set of in-scope traits. This line makes sure
37+
// it's available.
38+
loader.add_traits_in_scope(CRATE_DEF_ID.to_def_id());
39+
3540
// Overridden `visit_item` below doesn't apply to the crate root,
3641
// so we have to visit its attributes and reexports separately.
3742
loader.load_links_in_attrs(&krate.attrs, krate.span);
@@ -180,6 +185,11 @@ impl Visitor<'_> for IntraLinkCrateLoader<'_, '_> {
180185
if let ItemKind::Mod(..) = item.kind {
181186
let old_mod = mem::replace(&mut self.current_mod, self.resolver.local_def_id(item.id));
182187

188+
// A module written with a outline doc comments will resolve traits relative
189+
// to the parent module. Make sure the parent module's traits-in-scope are
190+
// loaded, even if the module itself has no doc comments.
191+
self.add_traits_in_parent_scope(self.current_mod.to_def_id());
192+
183193
self.load_links_in_attrs(&item.attrs, item.span);
184194
self.process_module_children_or_reexports(self.current_mod.to_def_id());
185195
visit::walk_item(self, item);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
pub struct Test<'a> {
2+
data: &'a (),
3+
}
4+
5+
impl<'a> Test<'a> {
6+
pub fn do_test(&self) {}
7+
}
8+
9+
// @has crate_relative/demo/index.html
10+
// @has - '//a/@href' '../struct.Test.html#method.do_test'
11+
pub mod demo {
12+
//! [`crate::Test::do_test`]
13+
}
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
pub mod wrapper {
2+
3+
pub struct Test<'a> {
4+
data: &'a (),
5+
}
6+
7+
impl<'a> Test<'a> {
8+
pub fn do_test(&self) {}
9+
}
10+
11+
// @has mod_relative/wrapper/demo/index.html
12+
// @has - '//a/@href' '../struct.Test.html#method.do_test'
13+
/// [`Test::do_test`]
14+
pub mod demo {
15+
}
16+
17+
}

0 commit comments

Comments
 (0)