Skip to main content

rustc_metadata/
creader.rs

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