From 6d590ba9426f60459c66d19f73b52f774e23e308 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Tue, 26 Apr 2022 21:33:17 +0300 Subject: [PATCH] rustdoc: Resolve doc links on fields during early resolution --- .../passes/collect_intra_doc_links/early.rs | 14 +++++++++- src/test/rustdoc-ui/intra-doc/assoc-field.rs | 26 +++++++++++++++++++ .../intra-doc/auxiliary/assoc-field-dep.rs | 18 +++++++++++++ 3 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 src/test/rustdoc-ui/intra-doc/assoc-field.rs create mode 100644 src/test/rustdoc-ui/intra-doc/auxiliary/assoc-field-dep.rs diff --git a/src/librustdoc/passes/collect_intra_doc_links/early.rs b/src/librustdoc/passes/collect_intra_doc_links/early.rs index 68e10e3a18c7e..1b80f6b93dc78 100644 --- a/src/librustdoc/passes/collect_intra_doc_links/early.rs +++ b/src/librustdoc/passes/collect_intra_doc_links/early.rs @@ -260,10 +260,22 @@ impl EarlyDocLinkResolver<'_, '_> { if let Res::Def(DefKind::Mod, ..) = child.res { self.resolve_doc_links_extern_inner(def_id); // Inner attribute scope } - // Traits are processed in `add_extern_traits_in_scope`. + // `DefKind::Trait`s are processed in `process_extern_impls`. if let Res::Def(DefKind::Mod | DefKind::Enum, ..) = child.res { self.process_module_children_or_reexports(def_id); } + if let Res::Def(DefKind::Struct | DefKind::Union | DefKind::Variant, _) = + child.res + { + let field_def_ids = Vec::from_iter( + self.resolver + .cstore() + .associated_item_def_ids_untracked(def_id, self.sess), + ); + for field_def_id in field_def_ids { + self.resolve_doc_links_extern_outer(field_def_id, scope_id); + } + } } } } diff --git a/src/test/rustdoc-ui/intra-doc/assoc-field.rs b/src/test/rustdoc-ui/intra-doc/assoc-field.rs new file mode 100644 index 0000000000000..e18404e44306e --- /dev/null +++ b/src/test/rustdoc-ui/intra-doc/assoc-field.rs @@ -0,0 +1,26 @@ +// Traits in scope are collected for doc links in field attributes. + +// check-pass +// aux-build: assoc-field-dep.rs + +extern crate assoc_field_dep; +pub use assoc_field_dep::*; + +#[derive(Clone)] +pub struct Struct; + +pub mod mod1 { + pub struct Fields { + /// [crate::Struct::clone] + pub field: u8, + } +} + +pub mod mod2 { + pub enum Fields { + V { + /// [crate::Struct::clone] + field: u8, + }, + } +} diff --git a/src/test/rustdoc-ui/intra-doc/auxiliary/assoc-field-dep.rs b/src/test/rustdoc-ui/intra-doc/auxiliary/assoc-field-dep.rs new file mode 100644 index 0000000000000..cfb24fc2c665c --- /dev/null +++ b/src/test/rustdoc-ui/intra-doc/auxiliary/assoc-field-dep.rs @@ -0,0 +1,18 @@ +#[derive(Clone)] +pub struct Struct; + +pub mod dep_mod1 { + pub struct Fields { + /// [crate::Struct::clone] + pub field: u8, + } +} + +pub mod dep_mod2 { + pub enum Fields { + V { + /// [crate::Struct::clone] + field: u8, + }, + } +}