Fix the bugs and add a regression test
- All attributes for an item need to be considered at once, they can't be considered a line at a time. - The top-level crate was not being visited. This bug was caught by `extern-crate-used-only-in-link`, which I'm very glad I added. - Make the loader private to the module, so that only one function is exposed.
This commit is contained in:
parent
d933edd5c6
commit
c60a370dac
5 changed files with 39 additions and 23 deletions
|
@ -1,4 +1,3 @@
|
||||||
use rustc_ast as ast;
|
|
||||||
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
|
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
|
||||||
use rustc_data_structures::sync::{self, Lrc};
|
use rustc_data_structures::sync::{self, Lrc};
|
||||||
use rustc_driver::abort_on_err;
|
use rustc_driver::abort_on_err;
|
||||||
|
@ -307,10 +306,7 @@ crate fn create_resolver<'a>(
|
||||||
let (krate, resolver, _) = &*abort_on_err(queries.expansion(), sess).peek();
|
let (krate, resolver, _) = &*abort_on_err(queries.expansion(), sess).peek();
|
||||||
let resolver = resolver.clone();
|
let resolver = resolver.clone();
|
||||||
|
|
||||||
let mut loader = crate::passes::collect_intra_doc_links::IntraLinkCrateLoader::new(resolver);
|
crate::passes::collect_intra_doc_links::load_intra_link_crates(resolver, krate)
|
||||||
ast::visit::walk_crate(&mut loader, krate);
|
|
||||||
|
|
||||||
loader.resolver
|
|
||||||
}
|
}
|
||||||
|
|
||||||
crate fn run_global_ctxt(
|
crate fn run_global_ctxt(
|
||||||
|
|
|
@ -38,7 +38,7 @@ use crate::lint::{BROKEN_INTRA_DOC_LINKS, PRIVATE_INTRA_DOC_LINKS};
|
||||||
use crate::passes::Pass;
|
use crate::passes::Pass;
|
||||||
|
|
||||||
mod early;
|
mod early;
|
||||||
crate use early::IntraLinkCrateLoader;
|
crate use early::load_intra_link_crates;
|
||||||
|
|
||||||
crate const COLLECT_INTRA_DOC_LINKS: Pass = Pass {
|
crate const COLLECT_INTRA_DOC_LINKS: Pass = Pass {
|
||||||
name: "collect-intra-doc-links",
|
name: "collect-intra-doc-links",
|
||||||
|
|
|
@ -1,34 +1,41 @@
|
||||||
use rustc_ast as ast;
|
use rustc_ast as ast;
|
||||||
use rustc_hir::def::Namespace::TypeNS;
|
use rustc_hir::def::Namespace::TypeNS;
|
||||||
use rustc_hir::def_id::{DefId, LocalDefId, CRATE_DEF_INDEX};
|
use rustc_hir::def_id::{LocalDefId, CRATE_DEF_ID};
|
||||||
use rustc_interface::interface;
|
use rustc_interface::interface;
|
||||||
|
use rustc_span::Span;
|
||||||
|
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::mem;
|
use std::mem;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
|
type Resolver = Rc<RefCell<interface::BoxedResolver>>;
|
||||||
// Letting the resolver escape at the end of the function leads to inconsistencies between the
|
// Letting the resolver escape at the end of the function leads to inconsistencies between the
|
||||||
// crates the TyCtxt sees and the resolver sees (because the resolver could load more crates
|
// crates the TyCtxt sees and the resolver sees (because the resolver could load more crates
|
||||||
// after escaping). Hopefully `IntraLinkCrateLoader` gets all the crates we need ...
|
// after escaping). Hopefully `IntraLinkCrateLoader` gets all the crates we need ...
|
||||||
crate struct IntraLinkCrateLoader {
|
crate fn load_intra_link_crates(resolver: Resolver, krate: &ast::Crate) -> Resolver {
|
||||||
current_mod: DefId,
|
let mut loader = IntraLinkCrateLoader { current_mod: CRATE_DEF_ID, resolver };
|
||||||
crate resolver: Rc<RefCell<interface::BoxedResolver>>,
|
// `walk_crate` doesn't visit the crate itself for some reason.
|
||||||
|
loader.load_links_in_attrs(&krate.attrs, krate.span);
|
||||||
|
ast::visit::walk_crate(&mut loader, krate);
|
||||||
|
loader.resolver
|
||||||
|
}
|
||||||
|
|
||||||
|
struct IntraLinkCrateLoader {
|
||||||
|
current_mod: LocalDefId,
|
||||||
|
resolver: Rc<RefCell<interface::BoxedResolver>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl IntraLinkCrateLoader {
|
impl IntraLinkCrateLoader {
|
||||||
crate fn new(resolver: Rc<RefCell<interface::BoxedResolver>>) -> Self {
|
fn load_links_in_attrs(&mut self, attrs: &[ast::Attribute], span: Span) {
|
||||||
let crate_id = LocalDefId { local_def_index: CRATE_DEF_INDEX }.to_def_id();
|
|
||||||
Self { current_mod: crate_id, resolver }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ast::visit::Visitor<'_> for IntraLinkCrateLoader {
|
|
||||||
fn visit_attribute(&mut self, attr: &ast::Attribute) {
|
|
||||||
use crate::html::markdown::markdown_links;
|
use crate::html::markdown::markdown_links;
|
||||||
use crate::passes::collect_intra_doc_links::preprocess_link;
|
use crate::passes::collect_intra_doc_links::preprocess_link;
|
||||||
|
|
||||||
if let Some(doc) = attr.doc_str() {
|
// FIXME: this probably needs to consider inlining
|
||||||
|
let attrs = crate::clean::Attributes::from_ast(attrs, None);
|
||||||
|
for (parent_module, doc) in attrs.collapsed_doc_value_by_module_level() {
|
||||||
|
debug!(?doc);
|
||||||
for link in markdown_links(&doc.as_str()) {
|
for link in markdown_links(&doc.as_str()) {
|
||||||
|
debug!(?link.link);
|
||||||
let path_str = if let Some(Ok(x)) = preprocess_link(&link) {
|
let path_str = if let Some(Ok(x)) = preprocess_link(&link) {
|
||||||
x.path_str
|
x.path_str
|
||||||
} else {
|
} else {
|
||||||
|
@ -36,27 +43,32 @@ impl ast::visit::Visitor<'_> for IntraLinkCrateLoader {
|
||||||
};
|
};
|
||||||
self.resolver.borrow_mut().access(|resolver| {
|
self.resolver.borrow_mut().access(|resolver| {
|
||||||
let _ = resolver.resolve_str_path_error(
|
let _ = resolver.resolve_str_path_error(
|
||||||
attr.span,
|
span,
|
||||||
&path_str,
|
&path_str,
|
||||||
TypeNS,
|
TypeNS,
|
||||||
self.current_mod,
|
parent_module.unwrap_or(self.current_mod.to_def_id()),
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ast::visit::walk_attribute(self, attr);
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ast::visit::Visitor<'_> for IntraLinkCrateLoader {
|
||||||
fn visit_item(&mut self, item: &ast::Item) {
|
fn visit_item(&mut self, item: &ast::Item) {
|
||||||
use rustc_ast_lowering::ResolverAstLowering;
|
use rustc_ast_lowering::ResolverAstLowering;
|
||||||
|
|
||||||
if let ast::ItemKind::Mod(..) = item.kind {
|
if let ast::ItemKind::Mod(..) = item.kind {
|
||||||
let new_mod =
|
let new_mod =
|
||||||
self.resolver.borrow_mut().access(|resolver| resolver.local_def_id(item.id));
|
self.resolver.borrow_mut().access(|resolver| resolver.local_def_id(item.id));
|
||||||
let old_mod = mem::replace(&mut self.current_mod, new_mod.to_def_id());
|
let old_mod = mem::replace(&mut self.current_mod, new_mod);
|
||||||
|
|
||||||
|
self.load_links_in_attrs(&item.attrs, item.span);
|
||||||
ast::visit::walk_item(self, item);
|
ast::visit::walk_item(self, item);
|
||||||
|
|
||||||
self.current_mod = old_mod;
|
self.current_mod = old_mod;
|
||||||
} else {
|
} else {
|
||||||
|
self.load_links_in_attrs(&item.attrs, item.span);
|
||||||
ast::visit::walk_item(self, item);
|
ast::visit::walk_item(self, item);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
1
src/test/rustdoc/intra-doc/auxiliary/pub-struct.rs
Normal file
1
src/test/rustdoc/intra-doc/auxiliary/pub-struct.rs
Normal file
|
@ -0,0 +1 @@
|
||||||
|
pub struct SomeStruct;
|
7
src/test/rustdoc/intra-doc/extern-reference-link.rs
Normal file
7
src/test/rustdoc/intra-doc/extern-reference-link.rs
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
// compile-flags: --extern pub_struct
|
||||||
|
// aux-build:pub-struct.rs
|
||||||
|
|
||||||
|
/// [SomeStruct]
|
||||||
|
///
|
||||||
|
/// [SomeStruct]: pub_struct::SomeStruct
|
||||||
|
pub fn foo() {}
|
Loading…
Add table
Add a link
Reference in a new issue