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