Skip to content

Commit 14fb87a

Browse files
authored
Rollup merge of #88215 - jyn514:lazy-loading, r=petrochenkov
Reland #83738: "rustdoc: Don't load all extern crates unconditionally" I hopefully found all the bugs 🤞 time for a take two. See the last commit for details on what went wrong before. r? `@petrochenkov` (but feel free to reassign to Guillaume if you don't have time.) Closes #68427. Includes a fix for #84738.
2 parents 8aa46e5 + c60a370 commit 14fb87a

File tree

11 files changed

+116
-41
lines changed

11 files changed

+116
-41
lines changed

src/librustdoc/core.rs

+5-34
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ use rustc_driver::abort_on_err;
44
use rustc_errors::emitter::{Emitter, EmitterWriter};
55
use rustc_errors::json::JsonEmitter;
66
use rustc_feature::UnstableFeatures;
7-
use rustc_hir::def::Namespace::TypeNS;
87
use rustc_hir::def::Res;
9-
use rustc_hir::def_id::{DefId, LocalDefId, CRATE_DEF_INDEX};
8+
use rustc_hir::def_id::{DefId, LocalDefId};
109
use rustc_hir::HirId;
1110
use rustc_hir::{
1211
intravisit::{self, NestedVisitorMap, Visitor},
@@ -23,7 +22,7 @@ use rustc_session::DiagnosticOutput;
2322
use rustc_session::Session;
2423
use rustc_span::source_map;
2524
use rustc_span::symbol::sym;
26-
use rustc_span::{Span, DUMMY_SP};
25+
use rustc_span::Span;
2726

2827
use std::cell::RefCell;
2928
use std::mem;
@@ -301,41 +300,13 @@ crate fn create_config(
301300
}
302301

303302
crate fn create_resolver<'a>(
304-
externs: config::Externs,
305303
queries: &Queries<'a>,
306304
sess: &Session,
307305
) -> Rc<RefCell<interface::BoxedResolver>> {
308-
let extern_names: Vec<String> = externs
309-
.iter()
310-
.filter(|(_, entry)| entry.add_prelude)
311-
.map(|(name, _)| name)
312-
.cloned()
313-
.collect();
314-
315-
let (_, resolver, _) = &*abort_on_err(queries.expansion(), sess).peek();
316-
317-
// Before we actually clone it, let's force all the extern'd crates to
318-
// actually be loaded, just in case they're only referred to inside
319-
// intra-doc links
320-
resolver.borrow_mut().access(|resolver| {
321-
sess.time("load_extern_crates", || {
322-
for extern_name in &extern_names {
323-
debug!("loading extern crate {}", extern_name);
324-
if let Err(()) = resolver
325-
.resolve_str_path_error(
326-
DUMMY_SP,
327-
extern_name,
328-
TypeNS,
329-
LocalDefId { local_def_index: CRATE_DEF_INDEX }.to_def_id(),
330-
) {
331-
warn!("unable to resolve external crate {} (do you have an unused `--extern` crate?)", extern_name)
332-
}
333-
}
334-
});
335-
});
306+
let (krate, resolver, _) = &*abort_on_err(queries.expansion(), sess).peek();
307+
let resolver = resolver.clone();
336308

337-
// Now we're good to clone the resolver because everything should be loaded
338-
resolver.clone()
309+
crate::passes::collect_intra_doc_links::load_intra_link_crates(resolver, krate)
339310
}
340311

