rustc_metadata/
creader.rs

1//! Validates all used crates and extern libraries and loads their metadata
2
3use std::error::Error;
4use std::path::Path;
5use std::str::FromStr;
6use std::time::Duration;
7use std::{cmp, env, iter};
8
9use rustc_ast::expand::allocator::{AllocatorKind, alloc_error_handler_name, global_fn_name};
10use rustc_ast::{self as ast, *};
11use rustc_data_structures::fx::FxHashSet;
12use rustc_data_structures::owned_slice::OwnedSlice;
13use rustc_data_structures::svh::Svh;
14use rustc_data_structures::sync::{self, FreezeReadGuard, FreezeWriteGuard};
15use rustc_errors::DiagCtxtHandle;
16use rustc_expand::base::SyntaxExtension;
17use rustc_fs_util::try_canonicalize;
18use rustc_hir as hir;
19use rustc_hir::def_id::{CrateNum, LOCAL_CRATE, LocalDefId, StableCrateId};
20use rustc_hir::definitions::Definitions;
21use rustc_index::IndexVec;
22use rustc_middle::bug;
23use rustc_middle::ty::data_structures::IndexSet;
24use rustc_middle::ty::{TyCtxt, TyCtxtFeed};
25use rustc_proc_macro::bridge::client::ProcMacro;
26use rustc_session::config::{
27    CrateType, ExtendedTargetModifierInfo, ExternLocation, OptionsTargetModifiers, TargetModifier,
28};
29use rustc_session::cstore::{CrateDepKind, CrateSource, ExternCrate, ExternCrateSource};
30use rustc_session::lint::{self, BuiltinLintDiag};
31use rustc_session::output::validate_crate_name;
32use rustc_session::search_paths::PathKind;
33use rustc_span::edition::Edition;
34use rustc_span::{DUMMY_SP, Ident, Span, Symbol, sym};
35use rustc_target::spec::{PanicStrategy, Target};
36use tracing::{debug, info, trace};
37
38use crate::errors;
39use crate::locator::{CrateError, CrateLocator, CratePaths, CrateRejections};
40use crate::rmeta::{
41    CrateDep, CrateMetadata, CrateNumMap, CrateRoot, MetadataBlob, TargetModifiers,
42};
43
44/// The backend's way to give the crate store access to the metadata in a library.
45/// Note that it returns the raw metadata bytes stored in the library file, whether
46/// it is compressed, uncompressed, some weird mix, etc.
47/// rmeta files are backend independent and not handled here.
48pub trait MetadataLoader {
49    fn get_rlib_metadata(&self, target: &Target, filename: &Path) -> Result<OwnedSlice, String>;
50    fn get_dylib_metadata(&self, target: &Target, filename: &Path) -> Result<OwnedSlice, String>;
51}
52
53pub type MetadataLoaderDyn = dyn MetadataLoader + Send + Sync + sync::DynSend + sync::DynSync;
54
55pub struct CStore {
56    metadata_loader: Box<MetadataLoaderDyn>,
57
58    metas: IndexVec<CrateNum, Option<Box<CrateMetadata>>>,
59    injected_panic_runtime: Option<CrateNum>,
60    /// This crate needs an allocator and either provides it itself, or finds it in a dependency.
61    /// If the above is true, then this field denotes the kind of the found allocator.
62    allocator_kind: Option<AllocatorKind>,
63    /// This crate needs an allocation error handler and either provides it itself, or finds it in a dependency.
64    /// If the above is true, then this field denotes the kind of the found allocator.
65    alloc_error_handler_kind: Option<AllocatorKind>,
66    /// This crate has a `#[global_allocator]` item.
67    has_global_allocator: bool,
68    /// This crate has a `#[alloc_error_handler]` item.
69    has_alloc_error_handler: bool,
70
71    /// Unused externs of the crate
72    unused_externs: Vec<Symbol>,
73}
74
75impl std::fmt::Debug for CStore {
76    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
77        f.debug_struct("CStore").finish_non_exhaustive()
78    }
79}
80
81pub struct CrateLoader<'a, 'tcx: 'a> {
82    // Immutable configuration.
83    tcx: TyCtxt<'tcx>,
84    // Mutable output.
85    cstore: &'a mut CStore,
86    used_extern_options: &'a mut FxHashSet<Symbol>,
87}
88
89impl<'a, 'tcx> std::ops::Deref for CrateLoader<'a, 'tcx> {
90    type Target = TyCtxt<'tcx>;
91
92    fn deref(&self) -> &Self::Target {
93        &self.tcx
94    }
95}
96
97impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
98    fn dcx(&self) -> DiagCtxtHandle<'tcx> {
99        self.tcx.dcx()
100    }
101}
102
103pub enum LoadedMacro {
104    MacroDef {
105        def: MacroDef,
106        ident: Ident,
107        attrs: Vec<hir::Attribute>,
108        span: Span,
109        edition: Edition,
110    },
111    ProcMacro(SyntaxExtension),
112}
113
114pub(crate) struct Library {
115    pub source: CrateSource,
116    pub metadata: MetadataBlob,
117}
118
119enum LoadResult {
120    Previous(CrateNum),
121    Loaded(Library),
122}
123
124/// A reference to `CrateMetadata` that can also give access to whole crate store when necessary.
125#[derive(Clone, Copy)]
126pub(crate) struct CrateMetadataRef<'a> {
127    pub cdata: &'a CrateMetadata,
128    pub cstore: &'a CStore,
129}
130
131impl std::ops::Deref for CrateMetadataRef<'_> {
132    type Target = CrateMetadata;
133
134    fn deref(&self) -> &Self::Target {
135        self.cdata
136    }
137}
138
139struct CrateDump<'a>(&'a CStore);
140
141impl<'a> std::fmt::Debug for CrateDump<'a> {
142    fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
143        writeln!(fmt, "resolved crates:")?;
144        for (cnum, data) in self.0.iter_crate_data() {
145            writeln!(fmt, "  name: {}", data.name())?;
146            writeln!(fmt, "  cnum: {cnum}")?;
147            writeln!(fmt, "  hash: {}", data.hash())?;
148            writeln!(fmt, "  reqd: {:?}", data.dep_kind())?;
149            writeln!(fmt, "  priv: {:?}", data.is_private_dep())?;
150            let CrateSource { dylib, rlib, rmeta, sdylib_interface } = data.source();
151            if let Some(dylib) = dylib {
152                writeln!(fmt, "  dylib: {}", dylib.0.display())?;
153            }
154            if let Some(rlib) = rlib {
155                writeln!(fmt, "   rlib: {}", rlib.0.display())?;
156            }
157            if let Some(rmeta) = rmeta {
158                writeln!(fmt, "   rmeta: {}", rmeta.0.display())?;
159            }
160            if let Some(sdylib_interface) = sdylib_interface {
161                writeln!(fmt, "   sdylib interface: {}", sdylib_interface.0.display())?;
162            }
163        }
164        Ok(())
165    }
166}
167
168/// Reason that a crate is being sourced as a dependency.
169#[derive(Clone, Copy)]
170enum CrateOrigin<'a> {
171    /// This crate was a dependency of another crate.
172    IndirectDependency {
173        /// Where this dependency was included from.
174        dep_root: &'a CratePaths,
175        /// True if the parent is private, meaning the dependent should also be private.
176        parent_private: bool,
177        /// Dependency info about this crate.
178        dep: &'a CrateDep,
179    },
180    /// Injected by `rustc`.
181    Injected,
182    /// Provided by `extern crate foo` or as part of the extern prelude.
183    Extern,
184}
185
186impl<'a> CrateOrigin<'a> {
187    /// Return the dependency root, if any.
188    fn dep_root(&self) -> Option<&'a CratePaths> {
189        match self {
190            CrateOrigin::IndirectDependency { dep_root, .. } => Some(dep_root),
191            _ => None,
192        }
193    }
194
195    /// Return dependency information, if any.
196    fn dep(&self) -> Option<&'a CrateDep> {
197        match self {
198            CrateOrigin::IndirectDependency { dep, .. } => Some(dep),
199            _ => None,
200        }
201    }
202
203    /// `Some(true)` if the dependency is private or its parent is private, `Some(false)` if the
204    /// dependency is not private, `None` if it could not be determined.
205    fn private_dep(&self) -> Option<bool> {
206        match self {
207            CrateOrigin::IndirectDependency { parent_private, dep, .. } => {
208                Some(dep.is_private || *parent_private)
209            }
210            _ => None,
211        }
212    }
213}
214
215impl CStore {
216    pub fn from_tcx(tcx: TyCtxt<'_>) -> FreezeReadGuard<'_, CStore> {
217        FreezeReadGuard::map(tcx.untracked().cstore.read(), |cstore| {
218            cstore.as_any().downcast_ref::<CStore>().expect("`tcx.cstore` is not a `CStore`")
219        })
220    }
221
222    pub fn from_tcx_mut(tcx: TyCtxt<'_>) -> FreezeWriteGuard<'_, CStore> {
223        FreezeWriteGuard::map(tcx.untracked().cstore.write(), |cstore| {
224            cstore.untracked_as_any().downcast_mut().expect("`tcx.cstore` is not a `CStore`")
225        })
226    }
227
228    fn intern_stable_crate_id<'tcx>(
229        &mut self,
230        root: &CrateRoot,
231        tcx: TyCtxt<'tcx>,
232    ) -> Result<TyCtxtFeed<'tcx, CrateNum>, CrateError> {
233        assert_eq!(self.metas.len(), tcx.untracked().stable_crate_ids.read().len());
234        let num = tcx.create_crate_num(root.stable_crate_id()).map_err(|existing| {
235            // Check for (potential) conflicts with the local crate
236            if existing == LOCAL_CRATE {
237                CrateError::SymbolConflictsCurrent(root.name())
238            } else if let Some(crate_name1) = self.metas[existing].as_ref().map(|data| data.name())
239            {
240                let crate_name0 = root.name();
241                CrateError::StableCrateIdCollision(crate_name0, crate_name1)
242            } else {
243                CrateError::NotFound(root.name())
244            }
245        })?;
246
247        self.metas.push(None);
248        Ok(num)
249    }
250
251    pub fn has_crate_data(&self, cnum: CrateNum) -> bool {
252        self.metas[cnum].is_some()
253    }
254
255    pub(crate) fn get_crate_data(&self, cnum: CrateNum) -> CrateMetadataRef<'_> {
256        let cdata = self.metas[cnum]
257            .as_ref()
258            .unwrap_or_else(|| panic!("Failed to get crate data for {cnum:?}"));
259        CrateMetadataRef { cdata, cstore: self }
260    }
261
262    pub(crate) fn get_crate_data_mut(&mut self, cnum: CrateNum) -> &mut CrateMetadata {
263        self.metas[cnum].as_mut().unwrap_or_else(|| panic!("Failed to get crate data for {cnum:?}"))
264    }
265
266    fn set_crate_data(&mut self, cnum: CrateNum, data: CrateMetadata) {
267        assert!(self.metas[cnum].is_none(), "Overwriting crate metadata entry");
268        self.metas[cnum] = Some(Box::new(data));
269    }
270
271    pub(crate) fn iter_crate_data(&self) -> impl Iterator<Item = (CrateNum, &CrateMetadata)> {
272        self.metas
273            .iter_enumerated()
274            .filter_map(|(cnum, data)| data.as_deref().map(|data| (cnum, data)))
275    }
276
277    fn push_dependencies_in_postorder(&self, deps: &mut IndexSet<CrateNum>, cnum: CrateNum) {
278        if !deps.contains(&cnum) {
279            let data = self.get_crate_data(cnum);
280            for dep in data.dependencies() {
281                if dep != cnum {
282                    self.push_dependencies_in_postorder(deps, dep);
283                }
284            }
285
286            deps.insert(cnum);
287        }
288    }
289
290    pub(crate) fn crate_dependencies_in_postorder(&self, cnum: CrateNum) -> IndexSet<CrateNum> {
291        let mut deps = IndexSet::default();
292        if cnum == LOCAL_CRATE {
293            for (cnum, _) in self.iter_crate_data() {
294                self.push_dependencies_in_postorder(&mut deps, cnum);
295            }
296        } else {
297            self.push_dependencies_in_postorder(&mut deps, cnum);
298        }
299        deps
300    }
301
302    pub(crate) fn injected_panic_runtime(&self) -> Option<CrateNum> {
303        self.injected_panic_runtime
304    }
305
306    pub(crate) fn allocator_kind(&self) -> Option<AllocatorKind> {
307        self.allocator_kind
308    }
309
310    pub(crate) fn alloc_error_handler_kind(&self) -> Option<AllocatorKind> {
311        self.alloc_error_handler_kind
312    }
313
314    pub(crate) fn has_global_allocator(&self) -> bool {
315        self.has_global_allocator
316    }
317
318    pub(crate) fn has_alloc_error_handler(&self) -> bool {
319        self.has_alloc_error_handler
320    }
321
322    pub fn report_unused_deps(&self, tcx: TyCtxt<'_>) {
323        let json_unused_externs = tcx.sess.opts.json_unused_externs;
324
325        // We put the check for the option before the lint_level_at_node call
326        // because the call mutates internal state and introducing it
327        // leads to some ui tests failing.
328        if !json_unused_externs.is_enabled() {
329            return;
330        }
331        let level = tcx
332            .lint_level_at_node(lint::builtin::UNUSED_CRATE_DEPENDENCIES, rustc_hir::CRATE_HIR_ID)
333            .level;
334        if level != lint::Level::Allow {
335            let unused_externs =
336                self.unused_externs.iter().map(|ident| ident.to_ident_string()).collect::<Vec<_>>();
337            let unused_externs = unused_externs.iter().map(String::as_str).collect::<Vec<&str>>();
338            tcx.dcx().emit_unused_externs(level, json_unused_externs.is_loud(), &unused_externs);
339        }
340    }
341
342    fn report_target_modifiers_extended(
343        tcx: TyCtxt<'_>,
344        krate: &Crate,
345        mods: &TargetModifiers,
346        dep_mods: &TargetModifiers,
347        data: &CrateMetadata,
348    ) {
349        let span = krate.spans.inner_span.shrink_to_lo();
350        let allowed_flag_mismatches = &tcx.sess.opts.cg.unsafe_allow_abi_mismatch;
351        let local_crate = tcx.crate_name(LOCAL_CRATE);
352        let tmod_extender = |tmod: &TargetModifier| (tmod.extend(), tmod.clone());
353        let report_diff = |prefix: &String,
354                           opt_name: &String,
355                           flag_local_value: Option<&String>,
356                           flag_extern_value: Option<&String>| {
357            if allowed_flag_mismatches.contains(&opt_name) {
358                return;
359            }
360            let extern_crate = data.name();
361            let flag_name = opt_name.clone();
362            let flag_name_prefixed = format!("-{}{}", prefix, opt_name);
363
364            match (flag_local_value, flag_extern_value) {
365                (Some(local_value), Some(extern_value)) => {
366                    tcx.dcx().emit_err(errors::IncompatibleTargetModifiers {
367                        span,
368                        extern_crate,
369                        local_crate,
370                        flag_name,
371                        flag_name_prefixed,
372                        local_value: local_value.to_string(),
373                        extern_value: extern_value.to_string(),
374                    })
375                }
376                (None, Some(extern_value)) => {
377                    tcx.dcx().emit_err(errors::IncompatibleTargetModifiersLMissed {
378                        span,
379                        extern_crate,
380                        local_crate,
381                        flag_name,
382                        flag_name_prefixed,
383                        extern_value: extern_value.to_string(),
384                    })
385                }
386                (Some(local_value), None) => {
387                    tcx.dcx().emit_err(errors::IncompatibleTargetModifiersRMissed {
388                        span,
389                        extern_crate,
390                        local_crate,
391                        flag_name,
392                        flag_name_prefixed,
393                        local_value: local_value.to_string(),
394                    })
395                }
396                (None, None) => panic!("Incorrect target modifiers report_diff(None, None)"),
397            };
398        };
399        let mut it1 = mods.iter().map(tmod_extender);
400        let mut it2 = dep_mods.iter().map(tmod_extender);
401        let mut left_name_val: Option<(ExtendedTargetModifierInfo, TargetModifier)> = None;
402        let mut right_name_val: Option<(ExtendedTargetModifierInfo, TargetModifier)> = None;
403        loop {
404            left_name_val = left_name_val.or_else(|| it1.next());
405            right_name_val = right_name_val.or_else(|| it2.next());
406            match (&left_name_val, &right_name_val) {
407                (Some(l), Some(r)) => match l.1.opt.cmp(&r.1.opt) {
408                    cmp::Ordering::Equal => {
409                        if l.0.tech_value != r.0.tech_value {
410                            report_diff(
411                                &l.0.prefix,
412                                &l.0.name,
413                                Some(&l.1.value_name),
414                                Some(&r.1.value_name),
415                            );
416                        }
417                        left_name_val = None;
418                        right_name_val = None;
419                    }
420                    cmp::Ordering::Greater => {
421                        report_diff(&r.0.prefix, &r.0.name, None, Some(&r.1.value_name));
422                        right_name_val = None;
423                    }
424                    cmp::Ordering::Less => {
425                        report_diff(&l.0.prefix, &l.0.name, Some(&l.1.value_name), None);
426                        left_name_val = None;
427                    }
428                },
429                (Some(l), None) => {
430                    report_diff(&l.0.prefix, &l.0.name, Some(&l.1.value_name), None);
431                    left_name_val = None;
432                }
433                (None, Some(r)) => {
434                    report_diff(&r.0.prefix, &r.0.name, None, Some(&r.1.value_name));
435                    right_name_val = None;
436                }
437                (None, None) => break,
438            }
439        }
440    }
441
442    pub fn report_incompatible_target_modifiers(&self, tcx: TyCtxt<'_>, krate: &Crate) {
443        for flag_name in &tcx.sess.opts.cg.unsafe_allow_abi_mismatch {
444            if !OptionsTargetModifiers::is_target_modifier(flag_name) {
445                tcx.dcx().emit_err(errors::UnknownTargetModifierUnsafeAllowed {
446                    span: krate.spans.inner_span.shrink_to_lo(),
447                    flag_name: flag_name.clone(),
448                });
449            }
450        }
451        let mods = tcx.sess.opts.gather_target_modifiers();
452        for (_cnum, data) in self.iter_crate_data() {
453            if data.is_proc_macro_crate() {
454                continue;
455            }
456            let dep_mods = data.target_modifiers();
457            if mods != dep_mods {
458                Self::report_target_modifiers_extended(tcx, krate, &mods, &dep_mods, data);
459            }
460        }
461    }
462
463    // Report about async drop types in dependency if async drop feature is disabled
464    pub fn report_incompatible_async_drop_feature(&self, tcx: TyCtxt<'_>, krate: &Crate) {
465        if tcx.features().async_drop() {
466            return;
467        }
468        for (_cnum, data) in self.iter_crate_data() {
469            if data.is_proc_macro_crate() {
470                continue;
471            }
472            if data.has_async_drops() {
473                let extern_crate = data.name();
474                let local_crate = tcx.crate_name(LOCAL_CRATE);
475                tcx.dcx().emit_warn(errors::AsyncDropTypesInDependency {
476                    span: krate.spans.inner_span.shrink_to_lo(),
477                    extern_crate,
478                    local_crate,
479                });
480            }
481        }
482    }
483
484    pub fn new(metadata_loader: Box<MetadataLoaderDyn>) -> CStore {
485        CStore {
486            metadata_loader,
487            // We add an empty entry for LOCAL_CRATE (which maps to zero) in
488            // order to make array indices in `metas` match with the
489            // corresponding `CrateNum`. This first entry will always remain
490            // `None`.
491            metas: IndexVec::from_iter(iter::once(None)),
492            injected_panic_runtime: None,
493            allocator_kind: None,
494            alloc_error_handler_kind: None,
495            has_global_allocator: false,
496            has_alloc_error_handler: false,
497            unused_externs: Vec::new(),
498        }
499    }
500}
501
502impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
503    pub fn new(
504        tcx: TyCtxt<'tcx>,
505        cstore: &'a mut CStore,
506        used_extern_options: &'a mut FxHashSet<Symbol>,
507    ) -> Self {
508        CrateLoader { tcx, cstore, used_extern_options }
509    }
510
511    fn existing_match(&self, name: Symbol, hash: Option<Svh>, kind: PathKind) -> Option<CrateNum> {
512        for (cnum, data) in self.cstore.iter_crate_data() {
513            if data.name() != name {
514                trace!("{} did not match {}", data.name(), name);
515                continue;
516            }
517
518            match hash {
519                Some(hash) if hash == data.hash() => return Some(cnum),
520                Some(hash) => {
521                    debug!("actual hash {} did not match expected {}", hash, data.hash());
522                    continue;
523                }
524                None => {}
525            }
526
527            // When the hash is None we're dealing with a top-level dependency
528            // in which case we may have a specification on the command line for
529            // this library. Even though an upstream library may have loaded
530            // something of the same name, we have to make sure it was loaded
531            // from the exact same location as well.
532            //
533            // We're also sure to compare *paths*, not actual byte slices. The
534            // `source` stores paths which are normalized which may be different
535            // from the strings on the command line.
536            let source = self.cstore.get_crate_data(cnum).cdata.source();
537            if let Some(entry) = self.sess.opts.externs.get(name.as_str()) {
538                // Only use `--extern crate_name=path` here, not `--extern crate_name`.
539                if let Some(mut files) = entry.files() {
540                    if files.any(|l| {
541                        let l = l.canonicalized();
542                        source.dylib.as_ref().map(|(p, _)| p) == Some(l)
543                            || source.rlib.as_ref().map(|(p, _)| p) == Some(l)
544                            || source.rmeta.as_ref().map(|(p, _)| p) == Some(l)
545                    }) {
546                        return Some(cnum);
547                    }
548                }
549                continue;
550            }
551
552            // Alright, so we've gotten this far which means that `data` has the
553            // right name, we don't have a hash, and we don't have a --extern
554            // pointing for ourselves. We're still not quite yet done because we
555            // have to make sure that this crate was found in the crate lookup
556            // path (this is a top-level dependency) as we don't want to
557            // implicitly load anything inside the dependency lookup path.
558            let prev_kind = source
559                .dylib
560                .as_ref()
561                .or(source.rlib.as_ref())
562                .or(source.rmeta.as_ref())
563                .expect("No sources for crate")
564                .1;
565            if kind.matches(prev_kind) {
566                return Some(cnum);
567            } else {
568                debug!(
569                    "failed to load existing crate {}; kind {:?} did not match prev_kind {:?}",
570                    name, kind, prev_kind
571                );
572            }
573        }
574
575        None
576    }
577
578    /// Determine whether a dependency should be considered private.
579    ///
580    /// Dependencies are private if they get extern option specified, e.g. `--extern priv:mycrate`.
581    /// This is stored in metadata, so `private_dep`  can be correctly set during load. A `Some`
582    /// value for `private_dep` indicates that the crate is known to be private or public (note
583    /// that any `None` or `Some(false)` use of the same crate will make it public).
584    ///
585    /// Sometimes the directly dependent crate is not specified by `--extern`, in this case,
586    /// `private-dep` is none during loading. This is equivalent to the scenario where the
587    /// command parameter is set to `public-dependency`
588    fn is_private_dep(
589        &self,
590        name: Symbol,
591        private_dep: Option<bool>,
592        origin: CrateOrigin<'_>,
593    ) -> bool {
594        if matches!(origin, CrateOrigin::Injected) {
595            return true;
596        }
597
598        let extern_private = self.sess.opts.externs.get(name.as_str()).map(|e| e.is_private_dep);
599        match (extern_private, private_dep) {
600            // Explicit non-private via `--extern`, explicit non-private from metadata, or
601            // unspecified with default to public.
602            (Some(false), _) | (_, Some(false)) | (None, None) => false,
603            // Marked private via `--extern priv:mycrate` or in metadata.
604            (Some(true) | None, Some(true) | None) => true,
605        }
606    }
607
608    fn register_crate(
609        &mut self,
610        host_lib: Option<Library>,
611        origin: CrateOrigin<'_>,
612        lib: Library,
613        dep_kind: CrateDepKind,
614        name: Symbol,
615        private_dep: Option<bool>,
616    ) -> Result<CrateNum, CrateError> {
617        let _prof_timer =
618            self.sess.prof.generic_activity_with_arg("metadata_register_crate", name.as_str());
619
620        let Library { source, metadata } = lib;
621        let crate_root = metadata.get_root();
622        let host_hash = host_lib.as_ref().map(|lib| lib.metadata.get_root().hash());
623        let private_dep = self.is_private_dep(name, private_dep, origin);
624
625        // Claim this crate number and cache it
626        let feed = self.cstore.intern_stable_crate_id(&crate_root, self.tcx)?;
627        let cnum = feed.key();
628
629        info!(
630            "register crate `{}` (cnum = {}. private_dep = {})",
631            crate_root.name(),
632            cnum,
633            private_dep
634        );
635
636        // Maintain a reference to the top most crate.
637        // Stash paths for top-most crate locally if necessary.
638        let crate_paths;
639        let dep_root = if let Some(dep_root) = origin.dep_root() {
640            dep_root
641        } else {
642            crate_paths = CratePaths::new(crate_root.name(), source.clone());
643            &crate_paths
644        };
645
646        let cnum_map =
647            self.resolve_crate_deps(dep_root, &crate_root, &metadata, cnum, dep_kind, private_dep)?;
648
649        let raw_proc_macros = if crate_root.is_proc_macro_crate() {
650            let temp_root;
651            let (dlsym_source, dlsym_root) = match &host_lib {
652                Some(host_lib) => (&host_lib.source, {
653                    temp_root = host_lib.metadata.get_root();
654                    &temp_root
655                }),
656                None => (&source, &crate_root),
657            };
658            let dlsym_dylib = dlsym_source.dylib.as_ref().expect("no dylib for a proc-macro crate");
659            Some(self.dlsym_proc_macros(&dlsym_dylib.0, dlsym_root.stable_crate_id())?)
660        } else {
661            None
662        };
663
664        let crate_metadata = CrateMetadata::new(
665            self.sess,
666            self.cstore,
667            metadata,
668            crate_root,
669            raw_proc_macros,
670            cnum,
671            cnum_map,
672            dep_kind,
673            source,
674            private_dep,
675            host_hash,
676        );
677
678        self.cstore.set_crate_data(cnum, crate_metadata);
679
680        Ok(cnum)
681    }
682
683    fn load_proc_macro<'b>(
684        &self,
685        locator: &mut CrateLocator<'b>,
686        crate_rejections: &mut CrateRejections,
687        path_kind: PathKind,
688        host_hash: Option<Svh>,
689    ) -> Result<Option<(LoadResult, Option<Library>)>, CrateError>
690    where
691        'a: 'b,
692    {
693        if self.sess.opts.unstable_opts.dual_proc_macros {
694            // Use a new crate locator and crate rejections so trying to load a proc macro doesn't
695            // affect the error message we emit
696            let mut proc_macro_locator = locator.clone();
697
698            // Try to load a proc macro
699            proc_macro_locator.for_target_proc_macro(self.sess, path_kind);
700
701            // Load the proc macro crate for the target
702            let target_result =
703                match self.load(&mut proc_macro_locator, &mut CrateRejections::default())? {
704                    Some(LoadResult::Previous(cnum)) => {
705                        return Ok(Some((LoadResult::Previous(cnum), None)));
706                    }
707                    Some(LoadResult::Loaded(library)) => Some(LoadResult::Loaded(library)),
708                    None => return Ok(None),
709                };
710
711            // Use the existing crate_rejections as we want the error message to be affected by
712            // loading the host proc macro.
713            *crate_rejections = CrateRejections::default();
714
715            // Load the proc macro crate for the host
716            locator.for_proc_macro(self.sess, path_kind);
717
718            locator.hash = host_hash;
719
720            let Some(host_result) = self.load(locator, crate_rejections)? else {
721                return Ok(None);
722            };
723
724            let host_result = match host_result {
725                LoadResult::Previous(..) => {
726                    panic!("host and target proc macros must be loaded in lock-step")
727                }
728                LoadResult::Loaded(library) => library,
729            };
730            Ok(Some((target_result.unwrap(), Some(host_result))))
731        } else {
732            // Use a new crate locator and crate rejections so trying to load a proc macro doesn't
733            // affect the error message we emit
734            let mut proc_macro_locator = locator.clone();
735
736            // Load the proc macro crate for the host
737            proc_macro_locator.for_proc_macro(self.sess, path_kind);
738
739            let Some(host_result) =
740                self.load(&mut proc_macro_locator, &mut CrateRejections::default())?
741            else {
742                return Ok(None);
743            };
744
745            Ok(Some((host_result, None)))
746        }
747    }
748
749    fn resolve_crate(
750        &mut self,
751        name: Symbol,
752        span: Span,
753        dep_kind: CrateDepKind,
754        origin: CrateOrigin<'_>,
755    ) -> Option<CrateNum> {
756        self.used_extern_options.insert(name);
757        match self.maybe_resolve_crate(name, dep_kind, origin) {
758            Ok(cnum) => {
759                self.cstore.set_used_recursively(cnum);
760                Some(cnum)
761            }
762            Err(err) => {
763                debug!("failed to resolve crate {} {:?}", name, dep_kind);
764                let missing_core = self
765                    .maybe_resolve_crate(sym::core, CrateDepKind::Explicit, CrateOrigin::Extern)
766                    .is_err();
767                err.report(self.sess, span, missing_core);
768                None
769            }
770        }
771    }
772
773    fn maybe_resolve_crate<'b>(
774        &'b mut self,
775        name: Symbol,
776        mut dep_kind: CrateDepKind,
777        origin: CrateOrigin<'b>,
778    ) -> Result<CrateNum, CrateError> {
779        info!("resolving crate `{}`", name);
780        if !name.as_str().is_ascii() {
781            return Err(CrateError::NonAsciiName(name));
782        }
783
784        let dep_root = origin.dep_root();
785        let dep = origin.dep();
786        let hash = dep.map(|d| d.hash);
787        let host_hash = dep.map(|d| d.host_hash).flatten();
788        let extra_filename = dep.map(|d| &d.extra_filename[..]);
789        let path_kind = if dep.is_some() { PathKind::Dependency } else { PathKind::Crate };
790        let private_dep = origin.private_dep();
791
792        let result = if let Some(cnum) = self.existing_match(name, hash, path_kind) {
793            (LoadResult::Previous(cnum), None)
794        } else {
795            info!("falling back to a load");
796            let mut locator = CrateLocator::new(
797                self.sess,
798                &*self.cstore.metadata_loader,
799                name,
800                // The all loop is because `--crate-type=rlib --crate-type=rlib` is
801                // legal and produces both inside this type.
802                self.tcx.crate_types().iter().all(|c| *c == CrateType::Rlib),
803                hash,
804                extra_filename,
805                path_kind,
806            );
807            let mut crate_rejections = CrateRejections::default();
808
809            match self.load(&mut locator, &mut crate_rejections)? {
810                Some(res) => (res, None),
811                None => {
812                    info!("falling back to loading proc_macro");
813                    dep_kind = CrateDepKind::MacrosOnly;
814                    match self.load_proc_macro(
815                        &mut locator,
816                        &mut crate_rejections,
817                        path_kind,
818                        host_hash,
819                    )? {
820                        Some(res) => res,
821                        None => return Err(locator.into_error(crate_rejections, dep_root.cloned())),
822                    }
823                }
824            }
825        };
826
827        match result {
828            (LoadResult::Previous(cnum), None) => {
829                info!("library for `{}` was loaded previously, cnum {cnum}", name);
830                // When `private_dep` is none, it indicates the directly dependent crate. If it is
831                // not specified by `--extern` on command line parameters, it may be
832                // `private-dependency` when `register_crate` is called for the first time. Then it must be updated to
833                // `public-dependency` here.
834                let private_dep = self.is_private_dep(name, private_dep, origin);
835                let data = self.cstore.get_crate_data_mut(cnum);
836                if data.is_proc_macro_crate() {
837                    dep_kind = CrateDepKind::MacrosOnly;
838                }
839                data.set_dep_kind(cmp::max(data.dep_kind(), dep_kind));
840                data.update_and_private_dep(private_dep);
841                Ok(cnum)
842            }
843            (LoadResult::Loaded(library), host_library) => {
844                info!("register newly loaded library for `{}`", name);
845                self.register_crate(host_library, origin, library, dep_kind, name, private_dep)
846            }
847            _ => panic!(),
848        }
849    }
850
851    fn load(
852        &self,
853        locator: &CrateLocator<'_>,
854        crate_rejections: &mut CrateRejections,
855    ) -> Result<Option<LoadResult>, CrateError> {
856        let Some(library) = locator.maybe_load_library_crate(crate_rejections)? else {
857            return Ok(None);
858        };
859
860        // In the case that we're loading a crate, but not matching
861        // against a hash, we could load a crate which has the same hash
862        // as an already loaded crate. If this is the case prevent
863        // duplicates by just using the first crate.
864        let root = library.metadata.get_root();
865        let mut result = LoadResult::Loaded(library);
866        for (cnum, data) in self.cstore.iter_crate_data() {
867            if data.name() == root.name() && root.hash() == data.hash() {
868                assert!(locator.hash.is_none());
869                info!("load success, going to previous cnum: {}", cnum);
870                result = LoadResult::Previous(cnum);
871                break;
872            }
873        }
874        Ok(Some(result))
875    }
876
877    /// Go through the crate metadata and load any crates that it references.
878    fn resolve_crate_deps(
879        &mut self,
880        dep_root: &CratePaths,
881        crate_root: &CrateRoot,
882        metadata: &MetadataBlob,
883        krate: CrateNum,
884        dep_kind: CrateDepKind,
885        parent_is_private: bool,
886    ) -> Result<CrateNumMap, CrateError> {
887        debug!(
888            "resolving deps of external crate `{}` with dep root `{}`",
889            crate_root.name(),
890            dep_root.name
891        );
892        if crate_root.is_proc_macro_crate() {
893            return Ok(CrateNumMap::new());
894        }
895
896        // The map from crate numbers in the crate we're resolving to local crate numbers.
897        // We map 0 and all other holes in the map to our parent crate. The "additional"
898        // self-dependencies should be harmless.
899        let deps = crate_root.decode_crate_deps(metadata);
900        let mut crate_num_map = CrateNumMap::with_capacity(1 + deps.len());
901        crate_num_map.push(krate);
902        for dep in deps {
903            info!(
904                "resolving dep `{}`->`{}` hash: `{}` extra filename: `{}` private {}",
905                crate_root.name(),
906                dep.name,
907                dep.hash,
908                dep.extra_filename,
909                dep.is_private,
910            );
911            let dep_kind = match dep_kind {
912                CrateDepKind::MacrosOnly => CrateDepKind::MacrosOnly,
913                _ => dep.kind,
914            };
915            let cnum = self.maybe_resolve_crate(
916                dep.name,
917                dep_kind,
918                CrateOrigin::IndirectDependency {
919                    dep_root,
920                    parent_private: parent_is_private,
921                    dep: &dep,
922                },
923            )?;
924            crate_num_map.push(cnum);
925        }
926
927        debug!("resolve_crate_deps: cnum_map for {:?} is {:?}", krate, crate_num_map);
928        Ok(crate_num_map)
929    }
930
931    fn dlsym_proc_macros(
932        &self,
933        path: &Path,
934        stable_crate_id: StableCrateId,
935    ) -> Result<&'static [ProcMacro], CrateError> {
936        let sym_name = self.sess.generate_proc_macro_decls_symbol(stable_crate_id);
937        debug!("trying to dlsym proc_macros {} for symbol `{}`", path.display(), sym_name);
938
939        unsafe {
940            let result = load_symbol_from_dylib::<*const &[ProcMacro]>(path, &sym_name);
941            match result {
942                Ok(result) => {
943                    debug!("loaded dlsym proc_macros {} for symbol `{}`", path.display(), sym_name);
944                    Ok(*result)
945                }
946                Err(err) => {
947                    debug!(
948                        "failed to dlsym proc_macros {} for symbol `{}`",
949                        path.display(),
950                        sym_name
951                    );
952                    Err(err.into())
953                }
954            }
955        }
956    }
957
958    fn inject_panic_runtime(&mut self, krate: &ast::Crate) {
959        // If we're only compiling an rlib, then there's no need to select a
960        // panic runtime, so we just skip this section entirely.
961        let only_rlib = self.tcx.crate_types().iter().all(|ct| *ct == CrateType::Rlib);
962        if only_rlib {
963            info!("panic runtime injection skipped, only generating rlib");
964            return;
965        }
966
967        // If we need a panic runtime, we try to find an existing one here. At
968        // the same time we perform some general validation of the DAG we've got
969        // going such as ensuring everything has a compatible panic strategy.
970        let mut needs_panic_runtime = attr::contains_name(&krate.attrs, sym::needs_panic_runtime);
971        for (_cnum, data) in self.cstore.iter_crate_data() {
972            needs_panic_runtime |= data.needs_panic_runtime();
973        }
974
975        // If we just don't need a panic runtime at all, then we're done here
976        // and there's nothing else to do.
977        if !needs_panic_runtime {
978            return;
979        }
980
981        // By this point we know that we need a panic runtime. Here we just load
982        // an appropriate default runtime for our panic strategy.
983        //
984        // We may resolve to an already loaded crate (as the crate may not have
985        // been explicitly linked prior to this), but this is fine.
986        //
987        // Also note that we have yet to perform validation of the crate graph
988        // in terms of everyone has a compatible panic runtime format, that's
989        // performed later as part of the `dependency_format` module.
990        let desired_strategy = self.sess.panic_strategy();
991        let name = match desired_strategy {
992            PanicStrategy::Unwind => sym::panic_unwind,
993            PanicStrategy::Abort => sym::panic_abort,
994        };
995        info!("panic runtime not found -- loading {}", name);
996
997        let Some(cnum) =
998            self.resolve_crate(name, DUMMY_SP, CrateDepKind::Implicit, CrateOrigin::Injected)
999        else {
1000            return;
1001        };
1002        let data = self.cstore.get_crate_data(cnum);
1003
1004        // Sanity check the loaded crate to ensure it is indeed a panic runtime
1005        // and the panic strategy is indeed what we thought it was.
1006        if !data.is_panic_runtime() {
1007            self.dcx().emit_err(errors::CrateNotPanicRuntime { crate_name: name });
1008        }
1009        if data.required_panic_strategy() != Some(desired_strategy) {
1010            self.dcx()
1011                .emit_err(errors::NoPanicStrategy { crate_name: name, strategy: desired_strategy });
1012        }
1013
1014        self.cstore.injected_panic_runtime = Some(cnum);
1015    }
1016
1017    fn inject_profiler_runtime(&mut self) {
1018        let needs_profiler_runtime =
1019            self.sess.instrument_coverage() || self.sess.opts.cg.profile_generate.enabled();
1020        if !needs_profiler_runtime || self.sess.opts.unstable_opts.no_profiler_runtime {
1021            return;
1022        }
1023
1024        info!("loading profiler");
1025
1026        let name = Symbol::intern(&self.sess.opts.unstable_opts.profiler_runtime);
1027        let Some(cnum) =
1028            self.resolve_crate(name, DUMMY_SP, CrateDepKind::Implicit, CrateOrigin::Injected)
1029        else {
1030            return;
1031        };
1032        let data = self.cstore.get_crate_data(cnum);
1033
1034        // Sanity check the loaded crate to ensure it is indeed a profiler runtime
1035        if !data.is_profiler_runtime() {
1036            self.dcx().emit_err(errors::NotProfilerRuntime { crate_name: name });
1037        }
1038    }
1039
1040    fn inject_allocator_crate(&mut self, krate: &ast::Crate) {
1041        self.cstore.has_global_allocator =
1042            match &*fn_spans(krate, Symbol::intern(&global_fn_name(sym::alloc))) {
1043                [span1, span2, ..] => {
1044                    self.dcx()
1045                        .emit_err(errors::NoMultipleGlobalAlloc { span2: *span2, span1: *span1 });
1046                    true
1047                }
1048                spans => !spans.is_empty(),
1049            };
1050        self.cstore.has_alloc_error_handler = match &*fn_spans(
1051            krate,
1052            Symbol::intern(alloc_error_handler_name(AllocatorKind::Global)),
1053        ) {
1054            [span1, span2, ..] => {
1055                self.dcx()
1056                    .emit_err(errors::NoMultipleAllocErrorHandler { span2: *span2, span1: *span1 });
1057                true
1058            }
1059            spans => !spans.is_empty(),
1060        };
1061
1062        // Check to see if we actually need an allocator. This desire comes
1063        // about through the `#![needs_allocator]` attribute and is typically
1064        // written down in liballoc.
1065        if !attr::contains_name(&krate.attrs, sym::needs_allocator)
1066            && !self.cstore.iter_crate_data().any(|(_, data)| data.needs_allocator())
1067        {
1068            return;
1069        }
1070
1071        // At this point we've determined that we need an allocator. Let's see
1072        // if our compilation session actually needs an allocator based on what
1073        // we're emitting.
1074        let all_rlib = self.tcx.crate_types().iter().all(|ct| matches!(*ct, CrateType::Rlib));
1075        if all_rlib {
1076            return;
1077        }
1078
1079        // Ok, we need an allocator. Not only that but we're actually going to
1080        // create an artifact that needs one linked in. Let's go find the one
1081        // that we're going to link in.
1082        //
1083        // First up we check for global allocators. Look at the crate graph here
1084        // and see what's a global allocator, including if we ourselves are a
1085        // global allocator.
1086        #[allow(rustc::symbol_intern_string_literal)]
1087        let this_crate = Symbol::intern("this crate");
1088
1089        let mut global_allocator = self.cstore.has_global_allocator.then_some(this_crate);
1090        for (_, data) in self.cstore.iter_crate_data() {
1091            if data.has_global_allocator() {
1092                match global_allocator {
1093                    Some(other_crate) => {
1094                        self.dcx().emit_err(errors::ConflictingGlobalAlloc {
1095                            crate_name: data.name(),
1096                            other_crate_name: other_crate,
1097                        });
1098                    }
1099                    None => global_allocator = Some(data.name()),
1100                }
1101            }
1102        }
1103        let mut alloc_error_handler = self.cstore.has_alloc_error_handler.then_some(this_crate);
1104        for (_, data) in self.cstore.iter_crate_data() {
1105            if data.has_alloc_error_handler() {
1106                match alloc_error_handler {
1107                    Some(other_crate) => {
1108                        self.dcx().emit_err(errors::ConflictingAllocErrorHandler {
1109                            crate_name: data.name(),
1110                            other_crate_name: other_crate,
1111                        });
1112                    }
1113                    None => alloc_error_handler = Some(data.name()),
1114                }
1115            }
1116        }
1117
1118        if global_allocator.is_some() {
1119            self.cstore.allocator_kind = Some(AllocatorKind::Global);
1120        } else {
1121            // Ok we haven't found a global allocator but we still need an
1122            // allocator. At this point our allocator request is typically fulfilled
1123            // by the standard library, denoted by the `#![default_lib_allocator]`
1124            // attribute.
1125            if !attr::contains_name(&krate.attrs, sym::default_lib_allocator)
1126                && !self.cstore.iter_crate_data().any(|(_, data)| data.has_default_lib_allocator())
1127            {
1128                self.dcx().emit_err(errors::GlobalAllocRequired);
1129            }
1130            self.cstore.allocator_kind = Some(AllocatorKind::Default);
1131        }
1132
1133        if alloc_error_handler.is_some() {
1134            self.cstore.alloc_error_handler_kind = Some(AllocatorKind::Global);
1135        } else {
1136            // The alloc crate provides a default allocation error handler if
1137            // one isn't specified.
1138            self.cstore.alloc_error_handler_kind = Some(AllocatorKind::Default);
1139        }
1140    }
1141
1142    fn inject_forced_externs(&mut self) {
1143        for (name, entry) in self.sess.opts.externs.iter() {
1144            if entry.force {
1145                let name_interned = Symbol::intern(name);
1146                if !self.used_extern_options.contains(&name_interned) {
1147                    self.resolve_crate(
1148                        name_interned,
1149                        DUMMY_SP,
1150                        CrateDepKind::Explicit,
1151                        CrateOrigin::Extern,
1152                    );
1153                }
1154            }
1155        }
1156    }
1157
1158    /// Inject the `compiler_builtins` crate if it is not already in the graph.
1159    fn inject_compiler_builtins(&mut self, krate: &ast::Crate) {
1160        // `compiler_builtins` does not get extern builtins, nor do `#![no_core]` crates
1161        if attr::contains_name(&krate.attrs, sym::compiler_builtins)
1162            || attr::contains_name(&krate.attrs, sym::no_core)
1163        {
1164            info!("`compiler_builtins` unneeded");
1165            return;
1166        }
1167
1168        // If a `#![compiler_builtins]` crate already exists, avoid injecting it twice. This is
1169        // the common case since usually it appears as a dependency of `std` or `alloc`.
1170        for (cnum, cmeta) in self.cstore.iter_crate_data() {
1171            if cmeta.is_compiler_builtins() {
1172                info!("`compiler_builtins` already exists (cnum = {cnum}); skipping injection");
1173                return;
1174            }
1175        }
1176
1177        // `compiler_builtins` is not yet in the graph; inject it. Error on resolution failure.
1178        let Some(cnum) = self.resolve_crate(
1179            sym::compiler_builtins,
1180            krate.spans.inner_span.shrink_to_lo(),
1181            CrateDepKind::Explicit,
1182            CrateOrigin::Injected,
1183        ) else {
1184            info!("`compiler_builtins` not resolved");
1185            return;
1186        };
1187
1188        // Sanity check that the loaded crate is `#![compiler_builtins]`
1189        let cmeta = self.cstore.get_crate_data(cnum);
1190        if !cmeta.is_compiler_builtins() {
1191            self.dcx().emit_err(errors::CrateNotCompilerBuiltins { crate_name: cmeta.name() });
1192        }
1193    }
1194
1195    fn report_unused_deps(&mut self, krate: &ast::Crate) {
1196        // Make a point span rather than covering the whole file
1197        let span = krate.spans.inner_span.shrink_to_lo();
1198        // Complain about anything left over
1199        for (name, entry) in self.sess.opts.externs.iter() {
1200            if let ExternLocation::FoundInLibrarySearchDirectories = entry.location {
1201                // Don't worry about pathless `--extern foo` sysroot references
1202                continue;
1203            }
1204            if entry.nounused_dep || entry.force {
1205                // We're not worried about this one
1206                continue;
1207            }
1208            let name_interned = Symbol::intern(name);
1209            if self.used_extern_options.contains(&name_interned) {
1210                continue;
1211            }
1212
1213            // Got a real unused --extern
1214            if self.sess.opts.json_unused_externs.is_enabled() {
1215                self.cstore.unused_externs.push(name_interned);
1216                continue;
1217            }
1218
1219            self.sess.psess.buffer_lint(
1220                lint::builtin::UNUSED_CRATE_DEPENDENCIES,
1221                span,
1222                ast::CRATE_NODE_ID,
1223                BuiltinLintDiag::UnusedCrateDependency {
1224                    extern_crate: name_interned,
1225                    local_crate: self.tcx.crate_name(LOCAL_CRATE),
1226                },
1227            );
1228        }
1229    }
1230
1231    fn report_future_incompatible_deps(&self, krate: &ast::Crate) {
1232        let name = self.tcx.crate_name(LOCAL_CRATE);
1233
1234        if name.as_str() == "wasm_bindgen" {
1235            let major = env::var("CARGO_PKG_VERSION_MAJOR")
1236                .ok()
1237                .and_then(|major| u64::from_str(&major).ok());
1238            let minor = env::var("CARGO_PKG_VERSION_MINOR")
1239                .ok()
1240                .and_then(|minor| u64::from_str(&minor).ok());
1241            let patch = env::var("CARGO_PKG_VERSION_PATCH")
1242                .ok()
1243                .and_then(|patch| u64::from_str(&patch).ok());
1244
1245            match (major, minor, patch) {
1246                // v1 or bigger is valid.
1247                (Some(1..), _, _) => return,
1248                // v0.3 or bigger is valid.
1249                (Some(0), Some(3..), _) => return,
1250                // v0.2.88 or bigger is valid.
1251                (Some(0), Some(2), Some(88..)) => return,
1252                // Not using Cargo.
1253                (None, None, None) => return,
1254                _ => (),
1255            }
1256
1257            // Make a point span rather than covering the whole file
1258            let span = krate.spans.inner_span.shrink_to_lo();
1259
1260            self.sess.dcx().emit_err(errors::WasmCAbi { span });
1261        }
1262    }
1263
1264    pub fn postprocess(&mut self, krate: &ast::Crate) {
1265        self.inject_compiler_builtins(krate);
1266        self.inject_forced_externs();
1267        self.inject_profiler_runtime();
1268        self.inject_allocator_crate(krate);
1269        self.inject_panic_runtime(krate);
1270
1271        self.report_unused_deps(krate);
1272        self.report_future_incompatible_deps(krate);
1273
1274        info!("{:?}", CrateDump(self.cstore));
1275    }
1276
1277    /// Process an `extern crate foo` AST node.
1278    pub fn process_extern_crate(
1279        &mut self,
1280        item: &ast::Item,
1281        def_id: LocalDefId,
1282        definitions: &Definitions,
1283    ) -> Option<CrateNum> {
1284        match item.kind {
1285            ast::ItemKind::ExternCrate(orig_name, ident) => {
1286                debug!("resolving extern crate stmt. ident: {} orig_name: {:?}", ident, orig_name);
1287                let name = match orig_name {
1288                    Some(orig_name) => {
1289                        validate_crate_name(self.sess, orig_name, Some(item.span));
1290                        orig_name
1291                    }
1292                    None => ident.name,
1293                };
1294                let dep_kind = if attr::contains_name(&item.attrs, sym::no_link) {
1295                    CrateDepKind::MacrosOnly
1296                } else {
1297                    CrateDepKind::Explicit
1298                };
1299
1300                let cnum = self.resolve_crate(name, item.span, dep_kind, CrateOrigin::Extern)?;
1301
1302                let path_len = definitions.def_path(def_id).data.len();
1303                self.cstore.update_extern_crate(
1304                    cnum,
1305                    ExternCrate {
1306                        src: ExternCrateSource::Extern(def_id.to_def_id()),
1307                        span: item.span,
1308                        path_len,
1309                        dependency_of: LOCAL_CRATE,
1310                    },
1311                );
1312                Some(cnum)
1313            }
1314            _ => bug!(),
1315        }
1316    }
1317
1318    pub fn process_path_extern(&mut self, name: Symbol, span: Span) -> Option<CrateNum> {
1319        let cnum = self.resolve_crate(name, span, CrateDepKind::Explicit, CrateOrigin::Extern)?;
1320
1321        self.cstore.update_extern_crate(
1322            cnum,
1323            ExternCrate {
1324                src: ExternCrateSource::Path,
1325                span,
1326                // to have the least priority in `update_extern_crate`
1327                path_len: usize::MAX,
1328                dependency_of: LOCAL_CRATE,
1329            },
1330        );
1331
1332        Some(cnum)
1333    }
1334
1335    pub fn maybe_process_path_extern(&mut self, name: Symbol) -> Option<CrateNum> {
1336        self.maybe_resolve_crate(name, CrateDepKind::Explicit, CrateOrigin::Extern).ok()
1337    }
1338}
1339
1340fn fn_spans(krate: &ast::Crate, name: Symbol) -> Vec<Span> {
1341    struct Finder {
1342        name: Symbol,
1343        spans: Vec<Span>,
1344    }
1345    impl<'ast> visit::Visitor<'ast> for Finder {
1346        fn visit_item(&mut self, item: &'ast ast::Item) {
1347            if let Some(ident) = item.kind.ident()
1348                && ident.name == self.name
1349                && attr::contains_name(&item.attrs, sym::rustc_std_internal_symbol)
1350            {
1351                self.spans.push(item.span);
1352            }
1353            visit::walk_item(self, item)
1354        }
1355    }
1356
1357    let mut f = Finder { name, spans: Vec::new() };
1358    visit::walk_crate(&mut f, krate);
1359    f.spans
1360}
1361
1362fn format_dlopen_err(e: &(dyn std::error::Error + 'static)) -> String {
1363    e.sources().map(|e| format!(": {e}")).collect()
1364}
1365
1366fn attempt_load_dylib(path: &Path) -> Result<libloading::Library, libloading::Error> {
1367    #[cfg(target_os = "aix")]
1368    if let Some(ext) = path.extension()
1369        && ext.eq("a")
1370    {
1371        // On AIX, we ship all libraries as .a big_af archive
1372        // the expected format is lib<name>.a(libname.so) for the actual
1373        // dynamic library
1374        let library_name = path.file_stem().expect("expect a library name");
1375        let mut archive_member = std::ffi::OsString::from("a(");
1376        archive_member.push(library_name);
1377        archive_member.push(".so)");
1378        let new_path = path.with_extension(archive_member);
1379
1380        // On AIX, we need RTLD_MEMBER to dlopen an archived shared
1381        let flags = libc::RTLD_LAZY | libc::RTLD_LOCAL | libc::RTLD_MEMBER;
1382        return unsafe { libloading::os::unix::Library::open(Some(&new_path), flags) }
1383            .map(|lib| lib.into());
1384    }
1385
1386    unsafe { libloading::Library::new(&path) }
1387}
1388
1389// On Windows the compiler would sometimes intermittently fail to open the
1390// proc-macro DLL with `Error::LoadLibraryExW`. It is suspected that something in the
1391// system still holds a lock on the file, so we retry a few times before calling it
1392// an error.
1393fn load_dylib(path: &Path, max_attempts: usize) -> Result<libloading::Library, String> {
1394    assert!(max_attempts > 0);
1395
1396    let mut last_error = None;
1397
1398    for attempt in 0..max_attempts {
1399        debug!("Attempt to load proc-macro `{}`.", path.display());
1400        match attempt_load_dylib(path) {
1401            Ok(lib) => {
1402                if attempt > 0 {
1403                    debug!(
1404                        "Loaded proc-macro `{}` after {} attempts.",
1405                        path.display(),
1406                        attempt + 1
1407                    );
1408                }
1409                return Ok(lib);
1410            }
1411            Err(err) => {
1412                // Only try to recover from this specific error.
1413                if !matches!(err, libloading::Error::LoadLibraryExW { .. }) {
1414                    debug!("Failed to load proc-macro `{}`. Not retrying", path.display());
1415                    let err = format_dlopen_err(&err);
1416                    // We include the path of the dylib in the error ourselves, so
1417                    // if it's in the error, we strip it.
1418                    if let Some(err) = err.strip_prefix(&format!(": {}", path.display())) {
1419                        return Err(err.to_string());
1420                    }
1421                    return Err(err);
1422                }
1423
1424                last_error = Some(err);
1425                std::thread::sleep(Duration::from_millis(100));
1426                debug!("Failed to load proc-macro `{}`. Retrying.", path.display());
1427            }
1428        }
1429    }
1430
1431    debug!("Failed to load proc-macro `{}` even after {} attempts.", path.display(), max_attempts);
1432
1433    let last_error = last_error.unwrap();
1434    let message = if let Some(src) = last_error.source() {
1435        format!("{} ({src}) (retried {max_attempts} times)", format_dlopen_err(&last_error))
1436    } else {
1437        format!("{} (retried {max_attempts} times)", format_dlopen_err(&last_error))
1438    };
1439    Err(message)
1440}
1441
1442pub enum DylibError {
1443    DlOpen(String, String),
1444    DlSym(String, String),
1445}
1446
1447impl From<DylibError> for CrateError {
1448    fn from(err: DylibError) -> CrateError {
1449        match err {
1450            DylibError::DlOpen(path, err) => CrateError::DlOpen(path, err),
1451            DylibError::DlSym(path, err) => CrateError::DlSym(path, err),
1452        }
1453    }
1454}
1455
1456pub unsafe fn load_symbol_from_dylib<T: Copy>(
1457    path: &Path,
1458    sym_name: &str,
1459) -> Result<T, DylibError> {
1460    // Make sure the path contains a / or the linker will search for it.
1461    let path = try_canonicalize(path).unwrap();
1462    let lib =
1463        load_dylib(&path, 5).map_err(|err| DylibError::DlOpen(path.display().to_string(), err))?;
1464
1465    let sym = unsafe { lib.get::<T>(sym_name.as_bytes()) }
1466        .map_err(|err| DylibError::DlSym(path.display().to_string(), format_dlopen_err(&err)))?;
1467
1468    // Intentionally leak the dynamic library. We can't ever unload it
1469    // since the library can make things that will live arbitrarily long.
1470    let sym = unsafe { sym.into_raw() };
1471    std::mem::forget(lib);
1472
1473    Ok(*sym)
1474}