diff --git a/compiler/rustc_resolve/src/late.rs b/compiler/rustc_resolve/src/late.rs
index e1787eadf5f..4050ec8eee5 100644
--- a/compiler/rustc_resolve/src/late.rs
+++ b/compiler/rustc_resolve/src/late.rs
@@ -12,6 +12,7 @@ use std::debug_assert_matches;
use std::mem::{replace, swap, take};
use std::ops::{ControlFlow, Range};
+use rustc_ast::attr::AttributeExt;
use rustc_ast::visit::{
AssocCtxt, BoundKind, FnCtxt, FnKind, Visitor, try_visit, visit_opt, walk_list,
};
@@ -2860,10 +2861,22 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
}
fn resolve_item(&mut self, item: &'ast Item) {
- let mod_inner_docs =
- matches!(item.kind, ItemKind::Mod(..)) && rustdoc::inner_docs(&item.attrs);
- if !mod_inner_docs && !matches!(item.kind, ItemKind::Impl(..) | ItemKind::Use(..)) {
- self.resolve_doc_links(&item.attrs, MaybeExported::Ok(item.id));
+ match item.kind {
+ ItemKind::Mod(..) => {
+ // We only handle outer doc comments for modules here.
+ let attrs = if let Some(pos) = item.attrs.iter().position(|a| {
+ a.doc_resolution_scope().is_some_and(|style| style == AttrStyle::Inner)
+ }) {
+ &item.attrs[..pos]
+ } else {
+ &item.attrs
+ };
+ self.resolve_doc_links(attrs, MaybeExported::Ok(item.id));
+ }
+ ItemKind::Impl(..) | ItemKind::Use(..) => {}
+ _ => {
+ self.resolve_doc_links(&item.attrs, MaybeExported::Ok(item.id));
+ }
}
debug!("(resolving item) resolving {:?} ({:?})", item.kind.ident(), item.kind);
@@ -2957,9 +2970,16 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
let orig_module = replace(&mut self.parent_scope.module, module);
self.with_rib(ValueNS, RibKind::Module(module.expect_local()), |this| {
this.with_rib(TypeNS, RibKind::Module(module.expect_local()), |this| {
- if mod_inner_docs {
- this.resolve_doc_links(&item.attrs, MaybeExported::Ok(item.id));
- }
+ // Outer doc comments were already handled above, now we handle
+ // inner doc comments.
+ let attrs = if let Some(pos) = item.attrs.iter().position(|a| {
+ a.doc_resolution_scope().is_some_and(|style| style == AttrStyle::Inner)
+ }) {
+ &item.attrs[pos..]
+ } else {
+ &[]
+ };
+ this.resolve_doc_links(attrs, MaybeExported::Ok(item.id));
let old_macro_rules = this.parent_scope.macro_rules;
visit::walk_item(this, item);
// Maintain macro_rules scopes in the same way as during early resolution
diff --git a/compiler/rustc_resolve/src/rustdoc.rs b/compiler/rustc_resolve/src/rustdoc.rs
index ddf5600f766..ac463297ad8 100644
--- a/compiler/rustc_resolve/src/rustdoc.rs
+++ b/compiler/rustc_resolve/src/rustdoc.rs
@@ -9,9 +9,9 @@ use pulldown_cmark::{
};
use rustc_ast as ast;
use rustc_ast::attr::AttributeExt;
-use rustc_ast::join_path_syms;
use rustc_ast::token::DocFragmentKind;
use rustc_ast::util::comments::beautify_doc_string;
+use rustc_ast::{AttrStyle, join_path_syms};
use rustc_data_structures::fx::FxIndexMap;
use rustc_data_structures::unord::UnordSet;
use rustc_middle::ty::TyCtxt;
@@ -48,6 +48,7 @@ pub struct DocFragment {
/// Because we tamper with the spans context, this information cannot be correctly retrieved
/// later on. So instead, we compute it and store it here.
pub from_expansion: bool,
+ pub style: AttrStyle,
}
#[derive(Clone, Copy, Debug)]
@@ -215,8 +216,15 @@ pub fn attrs_to_doc_fragments<'a, A: AttributeExt + Clone + 'a>(
(value_span.with_ctxt(attr_span.ctxt()), value_span.from_expansion())
}
};
- let fragment =
- DocFragment { span, doc, kind: fragment_kind, item_id, indent: 0, from_expansion };
+ let fragment = DocFragment {
+ span,
+ doc,
+ kind: fragment_kind,
+ item_id,
+ indent: 0,
+ from_expansion,
+ style: attr.doc_resolution_scope().unwrap(),
+ };
doc_fragments.push(fragment);
} else if !doc_only {
other_attrs.push(attr.clone());
@@ -231,6 +239,14 @@ pub fn attrs_to_doc_fragments<'a, A: AttributeExt + Clone + 'a>(
(doc_fragments, other_attrs)
}
+/// Represents a doc comment, split in two parts, outer and inner attributes. It's needed for
+/// the intra-doc link resolution context.
+#[derive(Default)]
+pub struct DocStrings {
+ pub outer: String,
+ pub inner: String,
+}
+
/// Return the doc-comments on this item, grouped by the module they came from.
/// The module can be different if this is a re-export with added documentation.
///
@@ -238,11 +254,16 @@ pub fn attrs_to_doc_fragments<'a, A: AttributeExt + Clone + 'a>(
/// early and late doc link resolution regardless of their position.
pub fn prepare_to_doc_link_resolution(
doc_fragments: &[DocFragment],
-) -> FxIndexMap