Skip to content

Commit dfdf5b8

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` Force-injected extern crates aren't included, since they're meant for the linker only
1 parent 117447f commit dfdf5b8

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
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_expand::base::SyntaxExtension;
1617
use rustc_fs_util::try_canonicalize;
1718
use rustc_hir as hir;
@@ -69,6 +70,9 @@ pub struct CStore {
6970
/// This crate has a `#[alloc_error_handler]` item.
7071
has_alloc_error_handler: bool,
7172

73+
/// Names that were used to load the crates via `extern crate` or paths.
74+
resolved_externs: UnordMap<Symbol, CrateNum>,
75+
7276
/// Unused externs of the crate
7377
unused_externs: Vec<Symbol>,
7478

@@ -249,6 +253,14 @@ impl CStore {
249253
self.metas[cnum] = Some(Box::new(data));
250254
}
251255

256+
/// Save the name used to resolve the extern crate in the local crate
257+
///
258+
/// The name isn't always the crate's own name, because `sess.opts.externs` can assign it another name.
259+
/// It's also not always the same as the `DefId`'s symbol due to renames `extern crate resolved_name as defid_name`.
260+
pub(crate) fn set_resolved_extern_crate_name(&mut self, name: Symbol, extern_crate: CrateNum) {
261+
self.resolved_externs.insert(name, extern_crate);
262+
}
263+
252264
pub(crate) fn iter_crate_data(&self) -> impl Iterator<Item = (CrateNum, &CrateMetadata)> {
253265
self.metas
254266
.iter_enumerated()
@@ -475,6 +487,7 @@ impl CStore {
475487
alloc_error_handler_kind: None,
476488
has_global_allocator: false,
477489
has_alloc_error_handler: false,
490+
resolved_externs: UnordMap::default(),
478491
unused_externs: Vec::new(),
479492
used_extern_options: Default::default(),
480493
}
@@ -1308,6 +1321,7 @@ impl CStore {
13081321
let path_len = definitions.def_path(def_id).data.len();
13091322
self.update_extern_crate(
13101323
cnum,
1324+
name,
13111325
ExternCrate {
13121326
src: ExternCrateSource::Extern(def_id.to_def_id()),
13131327
span: item.span,
@@ -1332,6 +1346,7 @@ impl CStore {
13321346

13331347
self.update_extern_crate(
13341348
cnum,
1349+
name,
13351350
ExternCrate {
13361351
src: ExternCrateSource::Path,
13371352
span,

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,15 +628,20 @@ impl CStore {
628628
}
629629

630630
/// Track how an extern crate has been loaded. Called after resolving an import in the local crate.
631+
///
632+
/// * the `name` is for [`Self::set_resolved_extern_crate_name`] saving `--extern name=`
633+
/// * `extern_crate` is for diagnostics
631634
pub(crate) fn update_extern_crate(
632635
&mut self,
633636
cnum: CrateNum,
637+
name: Symbol,
634638
extern_crate: ExternCrate,
635639
) {
636640
debug_assert_eq!(
637641
extern_crate.dependency_of, LOCAL_CRATE,
638642
"this function should not be called on transitive dependencies"
639643
);
644+
self.set_resolved_extern_crate_name(name, cnum);
640645
self.update_transitive_extern_crate_diagnostics(cnum, extern_crate);
641646
}
642647

0 commit comments

Comments
 (0)