1use std::any::Any;
2use std::ffi::OsString;
3use std::io::{self, BufWriter, Write};
4use std::path::{Path, PathBuf};
5use std::sync::{Arc, LazyLock};
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, OnceLock, 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::ty::{self, CurrentGcx, GlobalCtxt, RegisteredTools, TyCtxt};
23use rustc_middle::util::Providers;
24use rustc_parse::{
25 new_parser_from_file, new_parser_from_source_str, unwrap_or_emit_fatal, validate_attr,
26};
27use rustc_passes::{abi_test, input_stats, layout_test};
28use rustc_resolve::Resolver;
29use rustc_session::config::{CrateType, Input, OutFileName, OutputFilenames, OutputType};
30use rustc_session::cstore::Untracked;
31use rustc_session::output::{collect_crate_types, filename_for_input, find_crate_name};
32use rustc_session::search_paths::PathKind;
33use rustc_session::{Limit, Session};
34use rustc_span::{ErrorGuaranteed, FileName, SourceFileHash, SourceFileHashAlgorithm, Symbol, sym};
35use rustc_target::spec::PanicStrategy;
36use rustc_trait_selection::traits;
37use tracing::{info, instrument};
38
39use crate::interface::Compiler;
40use crate::{errors, proc_macro_decls, util};
41
42pub fn parse<'a>(sess: &'a Session) -> ast::Crate {
43 let krate = sess
44 .time("parse_crate", || {
45 let mut parser = unwrap_or_emit_fatal(match &sess.io.input {
46 Input::File(file) => new_parser_from_file(&sess.psess, file, None),
47 Input::Str { input, name } => {
48 new_parser_from_source_str(&sess.psess, name.clone(), input.clone())
49 }
50 });
51 parser.parse_crate_mod()
52 })
53 .unwrap_or_else(|parse_error| {
54 let guar: ErrorGuaranteed = parse_error.emit();
55 guar.raise_fatal();
56 });
57
58 if sess.opts.unstable_opts.input_stats {
59 input_stats::print_ast_stats(&krate, "PRE EXPANSION AST STATS", "ast-stats-1");
60 }
61
62 krate
63}
64
65fn pre_expansion_lint<'a>(
66 sess: &Session,
67 features: &Features,
68 lint_store: &LintStore,
69 registered_tools: &RegisteredTools,
70 check_node: impl EarlyCheckNode<'a>,
71 node_name: Symbol,
72) {
73 sess.prof.generic_activity_with_arg("pre_AST_expansion_lint_checks", node_name.as_str()).run(
74 || {
75 rustc_lint::check_ast_node(
76 sess,
77 None,
78 features,
79 true,
80 lint_store,
81 registered_tools,
82 None,
83 rustc_lint::BuiltinCombinedPreExpansionLintPass::new(),
84 check_node,
85 );
86 },
87 );
88}
89
90struct LintStoreExpandImpl<'a>(&'a LintStore);
92
93impl LintStoreExpand for LintStoreExpandImpl<'_> {
94 fn pre_expansion_lint(
95 &self,
96 sess: &Session,
97 features: &Features,
98 registered_tools: &RegisteredTools,
99 node_id: ast::NodeId,
100 attrs: &[ast::Attribute],
101 items: &[rustc_ast::ptr::P<ast::Item>],
102 name: Symbol,
103 ) {
104 pre_expansion_lint(sess, features, self.0, registered_tools, (node_id, attrs, items), name);
105 }
106}
107
108#[instrument(level = "trace", skip(krate, resolver))]
113fn configure_and_expand(
114 mut krate: ast::Crate,
115 pre_configured_attrs: &[ast::Attribute],
116 resolver: &mut Resolver<'_, '_>,
117) -> ast::Crate {
118 let tcx = resolver.tcx();
119 let sess = tcx.sess;
120 let features = tcx.features();
121 let lint_store = unerased_lint_store(tcx.sess);
122 let crate_name = tcx.crate_name(LOCAL_CRATE);
123 let lint_check_node = (&krate, pre_configured_attrs);
124 pre_expansion_lint(
125 sess,
126 features,
127 lint_store,
128 tcx.registered_tools(()),
129 lint_check_node,
130 crate_name,
131 );
132 rustc_builtin_macros::register_builtin_macros(resolver);
133
134 let num_standard_library_imports = sess.time("crate_injection", || {
135 rustc_builtin_macros::standard_library_imports::inject(
136 &mut krate,
137 pre_configured_attrs,
138 resolver,
139 sess,
140 features,
141 )
142 });
143
144 util::check_attr_crate_type(sess, pre_configured_attrs, resolver.lint_buffer());
145
146 krate = sess.time("macro_expand_crate", || {
148 let mut old_path = OsString::new();
162 if cfg!(windows) {
163 old_path = env::var_os("PATH").unwrap_or(old_path);
164 let mut new_path = Vec::from_iter(
165 sess.host_filesearch().search_paths(PathKind::All).map(|p| p.dir.clone()),
166 );
167 for path in env::split_paths(&old_path) {
168 if !new_path.contains(&path) {
169 new_path.push(path);
170 }
171 }
172 env::set_var(
173 "PATH",
174 &env::join_paths(
175 new_path.iter().filter(|p| env::join_paths(iter::once(p)).is_ok()),
176 )
177 .unwrap(),
178 );
179 }
180
181 let recursion_limit = get_recursion_limit(pre_configured_attrs, sess);
183 let cfg = rustc_expand::expand::ExpansionConfig {
184 crate_name: crate_name.to_string(),
185 features,
186 recursion_limit,
187 trace_mac: sess.opts.unstable_opts.trace_macros,
188 should_test: sess.is_test_crate(),
189 span_debug: sess.opts.unstable_opts.span_debug,
190 proc_macro_backtrace: sess.opts.unstable_opts.proc_macro_backtrace,
191 };
192
193 let lint_store = LintStoreExpandImpl(lint_store);
194 let mut ecx = ExtCtxt::new(sess, cfg, resolver, Some(&lint_store));
195 ecx.num_standard_library_imports = num_standard_library_imports;
196 let krate = sess.time("expand_crate", || ecx.monotonic_expander().expand_crate(krate));
198
199 sess.psess.buffered_lints.with_lock(|buffered_lints: &mut Vec<BufferedEarlyLint>| {
202 buffered_lints.append(&mut ecx.buffered_early_lint);
203 });
204
205 sess.time("check_unused_macros", || {
206 ecx.check_unused_macros();
207 });
208
209 if ecx.reduced_recursion_limit.is_some() {
212 sess.dcx().abort_if_errors();
213 unreachable!();
214 }
215
216 if cfg!(windows) {
217 env::set_var("PATH", &old_path);
218 }
219
220 krate
221 });
222
223 sess.time("maybe_building_test_harness", || {
224 rustc_builtin_macros::test_harness::inject(&mut krate, sess, features, resolver)
225 });
226
227 let has_proc_macro_decls = sess.time("AST_validation", || {
228 rustc_ast_passes::ast_validation::check_crate(
229 sess,
230 features,
231 &krate,
232 resolver.lint_buffer(),
233 )
234 });
235
236 let crate_types = tcx.crate_types();
237 let is_executable_crate = crate_types.contains(&CrateType::Executable);
238 let is_proc_macro_crate = crate_types.contains(&CrateType::ProcMacro);
239
240 if crate_types.len() > 1 {
241 if is_executable_crate {
242 sess.dcx().emit_err(errors::MixedBinCrate);
243 }
244 if is_proc_macro_crate {
245 sess.dcx().emit_err(errors::MixedProcMacroCrate);
246 }
247 }
248
249 if is_proc_macro_crate && sess.panic_strategy() == PanicStrategy::Abort {
250 sess.dcx().emit_warn(errors::ProcMacroCratePanicAbort);
251 }
252
253 sess.time("maybe_create_a_macro_crate", || {
254 let is_test_crate = sess.is_test_crate();
255 rustc_builtin_macros::proc_macro_harness::inject(
256 &mut krate,
257 sess,
258 features,
259 resolver,
260 is_proc_macro_crate,
261 has_proc_macro_decls,
262 is_test_crate,
263 sess.dcx(),
264 )
265 });
266
267 resolver.resolve_crate(&krate);
270
271 CStore::from_tcx(tcx).report_incompatible_target_modifiers(tcx, &krate);
272 krate
273}
274
275fn early_lint_checks(tcx: TyCtxt<'_>, (): ()) {
276 let sess = tcx.sess;
277 let (resolver, krate) = &*tcx.resolver_for_lowering().borrow();
278 let mut lint_buffer = resolver.lint_buffer.steal();
279
280 if sess.opts.unstable_opts.input_stats {
281 input_stats::print_ast_stats(krate, "POST EXPANSION AST STATS", "ast-stats-2");
282 }
283
284 sess.time("complete_gated_feature_checking", || {
286 rustc_ast_passes::feature_gate::check_crate(krate, sess, tcx.features());
287 });
288
289 sess.psess.buffered_lints.with_lock(|buffered_lints| {
291 info!("{} parse sess buffered_lints", buffered_lints.len());
292 for early_lint in buffered_lints.drain(..) {
293 lint_buffer.add_early_lint(early_lint);
294 }
295 });
296
297 sess.psess.bad_unicode_identifiers.with_lock(|identifiers| {
299 for (ident, mut spans) in identifiers.drain(..) {
300 spans.sort();
301 if ident == sym::ferris {
302 let first_span = spans[0];
303 sess.dcx().emit_err(errors::FerrisIdentifier { spans, first_span });
304 } else {
305 sess.dcx().emit_err(errors::EmojiIdentifier { spans, ident });
306 }
307 }
308 });
309
310 let lint_store = unerased_lint_store(tcx.sess);
311 rustc_lint::check_ast_node(
312 sess,
313 Some(tcx),
314 tcx.features(),
315 false,
316 lint_store,
317 tcx.registered_tools(()),
318 Some(lint_buffer),
319 rustc_lint::BuiltinCombinedEarlyLintPass::new(),
320 (&**krate, &*krate.attrs),
321 )
322}
323
324fn generated_output_paths(
326 tcx: TyCtxt<'_>,
327 outputs: &OutputFilenames,
328 exact_name: bool,
329 crate_name: Symbol,
330) -> Vec<PathBuf> {
331 let sess = tcx.sess;
332 let mut out_filenames = Vec::new();
333 for output_type in sess.opts.output_types.keys() {
334 let out_filename = outputs.path(*output_type);
335 let file = out_filename.as_path().to_path_buf();
336 match *output_type {
337 OutputType::Exe if !exact_name => {
340 for crate_type in tcx.crate_types().iter() {
341 let p = filename_for_input(sess, *crate_type, crate_name, outputs);
342 out_filenames.push(p.as_path().to_path_buf());
343 }
344 }
345 OutputType::DepInfo if sess.opts.unstable_opts.dep_info_omit_d_target => {
346 }
348 OutputType::DepInfo if out_filename.is_stdout() => {
349 }
351 _ => {
352 out_filenames.push(file);
353 }
354 }
355 }
356 out_filenames
357}
358
359fn output_contains_path(output_paths: &[PathBuf], input_path: &Path) -> bool {
360 let input_path = try_canonicalize(input_path).ok();
361 if input_path.is_none() {
362 return false;
363 }
364 output_paths.iter().any(|output_path| try_canonicalize(output_path).ok() == input_path)
365}
366
367fn output_conflicts_with_dir(output_paths: &[PathBuf]) -> Option<&PathBuf> {
368 output_paths.iter().find(|output_path| output_path.is_dir())
369}
370
371fn escape_dep_filename(filename: &str) -> String {
372 filename.replace(' ', "\\ ")
375}
376
377fn escape_dep_env(symbol: Symbol) -> String {
380 let s = symbol.as_str();
381 let mut escaped = String::with_capacity(s.len());
382 for c in s.chars() {
383 match c {
384 '\n' => escaped.push_str(r"\n"),
385 '\r' => escaped.push_str(r"\r"),
386 '\\' => escaped.push_str(r"\\"),
387 _ => escaped.push(c),
388 }
389 }
390 escaped
391}
392
393fn write_out_deps(tcx: TyCtxt<'_>, outputs: &OutputFilenames, out_filenames: &[PathBuf]) {
394 let sess = tcx.sess;
396 if !sess.opts.output_types.contains_key(&OutputType::DepInfo) {
397 return;
398 }
399 let deps_output = outputs.path(OutputType::DepInfo);
400 let deps_filename = deps_output.as_path();
401
402 let result: io::Result<()> = try {
403 let mut files: Vec<(String, u64, Option<SourceFileHash>)> = sess
406 .source_map()
407 .files()
408 .iter()
409 .filter(|fmap| fmap.is_real_file())
410 .filter(|fmap| !fmap.is_imported())
411 .map(|fmap| {
412 (
413 escape_dep_filename(&fmap.name.prefer_local().to_string()),
414 fmap.source_len.0 as u64,
415 fmap.checksum_hash,
416 )
417 })
418 .collect();
419
420 let checksum_hash_algo = sess.opts.unstable_opts.checksum_hash_algorithm;
421
422 let file_depinfo = sess.psess.file_depinfo.borrow();
425
426 let normalize_path = |path: PathBuf| {
427 let file = FileName::from(path);
428 escape_dep_filename(&file.prefer_local().to_string())
429 };
430
431 fn hash_iter_files<P: AsRef<Path>>(
434 it: impl Iterator<Item = P>,
435 checksum_hash_algo: Option<SourceFileHashAlgorithm>,
436 ) -> impl Iterator<Item = (P, u64, Option<SourceFileHash>)> {
437 it.map(move |path| {
438 match checksum_hash_algo.and_then(|algo| {
439 fs::File::open(path.as_ref())
440 .and_then(|mut file| {
441 SourceFileHash::new(algo, &mut file).map(|h| (file, h))
442 })
443 .and_then(|(file, h)| file.metadata().map(|m| (m.len(), h)))
444 .map_err(|e| {
445 tracing::error!(
446 "failed to compute checksum, omitting it from dep-info {} {e}",
447 path.as_ref().display()
448 )
449 })
450 .ok()
451 }) {
452 Some((file_len, checksum)) => (path, file_len, Some(checksum)),
453 None => (path, 0, None),
454 }
455 })
456 }
457
458 let extra_tracked_files = hash_iter_files(
459 file_depinfo.iter().map(|path_sym| normalize_path(PathBuf::from(path_sym.as_str()))),
460 checksum_hash_algo,
461 );
462 files.extend(extra_tracked_files);
463
464 if let Some(ref profile_instr) = sess.opts.cg.profile_use {
466 files.extend(hash_iter_files(
467 iter::once(normalize_path(profile_instr.as_path().to_path_buf())),
468 checksum_hash_algo,
469 ));
470 }
471 if let Some(ref profile_sample) = sess.opts.unstable_opts.profile_sample_use {
472 files.extend(hash_iter_files(
473 iter::once(normalize_path(profile_sample.as_path().to_path_buf())),
474 checksum_hash_algo,
475 ));
476 }
477
478 for debugger_visualizer in tcx.debugger_visualizers(LOCAL_CRATE) {
480 files.extend(hash_iter_files(
481 iter::once(normalize_path(debugger_visualizer.path.clone().unwrap())),
482 checksum_hash_algo,
483 ));
484 }
485
486 if sess.binary_dep_depinfo() {
487 if let Some(ref backend) = sess.opts.unstable_opts.codegen_backend {
488 if backend.contains('.') {
489 files.extend(hash_iter_files(
492 iter::once(backend.to_string()),
493 checksum_hash_algo,
494 ));
495 }
496 }
497
498 for &cnum in tcx.crates(()) {
499 let source = tcx.used_crate_source(cnum);
500 if let Some((path, _)) = &source.dylib {
501 files.extend(hash_iter_files(
502 iter::once(escape_dep_filename(&path.display().to_string())),
503 checksum_hash_algo,
504 ));
505 }
506 if let Some((path, _)) = &source.rlib {
507 files.extend(hash_iter_files(
508 iter::once(escape_dep_filename(&path.display().to_string())),
509 checksum_hash_algo,
510 ));
511 }
512 if let Some((path, _)) = &source.rmeta {
513 files.extend(hash_iter_files(
514 iter::once(escape_dep_filename(&path.display().to_string())),
515 checksum_hash_algo,
516 ));
517 }
518 }
519 }
520
521 let write_deps_to_file = |file: &mut dyn Write| -> io::Result<()> {
522 for path in out_filenames {
523 writeln!(
524 file,
525 "{}: {}\n",
526 path.display(),
527 files
528 .iter()
529 .map(|(path, _file_len, _checksum_hash_algo)| path.as_str())
530 .intersperse(" ")
531 .collect::<String>()
532 )?;
533 }
534
535 for (path, _file_len, _checksum_hash_algo) in &files {
539 writeln!(file, "{path}:")?;
540 }
541
542 let env_depinfo = sess.psess.env_depinfo.borrow();
544 if !env_depinfo.is_empty() {
545 #[allow(rustc::potential_query_instability)]
547 let mut envs: Vec<_> = env_depinfo
548 .iter()
549 .map(|(k, v)| (escape_dep_env(*k), v.map(escape_dep_env)))
550 .collect();
551 envs.sort_unstable();
552 writeln!(file)?;
553 for (k, v) in envs {
554 write!(file, "# env-dep:{k}")?;
555 if let Some(v) = v {
556 write!(file, "={v}")?;
557 }
558 writeln!(file)?;
559 }
560 }
561
562 if sess.opts.unstable_opts.checksum_hash_algorithm().is_some() {
565 files
566 .iter()
567 .filter_map(|(path, file_len, hash_algo)| {
568 hash_algo.map(|hash_algo| (path, file_len, hash_algo))
569 })
570 .try_for_each(|(path, file_len, checksum_hash)| {
571 writeln!(file, "# checksum:{checksum_hash} file_len:{file_len} {path}")
572 })?;
573 }
574
575 Ok(())
576 };
577
578 match deps_output {
579 OutFileName::Stdout => {
580 let mut file = BufWriter::new(io::stdout());
581 write_deps_to_file(&mut file)?;
582 }
583 OutFileName::Real(ref path) => {
584 let mut file = fs::File::create_buffered(path)?;
585 write_deps_to_file(&mut file)?;
586 }
587 }
588 };
589
590 match result {
591 Ok(_) => {
592 if sess.opts.json_artifact_notifications {
593 sess.dcx().emit_artifact_notification(deps_filename, "dep-info");
594 }
595 }
596 Err(error) => {
597 sess.dcx().emit_fatal(errors::ErrorWritingDependencies { path: deps_filename, error });
598 }
599 }
600}
601
602fn resolver_for_lowering_raw<'tcx>(
603 tcx: TyCtxt<'tcx>,
604 (): (),
605) -> (&'tcx Steal<(ty::ResolverAstLowering, Arc<ast::Crate>)>, &'tcx ty::ResolverGlobalCtxt) {
606 let arenas = Resolver::arenas();
607 let _ = tcx.registered_tools(()); let (krate, pre_configured_attrs) = tcx.crate_for_resolver(()).steal();
609 let mut resolver = Resolver::new(
610 tcx,
611 &pre_configured_attrs,
612 krate.spans.inner_span,
613 krate.spans.inject_use_span,
614 &arenas,
615 );
616 let krate = configure_and_expand(krate, &pre_configured_attrs, &mut resolver);
617
618 tcx.untracked().cstore.freeze();
620
621 let ty::ResolverOutputs {
622 global_ctxt: untracked_resolutions,
623 ast_lowering: untracked_resolver_for_lowering,
624 } = resolver.into_outputs();
625
626 let resolutions = tcx.arena.alloc(untracked_resolutions);
627 (tcx.arena.alloc(Steal::new((untracked_resolver_for_lowering, Arc::new(krate)))), resolutions)
628}
629
630pub fn write_dep_info(tcx: TyCtxt<'_>) {
631 let _ = tcx.resolver_for_lowering();
635
636 let sess = tcx.sess;
637 let _timer = sess.timer("write_dep_info");
638 let crate_name = tcx.crate_name(LOCAL_CRATE);
639
640 let outputs = tcx.output_filenames(());
641 let output_paths =
642 generated_output_paths(tcx, &outputs, sess.io.output_file.is_some(), crate_name);
643
644 if let Some(input_path) = sess.io.input.opt_path() {
646 if sess.opts.will_create_output_file() {
647 if output_contains_path(&output_paths, input_path) {
648 sess.dcx().emit_fatal(errors::InputFileWouldBeOverWritten { path: input_path });
649 }
650 if let Some(dir_path) = output_conflicts_with_dir(&output_paths) {
651 sess.dcx().emit_fatal(errors::GeneratedFileConflictsWithDirectory {
652 input_path,
653 dir_path,
654 });
655 }
656 }
657 }
658
659 if let Some(ref dir) = sess.io.temps_dir {
660 if fs::create_dir_all(dir).is_err() {
661 sess.dcx().emit_fatal(errors::TempsDirError);
662 }
663 }
664
665 write_out_deps(tcx, &outputs, &output_paths);
666
667 let only_dep_info = sess.opts.output_types.contains_key(&OutputType::DepInfo)
668 && sess.opts.output_types.len() == 1;
669
670 if !only_dep_info {
671 if let Some(ref dir) = sess.io.output_dir {
672 if fs::create_dir_all(dir).is_err() {
673 sess.dcx().emit_fatal(errors::OutDirError);
674 }
675 }
676 }
677}
678
679pub static DEFAULT_QUERY_PROVIDERS: LazyLock<Providers> = LazyLock::new(|| {
680 let providers = &mut Providers::default();
681 providers.analysis = analysis;
682 providers.hir_crate = rustc_ast_lowering::lower_to_hir;
683 providers.resolver_for_lowering_raw = resolver_for_lowering_raw;
684 providers.stripped_cfg_items =
685 |tcx, _| tcx.arena.alloc_from_iter(tcx.resolutions(()).stripped_cfg_items.steal());
686 providers.resolutions = |tcx, ()| tcx.resolver_for_lowering_raw(()).1;
687 providers.early_lint_checks = early_lint_checks;
688 proc_macro_decls::provide(providers);
689 rustc_const_eval::provide(providers);
690 rustc_middle::hir::provide(providers);
691 rustc_borrowck::provide(providers);
692 rustc_incremental::provide(providers);
693 rustc_mir_build::provide(providers);
694 rustc_mir_transform::provide(providers);
695 rustc_monomorphize::provide(providers);
696 rustc_privacy::provide(providers);
697 rustc_query_impl::provide(providers);
698 rustc_resolve::provide(providers);
699 rustc_hir_analysis::provide(providers);
700 rustc_hir_typeck::provide(providers);
701 ty::provide(providers);
702 traits::provide(providers);
703 rustc_passes::provide(providers);
704 rustc_traits::provide(providers);
705 rustc_ty_utils::provide(providers);
706 rustc_metadata::provide(providers);
707 rustc_lint::provide(providers);
708 rustc_symbol_mangling::provide(providers);
709 rustc_codegen_ssa::provide(providers);
710 *providers
711});
712
713pub fn create_and_enter_global_ctxt<T, F: for<'tcx> FnOnce(TyCtxt<'tcx>) -> T>(
714 compiler: &Compiler,
715 mut krate: rustc_ast::Crate,
716 f: F,
717) -> T {
718 let sess = &compiler.sess;
719
720 rustc_builtin_macros::cmdline_attrs::inject(
721 &mut krate,
722 &sess.psess,
723 &sess.opts.unstable_opts.crate_attr,
724 );
725
726 let pre_configured_attrs = rustc_expand::config::pre_configure_attrs(sess, &krate.attrs);
727
728 let crate_name = find_crate_name(sess, &pre_configured_attrs);
730 let crate_types = collect_crate_types(sess, &pre_configured_attrs);
731 let stable_crate_id = StableCrateId::new(
732 crate_name,
733 crate_types.contains(&CrateType::Executable),
734 sess.opts.cg.metadata.clone(),
735 sess.cfg_version,
736 );
737 let outputs = util::build_output_filenames(&pre_configured_attrs, sess);
738 let dep_graph = setup_dep_graph(sess);
739
740 let cstore =
741 FreezeLock::new(Box::new(CStore::new(compiler.codegen_backend.metadata_loader())) as _);
742 let definitions = FreezeLock::new(Definitions::new(stable_crate_id));
743
744 let stable_crate_ids = FreezeLock::new(StableCrateIdMap::default());
745 let untracked =
746 Untracked { cstore, source_span: AppendOnlyIndexVec::new(), definitions, stable_crate_ids };
747
748 dep_graph.assert_ignored();
752
753 let query_result_on_disk_cache = rustc_incremental::load_query_result_cache(sess);
754
755 let codegen_backend = &compiler.codegen_backend;
756 let mut providers = *DEFAULT_QUERY_PROVIDERS;
757 codegen_backend.provide(&mut providers);
758
759 if let Some(callback) = compiler.override_queries {
760 callback(sess, &mut providers);
761 }
762
763 let incremental = dep_graph.is_fully_enabled();
764
765 let gcx_cell = OnceLock::new();
766 let arena = WorkerLocal::new(|_| Arena::default());
767 let hir_arena = WorkerLocal::new(|_| rustc_hir::Arena::default());
768
769 let inner: Box<
772 dyn for<'tcx> FnOnce(
773 &'tcx Session,
774 CurrentGcx,
775 &'tcx OnceLock<GlobalCtxt<'tcx>>,
776 &'tcx WorkerLocal<Arena<'tcx>>,
777 &'tcx WorkerLocal<rustc_hir::Arena<'tcx>>,
778 F,
779 ) -> T,
780 > = Box::new(move |sess, current_gcx, gcx_cell, arena, hir_arena, f| {
781 TyCtxt::create_global_ctxt(
782 gcx_cell,
783 sess,
784 crate_types,
785 stable_crate_id,
786 arena,
787 hir_arena,
788 untracked,
789 dep_graph,
790 rustc_query_impl::query_callbacks(arena),
791 rustc_query_impl::query_system(
792 providers.queries,
793 providers.extern_queries,
794 query_result_on_disk_cache,
795 incremental,
796 ),
797 providers.hooks,
798 current_gcx,
799 |tcx| {
800 let feed = tcx.create_crate_num(stable_crate_id).unwrap();
801 assert_eq!(feed.key(), LOCAL_CRATE);
802 feed.crate_name(crate_name);
803
804 let feed = tcx.feed_unit_query();
805 feed.features_query(tcx.arena.alloc(rustc_expand::config::features(
806 tcx.sess,
807 &pre_configured_attrs,
808 crate_name,
809 )));
810 feed.crate_for_resolver(tcx.arena.alloc(Steal::new((krate, pre_configured_attrs))));
811 feed.output_filenames(Arc::new(outputs));
812
813 let res = f(tcx);
814 tcx.finish();
816 res
817 },
818 )
819 });
820
821 inner(&compiler.sess, compiler.current_gcx.clone(), &gcx_cell, &arena, &hir_arena, f)
822}
823
824fn run_required_analyses(tcx: TyCtxt<'_>) {
827 if tcx.sess.opts.unstable_opts.input_stats {
828 rustc_passes::input_stats::print_hir_stats(tcx);
829 }
830 #[cfg(all(not(doc), debug_assertions))]
833 rustc_passes::hir_id_validator::check_crate(tcx);
834 let sess = tcx.sess;
835 sess.time("misc_checking_1", || {
836 parallel!(
837 {
838 sess.time("looking_for_entry_point", || tcx.ensure_ok().entry_fn(()));
839
840 sess.time("looking_for_derive_registrar", || {
841 tcx.ensure_ok().proc_macro_decls_static(())
842 });
843
844 CStore::from_tcx(tcx).report_unused_deps(tcx);
845 },
846 {
847 tcx.hir().par_for_each_module(|module| {
848 tcx.ensure_ok().check_mod_loops(module);
849 tcx.ensure_ok().check_mod_attrs(module);
850 tcx.ensure_ok().check_mod_naked_functions(module);
851 tcx.ensure_ok().check_mod_unstable_api_usage(module);
852 });
853 },
854 {
855 sess.time("unused_lib_feature_checking", || {
856 rustc_passes::stability::check_unused_or_stable_features(tcx)
857 });
858 },
859 {
860 tcx.ensure_ok().limits(());
865 tcx.ensure_ok().stability_index(());
866 }
867 );
868 });
869
870 rustc_hir_analysis::check_crate(tcx);
871 sess.time("MIR_coroutine_by_move_body", || {
872 tcx.hir().par_body_owners(|def_id| {
873 if tcx.needs_coroutine_by_move_body_def_id(def_id.to_def_id()) {
874 tcx.ensure_done().coroutine_by_move_body_def_id(def_id);
875 }
876 });
877 });
878 tcx.untracked().definitions.freeze();
884
885 sess.time("MIR_borrow_checking", || {
886 tcx.hir().par_body_owners(|def_id| {
887 tcx.ensure_ok().check_unsafety(def_id);
890 tcx.ensure_ok().mir_borrowck(def_id)
891 });
892 });
893 sess.time("MIR_effect_checking", || {
894 tcx.hir().par_body_owners(|def_id| {
895 tcx.ensure_ok().has_ffi_unwind_calls(def_id);
896
897 if tcx.sess.opts.output_types.should_codegen()
901 || tcx.hir().body_const_context(def_id).is_some()
902 {
903 tcx.ensure_ok().mir_drops_elaborated_and_const_checked(def_id);
904 }
905 });
906 });
907 sess.time("coroutine_obligations", || {
908 tcx.hir().par_body_owners(|def_id| {
909 if tcx.is_coroutine(def_id.to_def_id()) {
910 tcx.ensure_ok().mir_coroutine_witnesses(def_id);
911 tcx.ensure_ok().check_coroutine_obligations(
912 tcx.typeck_root_def_id(def_id.to_def_id()).expect_local(),
913 );
914 tcx.ensure_ok().layout_of(
916 ty::TypingEnv::post_analysis(tcx, def_id.to_def_id())
917 .as_query_input(tcx.type_of(def_id).instantiate_identity()),
918 );
919 }
920 });
921 });
922
923 sess.time("layout_testing", || layout_test::test_layout(tcx));
924 sess.time("abi_testing", || abi_test::test_abi(tcx));
925
926 if tcx.sess.opts.unstable_opts.validate_mir {
931 sess.time("ensuring_final_MIR_is_computable", || {
932 tcx.hir().par_body_owners(|def_id| {
933 tcx.instance_mir(ty::InstanceKind::Item(def_id.into()));
934 });
935 });
936 }
937}
938
939fn analysis(tcx: TyCtxt<'_>, (): ()) {
942 run_required_analyses(tcx);
943
944 let sess = tcx.sess;
945
946 if let Some(guar) = sess.dcx().has_errors_excluding_lint_errors() {
955 guar.raise_fatal();
956 }
957
958 sess.time("misc_checking_3", || {
959 parallel!(
960 {
961 tcx.ensure_ok().effective_visibilities(());
962
963 parallel!(
964 {
965 tcx.ensure_ok().check_private_in_public(());
966 },
967 {
968 tcx.hir().par_for_each_module(|module| {
969 tcx.ensure_ok().check_mod_deathness(module)
970 });
971 },
972 {
973 sess.time("lint_checking", || {
974 rustc_lint::check_crate(tcx);
975 });
976 },
977 {
978 tcx.ensure_ok().clashing_extern_declarations(());
979 }
980 );
981 },
982 {
983 sess.time("privacy_checking_modules", || {
984 tcx.hir().par_for_each_module(|module| {
985 tcx.ensure_ok().check_mod_privacy(module);
986 });
987 });
988 }
989 );
990
991 sess.time("check_lint_expectations", || tcx.ensure_ok().check_expectations(None));
994
995 let _ = tcx.all_diagnostic_items(());
999 });
1000}
1001
1002fn check_for_rustc_errors_attr(tcx: TyCtxt<'_>) {
1006 let Some((def_id, _)) = tcx.entry_fn(()) else { return };
1007 for attr in tcx.get_attrs(def_id, sym::rustc_error) {
1008 match attr.meta_item_list() {
1009 Some(list)
1011 if list.iter().any(|list_item| {
1012 matches!(
1013 list_item.ident().map(|i| i.name),
1014 Some(sym::delayed_bug_from_inside_query)
1015 )
1016 }) =>
1017 {
1018 tcx.ensure_ok().trigger_delayed_bug(def_id);
1019 }
1020
1021 None => {
1023 tcx.dcx().emit_fatal(errors::RustcErrorFatal { span: tcx.def_span(def_id) });
1024 }
1025
1026 Some(_) => {
1028 tcx.dcx().emit_warn(errors::RustcErrorUnexpectedAnnotation {
1029 span: tcx.def_span(def_id),
1030 });
1031 }
1032 }
1033 }
1034}
1035
1036pub(crate) fn start_codegen<'tcx>(
1039 codegen_backend: &dyn CodegenBackend,
1040 tcx: TyCtxt<'tcx>,
1041) -> Box<dyn Any> {
1042 if let Some(guar) = tcx.sess.dcx().has_errors_or_delayed_bugs() {
1046 guar.raise_fatal();
1047 }
1048
1049 check_for_rustc_errors_attr(tcx);
1051
1052 info!("Pre-codegen\n{:?}", tcx.debug_stats());
1053
1054 let (metadata, need_metadata_module) = rustc_metadata::fs::encode_and_write_metadata(tcx);
1055
1056 let codegen = tcx.sess.time("codegen_crate", move || {
1057 codegen_backend.codegen_crate(tcx, metadata, need_metadata_module)
1058 });
1059
1060 if tcx.sess.opts.output_types.should_codegen() {
1063 rustc_symbol_mangling::test::report_symbol_names(tcx);
1064 }
1065
1066 info!("Post-codegen\n{:?}", tcx.debug_stats());
1067
1068 if tcx.sess.opts.output_types.contains_key(&OutputType::Mir) {
1069 if let Err(error) = rustc_mir_transform::dump_mir::emit_mir(tcx) {
1070 tcx.dcx().emit_fatal(errors::CantEmitMIR { error });
1071 }
1072 }
1073
1074 if tcx.sess.opts.unstable_opts.print_type_sizes {
1077 tcx.sess.code_stats.print_type_sizes();
1078 }
1079
1080 codegen
1081}
1082
1083fn get_recursion_limit(krate_attrs: &[ast::Attribute], sess: &Session) -> Limit {
1084 if let Some(attr) = krate_attrs
1085 .iter()
1086 .find(|attr| attr.has_name(sym::recursion_limit) && attr.value_str().is_none())
1087 {
1088 validate_attr::emit_fatal_malformed_builtin_attribute(
1096 &sess.psess,
1097 attr,
1098 sym::recursion_limit,
1099 );
1100 }
1101 rustc_middle::middle::limits::get_recursion_limit(krate_attrs, sess)
1102}