341312
crate fn run_global_ctxt(

src/librustdoc/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ extern crate tracing;
3030
// Dependencies listed in Cargo.toml do not need `extern crate`.
3131

3232
extern crate rustc_ast;
33+
extern crate rustc_ast_lowering;
3334
extern crate rustc_ast_pretty;
3435
extern crate rustc_attr;
3536
extern crate rustc_data_structures;
@@ -724,7 +725,6 @@ fn main_options(options: config::Options) -> MainResult {
724725
let default_passes = options.default_passes;
725726
let output_format = options.output_format;
726727
// FIXME: fix this clone (especially render_options)
727-
let externs = options.externs.clone();
728728
let manual_passes = options.manual_passes.clone();
729729
let render_options = options.render_options.clone();
730730
let config = core::create_config(options);
@@ -742,7 +742,7 @@ fn main_options(options: config::Options) -> MainResult {
742742
// We need to hold on to the complete resolver, so we cause everything to be
743743
// cloned for the analysis passes to use. Suboptimal, but necessary in the
744744
// current architecture.
745-
let resolver = core::create_resolver(externs, queries, &sess);
745+
let resolver = core::create_resolver(queries, &sess);
746746

747747
if sess.has_errors() {
748748
sess.fatal("Compilation failed, aborting rustdoc");

src/librustdoc/passes/collect_intra_doc_links.rs

+3
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ use crate::html::markdown::{markdown_links, MarkdownLink};
3737
use crate::lint::{BROKEN_INTRA_DOC_LINKS, PRIVATE_INTRA_DOC_LINKS};
3838
use crate::passes::Pass;
3939

40+
mod early;
41+
crate use early::load_intra_link_crates;
42+
4043
crate const COLLECT_INTRA_DOC_LINKS: Pass = Pass {
4144
name: "collect-intra-doc-links",
4245
run: collect_intra_doc_links,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
use rustc_ast as ast;
2+
use rustc_hir::def::Namespace::TypeNS;
3+
use rustc_hir::def_id::{LocalDefId, CRATE_DEF_ID};
4+
use rustc_interface::interface;
5+
use rustc_span::Span;
6+
7+
use std::cell::RefCell;
8+
use std::mem;
9+
use std::rc::Rc;
10+
11+
type Resolver = Rc<RefCell<interface::BoxedResolver>>;
12+
// Letting the resolver escape at the end of the function leads to inconsistencies between the
13+
// crates the TyCtxt sees and the resolver sees (because the resolver could load more crates
14+
// after escaping). Hopefully `IntraLinkCrateLoader` gets all the crates we need ...
15+
crate fn load_intra_link_crates(resolver: Resolver, krate: &ast::Crate) -> Resolver {
16+
let mut loader = IntraLinkCrateLoader { current_mod: CRATE_DEF_ID, resolver };
17+
// `walk_crate` doesn't visit the crate itself for some reason.
18+
loader.load_links_in_attrs(&krate.attrs, krate.span);
19+
ast::visit::walk_crate(&mut loader, krate);
20+
loader.resolver
21+
}
22+
23+
struct IntraLinkCrateLoader {
24+
current_mod: LocalDefId,
25+
resolver: Rc<RefCell<interface::BoxedResolver>>,
26+
}
27+
28+
impl IntraLinkCrateLoader {
29+
fn load_links_in_attrs(&mut self, attrs: &[ast::Attribute], span: Span) {
30+
use crate::html::markdown::markdown_links;
31+
use crate::passes::collect_intra_doc_links::preprocess_link;
32+
33+
// FIXME: this probably needs to consider inlining
34+
let attrs = crate::clean::Attributes::from_ast(attrs, None);
35+
for (parent_module, doc) in attrs.collapsed_doc_value_by_module_level() {
36+
debug!(?doc);
37+
for link in markdown_links(&doc.as_str()) {
38+
debug!(?link.link);
39+
let path_str = if let Some(Ok(x)) = preprocess_link(&link) {
40+
x.path_str
41+
} else {
42+
continue;
43+
};
44+
self.resolver.borrow_mut().access(|resolver| {
45+
let _ = resolver.resolve_str_path_error(
46+
span,
47+
&path_str,
48+
TypeNS,
49+
parent_module.unwrap_or(self.current_mod.to_def_id()),
50+
);
51+
});
52+
}
53+
}
54+
}
55+
}
56+
57+
impl ast::visit::Visitor<'_> for IntraLinkCrateLoader {
58+
fn visit_item(&mut self, item: &ast::Item) {
59+
use rustc_ast_lowering::ResolverAstLowering;
60+
61+
if let ast::ItemKind::Mod(..) = item.kind {
62+
let new_mod =
63+
self.resolver.borrow_mut().access(|resolver| resolver.local_def_id(item.id));
64+
let old_mod = mem::replace(&mut self.current_mod, new_mod);
65+
66+
self.load_links_in_attrs(&item.attrs, item.span);
67+
ast::visit::walk_item(self, item);
68+
69+
self.current_mod = old_mod;
70+
} else {
71+
self.load_links_in_attrs(&item.attrs, item.span);
72+
ast::visit::walk_item(self, item);
73+
}
74+
}
75+
}

src/librustdoc/passes/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ crate use self::unindent_comments::UNINDENT_COMMENTS;
3030
mod propagate_doc_cfg;
3131
crate use self::propagate_doc_cfg::PROPAGATE_DOC_CFG;
3232

33-
mod collect_intra_doc_links;
33+
crate mod collect_intra_doc_links;
3434
crate use self::collect_intra_doc_links::COLLECT_INTRA_DOC_LINKS;
3535

3636
mod doc_test_lints;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// no-prefer-dynamic
2+
#![crate_type = "lib"]
3+
#![no_std]
4+
#![feature(lang_items)]
5+
6+
use core::panic::PanicInfo;
7+
use core::sync::atomic::{self, Ordering};
8+
9+
#[panic_handler]
10+
fn panic(_info: &PanicInfo) -> ! {
11+
loop {
12+
atomic::compiler_fence(Ordering::SeqCst);
13+
}
14+
}
15+
16+
#[lang = "eh_personality"]
17+
fn foo() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// check-pass
2+
// aux-crate:panic_item=panic-item.rs
3+
// @has unused_extern_crate/index.html

src/test/rustdoc/auxiliary/issue-66159-1.rs

-2
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub struct SomeStruct;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// compile-flags: --extern pub_struct
2+
// aux-build:pub-struct.rs
3+
4+
/// [SomeStruct]
5+
///
6+
/// [SomeStruct]: pub_struct::SomeStruct
7+
pub fn foo() {}
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// aux-crate:priv:issue_66159_1=issue-66159-1.rs
1+
// aux-crate:priv:pub_struct=pub-struct.rs
22
// compile-flags:-Z unstable-options
33

44
// The issue was an ICE which meant that we never actually generated the docs
@@ -7,4 +7,4 @@
77
// verify that the struct is linked correctly.
88

99
// @has issue_66159/index.html
10-
//! [issue_66159_1::Something]
10+
//! [pub_struct::SomeStruct]

0 commit comments

Comments
 (0)