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