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