Skip to content

Commit d933edd

Browse files
committed
Revert "Revert "Don't load all extern crates unconditionally""
This reverts commit 5f0c54d.
1 parent 99b73e8 commit d933edd

File tree

9 files changed

+100
-41
lines changed

9 files changed

+100
-41
lines changed

src/librustdoc/core.rs

+9-34
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1+
use rustc_ast as ast;
12
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
23
use rustc_data_structures::sync::{self, Lrc};
34
use rustc_driver::abort_on_err;
45
use rustc_errors::emitter::{Emitter, EmitterWriter};
56
use rustc_errors::json::JsonEmitter;
67
use rustc_feature::UnstableFeatures;
7-
use rustc_hir::def::Namespace::TypeNS;
88
use rustc_hir::def::Res;
9-
use rustc_hir::def_id::{DefId, LocalDefId, CRATE_DEF_INDEX};
9+
use rustc_hir::def_id::{DefId, LocalDefId};
1010
use rustc_hir::HirId;
1111
use rustc_hir::{
1212
intravisit::{self, NestedVisitorMap, Visitor},
@@ -23,7 +23,7 @@ use rustc_session::DiagnosticOutput;
2323
use rustc_session::Session;
2424
use rustc_span::source_map;
2525
use rustc_span::symbol::sym;
26-
use rustc_span::{Span, DUMMY_SP};
26+
use rustc_span::Span;
2727

2828
use std::cell::RefCell;
2929
use std::mem;
@@ -301,41 +301,16 @@ crate fn create_config(
301301
}
302302

303303
crate fn create_resolver<'a>(
304-
externs: config::Externs,
305304
queries: &Queries<'a>,
306305
sess: &Session,
307306
) -> 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-
});
307+
let (krate, resolver, _) = &*abort_on_err(queries.expansion(), sess).peek();
308+
let resolver = resolver.clone();
309+
310+
let mut loader = crate::passes::collect_intra_doc_links::IntraLinkCrateLoader::new(resolver);
311+
ast::visit::walk_crate(&mut loader, krate);
336312

337-
// Now we're good to clone the resolver because everything should be loaded
338-
resolver.clone()
313+
loader.resolver
339314
}
340315

341316
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::IntraLinkCrateLoader;
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,63 @@
1+
use rustc_ast as ast;
2+
use rustc_hir::def::Namespace::TypeNS;
3+
use rustc_hir::def_id::{DefId, LocalDefId, CRATE_DEF_INDEX};
4+
use rustc_interface::interface;
5+
6+
use std::cell::RefCell;
7+
use std::mem;
8+
use std::rc::Rc;
9+
10+
// Letting the resolver escape at the end of the function leads to inconsistencies between the
11+
// crates the TyCtxt sees and the resolver sees (because the resolver could load more crates
12+
// after escaping). Hopefully `IntraLinkCrateLoader` gets all the crates we need ...
13+
crate struct IntraLinkCrateLoader {
14+
current_mod: DefId,
15+
crate resolver: Rc<RefCell<interface::BoxedResolver>>,
16+
}
17+
18+
impl IntraLinkCrateLoader {
19+
crate fn new(resolver: Rc<RefCell<interface::BoxedResolver>>) -> Self {
20+
let crate_id = LocalDefId { local_def_index: CRATE_DEF_INDEX }.to_def_id();
21+
Self { current_mod: crate_id, resolver }
22+
}
23+
}
24+
25+
impl ast::visit::Visitor<'_> for IntraLinkCrateLoader {
26+
fn visit_attribute(&mut self, attr: &ast::Attribute) {
27+
use crate::html::markdown::markdown_links;
28+
use crate::passes::collect_intra_doc_links::preprocess_link;
29+
30+
if let Some(doc) = attr.doc_str() {
31+
for link in markdown_links(&doc.as_str()) {
32+
let path_str = if let Some(Ok(x)) = preprocess_link(&link) {
33+
x.path_str
34+
} else {
35+
continue;
36+
};
37+
self.resolver.borrow_mut().access(|resolver| {
38+
let _ = resolver.resolve_str_path_error(
39+
attr.span,
40+
&path_str,
41+
TypeNS,
42+
self.current_mod,
43+
);
44+
});
45+
}
46+
}
47+
ast::visit::walk_attribute(self, attr);
48+
}
49+
50+
fn visit_item(&mut self, item: &ast::Item) {
51+
use rustc_ast_lowering::ResolverAstLowering;
52+
53+
if let ast::ItemKind::Mod(..) = item.kind {
54+
let new_mod =
55+
self.resolver.borrow_mut().access(|resolver| resolver.local_def_id(item.id));
56+
let old_mod = mem::replace(&mut self.current_mod, new_mod.to_def_id());
57+
ast::visit::walk_item(self, item);
58+
self.current_mod = old_mod;
59+
} else {
60+
ast::visit::walk_item(self, item);
61+
}
62+
}
63+
}

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
@@ -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)