rustc_interface/
passes.rs

1use std::any::Any;
2use std::ffi::{OsStr, OsString};
3use std::io::{self, BufWriter, Write};
4use std::path::{Path, PathBuf};
5use std::sync::{Arc, LazyLock, OnceLock};
6use std::{env, fs, iter};
7
8use rustc_ast as ast;
9use rustc_codegen_ssa::traits::CodegenBackend;
10use rustc_data_structures::parallel;
11use rustc_data_structures::steal::Steal;
12use rustc_data_structures::sync::{AppendOnlyIndexVec, FreezeLock, WorkerLocal};
13use rustc_expand::base::{ExtCtxt, LintStoreExpand};
14use rustc_feature::Features;
15use rustc_fs_util::try_canonicalize;
16use rustc_hir::def_id::{LOCAL_CRATE, StableCrateId, StableCrateIdMap};
17use rustc_hir::definitions::Definitions;
18use rustc_incremental::setup_dep_graph;
19use rustc_lint::{BufferedEarlyLint, EarlyCheckNode, LintStore, unerased_lint_store};
20use rustc_metadata::creader::CStore;
21use rustc_middle::arena::Arena;
22use rustc_middle::dep_graph::DepsType;
23use rustc_middle::ty::{self, CurrentGcx, GlobalCtxt, RegisteredTools, TyCtxt};
24use rustc_middle::util::Providers;
25use rustc_parse::{
26    new_parser_from_file, new_parser_from_source_str, unwrap_or_emit_fatal, validate_attr,
27};
28use rustc_passes::{abi_test, input_stats, layout_test};
29use rustc_resolve::Resolver;
30use rustc_session::config::{CrateType, Input, OutFileName, OutputFilenames, OutputType};
31use rustc_session::cstore::Untracked;
32use rustc_session::output::{collect_crate_types, filename_for_input};
33use rustc_session::search_paths::PathKind;
34use rustc_session::{Limit, Session};
35use rustc_span::{
36    ErrorGuaranteed, FileName, SourceFileHash, SourceFileHashAlgorithm, Span, Symbol, sym,
37};
38use rustc_target::spec::PanicStrategy;
39use rustc_trait_selection::traits;
40use tracing::{info, instrument};
41
42use crate::interface::Compiler;
43use crate::{errors, limits, proc_macro_decls, util};
44
45pub fn parse<'a>(sess: &'a Session) -> ast::Crate {
46    let krate = sess
47        .time("parse_crate", || {
48            let mut parser = unwrap_or_emit_fatal(match &sess.io.input {
49                Input::File(file) => new_parser_from_file(&sess.psess, file, None),
50                Input::Str { input, name } => {
51                    new_parser_from_source_str(&sess.psess, name.clone(), input.clone())
52                }
53            });
54            parser.parse_crate_mod()
55        })
56        .unwrap_or_else(|parse_error| {
57            let guar: ErrorGuaranteed = parse_error.emit();
58            guar.raise_fatal();
59        });
60
61    if sess.opts.unstable_opts.input_stats {
62        input_stats::print_ast_stats(&krate, "PRE EXPANSION AST STATS", "ast-stats-1");
63    }
64
65    krate
66}
67
68fn pre_expansion_lint<'a>(
69    sess: &Session,
70    features: &Features,
71    lint_store: &LintStore,
72    registered_tools: &RegisteredTools,
73    check_node: impl EarlyCheckNode<'a>,
74    node_name: Symbol,
75) {
76    sess.prof.generic_activity_with_arg("pre_AST_expansion_lint_checks", node_name.as_str()).run(
77        || {
78            rustc_lint::check_ast_node(
79                sess,
80                None,
81                features,
82                true,
83                lint_store,
84                registered_tools,
85                None,
86                rustc_lint::BuiltinCombinedPreExpansionLintPass::new(),
87                check_node,
88            );
89        },
90    );
91}
92
93// Cannot implement directly for `LintStore` due to trait coherence.
94struct LintStoreExpandImpl<'a>(&'a LintStore);
95
96impl LintStoreExpand for LintStoreExpandImpl<'_> {
97    fn pre_expansion_lint(
98        &self,
99        sess: &Session,
100        features: &Features,
101        registered_tools: &RegisteredTools,
102        node_id: ast::NodeId,
103        attrs: &[ast::Attribute],
104        items: &[rustc_ast::ptr::P<ast::Item>],
105        name: Symbol,
106    ) {
107        pre_expansion_lint(sess, features, self.0, registered_tools, (node_id, attrs, items), name);
108    }
109}
110
111/// Runs the "early phases" of the compiler: initial `cfg` processing,
112/// syntax expansion, secondary `cfg` expansion, synthesis of a test
113/// harness if one is to be provided, injection of a dependency on the
114/// standard library and prelude, and name resolution.
115#[instrument(level = "trace", skip(krate, resolver))]
116fn configure_and_expand(
117    mut krate: ast::Crate,
118    pre_configured_attrs: &[ast::Attribute],
119    resolver: &mut Resolver<'_, '_>,
120) -> ast::Crate {
121    let tcx = resolver.tcx();
122    let sess = tcx.sess;
123    let features = tcx.features();
124    let lint_store = unerased_lint_store(tcx.sess);
125    let crate_name = tcx.crate_name(LOCAL_CRATE);
126    let lint_check_node = (&krate, pre_configured_attrs);
127    pre_expansion_lint(
128        sess,
129        features,
130        lint_store,
131        tcx.registered_tools(()),
132        lint_check_node,
133        crate_name,
134    );
135    rustc_builtin_macros::register_builtin_macros(resolver);
136
137    let num_standard_library_imports = sess.time("crate_injection", || {
138        rustc_builtin_macros::standard_library_imports::inject(
139            &mut krate,
140            pre_configured_attrs,
141            resolver,
142            sess,
143            features,
144        )
145    });
146
147    util::check_attr_crate_type(sess, pre_configured_attrs, resolver.lint_buffer());
148
149    // Expand all macros
150    krate = sess.time("macro_expand_crate", || {
151        // Windows dlls do not have rpaths, so they don't know how to find their
152        // dependencies. It's up to us to tell the system where to find all the
153        // dependent dlls. Note that this uses cfg!(windows) as opposed to
154        // targ_cfg because syntax extensions are always loaded for the host
155        // compiler, not for the target.
156        //
157        // This is somewhat of an inherently racy operation, however, as
158        // multiple threads calling this function could possibly continue
159        // extending PATH far beyond what it should. To solve this for now we
160        // just don't add any new elements to PATH which are already there
161        // within PATH. This is basically a targeted fix at #17360 for rustdoc
162        // which runs rustc in parallel but has been seen (#33844) to cause
163        // problems with PATH becoming too long.
164        let mut old_path = OsString::new();
165        if cfg!(windows) {
166            old_path = env::var_os("PATH").unwrap_or(old_path);
167            let mut new_path = Vec::from_iter(
168                sess.host_filesearch().search_paths(PathKind::All).map(|p| p.dir.clone()),
169            );
170            for path in env::split_paths(&old_path) {
171                if !new_path.contains(&path) {
172                    new_path.push(path);
173                }
174            }
175            unsafe {
176                env::set_var(
177                    "PATH",
178                    &env::join_paths(
179                        new_path.iter().filter(|p| env::join_paths(iter::once(p)).is_ok()),
180                    )
181                    .unwrap(),
182                );
183            }
184        }
185
186        // Create the config for macro expansion
187        let recursion_limit = get_recursion_limit(pre_configured_attrs, sess);
188        let cfg = rustc_expand::expand::ExpansionConfig {
189            crate_name: crate_name.to_string(),
190            features,
191            recursion_limit,
192            trace_mac: sess.opts.unstable_opts.trace_macros,
193            should_test: sess.is_test_crate(),
194            span_debug: sess.opts.unstable_opts.span_debug,
195            proc_macro_backtrace: sess.opts.unstable_opts.proc_macro_backtrace,
196        };
197
198        let lint_store = LintStoreExpandImpl(lint_store);
199        let mut ecx = ExtCtxt::new(sess, cfg, resolver, Some(&lint_store));
200        ecx.num_standard_library_imports = num_standard_library_imports;
201        // Expand macros now!
202        let krate = sess.time("expand_crate", || ecx.monotonic_expander().expand_crate(krate));
203
204        // The rest is error reporting
205
206        sess.psess.buffered_lints.with_lock(|buffered_lints: &mut Vec<BufferedEarlyLint>| {
207            buffered_lints.append(&mut ecx.buffered_early_lint);
208        });
209
210        sess.time("check_unused_macros", || {
211            ecx.check_unused_macros();
212        });
213
214        // If we hit a recursion limit, exit early to avoid later passes getting overwhelmed
215        // with a large AST
216        if ecx.reduced_recursion_limit.is_some() {
217            sess.dcx().abort_if_errors();
218            unreachable!();
219        }
220
221        if cfg!(windows) {
222            unsafe {
223                env::set_var("PATH", &old_path);
224            }
225        }
226
227        krate
228    });
229
230    sess.time("maybe_building_test_harness", || {
231        rustc_builtin_macros::test_harness::inject(&mut krate, sess, features, resolver)
232    });
233
234    let has_proc_macro_decls = sess.time("AST_validation", || {
235        rustc_ast_passes::ast_validation::check_crate(
236            sess,
237            features,
238            &krate,
239            resolver.lint_buffer(),
240        )
241    });
242
243    let crate_types = tcx.crate_types();
244    let is_executable_crate = crate_types.contains(&CrateType::Executable);
245    let is_proc_macro_crate = crate_types.contains(&CrateType::ProcMacro);
246
247    if crate_types.len() > 1 {
248        if is_executable_crate {
249            sess.dcx().emit_err(errors::MixedBinCrate);
250        }
251        if is_proc_macro_crate {
252            sess.dcx().emit_err(errors::MixedProcMacroCrate);
253        }
254    }
255
256    if is_proc_macro_crate && sess.panic_strategy() == PanicStrategy::Abort {
257        sess.dcx().emit_warn(errors::ProcMacroCratePanicAbort);
258    }
259
260    sess.time("maybe_create_a_macro_crate", || {
261        let is_test_crate = sess.is_test_crate();
262        rustc_builtin_macros::proc_macro_harness::inject(
263            &mut krate,
264            sess,
265            features,
266            resolver,
267            is_proc_macro_crate,
268            has_proc_macro_decls,
269            is_test_crate,
270            sess.dcx(),
271        )
272    });
273
274    // Done with macro expansion!
275
276    resolver.resolve_crate(&krate);
277
278    CStore::from_tcx(tcx).report_incompatible_target_modifiers(tcx, &krate);
279    krate
280}
281
282fn early_lint_checks(tcx: TyCtxt<'_>, (): ()) {
283    let sess = tcx.sess;
284    let (resolver, krate) = &*tcx.resolver_for_lowering().borrow();
285    let mut lint_buffer = resolver.lint_buffer.steal();
286
287    if sess.opts.unstable_opts.input_stats {
288        input_stats::print_ast_stats(krate, "POST EXPANSION AST STATS", "ast-stats-2");
289    }
290
291    // Needs to go *after* expansion to be able to check the results of macro expansion.
292    sess.time("complete_gated_feature_checking", || {
293        rustc_ast_passes::feature_gate::check_crate(krate, sess, tcx.features());
294    });
295
296    // Add all buffered lints from the `ParseSess` to the `Session`.
297    sess.psess.buffered_lints.with_lock(|buffered_lints| {
298        info!("{} parse sess buffered_lints", buffered_lints.len());
299        for early_lint in buffered_lints.drain(..) {
300            lint_buffer.add_early_lint(early_lint);
301        }
302    });
303
304    // Gate identifiers containing invalid Unicode codepoints that were recovered during lexing.
305    sess.psess.bad_unicode_identifiers.with_lock(|identifiers| {
306        for (ident, mut spans) in identifiers.drain(..) {
307            spans.sort();
308            if ident == sym::ferris {
309                enum FerrisFix {
310                    SnakeCase,
311                    ScreamingSnakeCase,
312                    PascalCase,
313                }
314
315                impl FerrisFix {
316                    const fn as_str(self) -> &'static str {
317                        match self {
318                            FerrisFix::SnakeCase => "ferris",
319                            FerrisFix::ScreamingSnakeCase => "FERRIS",
320                            FerrisFix::PascalCase => "Ferris",
321                        }
322                    }
323                }
324
325                let first_span = spans[0];
326                let prev_source = sess.psess.source_map().span_to_prev_source(first_span);
327                let ferris_fix = prev_source
328                    .map_or(FerrisFix::SnakeCase, |source| {
329                        let mut source_before_ferris = source.trim_end().split_whitespace().rev();
330                        match source_before_ferris.next() {
331                            Some("struct" | "trait" | "mod" | "union" | "type" | "enum") => {
332                                FerrisFix::PascalCase
333                            }
334                            Some("const" | "static") => FerrisFix::ScreamingSnakeCase,
335                            Some("mut") if source_before_ferris.next() == Some("static") => {
336                                FerrisFix::ScreamingSnakeCase
337                            }
338                            _ => FerrisFix::SnakeCase,
339                        }
340                    })
341                    .as_str();
342
343                sess.dcx().emit_err(errors::FerrisIdentifier { spans, first_span, ferris_fix });
344            } else {
345                sess.dcx().emit_err(errors::EmojiIdentifier { spans, ident });
346            }
347        }
348    });
349
350    let lint_store = unerased_lint_store(tcx.sess);
351    rustc_lint::check_ast_node(
352        sess,
353        Some(tcx),
354        tcx.features(),
355        false,
356        lint_store,
357        tcx.registered_tools(()),
358        Some(lint_buffer),
359        rustc_lint::BuiltinCombinedEarlyLintPass::new(),
360        (&**krate, &*krate.attrs),
361    )
362}
363
364fn env_var_os<'tcx>(tcx: TyCtxt<'tcx>, key: &'tcx OsStr) -> Option<&'tcx OsStr> {
365    let value = env::var_os(key);
366
367    let value_tcx = value.as_ref().map(|value| {
368        let encoded_bytes = tcx.arena.alloc_slice(value.as_encoded_bytes());
369        debug_assert_eq!(value.as_encoded_bytes(), encoded_bytes);
370        // SAFETY: The bytes came from `as_encoded_bytes`, and we assume that
371        // `alloc_slice` is implemented correctly, and passes the same bytes
372        // back (debug asserted above).
373        unsafe { OsStr::from_encoded_bytes_unchecked(encoded_bytes) }
374    });
375
376    // Also add the variable to Cargo's dependency tracking
377    //
378    // NOTE: This only works for passes run before `write_dep_info`. See that
379    // for extension points for configuring environment variables to be
380    // properly change-tracked.
381    tcx.sess.psess.env_depinfo.borrow_mut().insert((
382        Symbol::intern(&key.to_string_lossy()),
383        value.as_ref().and_then(|value| value.to_str()).map(|value| Symbol::intern(&value)),
384    ));
385
386    value_tcx
387}
388
389// Returns all the paths that correspond to generated files.
390fn generated_output_paths(
391    tcx: TyCtxt<'_>,
392    outputs: &OutputFilenames,
393    exact_name: bool,
394    crate_name: Symbol,
395) -> Vec<PathBuf> {
396    let sess = tcx.sess;
397    let mut out_filenames = Vec::new();
398    for output_type in sess.opts.output_types.keys() {
399        let out_filename = outputs.path(*output_type);
400        let file = out_filename.as_path().to_path_buf();
401        match *output_type {
402            // If the filename has been overridden using `-o`, it will not be modified
403            // by appending `.rlib`, `.exe`, etc., so we can skip this transformation.
404            OutputType::Exe if !exact_name => {
405                for crate_type in tcx.crate_types().iter() {
406                    let p = filename_for_input(sess, *crate_type, crate_name, outputs);
407                    out_filenames.push(p.as_path().to_path_buf());
408                }
409            }
410            OutputType::DepInfo if sess.opts.unstable_opts.dep_info_omit_d_target => {
411                // Don't add the dep-info output when omitting it from dep-info targets
412            }
413            OutputType::DepInfo if out_filename.is_stdout() => {
414                // Don't add the dep-info output when it goes to stdout
415            }
416            _ => {
417                out_filenames.push(file);
418            }
419        }
420    }
421    out_filenames
422}
423
424fn output_contains_path(output_paths: &[PathBuf], input_path: &Path) -> bool {
425    let input_path = try_canonicalize(input_path).ok();
426    if input_path.is_none() {
427        return false;
428    }
429    output_paths.iter().any(|output_path| try_canonicalize(output_path).ok() == input_path)
430}
431
432fn output_conflicts_with_dir(output_paths: &[PathBuf]) -> Option<&PathBuf> {
433    output_paths.iter().find(|output_path| output_path.is_dir())
434}
435
436fn escape_dep_filename(filename: &str) -> String {
437    // Apparently clang and gcc *only* escape spaces:
438    // https://llvm.org/klaus/clang/commit/9d50634cfc268ecc9a7250226dd5ca0e945240d4
439    filename.replace(' ', "\\ ")
440}
441
442// Makefile comments only need escaping newlines and `\`.
443// The result can be unescaped by anything that can unescape `escape_default` and friends.
444fn escape_dep_env(symbol: Symbol) -> String {
445    let s = symbol.as_str();
446    let mut escaped = String::with_capacity(s.len());
447    for c in s.chars() {
448        match c {
449            '\n' => escaped.push_str(r"\n"),
450            '\r' => escaped.push_str(r"\r"),
451            '\\' => escaped.push_str(r"\\"),
452            _ => escaped.push(c),
453        }
454    }
455    escaped
456}
457
458fn write_out_deps(tcx: TyCtxt<'_>, outputs: &OutputFilenames, out_filenames: &[PathBuf]) {
459    // Write out dependency rules to the dep-info file if requested
460    let sess = tcx.sess;
461    if !sess.opts.output_types.contains_key(&OutputType::DepInfo) {
462        return;
463    }
464    let deps_output = outputs.path(OutputType::DepInfo);
465    let deps_filename = deps_output.as_path();
466
467    let result: io::Result<()> = try {
468        // Build a list of files used to compile the output and
469        // write Makefile-compatible dependency rules
470        let mut files: Vec<(String, u64, Option<SourceFileHash>)> = sess
471            .source_map()
472            .files()
473            .iter()
474            .filter(|fmap| fmap.is_real_file())
475            .filter(|fmap| !fmap.is_imported())
476            .map(|fmap| {
477                (
478                    escape_dep_filename(&fmap.name.prefer_local().to_string()),
479                    fmap.source_len.0 as u64,
480                    fmap.checksum_hash,
481                )
482            })
483            .collect();
484
485        let checksum_hash_algo = sess.opts.unstable_opts.checksum_hash_algorithm;
486
487        // Account for explicitly marked-to-track files
488        // (e.g. accessed in proc macros).
489        let file_depinfo = sess.psess.file_depinfo.borrow();
490
491        let normalize_path = |path: PathBuf| {
492            let file = FileName::from(path);
493            escape_dep_filename(&file.prefer_local().to_string())
494        };
495
496        // The entries will be used to declare dependencies between files in a
497        // Makefile-like output, so the iteration order does not matter.
498        fn hash_iter_files<P: AsRef<Path>>(
499            it: impl Iterator<Item = P>,
500            checksum_hash_algo: Option<SourceFileHashAlgorithm>,
501        ) -> impl Iterator<Item = (P, u64, Option<SourceFileHash>)> {
502            it.map(move |path| {
503                match checksum_hash_algo.and_then(|algo| {
504                    fs::File::open(path.as_ref())
505                        .and_then(|mut file| {
506                            SourceFileHash::new(algo, &mut file).map(|h| (file, h))
507                        })
508                        .and_then(|(file, h)| file.metadata().map(|m| (m.len(), h)))
509                        .map_err(|e| {
510                            tracing::error!(
511                                "failed to compute checksum, omitting it from dep-info {} {e}",
512                                path.as_ref().display()
513                            )
514                        })
515                        .ok()
516                }) {
517                    Some((file_len, checksum)) => (path, file_len, Some(checksum)),
518                    None => (path, 0, None),
519                }
520            })
521        }
522
523        let extra_tracked_files = hash_iter_files(
524            file_depinfo.iter().map(|path_sym| normalize_path(PathBuf::from(path_sym.as_str()))),
525            checksum_hash_algo,
526        );
527        files.extend(extra_tracked_files);
528
529        // We also need to track used PGO profile files
530        if let Some(ref profile_instr) = sess.opts.cg.profile_use {
531            files.extend(hash_iter_files(
532                iter::once(normalize_path(profile_instr.as_path().to_path_buf())),
533                checksum_hash_algo,
534            ));
535        }
536        if let Some(ref profile_sample) = sess.opts.unstable_opts.profile_sample_use {
537            files.extend(hash_iter_files(
538                iter::once(normalize_path(profile_sample.as_path().to_path_buf())),
539                checksum_hash_algo,
540            ));
541        }
542
543        // Debugger visualizer files
544        for debugger_visualizer in tcx.debugger_visualizers(LOCAL_CRATE) {
545            files.extend(hash_iter_files(
546                iter::once(normalize_path(debugger_visualizer.path.clone().unwrap())),
547                checksum_hash_algo,
548            ));
549        }
550
551        if sess.binary_dep_depinfo() {
552            if let Some(ref backend) = sess.opts.unstable_opts.codegen_backend {
553                if backend.contains('.') {
554                    // If the backend name contain a `.`, it is the path to an external dynamic
555                    // library. If not, it is not a path.
556                    files.extend(hash_iter_files(
557                        iter::once(backend.to_string()),
558                        checksum_hash_algo,
559                    ));
560                }
561            }
562
563            for &cnum in tcx.crates(()) {
564                let source = tcx.used_crate_source(cnum);
565                if let Some((path, _)) = &source.dylib {
566                    files.extend(hash_iter_files(
567                        iter::once(escape_dep_filename(&path.display().to_string())),
568                        checksum_hash_algo,
569                    ));
570                }
571                if let Some((path, _)) = &source.rlib {
572                    files.extend(hash_iter_files(
573                        iter::once(escape_dep_filename(&path.display().to_string())),
574                        checksum_hash_algo,
575                    ));
576                }
577                if let Some((path, _)) = &source.rmeta {
578                    files.extend(hash_iter_files(
579                        iter::once(escape_dep_filename(&path.display().to_string())),
580                        checksum_hash_algo,
581                    ));
582                }
583            }
584        }
585
586        let write_deps_to_file = |file: &mut dyn Write| -> io::Result<()> {
587            for path in out_filenames {
588                writeln!(
589                    file,
590                    "{}: {}\n",
591                    path.display(),
592                    files
593                        .iter()
594                        .map(|(path, _file_len, _checksum_hash_algo)| path.as_str())
595                        .intersperse(" ")
596                        .collect::<String>()
597                )?;
598            }
599
600            // Emit a fake target for each input file to the compilation. This
601            // prevents `make` from spitting out an error if a file is later
602            // deleted. For more info see #28735
603            for (path, _file_len, _checksum_hash_algo) in &files {
604                writeln!(file, "{path}:")?;
605            }
606
607            // Emit special comments with information about accessed environment variables.
608            let env_depinfo = sess.psess.env_depinfo.borrow();
609            if !env_depinfo.is_empty() {
610                // We will soon sort, so the initial order does not matter.
611                #[allow(rustc::potential_query_instability)]
612                let mut envs: Vec<_> = env_depinfo
613                    .iter()
614                    .map(|(k, v)| (escape_dep_env(*k), v.map(escape_dep_env)))
615                    .collect();
616                envs.sort_unstable();
617                writeln!(file)?;
618                for (k, v) in envs {
619                    write!(file, "# env-dep:{k}")?;
620                    if let Some(v) = v {
621                        write!(file, "={v}")?;
622                    }
623                    writeln!(file)?;
624                }
625            }
626
627            // If caller requested this information, add special comments about source file checksums.
628            // These are not necessarily the same checksums as was used in the debug files.
629            if sess.opts.unstable_opts.checksum_hash_algorithm().is_some() {
630                files
631                    .iter()
632                    .filter_map(|(path, file_len, hash_algo)| {
633                        hash_algo.map(|hash_algo| (path, file_len, hash_algo))
634                    })
635                    .try_for_each(|(path, file_len, checksum_hash)| {
636                        writeln!(file, "# checksum:{checksum_hash} file_len:{file_len} {path}")
637                    })?;
638            }
639
640            Ok(())
641        };
642
643        match deps_output {
644            OutFileName::Stdout => {
645                let mut file = BufWriter::new(io::stdout());
646                write_deps_to_file(&mut file)?;
647            }
648            OutFileName::Real(ref path) => {
649                let mut file = fs::File::create_buffered(path)?;
650                write_deps_to_file(&mut file)?;
651            }
652        }
653    };
654
655    match result {
656        Ok(_) => {
657            if sess.opts.json_artifact_notifications {
658                sess.dcx().emit_artifact_notification(deps_filename, "dep-info");
659            }
660        }
661        Err(error) => {
662            sess.dcx().emit_fatal(errors::ErrorWritingDependencies { path: deps_filename, error });
663        }
664    }
665}
666
667fn resolver_for_lowering_raw<'tcx>(
668    tcx: TyCtxt<'tcx>,
669    (): (),
670) -> (&'tcx Steal<(ty::ResolverAstLowering, Arc<ast::Crate>)>, &'tcx ty::ResolverGlobalCtxt) {
671    let arenas = Resolver::arenas();
672    let _ = tcx.registered_tools(()); // Uses `crate_for_resolver`.
673    let (krate, pre_configured_attrs) = tcx.crate_for_resolver(()).steal();
674    let mut resolver = Resolver::new(
675        tcx,
676        &pre_configured_attrs,
677        krate.spans.inner_span,
678        krate.spans.inject_use_span,
679        &arenas,
680    );
681    let krate = configure_and_expand(krate, &pre_configured_attrs, &mut resolver);
682
683    // Make sure we don't mutate the cstore from here on.
684    tcx.untracked().cstore.freeze();
685
686    let ty::ResolverOutputs {
687        global_ctxt: untracked_resolutions,
688        ast_lowering: untracked_resolver_for_lowering,
689    } = resolver.into_outputs();
690
691    let resolutions = tcx.arena.alloc(untracked_resolutions);
692    (tcx.arena.alloc(Steal::new((untracked_resolver_for_lowering, Arc::new(krate)))), resolutions)
693}
694
695pub fn write_dep_info(tcx: TyCtxt<'_>) {
696    // Make sure name resolution and macro expansion is run for
697    // the side-effect of providing a complete set of all
698    // accessed files and env vars.
699    let _ = tcx.resolver_for_lowering();
700
701    let sess = tcx.sess;
702    let _timer = sess.timer("write_dep_info");
703    let crate_name = tcx.crate_name(LOCAL_CRATE);
704
705    let outputs = tcx.output_filenames(());
706    let output_paths =
707        generated_output_paths(tcx, &outputs, sess.io.output_file.is_some(), crate_name);
708
709    // Ensure the source file isn't accidentally overwritten during compilation.
710    if let Some(input_path) = sess.io.input.opt_path() {
711        if sess.opts.will_create_output_file() {
712            if output_contains_path(&output_paths, input_path) {
713                sess.dcx().emit_fatal(errors::InputFileWouldBeOverWritten { path: input_path });
714            }
715            if let Some(dir_path) = output_conflicts_with_dir(&output_paths) {
716                sess.dcx().emit_fatal(errors::GeneratedFileConflictsWithDirectory {
717                    input_path,
718                    dir_path,
719                });
720            }
721        }
722    }
723
724    if let Some(ref dir) = sess.io.temps_dir {
725        if fs::create_dir_all(dir).is_err() {
726            sess.dcx().emit_fatal(errors::TempsDirError);
727        }
728    }
729
730    write_out_deps(tcx, &outputs, &output_paths);
731
732    let only_dep_info = sess.opts.output_types.contains_key(&OutputType::DepInfo)
733        && sess.opts.output_types.len() == 1;
734
735    if !only_dep_info {
736        if let Some(ref dir) = sess.io.output_dir {
737            if fs::create_dir_all(dir).is_err() {
738                sess.dcx().emit_fatal(errors::OutDirError);
739            }
740        }
741    }
742}
743
744pub static DEFAULT_QUERY_PROVIDERS: LazyLock<Providers> = LazyLock::new(|| {
745    let providers = &mut Providers::default();
746    providers.analysis = analysis;
747    providers.hir_crate = rustc_ast_lowering::lower_to_hir;
748    providers.resolver_for_lowering_raw = resolver_for_lowering_raw;
749    providers.stripped_cfg_items =
750        |tcx, _| tcx.arena.alloc_from_iter(tcx.resolutions(()).stripped_cfg_items.steal());
751    providers.resolutions = |tcx, ()| tcx.resolver_for_lowering_raw(()).1;
752    providers.early_lint_checks = early_lint_checks;
753    providers.env_var_os = env_var_os;
754    limits::provide(providers);
755    proc_macro_decls::provide(providers);
756    rustc_const_eval::provide(providers);
757    rustc_middle::hir::provide(providers);
758    rustc_borrowck::provide(providers);
759    rustc_incremental::provide(providers);
760    rustc_mir_build::provide(providers);
761    rustc_mir_transform::provide(providers);
762    rustc_monomorphize::provide(providers);
763    rustc_privacy::provide(providers);
764    rustc_query_impl::provide(providers);
765    rustc_resolve::provide(providers);
766    rustc_hir_analysis::provide(providers);
767    rustc_hir_typeck::provide(providers);
768    ty::provide(providers);
769    traits::provide(providers);
770    rustc_passes::provide(providers);
771    rustc_traits::provide(providers);
772    rustc_ty_utils::provide(providers);
773    rustc_metadata::provide(providers);
774    rustc_lint::provide(providers);
775    rustc_symbol_mangling::provide(providers);
776    rustc_codegen_ssa::provide(providers);
777    *providers
778});
779
780pub fn create_and_enter_global_ctxt<T, F: for<'tcx> FnOnce(TyCtxt<'tcx>) -> T>(
781    compiler: &Compiler,
782    mut krate: rustc_ast::Crate,
783    f: F,
784) -> T {
785    let sess = &compiler.sess;
786
787    rustc_builtin_macros::cmdline_attrs::inject(
788        &mut krate,
789        &sess.psess,
790        &sess.opts.unstable_opts.crate_attr,
791    );
792
793    let pre_configured_attrs = rustc_expand::config::pre_configure_attrs(sess, &krate.attrs);
794
795    let crate_name = get_crate_name(sess, &pre_configured_attrs);
796    let crate_types = collect_crate_types(sess, &pre_configured_attrs);
797    let stable_crate_id = StableCrateId::new(
798        crate_name,
799        crate_types.contains(&CrateType::Executable),
800        sess.opts.cg.metadata.clone(),
801        sess.cfg_version,
802    );
803    let outputs = util::build_output_filenames(&pre_configured_attrs, sess);
804
805    let dep_type = DepsType { dep_names: rustc_query_impl::dep_kind_names() };
806    let dep_graph = setup_dep_graph(sess, crate_name, &dep_type);
807
808    let cstore =
809        FreezeLock::new(Box::new(CStore::new(compiler.codegen_backend.metadata_loader())) as _);
810    let definitions = FreezeLock::new(Definitions::new(stable_crate_id));
811
812    let stable_crate_ids = FreezeLock::new(StableCrateIdMap::default());
813    let untracked =
814        Untracked { cstore, source_span: AppendOnlyIndexVec::new(), definitions, stable_crate_ids };
815
816    // We're constructing the HIR here; we don't care what we will
817    // read, since we haven't even constructed the *input* to
818    // incr. comp. yet.
819    dep_graph.assert_ignored();
820
821    let query_result_on_disk_cache = rustc_incremental::load_query_result_cache(sess);
822
823    let codegen_backend = &compiler.codegen_backend;
824    let mut providers = *DEFAULT_QUERY_PROVIDERS;
825    codegen_backend.provide(&mut providers);
826
827    if let Some(callback) = compiler.override_queries {
828        callback(sess, &mut providers);
829    }
830
831    let incremental = dep_graph.is_fully_enabled();
832
833    let gcx_cell = OnceLock::new();
834    let arena = WorkerLocal::new(|_| Arena::default());
835    let hir_arena = WorkerLocal::new(|_| rustc_hir::Arena::default());
836
837    // This closure is necessary to force rustc to perform the correct lifetime
838    // subtyping for GlobalCtxt::enter to be allowed.
839    let inner: Box<
840        dyn for<'tcx> FnOnce(
841            &'tcx Session,
842            CurrentGcx,
843            &'tcx OnceLock<GlobalCtxt<'tcx>>,
844            &'tcx WorkerLocal<Arena<'tcx>>,
845            &'tcx WorkerLocal<rustc_hir::Arena<'tcx>>,
846            F,
847        ) -> T,
848    > = Box::new(move |sess, current_gcx, gcx_cell, arena, hir_arena, f| {
849        TyCtxt::create_global_ctxt(
850            gcx_cell,
851            sess,
852            crate_types,
853            stable_crate_id,
854            arena,
855            hir_arena,
856            untracked,
857            dep_graph,
858            rustc_query_impl::query_callbacks(arena),
859            rustc_query_impl::query_system(
860                providers.queries,
861                providers.extern_queries,
862                query_result_on_disk_cache,
863                incremental,
864            ),
865            providers.hooks,
866            current_gcx,
867            |tcx| {
868                let feed = tcx.create_crate_num(stable_crate_id).unwrap();
869                assert_eq!(feed.key(), LOCAL_CRATE);
870                feed.crate_name(crate_name);
871
872                let feed = tcx.feed_unit_query();
873                feed.features_query(tcx.arena.alloc(rustc_expand::config::features(
874                    tcx.sess,
875                    &pre_configured_attrs,
876                    crate_name,
877                )));
878                feed.crate_for_resolver(tcx.arena.alloc(Steal::new((krate, pre_configured_attrs))));
879                feed.output_filenames(Arc::new(outputs));
880
881                let res = f(tcx);
882                // FIXME maybe run finish even when a fatal error occured? or at least tcx.alloc_self_profile_query_strings()?
883                tcx.finish();
884                res
885            },
886        )
887    });
888
889    inner(&compiler.sess, compiler.current_gcx.clone(), &gcx_cell, &arena, &hir_arena, f)
890}
891
892/// Runs all analyses that we guarantee to run, even if errors were reported in earlier analyses.
893/// This function never fails.
894fn run_required_analyses(tcx: TyCtxt<'_>) {
895    if tcx.sess.opts.unstable_opts.input_stats {
896        rustc_passes::input_stats::print_hir_stats(tcx);
897    }
898    // When using rustdoc's "jump to def" feature, it enters this code and `check_crate`
899    // is not defined. So we need to cfg it out.
900    #[cfg(all(not(doc), debug_assertions))]
901    rustc_passes::hir_id_validator::check_crate(tcx);
902    let sess = tcx.sess;
903    sess.time("misc_checking_1", || {
904        parallel!(
905            {
906                sess.time("looking_for_entry_point", || tcx.ensure_ok().entry_fn(()));
907
908                sess.time("looking_for_derive_registrar", || {
909                    tcx.ensure_ok().proc_macro_decls_static(())
910                });
911
912                CStore::from_tcx(tcx).report_unused_deps(tcx);
913            },
914            {
915                tcx.par_hir_for_each_module(|module| {
916                    tcx.ensure_ok().check_mod_loops(module);
917                    tcx.ensure_ok().check_mod_attrs(module);
918                    tcx.ensure_ok().check_mod_naked_functions(module);
919                    tcx.ensure_ok().check_mod_unstable_api_usage(module);
920                });
921            },
922            {
923                sess.time("unused_lib_feature_checking", || {
924                    rustc_passes::stability::check_unused_or_stable_features(tcx)
925                });
926            },
927            {
928                // We force these queries to run,
929                // since they might not otherwise get called.
930                // This marks the corresponding crate-level attributes
931                // as used, and ensures that their values are valid.
932                tcx.ensure_ok().limits(());
933                tcx.ensure_ok().stability_index(());
934            }
935        );
936    });
937
938    rustc_hir_analysis::check_crate(tcx);
939    sess.time("MIR_coroutine_by_move_body", || {
940        tcx.par_hir_body_owners(|def_id| {
941            if tcx.needs_coroutine_by_move_body_def_id(def_id.to_def_id()) {
942                tcx.ensure_done().coroutine_by_move_body_def_id(def_id);
943            }
944        });
945    });
946    // Freeze definitions as we don't add new ones at this point.
947    // We need to wait until now since we synthesize a by-move body
948    // for all coroutine-closures.
949    //
950    // This improves performance by allowing lock-free access to them.
951    tcx.untracked().definitions.freeze();
952
953    sess.time("MIR_borrow_checking", || {
954        tcx.par_hir_body_owners(|def_id| {
955            // Run unsafety check because it's responsible for stealing and
956            // deallocating THIR.
957            tcx.ensure_ok().check_unsafety(def_id);
958            tcx.ensure_ok().mir_borrowck(def_id)
959        });
960    });
961    sess.time("MIR_effect_checking", || {
962        tcx.par_hir_body_owners(|def_id| {
963            tcx.ensure_ok().has_ffi_unwind_calls(def_id);
964
965            // If we need to codegen, ensure that we emit all errors from
966            // `mir_drops_elaborated_and_const_checked` now, to avoid discovering
967            // them later during codegen.
968            if tcx.sess.opts.output_types.should_codegen()
969                || tcx.hir_body_const_context(def_id).is_some()
970            {
971                tcx.ensure_ok().mir_drops_elaborated_and_const_checked(def_id);
972            }
973        });
974    });
975    sess.time("coroutine_obligations", || {
976        tcx.par_hir_body_owners(|def_id| {
977            if tcx.is_coroutine(def_id.to_def_id()) {
978                tcx.ensure_ok().mir_coroutine_witnesses(def_id);
979                tcx.ensure_ok().check_coroutine_obligations(
980                    tcx.typeck_root_def_id(def_id.to_def_id()).expect_local(),
981                );
982                // Eagerly check the unsubstituted layout for cycles.
983                tcx.ensure_ok().layout_of(
984                    ty::TypingEnv::post_analysis(tcx, def_id.to_def_id())
985                        .as_query_input(tcx.type_of(def_id).instantiate_identity()),
986                );
987            }
988        });
989    });
990
991    sess.time("layout_testing", || layout_test::test_layout(tcx));
992    sess.time("abi_testing", || abi_test::test_abi(tcx));
993
994    // If `-Zvalidate-mir` is set, we also want to compute the final MIR for each item
995    // (either its `mir_for_ctfe` or `optimized_mir`) since that helps uncover any bugs
996    // in MIR optimizations that may only be reachable through codegen, or other codepaths
997    // that requires the optimized/ctfe MIR, coroutine bodies, or evaluating consts.
998    if tcx.sess.opts.unstable_opts.validate_mir {
999        sess.time("ensuring_final_MIR_is_computable", || {
1000            tcx.par_hir_body_owners(|def_id| {
1001                tcx.instance_mir(ty::InstanceKind::Item(def_id.into()));
1002            });
1003        });
1004    }
1005}
1006
1007/// Runs the type-checking, region checking and other miscellaneous analysis
1008/// passes on the crate.
1009fn analysis(tcx: TyCtxt<'_>, (): ()) {
1010    run_required_analyses(tcx);
1011
1012    let sess = tcx.sess;
1013
1014    // Avoid overwhelming user with errors if borrow checking failed.
1015    // I'm not sure how helpful this is, to be honest, but it avoids a
1016    // lot of annoying errors in the ui tests (basically,
1017    // lint warnings and so on -- kindck used to do this abort, but
1018    // kindck is gone now). -nmatsakis
1019    //
1020    // But we exclude lint errors from this, because lint errors are typically
1021    // less serious and we're more likely to want to continue (#87337).
1022    if let Some(guar) = sess.dcx().has_errors_excluding_lint_errors() {
1023        guar.raise_fatal();
1024    }
1025
1026    sess.time("misc_checking_3", || {
1027        parallel!(
1028            {
1029                tcx.ensure_ok().effective_visibilities(());
1030
1031                parallel!(
1032                    {
1033                        tcx.ensure_ok().check_private_in_public(());
1034                    },
1035                    {
1036                        tcx.par_hir_for_each_module(|module| {
1037                            tcx.ensure_ok().check_mod_deathness(module)
1038                        });
1039                    },
1040                    {
1041                        sess.time("lint_checking", || {
1042                            rustc_lint::check_crate(tcx);
1043                        });
1044                    },
1045                    {
1046                        tcx.ensure_ok().clashing_extern_declarations(());
1047                    }
1048                );
1049            },
1050            {
1051                sess.time("privacy_checking_modules", || {
1052                    tcx.par_hir_for_each_module(|module| {
1053                        tcx.ensure_ok().check_mod_privacy(module);
1054                    });
1055                });
1056            }
1057        );
1058
1059        // This check has to be run after all lints are done processing. We don't
1060        // define a lint filter, as all lint checks should have finished at this point.
1061        sess.time("check_lint_expectations", || tcx.ensure_ok().check_expectations(None));
1062
1063        // This query is only invoked normally if a diagnostic is emitted that needs any
1064        // diagnostic item. If the crate compiles without checking any diagnostic items,
1065        // we will fail to emit overlap diagnostics. Thus we invoke it here unconditionally.
1066        let _ = tcx.all_diagnostic_items(());
1067    });
1068}
1069
1070/// Check for the `#[rustc_error]` annotation, which forces an error in codegen. This is used
1071/// to write UI tests that actually test that compilation succeeds without reporting
1072/// an error.
1073fn check_for_rustc_errors_attr(tcx: TyCtxt<'_>) {
1074    let Some((def_id, _)) = tcx.entry_fn(()) else { return };
1075    for attr in tcx.get_attrs(def_id, sym::rustc_error) {
1076        match attr.meta_item_list() {
1077            // Check if there is a `#[rustc_error(delayed_bug_from_inside_query)]`.
1078            Some(list)
1079                if list.iter().any(|list_item| {
1080                    matches!(
1081                        list_item.ident().map(|i| i.name),
1082                        Some(sym::delayed_bug_from_inside_query)
1083                    )
1084                }) =>
1085            {
1086                tcx.ensure_ok().trigger_delayed_bug(def_id);
1087            }
1088
1089            // Bare `#[rustc_error]`.
1090            None => {
1091                tcx.dcx().emit_fatal(errors::RustcErrorFatal { span: tcx.def_span(def_id) });
1092            }
1093
1094            // Some other attribute.
1095            Some(_) => {
1096                tcx.dcx().emit_warn(errors::RustcErrorUnexpectedAnnotation {
1097                    span: tcx.def_span(def_id),
1098                });
1099            }
1100        }
1101    }
1102}
1103
1104/// Runs the codegen backend, after which the AST and analysis can
1105/// be discarded.
1106pub(crate) fn start_codegen<'tcx>(
1107    codegen_backend: &dyn CodegenBackend,
1108    tcx: TyCtxt<'tcx>,
1109) -> Box<dyn Any> {
1110    // Hook for UI tests.
1111    check_for_rustc_errors_attr(tcx);
1112
1113    // Don't run this test assertions when not doing codegen. Compiletest tries to build
1114    // build-fail tests in check mode first and expects it to not give an error in that case.
1115    if tcx.sess.opts.output_types.should_codegen() {
1116        rustc_symbol_mangling::test::report_symbol_names(tcx);
1117    }
1118
1119    // Don't do code generation if there were any errors. Likewise if
1120    // there were any delayed bugs, because codegen will likely cause
1121    // more ICEs, obscuring the original problem.
1122    if let Some(guar) = tcx.sess.dcx().has_errors_or_delayed_bugs() {
1123        guar.raise_fatal();
1124    }
1125
1126    info!("Pre-codegen\n{:?}", tcx.debug_stats());
1127
1128    let (metadata, need_metadata_module) = rustc_metadata::fs::encode_and_write_metadata(tcx);
1129
1130    let codegen = tcx.sess.time("codegen_crate", move || {
1131        codegen_backend.codegen_crate(tcx, metadata, need_metadata_module)
1132    });
1133
1134    info!("Post-codegen\n{:?}", tcx.debug_stats());
1135
1136    // This must run after monomorphization so that all generic types
1137    // have been instantiated.
1138    if tcx.sess.opts.unstable_opts.print_type_sizes {
1139        tcx.sess.code_stats.print_type_sizes();
1140    }
1141
1142    codegen
1143}
1144
1145/// Compute and validate the crate name.
1146pub fn get_crate_name(sess: &Session, krate_attrs: &[ast::Attribute]) -> Symbol {
1147    // We validate *all* occurrences of `#![crate_name]`, pick the first find and
1148    // if a crate name was passed on the command line via `--crate-name` we enforce
1149    // that they match.
1150    // We perform the validation step here instead of later to ensure it gets run
1151    // in all code paths that require the crate name very early on, namely before
1152    // macro expansion.
1153
1154    let attr_crate_name =
1155        validate_and_find_value_str_builtin_attr(sym::crate_name, sess, krate_attrs);
1156
1157    let validate = |name, span| {
1158        rustc_session::output::validate_crate_name(sess, name, span);
1159        name
1160    };
1161
1162    if let Some(crate_name) = &sess.opts.crate_name {
1163        let crate_name = Symbol::intern(crate_name);
1164        if let Some((attr_crate_name, span)) = attr_crate_name
1165            && attr_crate_name != crate_name
1166        {
1167            sess.dcx().emit_err(errors::CrateNameDoesNotMatch {
1168                span,
1169                crate_name,
1170                attr_crate_name,
1171            });
1172        }
1173        return validate(crate_name, None);
1174    }
1175
1176    if let Some((crate_name, span)) = attr_crate_name {
1177        return validate(crate_name, Some(span));
1178    }
1179
1180    if let Input::File(ref path) = sess.io.input
1181        && let Some(file_stem) = path.file_stem().and_then(|s| s.to_str())
1182    {
1183        if file_stem.starts_with('-') {
1184            sess.dcx().emit_err(errors::CrateNameInvalid { crate_name: file_stem });
1185        } else {
1186            return validate(Symbol::intern(&file_stem.replace('-', "_")), None);
1187        }
1188    }
1189
1190    sym::rust_out
1191}
1192
1193fn get_recursion_limit(krate_attrs: &[ast::Attribute], sess: &Session) -> Limit {
1194    // We don't permit macro calls inside of the attribute (e.g., #![recursion_limit = `expand!()`])
1195    // because that would require expanding this while in the middle of expansion, which needs to
1196    // know the limit before expanding.
1197    let _ = validate_and_find_value_str_builtin_attr(sym::recursion_limit, sess, krate_attrs);
1198    crate::limits::get_recursion_limit(krate_attrs, sess)
1199}
1200
1201/// Validate *all* occurrences of the given "[value-str]" built-in attribute and return the first find.
1202///
1203/// This validator is intended for built-in attributes whose value needs to be known very early
1204/// during compilation (namely, before macro expansion) and it mainly exists to reject macro calls
1205/// inside of the attributes, such as in `#![name = expand!()]`. Normal attribute validation happens
1206/// during semantic analysis via [`TyCtxt::check_mod_attrs`] which happens *after* macro expansion
1207/// when such macro calls (here: `expand`) have already been expanded and we can no longer check for
1208/// their presence.
1209///
1210/// [value-str]: ast::Attribute::value_str
1211fn validate_and_find_value_str_builtin_attr(
1212    name: Symbol,
1213    sess: &Session,
1214    krate_attrs: &[ast::Attribute],
1215) -> Option<(Symbol, Span)> {
1216    let mut result = None;
1217    // Validate *all* relevant attributes, not just the first occurrence.
1218    for attr in ast::attr::filter_by_name(krate_attrs, name) {
1219        let Some(value) = attr.value_str() else {
1220            validate_attr::emit_fatal_malformed_builtin_attribute(&sess.psess, attr, name)
1221        };
1222        // Choose the first occurrence as our result.
1223        result.get_or_insert((value, attr.span));
1224    }
1225    result
1226}