Skip to content

Commit 3b08662

Browse files
committed
Stop resolving doc links on mod items twice
1 parent b62b82a commit 3b08662

File tree

3 files changed

+19
-15
lines changed

3 files changed

+19
-15
lines changed

compiler/rustc_resolve/src/late.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -2199,7 +2199,11 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
21992199
}
22002200

22012201
fn resolve_item(&mut self, item: &'ast Item) {
2202-
self.resolve_doc_links(&item.attrs);
2202+
let mod_inner_docs =
2203+
matches!(item.kind, ItemKind::Mod(..)) && rustdoc::inner_docs(&item.attrs);
2204+
if !mod_inner_docs {
2205+
self.resolve_doc_links(&item.attrs);
2206+
}
22032207

22042208
let name = item.ident.name;
22052209
debug!("(resolving item) resolving {} ({:?})", name, item.kind);
@@ -2292,7 +2296,9 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
22922296

22932297
ItemKind::Mod(..) => {
22942298
self.with_scope(item.id, |this| {
2295-
this.resolve_doc_links(&item.attrs);
2299+
if mod_inner_docs {
2300+
this.resolve_doc_links(&item.attrs);
2301+
}
22962302
let old_macro_rules = this.parent_scope.macro_rules;
22972303
visit::walk_item(this, item);
22982304
// Maintain macro_rules scopes in the same way as during early resolution

compiler/rustc_resolve/src/rustdoc.rs

+8
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,14 @@ pub fn strip_generics_from_path(path_str: &str) -> Result<String, MalformedGener
326326
if !stripped_path.is_empty() { Ok(stripped_path) } else { Err(MalformedGenerics::MissingType) }
327327
}
328328

329+
/// Returns whether the first doc-comment is an inner attribute.
330+
///
331+
//// If there are no doc-comments, return true.
332+
/// FIXME(#78591): Support both inner and outer attributes on the same item.
333+
pub fn inner_docs(attrs: &[ast::Attribute]) -> bool {
334+
attrs.iter().find(|a| a.doc_str().is_some()).map_or(true, |a| a.style == ast::AttrStyle::Inner)
335+
}
336+
329337
/// Simplified version of the corresponding function in rustdoc.
330338
/// If the rustdoc version returns a successful result, this function must return the same result.
331339
/// Otherwise this function may return anything.

src/librustdoc/clean/types.rs

+3-13
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::{fmt, iter};
1010
use arrayvec::ArrayVec;
1111
use thin_vec::ThinVec;
1212

13-
use rustc_ast::{self as ast, AttrStyle};
13+
use rustc_ast as ast;
1414
use rustc_attr::{ConstStability, Deprecation, Stability, StabilityLevel};
1515
use rustc_const_eval::const_eval::is_unstable_const_fn;
1616
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
@@ -23,7 +23,7 @@ use rustc_hir_analysis::check::intrinsic::intrinsic_operation_unsafety;
2323
use rustc_index::vec::IndexVec;
2424
use rustc_middle::ty::fast_reject::SimplifiedType;
2525
use rustc_middle::ty::{self, DefIdTree, TyCtxt, Visibility};
26-
use rustc_resolve::rustdoc::{add_doc_fragment, attrs_to_doc_fragments, DocFragment};
26+
use rustc_resolve::rustdoc::{add_doc_fragment, attrs_to_doc_fragments, inner_docs, DocFragment};
2727
use rustc_session::Session;
2828
use rustc_span::hygiene::MacroKind;
2929
use rustc_span::symbol::{kw, sym, Ident, Symbol};
@@ -405,7 +405,7 @@ impl Item {
405405
pub(crate) fn inner_docs(&self, tcx: TyCtxt<'_>) -> bool {
406406
self.item_id
407407
.as_def_id()
408-
.map(|did| tcx.get_attrs_unchecked(did).inner_docs())
408+
.map(|did| inner_docs(tcx.get_attrs_unchecked(did)))
409409
.unwrap_or(false)
410410
}
411411

@@ -874,8 +874,6 @@ pub(crate) trait AttributesExt {
874874

875875
fn span(&self) -> Option<rustc_span::Span>;
876876

877-
fn inner_docs(&self) -> bool;
878-
879877
fn cfg(&self, tcx: TyCtxt<'_>, hidden_cfg: &FxHashSet<Cfg>) -> Option<Arc<Cfg>>;
880878
}
881879

@@ -894,14 +892,6 @@ impl AttributesExt for [ast::Attribute] {
894892
self.iter().find(|attr| attr.doc_str().is_some()).map(|attr| attr.span)
895893
}
896894

897-
/// Returns whether the first doc-comment is an inner attribute.
898-
///
899-
//// If there are no doc-comments, return true.
900-
/// FIXME(#78591): Support both inner and outer attributes on the same item.
901-
fn inner_docs(&self) -> bool {
902-
self.iter().find(|a| a.doc_str().is_some()).map_or(true, |a| a.style == AttrStyle::Inner)
903-
}
904-
905895
fn cfg(&self, tcx: TyCtxt<'_>, hidden_cfg: &FxHashSet<Cfg>) -> Option<Arc<Cfg>> {
906896
let sess = tcx.sess;
907897
let doc_cfg_active = tcx.features().doc_cfg;

0 commit comments

Comments
 (0)