Skip to content

Commit 9430fd3

Browse files
committed
Save names of used extern crates
Tracks association between `self.sess.opts.externs` (aliases in `--extern alias=rlib`) and resolved `CrateNum` Intended to allow Rustdoc match the aliases in `--extern-html-root-url` #76296 Force-injected extern crates aren't included, since they're meant for the linker only
1 parent f9f6a70 commit 9430fd3

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

compiler/rustc_metadata/src/creader.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use rustc_data_structures::fx::FxHashSet;
1212
use rustc_data_structures::owned_slice::OwnedSlice;
1313
use rustc_data_structures::svh::Svh;
1414
use rustc_data_structures::sync::{self, FreezeReadGuard, FreezeWriteGuard};
15+
use rustc_data_structures::unord::UnordMap;
1516
use rustc_errors::DiagCtxtHandle;
1617
use rustc_expand::base::SyntaxExtension;
1718
use rustc_fs_util::try_canonicalize;
@@ -68,6 +69,9 @@ pub struct CStore {
6869
/// This crate has a `#[alloc_error_handler]` item.
6970
has_alloc_error_handler: bool,
7071

72+
/// Names that were used to load the crates via `extern crate` or paths.
73+
resolved_externs: UnordMap<Symbol, CrateNum>,
74+
7175
/// Unused externs of the crate
7276
unused_externs: Vec<Symbol>,
7377
}
@@ -268,6 +272,14 @@ impl CStore {
268272
self.metas[cnum] = Some(Box::new(data));
269273
}
270274

275+
/// Save the name used to resolve the extern crate in the local crate
276+
///
277+
/// The name isn't always the crate's own name, because `sess.opts.externs` can assign it another name.
278+
/// It's also not always the same as the `DefId`'s symbol due to renames `extern crate name as defid_name`.
279+
pub(crate) fn set_resolved_extern_crate_name(&mut self, name: Symbol, extern_crate: CrateNum) {
280+
self.resolved_externs.insert(name, extern_crate);
281+
}
282+
271283
pub(crate) fn iter_crate_data(&self) -> impl Iterator<Item = (CrateNum, &CrateMetadata)> {
272284
self.metas
273285
.iter_enumerated()
@@ -494,6 +506,7 @@ impl CStore {
494506
alloc_error_handler_kind: None,
495507
has_global_allocator: false,
496508
has_alloc_error_handler: false,
509+
resolved_externs: UnordMap::default(),
497510
unused_externs: Vec::new(),
498511
}
499512
}
@@ -1302,6 +1315,7 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
13021315
let path_len = definitions.def_path(def_id).data.len();
13031316
self.cstore.update_extern_crate(
13041317
cnum,
1318+
name,
13051319
ExternCrate {
13061320
src: ExternCrateSource::Extern(def_id.to_def_id()),
13071321
span: item.span,
@@ -1320,6 +1334,7 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
13201334

13211335
self.cstore.update_extern_crate(
13221336
cnum,
1337+
name,
13231338
ExternCrate {
13241339
src: ExternCrateSource::Path,
13251340
span,

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -630,14 +630,29 @@ impl CStore {
630630
}
631631
}
632632

633-
pub(crate) fn update_extern_crate(&mut self, cnum: CrateNum, extern_crate: ExternCrate) {
633+
/// Track how an extern crate has been loaded
634+
///
635+
/// * the `name` is for [`Self::set_resolved_extern_crate_name`]
636+
/// * `extern_crate` is for diagnostics
637+
pub(crate) fn update_extern_crate(
638+
&mut self,
639+
cnum: CrateNum,
640+
name: Symbol,
641+
extern_crate: ExternCrate,
642+
) {
643+
debug_assert_eq!(extern_crate.dependency_of, LOCAL_CRATE);
644+
self.set_resolved_extern_crate_name(name, cnum);
645+
self.update_extern_crate_inner(cnum, extern_crate);
646+
}
647+
648+
fn update_extern_crate_inner(&mut self, cnum: CrateNum, extern_crate: ExternCrate) {
634649
let cmeta = self.get_crate_data_mut(cnum);
635650
if cmeta.update_extern_crate(extern_crate) {
636651
// Propagate the extern crate info to dependencies if it was updated.
637652
let extern_crate = ExternCrate { dependency_of: cnum, ..extern_crate };
638653
let dependencies = mem::take(&mut cmeta.dependencies);
639654
for &dep_cnum in &dependencies {
640-
self.update_extern_crate(dep_cnum, extern_crate);
655+
self.update_extern_crate_inner(dep_cnum, extern_crate);
641656
}
642657
self.get_crate_data_mut(cnum).dependencies = dependencies;
643658
}

0 commit comments

Comments
 (0)