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 if sess.opts.unstable_opts.input_stats {
64 input_stats::print_ast_stats(&krate, "PRE EXPANSION AST STATS", "ast-stats-1");
65 }
66
67 rustc_builtin_macros::cmdline_attrs::inject(
68 &mut krate,
69 &sess.psess,
70 &sess.opts.unstable_opts.crate_attr,
71 );
72
73 krate
74}
75
76fn pre_expansion_lint<'a>(
77 sess: &Session,
78 features: &Features,
79 lint_store: &LintStore,
80 registered_tools: &RegisteredTools,
81 check_node: impl EarlyCheckNode<'a>,
82 node_name: Symbol,
83) {
84 sess.prof.generic_activity_with_arg("pre_AST_expansion_lint_checks", node_name.as_str()).run(
85 || {
86 rustc_lint::check_ast_node(
87 sess,
88 None,
89 features,
90 true,
91 lint_store,
92 registered_tools,
93 None,
94 rustc_lint::BuiltinCombinedPreExpansionLintPass::new(),
95 check_node,
96 );
97 },
98 );
99}
100
101struct LintStoreExpandImpl<'a>(&'a LintStore);
103
104impl LintStoreExpand for LintStoreExpandImpl<'_> {
105 fn pre_expansion_lint(
106 &self,
107 sess: &Session,
108 features: &Features,
109 registered_tools: &RegisteredTools,
110 node_id: ast::NodeId,
111 attrs: &[ast::Attribute],
112 items: &[rustc_ast::ptr::P<ast::Item>],
113 name: Symbol,
114 ) {
115 pre_expansion_lint(sess, features, self.0, registered_tools, (node_id, attrs, items), name);
116 }
117}
118
119#[instrument(level = "trace", skip(krate, resolver))]
124fn configure_and_expand(
125 mut krate: ast::Crate,
126 pre_configured_attrs: &[ast::Attribute],
127 resolver: &mut Resolver<'_, '_>,
128) -> ast::Crate {
129 let tcx = resolver.tcx();
130 let sess = tcx.sess;
131 let features = tcx.features();
132 let lint_store = unerased_lint_store(tcx.sess);
133 let crate_name = tcx.crate_name(LOCAL_CRATE);
134 let lint_check_node = (&krate, pre_configured_attrs);
135 pre_expansion_lint(
136 sess,
137 features,
138 lint_store,
139 tcx.registered_tools(()),
140 lint_check_node,
141 crate_name,
142 );
143 rustc_builtin_macros::register_builtin_macros(resolver);
144
145 let num_standard_library_imports = sess.time("crate_injection", || {
146 rustc_builtin_macros::standard_library_imports::inject(
147 &mut krate,
148 pre_configured_attrs,
149 resolver,
150 sess,
151 features,
152 )
153 });
154
155 util::check_attr_crate_type(sess, pre_configured_attrs, resolver.lint_buffer());
156
157 krate = sess.time("macro_expand_crate", || {
159 let mut old_path = OsString::new();
173 if cfg!(windows) {
174 old_path = env::var_os("PATH").unwrap_or(old_path);
175 let mut new_path = Vec::from_iter(
176 sess.host_filesearch().search_paths(PathKind::All).map(|p| p.dir.clone()),
177 );
178 for path in env::split_paths(&old_path) {
179 if !new_path.contains(&path) {
180 new_path.push(path);
181 }
182 }
183 unsafe {
184 env::set_var(
185 "PATH",
186 &env::join_paths(
187 new_path.iter().filter(|p| env::join_paths(iter::once(p)).is_ok()),
188 )
189 .unwrap(),
190 );
191 }
192 }
193
194 let recursion_limit = get_recursion_limit(pre_configured_attrs, sess);
196 let cfg = rustc_expand::expand::ExpansionConfig {
197 crate_name: crate_name.to_string(),
198 features,
199 recursion_limit,
200 trace_mac: sess.opts.unstable_opts.trace_macros,
201 should_test: sess.is_test_crate(),
202 span_debug: sess.opts.unstable_opts.span_debug,
203 proc_macro_backtrace: sess.opts.unstable_opts.proc_macro_backtrace,
204 };
205
206 let lint_store = LintStoreExpandImpl(lint_store);
207 let mut ecx = ExtCtxt::new(sess, cfg, resolver, Some(&lint_store));
208 ecx.num_standard_library_imports = num_standard_library_imports;
209 let krate = sess.time("expand_crate", || ecx.monotonic_expander().expand_crate(krate));
211
212 sess.psess.buffered_lints.with_lock(|buffered_lints: &mut Vec<BufferedEarlyLint>| {
215 buffered_lints.append(&mut ecx.buffered_early_lint);
216 });
217
218 sess.time("check_unused_macros", || {
219 ecx.check_unused_macros();
220 });
221
222 if ecx.reduced_recursion_limit.is_some() {
225 sess.dcx().abort_if_errors();
226 unreachable!();
227 }
228
229 if cfg!(windows) {
230 unsafe {
231 env::set_var("PATH", &old_path);
232 }
233 }
234
235 krate
236 });
237
238 sess.time("maybe_building_test_harness", || {
239 rustc_builtin_macros::test_harness::inject(&mut krate, sess, features, resolver)
240 });
241
242 let has_proc_macro_decls = sess.time("AST_validation", || {
243 rustc_ast_passes::ast_validation::check_crate(
244 sess,
245 features,
246 &krate,
247 tcx.is_sdylib_interface_build(),
248 resolver.lint_buffer(),
249 )
250 });
251
252 let crate_types = tcx.crate_types();
253 let is_executable_crate = crate_types.contains(&CrateType::Executable);
254 let is_proc_macro_crate = crate_types.contains(&CrateType::ProcMacro);
255
256 if crate_types.len() > 1 {
257 if is_executable_crate {
258 sess.dcx().emit_err(errors::MixedBinCrate);
259 }
260 if is_proc_macro_crate {
261 sess.dcx().emit_err(errors::MixedProcMacroCrate);
262 }
263 }
264 if crate_types.contains(&CrateType::Sdylib) && !tcx.features().export_stable() {
265 feature_err(sess, sym::export_stable, DUMMY_SP, "`sdylib` crate type is unstable").emit();
266 }
267
268 if is_proc_macro_crate && sess.panic_strategy() == PanicStrategy::Abort {
269 sess.dcx().emit_warn(errors::ProcMacroCratePanicAbort);
270 }
271
272 sess.time("maybe_create_a_macro_crate", || {
273 let is_test_crate = sess.is_test_crate();
274 rustc_builtin_macros::proc_macro_harness::inject(
275 &mut krate,
276 sess,
277 features,
278 resolver,
279 is_proc_macro_crate,
280 has_proc_macro_decls,
281 is_test_crate,
282 sess.dcx(),
283 )
284 });
285
286 resolver.resolve_crate(&krate);
289
290 CStore::from_tcx(tcx).report_incompatible_target_modifiers(tcx, &krate);
291 CStore::from_tcx(tcx).report_incompatible_async_drop_feature(tcx, &krate);
292 krate
293}
294
295fn early_lint_checks(tcx: TyCtxt<'_>, (): ()) {
296 let sess = tcx.sess;
297 let (resolver, krate) = &*tcx.resolver_for_lowering().borrow();
298 let mut lint_buffer = resolver.lint_buffer.steal();
299
300 if sess.opts.unstable_opts.input_stats {
301 input_stats::print_ast_stats(krate, "POST EXPANSION AST STATS", "ast-stats-2");
302 }
303
304 sess.time("complete_gated_feature_checking", || {
306 rustc_ast_passes::feature_gate::check_crate(krate, sess, tcx.features());
307 });
308
309 sess.psess.buffered_lints.with_lock(|buffered_lints| {
311 info!("{} parse sess buffered_lints", buffered_lints.len());
312 for early_lint in buffered_lints.drain(..) {
313 lint_buffer.add_early_lint(early_lint);
314 }
315 });
316
317 sess.psess.bad_unicode_identifiers.with_lock(|identifiers| {
319 for (ident, mut spans) in identifiers.drain(..) {
320 spans.sort();
321 if ident == sym::ferris {
322 enum FerrisFix {
323 SnakeCase,
324 ScreamingSnakeCase,
325 PascalCase,
326 }
327
328 impl FerrisFix {
329 const fn as_str(self) -> &'static str {
330 match self {
331 FerrisFix::SnakeCase => "ferris",
332 FerrisFix::ScreamingSnakeCase => "FERRIS",
333 FerrisFix::PascalCase => "Ferris",
334 }
335 }
336 }
337
338 let first_span = spans[0];
339 let prev_source = sess.psess.source_map().span_to_prev_source(first_span);
340 let ferris_fix = prev_source
341 .map_or(FerrisFix::SnakeCase, |source| {
342 let mut source_before_ferris = source.trim_end().split_whitespace().rev();
343 match source_before_ferris.next() {
344 Some("struct" | "trait" | "mod" | "union" | "type" | "enum") => {
345 FerrisFix::PascalCase
346 }
347 Some("const" | "static") => FerrisFix::ScreamingSnakeCase,
348 Some("mut") if source_before_ferris.next() == Some("static") => {
349 FerrisFix::ScreamingSnakeCase
350 }
351 _ => FerrisFix::SnakeCase,
352 }
353 })
354 .as_str();
355
356 sess.dcx().emit_err(errors::FerrisIdentifier { spans, first_span, ferris_fix });
357 } else {
358 sess.dcx().emit_err(errors::EmojiIdentifier { spans, ident });
359 }
360 }
361 });
362
363 let lint_store = unerased_lint_store(tcx.sess);
364 rustc_lint::check_ast_node(
365 sess,
366 Some(tcx),
367 tcx.features(),
368 false,
369 lint_store,
370 tcx.registered_tools(()),
371 Some(lint_buffer),
372 rustc_lint::BuiltinCombinedEarlyLintPass::new(),
373 (&**krate, &*krate.attrs),
374 )
375}
376
377fn env_var_os<'tcx>(tcx: TyCtxt<'tcx>, key: &'tcx OsStr) -> Option<&'tcx OsStr> {
378 let value = env::var_os(key);
379
380 let value_tcx = value.as_ref().map(|value| {
381 let encoded_bytes = tcx.arena.alloc_slice(value.as_encoded_bytes());
382 debug_assert_eq!(value.as_encoded_bytes(), encoded_bytes);
383 unsafe { OsStr::from_encoded_bytes_unchecked(encoded_bytes) }
387 });
388
389 tcx.sess.psess.env_depinfo.borrow_mut().insert((
395 Symbol::intern(&key.to_string_lossy()),
396 value.as_ref().and_then(|value| value.to_str()).map(|value| Symbol::intern(&value)),
397 ));
398
399 value_tcx
400}
401
402fn generated_output_paths(
404 tcx: TyCtxt<'_>,
405 outputs: &OutputFilenames,
406 exact_name: bool,
407 crate_name: Symbol,
408) -> Vec<PathBuf> {
409 let sess = tcx.sess;
410 let mut out_filenames = Vec::new();
411 for output_type in sess.opts.output_types.keys() {
412 let out_filename = outputs.path(*output_type);
413 let file = out_filename.as_path().to_path_buf();
414 match *output_type {
415 OutputType::Exe if !exact_name => {
418 for crate_type in tcx.crate_types().iter() {
419 let p = filename_for_input(sess, *crate_type, crate_name, outputs);
420 out_filenames.push(p.as_path().to_path_buf());
421 }
422 }
423 OutputType::DepInfo if sess.opts.unstable_opts.dep_info_omit_d_target => {
424 }
426 OutputType::DepInfo if out_filename.is_stdout() => {
427 }
429 _ => {
430 out_filenames.push(file);
431 }
432 }
433 }
434 out_filenames
435}
436
437fn output_contains_path(output_paths: &[PathBuf], input_path: &Path) -> bool {
438 let input_path = try_canonicalize(input_path).ok();
439 if input_path.is_none() {
440 return false;
441 }
442 output_paths.iter().any(|output_path| try_canonicalize(output_path).ok() == input_path)
443}
444
445fn output_conflicts_with_dir(output_paths: &[PathBuf]) -> Option<&PathBuf> {
446 output_paths.iter().find(|output_path| output_path.is_dir())
447}
448
449fn escape_dep_filename(filename: &str) -> String {
450 filename.replace(' ', "\\ ")
453}
454
455fn escape_dep_env(symbol: Symbol) -> String {
458 let s = symbol.as_str();
459 let mut escaped = String::with_capacity(s.len());
460 for c in s.chars() {
461 match c {
462 '\n' => escaped.push_str(r"\n"),
463 '\r' => escaped.push_str(r"\r"),
464 '\\' => escaped.push_str(r"\\"),
465 _ => escaped.push(c),
466 }
467 }
468 escaped
469}
470
471fn write_out_deps(tcx: TyCtxt<'_>, outputs: &OutputFilenames, out_filenames: &[PathBuf]) {
472 let sess = tcx.sess;
474 if !sess.opts.output_types.contains_key(&OutputType::DepInfo) {
475 return;
476 }
477 let deps_output = outputs.path(OutputType::DepInfo);
478 let deps_filename = deps_output.as_path();
479
480 let result: io::Result<()> = try {
481 let mut files: Vec<(String, u64, Option<SourceFileHash>)> = sess
484 .source_map()
485 .files()
486 .iter()
487 .filter(|fmap| fmap.is_real_file())
488 .filter(|fmap| !fmap.is_imported())
489 .map(|fmap| {
490 (
491 escape_dep_filename(&fmap.name.prefer_local().to_string()),
492 fmap.source_len.0 as u64,
493 fmap.checksum_hash,
494 )
495 })
496 .collect();
497
498 let checksum_hash_algo = sess.opts.unstable_opts.checksum_hash_algorithm;
499
500 let file_depinfo = sess.psess.file_depinfo.borrow();
503
504 let normalize_path = |path: PathBuf| {
505 let file = FileName::from(path);
506 escape_dep_filename(&file.prefer_local().to_string())
507 };
508
509 fn hash_iter_files<P: AsRef<Path>>(
512 it: impl Iterator<Item = P>,
513 checksum_hash_algo: Option<SourceFileHashAlgorithm>,
514 ) -> impl Iterator<Item = (P, u64, Option<SourceFileHash>)> {
515 it.map(move |path| {
516 match checksum_hash_algo.and_then(|algo| {
517 fs::File::open(path.as_ref())
518 .and_then(|mut file| {
519 SourceFileHash::new(algo, &mut file).map(|h| (file, h))
520 })
521 .and_then(|(file, h)| file.metadata().map(|m| (m.len(), h)))
522 .map_err(|e| {
523 tracing::error!(
524 "failed to compute checksum, omitting it from dep-info {} {e}",
525 path.as_ref().display()
526 )
527 })
528 .ok()
529 }) {
530 Some((file_len, checksum)) => (path, file_len, Some(checksum)),
531 None => (path, 0, None),
532 }
533 })
534 }
535
536 let extra_tracked_files = hash_iter_files(
537 file_depinfo.iter().map(|path_sym| normalize_path(PathBuf::from(path_sym.as_str()))),
538 checksum_hash_algo,
539 );
540 files.extend(extra_tracked_files);
541
542 if let Some(ref profile_instr) = sess.opts.cg.profile_use {
544 files.extend(hash_iter_files(
545 iter::once(normalize_path(profile_instr.as_path().to_path_buf())),
546 checksum_hash_algo,
547 ));
548 }
549 if let Some(ref profile_sample) = sess.opts.unstable_opts.profile_sample_use {
550 files.extend(hash_iter_files(
551 iter::once(normalize_path(profile_sample.as_path().to_path_buf())),
552 checksum_hash_algo,
553 ));
554 }
555
556 for debugger_visualizer in tcx.debugger_visualizers(LOCAL_CRATE) {
558 files.extend(hash_iter_files(
559 iter::once(normalize_path(debugger_visualizer.path.clone().unwrap())),
560 checksum_hash_algo,
561 ));
562 }
563
564 if sess.binary_dep_depinfo() {
565 if let Some(ref backend) = sess.opts.unstable_opts.codegen_backend {
566 if backend.contains('.') {
567 files.extend(hash_iter_files(
570 iter::once(backend.to_string()),
571 checksum_hash_algo,
572 ));
573 }
574 }
575
576 for &cnum in tcx.crates(()) {
577 let source = tcx.used_crate_source(cnum);
578 if let Some((path, _)) = &source.dylib {
579 files.extend(hash_iter_files(
580 iter::once(escape_dep_filename(&path.display().to_string())),
581 checksum_hash_algo,
582 ));
583 }
584 if let Some((path, _)) = &source.rlib {
585 files.extend(hash_iter_files(
586 iter::once(escape_dep_filename(&path.display().to_string())),
587 checksum_hash_algo,
588 ));
589 }
590 if let Some((path, _)) = &source.rmeta {
591 files.extend(hash_iter_files(
592 iter::once(escape_dep_filename(&path.display().to_string())),
593 checksum_hash_algo,
594 ));
595 }
596 }
597 }
598
599 let write_deps_to_file = |file: &mut dyn Write| -> io::Result<()> {
600 for path in out_filenames {
601 writeln!(
602 file,
603 "{}: {}\n",
604 path.display(),
605 files
606 .iter()
607 .map(|(path, _file_len, _checksum_hash_algo)| path.as_str())
608 .intersperse(" ")
609 .collect::<String>()
610 )?;
611 }
612
613 for (path, _file_len, _checksum_hash_algo) in &files {
617 writeln!(file, "{path}:")?;
618 }
619
620 let env_depinfo = sess.psess.env_depinfo.borrow();
622 if !env_depinfo.is_empty() {
623 #[allow(rustc::potential_query_instability)]
625 let mut envs: Vec<_> = env_depinfo
626 .iter()
627 .map(|(k, v)| (escape_dep_env(*k), v.map(escape_dep_env)))
628 .collect();
629 envs.sort_unstable();
630 writeln!(file)?;
631 for (k, v) in envs {
632 write!(file, "# env-dep:{k}")?;
633 if let Some(v) = v {
634 write!(file, "={v}")?;
635 }
636 writeln!(file)?;
637 }
638 }
639
640 if sess.opts.unstable_opts.checksum_hash_algorithm().is_some() {
643 files
644 .iter()
645 .filter_map(|(path, file_len, hash_algo)| {
646 hash_algo.map(|hash_algo| (path, file_len, hash_algo))
647 })
648 .try_for_each(|(path, file_len, checksum_hash)| {
649 writeln!(file, "# checksum:{checksum_hash} file_len:{file_len} {path}")
650 })?;
651 }
652
653 Ok(())
654 };
655
656 match deps_output {
657 OutFileName::Stdout => {
658 let mut file = BufWriter::new(io::stdout());
659 write_deps_to_file(&mut file)?;
660 }
661 OutFileName::Real(ref path) => {
662 let mut file = fs::File::create_buffered(path)?;
663 write_deps_to_file(&mut file)?;
664 }
665 }
666 };
667
668 match result {
669 Ok(_) => {
670 if sess.opts.json_artifact_notifications {
671 sess.dcx().emit_artifact_notification(deps_filename, "dep-info");
672 }
673 }
674 Err(error) => {
675 sess.dcx().emit_fatal(errors::ErrorWritingDependencies { path: deps_filename, error });
676 }
677 }
678}
679
680fn resolver_for_lowering_raw<'tcx>(
681 tcx: TyCtxt<'tcx>,
682 (): (),
683) -> (&'tcx Steal<(ty::ResolverAstLowering, Arc<ast::Crate>)>, &'tcx ty::ResolverGlobalCtxt) {
684 let arenas = Resolver::arenas();
685 let _ = tcx.registered_tools(()); let (krate, pre_configured_attrs) = tcx.crate_for_resolver(()).steal();
687 let mut resolver = Resolver::new(
688 tcx,
689 &pre_configured_attrs,
690 krate.spans.inner_span,
691 krate.spans.inject_use_span,
692 &arenas,
693 );
694 let krate = configure_and_expand(krate, &pre_configured_attrs, &mut resolver);
695
696 tcx.untracked().cstore.freeze();
698
699 let ty::ResolverOutputs {
700 global_ctxt: untracked_resolutions,
701 ast_lowering: untracked_resolver_for_lowering,
702 } = resolver.into_outputs();
703
704 let resolutions = tcx.arena.alloc(untracked_resolutions);
705 (tcx.arena.alloc(Steal::new((untracked_resolver_for_lowering, Arc::new(krate)))), resolutions)
706}
707
708pub fn write_dep_info(tcx: TyCtxt<'_>) {
709 let _ = tcx.resolver_for_lowering();
713
714 let sess = tcx.sess;
715 let _timer = sess.timer("write_dep_info");
716 let crate_name = tcx.crate_name(LOCAL_CRATE);
717
718 let outputs = tcx.output_filenames(());
719 let output_paths =
720 generated_output_paths(tcx, &outputs, sess.io.output_file.is_some(), crate_name);
721
722 if let Some(input_path) = sess.io.input.opt_path() {
724 if sess.opts.will_create_output_file() {
725 if output_contains_path(&output_paths, input_path) {
726 sess.dcx().emit_fatal(errors::InputFileWouldBeOverWritten { path: input_path });
727 }
728 if let Some(dir_path) = output_conflicts_with_dir(&output_paths) {
729 sess.dcx().emit_fatal(errors::GeneratedFileConflictsWithDirectory {
730 input_path,
731 dir_path,
732 });
733 }
734 }
735 }
736
737 if let Some(ref dir) = sess.io.temps_dir {
738 if fs::create_dir_all(dir).is_err() {
739 sess.dcx().emit_fatal(errors::TempsDirError);
740 }
741 }
742
743 write_out_deps(tcx, &outputs, &output_paths);
744
745 let only_dep_info = sess.opts.output_types.contains_key(&OutputType::DepInfo)
746 && sess.opts.output_types.len() == 1;
747
748 if !only_dep_info {
749 if let Some(ref dir) = sess.io.output_dir {
750 if fs::create_dir_all(dir).is_err() {
751 sess.dcx().emit_fatal(errors::OutDirError);
752 }
753 }
754 }
755}
756
757pub fn write_interface<'tcx>(tcx: TyCtxt<'tcx>) {
758 if !tcx.crate_types().contains(&rustc_session::config::CrateType::Sdylib) {
759 return;
760 }
761 let _timer = tcx.sess.timer("write_interface");
762 let (_, krate) = &*tcx.resolver_for_lowering().borrow();
763
764 let krate = rustc_ast_pretty::pprust::print_crate_as_interface(
765 krate,
766 tcx.sess.psess.edition,
767 &tcx.sess.psess.attr_id_generator,
768 );
769 let export_output = tcx.output_filenames(()).interface_path();
770 let mut file = fs::File::create_buffered(export_output).unwrap();
771 if let Err(err) = write!(file, "{}", krate) {
772 tcx.dcx().fatal(format!("error writing interface file: {}", err));
773 }
774}
775
776pub static DEFAULT_QUERY_PROVIDERS: LazyLock<Providers> = LazyLock::new(|| {
777 let providers = &mut Providers::default();
778 providers.analysis = analysis;
779 providers.hir_crate = rustc_ast_lowering::lower_to_hir;
780 providers.resolver_for_lowering_raw = resolver_for_lowering_raw;
781 providers.stripped_cfg_items =
782 |tcx, _| tcx.arena.alloc_from_iter(tcx.resolutions(()).stripped_cfg_items.steal());
783 providers.resolutions = |tcx, ()| tcx.resolver_for_lowering_raw(()).1;
784 providers.early_lint_checks = early_lint_checks;
785 providers.env_var_os = env_var_os;
786 limits::provide(providers);
787 proc_macro_decls::provide(providers);
788 rustc_const_eval::provide(providers);
789 rustc_middle::hir::provide(providers);
790 rustc_borrowck::provide(providers);
791 rustc_incremental::provide(providers);
792 rustc_mir_build::provide(providers);
793 rustc_mir_transform::provide(providers);
794 rustc_monomorphize::provide(providers);
795 rustc_privacy::provide(providers);
796 rustc_query_impl::provide(providers);
797 rustc_resolve::provide(providers);
798 rustc_hir_analysis::provide(providers);
799 rustc_hir_typeck::provide(providers);
800 ty::provide(providers);
801 traits::provide(providers);
802 rustc_passes::provide(providers);
803 rustc_traits::provide(providers);
804 rustc_ty_utils::provide(providers);
805 rustc_metadata::provide(providers);
806 rustc_lint::provide(providers);
807 rustc_symbol_mangling::provide(providers);
808 rustc_codegen_ssa::provide(providers);
809 *providers
810});
811
812pub fn create_and_enter_global_ctxt<T, F: for<'tcx> FnOnce(TyCtxt<'tcx>) -> T>(
813 compiler: &Compiler,
814 krate: rustc_ast::Crate,
815 f: F,
816) -> T {
817 let sess = &compiler.sess;
818
819 let pre_configured_attrs = rustc_expand::config::pre_configure_attrs(sess, &krate.attrs);
820
821 let crate_name = get_crate_name(sess, &pre_configured_attrs);
822 let crate_types = collect_crate_types(sess, &pre_configured_attrs);
823 let stable_crate_id = StableCrateId::new(
824 crate_name,
825 crate_types.contains(&CrateType::Executable),
826 sess.opts.cg.metadata.clone(),
827 sess.cfg_version,
828 );
829
830 let outputs = util::build_output_filenames(&pre_configured_attrs, sess);
831
832 let dep_type = DepsType { dep_names: rustc_query_impl::dep_kind_names() };
833 let dep_graph = setup_dep_graph(sess, crate_name, &dep_type);
834
835 let cstore =
836 FreezeLock::new(Box::new(CStore::new(compiler.codegen_backend.metadata_loader())) as _);
837 let definitions = FreezeLock::new(Definitions::new(stable_crate_id));
838
839 let stable_crate_ids = FreezeLock::new(StableCrateIdMap::default());
840 let untracked =
841 Untracked { cstore, source_span: AppendOnlyIndexVec::new(), definitions, stable_crate_ids };
842
843 dep_graph.assert_ignored();
847
848 let query_result_on_disk_cache = rustc_incremental::load_query_result_cache(sess);
849
850 let codegen_backend = &compiler.codegen_backend;
851 let mut providers = *DEFAULT_QUERY_PROVIDERS;
852 codegen_backend.provide(&mut providers);
853
854 if let Some(callback) = compiler.override_queries {
855 callback(sess, &mut providers);
856 }
857
858 let incremental = dep_graph.is_fully_enabled();
859
860 let gcx_cell = OnceLock::new();
861 let arena = WorkerLocal::new(|_| Arena::default());
862 let hir_arena = WorkerLocal::new(|_| rustc_hir::Arena::default());
863
864 let inner: Box<
867 dyn for<'tcx> FnOnce(
868 &'tcx Session,
869 CurrentGcx,
870 Arc<Proxy>,
871 &'tcx OnceLock<GlobalCtxt<'tcx>>,
872 &'tcx WorkerLocal<Arena<'tcx>>,
873 &'tcx WorkerLocal<rustc_hir::Arena<'tcx>>,
874 F,
875 ) -> T,
876 > = Box::new(move |sess, current_gcx, jobserver_proxy, gcx_cell, arena, hir_arena, f| {
877 TyCtxt::create_global_ctxt(
878 gcx_cell,
879 sess,
880 crate_types,
881 stable_crate_id,
882 arena,
883 hir_arena,
884 untracked,
885 dep_graph,
886 rustc_query_impl::query_callbacks(arena),
887 rustc_query_impl::query_system(
888 providers.queries,
889 providers.extern_queries,
890 query_result_on_disk_cache,
891 incremental,
892 ),
893 providers.hooks,
894 current_gcx,
895 jobserver_proxy,
896 |tcx| {
897 let feed = tcx.create_crate_num(stable_crate_id).unwrap();
898 assert_eq!(feed.key(), LOCAL_CRATE);
899 feed.crate_name(crate_name);
900
901 let feed = tcx.feed_unit_query();
902 feed.features_query(tcx.arena.alloc(rustc_expand::config::features(
903 tcx.sess,
904 &pre_configured_attrs,
905 crate_name,
906 )));
907 feed.crate_for_resolver(tcx.arena.alloc(Steal::new((krate, pre_configured_attrs))));
908 feed.output_filenames(Arc::new(outputs));
909
910 let res = f(tcx);
911 tcx.finish();
913 res
914 },
915 )
916 });
917
918 inner(
919 &compiler.sess,
920 compiler.current_gcx.clone(),
921 Arc::clone(&compiler.jobserver_proxy),
922 &gcx_cell,
923 &arena,
924 &hir_arena,
925 f,
926 )
927}
928
929fn run_required_analyses(tcx: TyCtxt<'_>) {
932 if tcx.sess.opts.unstable_opts.input_stats {
933 rustc_passes::input_stats::print_hir_stats(tcx);
934 }
935 #[cfg(all(not(doc), debug_assertions))]
938 rustc_passes::hir_id_validator::check_crate(tcx);
939
940 tcx.ensure_done().hir_crate(());
944
945 let sess = tcx.sess;
946 sess.time("misc_checking_1", || {
947 parallel!(
948 {
949 sess.time("looking_for_entry_point", || tcx.ensure_ok().entry_fn(()));
950
951 sess.time("looking_for_derive_registrar", || {
952 tcx.ensure_ok().proc_macro_decls_static(())
953 });
954
955 CStore::from_tcx(tcx).report_unused_deps(tcx);
956 },
957 {
958 tcx.ensure_ok().exportable_items(LOCAL_CRATE);
959 tcx.ensure_ok().stable_order_of_exportable_impls(LOCAL_CRATE);
960 tcx.par_hir_for_each_module(|module| {
961 tcx.ensure_ok().check_mod_loops(module);
962 tcx.ensure_ok().check_mod_attrs(module);
963 tcx.ensure_ok().check_mod_naked_functions(module);
964 tcx.ensure_ok().check_mod_unstable_api_usage(module);
965 });
966 },
967 {
968 sess.time("unused_lib_feature_checking", || {
969 rustc_passes::stability::check_unused_or_stable_features(tcx)
970 });
971 },
972 {
973 tcx.ensure_ok().limits(());
978 tcx.ensure_ok().stability_index(());
979 }
980 );
981 });
982
983 rustc_hir_analysis::check_crate(tcx);
984 sess.time("MIR_coroutine_by_move_body", || {
985 tcx.par_hir_body_owners(|def_id| {
986 if tcx.needs_coroutine_by_move_body_def_id(def_id.to_def_id()) {
987 tcx.ensure_done().coroutine_by_move_body_def_id(def_id);
988 }
989 });
990 });
991 tcx.untracked().definitions.freeze();
997
998 sess.time("MIR_borrow_checking", || {
999 tcx.par_hir_body_owners(|def_id| {
1000 if !tcx.is_typeck_child(def_id.to_def_id()) {
1001 tcx.ensure_ok().check_unsafety(def_id);
1003 tcx.ensure_ok().mir_borrowck(def_id)
1004 }
1005 tcx.ensure_ok().has_ffi_unwind_calls(def_id);
1006
1007 if tcx.sess.opts.output_types.should_codegen()
1011 || tcx.hir_body_const_context(def_id).is_some()
1012 {
1013 tcx.ensure_ok().mir_drops_elaborated_and_const_checked(def_id);
1014 }
1015 });
1016 });
1017 sess.time("coroutine_obligations", || {
1018 tcx.par_hir_body_owners(|def_id| {
1019 if tcx.is_coroutine(def_id.to_def_id()) {
1020 tcx.ensure_ok().mir_coroutine_witnesses(def_id);
1021 let _ = tcx.ensure_ok().check_coroutine_obligations(
1022 tcx.typeck_root_def_id(def_id.to_def_id()).expect_local(),
1023 );
1024 if !tcx.is_async_drop_in_place_coroutine(def_id.to_def_id()) {
1025 tcx.ensure_ok().layout_of(
1027 ty::TypingEnv::post_analysis(tcx, def_id.to_def_id())
1028 .as_query_input(tcx.type_of(def_id).instantiate_identity()),
1029 );
1030 }
1031 }
1032 });
1033 });
1034
1035 sess.time("layout_testing", || layout_test::test_layout(tcx));
1036 sess.time("abi_testing", || abi_test::test_abi(tcx));
1037
1038 if tcx.sess.opts.unstable_opts.validate_mir {
1043 sess.time("ensuring_final_MIR_is_computable", || {
1044 tcx.par_hir_body_owners(|def_id| {
1045 tcx.instance_mir(ty::InstanceKind::Item(def_id.into()));
1046 });
1047 });
1048 }
1049}
1050
1051fn analysis(tcx: TyCtxt<'_>, (): ()) {
1054 run_required_analyses(tcx);
1055
1056 let sess = tcx.sess;
1057
1058 if let Some(guar) = sess.dcx().has_errors_excluding_lint_errors() {
1067 guar.raise_fatal();
1068 }
1069
1070 sess.time("misc_checking_3", || {
1071 parallel!(
1072 {
1073 tcx.ensure_ok().effective_visibilities(());
1074
1075 parallel!(
1076 {
1077 tcx.ensure_ok().check_private_in_public(());
1078 },
1079 {
1080 tcx.par_hir_for_each_module(|module| {
1081 tcx.ensure_ok().check_mod_deathness(module)
1082 });
1083 },
1084 {
1085 sess.time("lint_checking", || {
1086 rustc_lint::check_crate(tcx);
1087 });
1088 },
1089 {
1090 tcx.ensure_ok().clashing_extern_declarations(());
1091 }
1092 );
1093 },
1094 {
1095 sess.time("privacy_checking_modules", || {
1096 tcx.par_hir_for_each_module(|module| {
1097 tcx.ensure_ok().check_mod_privacy(module);
1098 });
1099 });
1100 }
1101 );
1102
1103 sess.time("check_lint_expectations", || tcx.ensure_ok().check_expectations(None));
1106
1107 let _ = tcx.all_diagnostic_items(());
1111 });
1112}
1113
1114pub(crate) fn start_codegen<'tcx>(
1117 codegen_backend: &dyn CodegenBackend,
1118 tcx: TyCtxt<'tcx>,
1119) -> Box<dyn Any> {
1120 if let Some((def_id, _)) = tcx.entry_fn(())
1122 && tcx.has_attr(def_id, sym::rustc_delayed_bug_from_inside_query)
1123 {
1124 tcx.ensure_ok().trigger_delayed_bug(def_id);
1125 }
1126
1127 if tcx.sess.opts.output_types.should_codegen() {
1130 rustc_symbol_mangling::test::report_symbol_names(tcx);
1131 }
1132
1133 if let Some(guar) = tcx.sess.dcx().has_errors_or_delayed_bugs() {
1137 guar.raise_fatal();
1138 }
1139
1140 info!("Pre-codegen\n{:?}", tcx.debug_stats());
1141
1142 let (metadata, need_metadata_module) = rustc_metadata::fs::encode_and_write_metadata(tcx);
1143
1144 let codegen = tcx.sess.time("codegen_crate", move || {
1145 codegen_backend.codegen_crate(tcx, metadata, need_metadata_module)
1146 });
1147
1148 info!("Post-codegen\n{:?}", tcx.debug_stats());
1149
1150 if tcx.sess.opts.unstable_opts.print_type_sizes {
1153 tcx.sess.code_stats.print_type_sizes();
1154 }
1155
1156 codegen
1157}
1158
1159pub fn get_crate_name(sess: &Session, krate_attrs: &[ast::Attribute]) -> Symbol {
1161 let attr_crate_name =
1169 validate_and_find_value_str_builtin_attr(sym::crate_name, sess, krate_attrs);
1170
1171 let validate = |name, span| {
1172 rustc_session::output::validate_crate_name(sess, name, span);
1173 name
1174 };
1175
1176 if let Some(crate_name) = &sess.opts.crate_name {
1177 let crate_name = Symbol::intern(crate_name);
1178 if let Some((attr_crate_name, span)) = attr_crate_name
1179 && attr_crate_name != crate_name
1180 {
1181 sess.dcx().emit_err(errors::CrateNameDoesNotMatch {
1182 span,
1183 crate_name,
1184 attr_crate_name,
1185 });
1186 }
1187 return validate(crate_name, None);
1188 }
1189
1190 if let Some((crate_name, span)) = attr_crate_name {
1191 return validate(crate_name, Some(span));
1192 }
1193
1194 if let Input::File(ref path) = sess.io.input
1195 && let Some(file_stem) = path.file_stem().and_then(|s| s.to_str())
1196 {
1197 if file_stem.starts_with('-') {
1198 sess.dcx().emit_err(errors::CrateNameInvalid { crate_name: file_stem });
1199 } else {
1200 return validate(Symbol::intern(&file_stem.replace('-', "_")), None);
1201 }
1202 }
1203
1204 sym::rust_out
1205}
1206
1207fn get_recursion_limit(krate_attrs: &[ast::Attribute], sess: &Session) -> Limit {
1208 let _ = validate_and_find_value_str_builtin_attr(sym::recursion_limit, sess, krate_attrs);
1212 crate::limits::get_recursion_limit(krate_attrs, sess)
1213}
1214
1215fn validate_and_find_value_str_builtin_attr(
1226 name: Symbol,
1227 sess: &Session,
1228 krate_attrs: &[ast::Attribute],
1229) -> Option<(Symbol, Span)> {
1230 let mut result = None;
1231 for attr in ast::attr::filter_by_name(krate_attrs, name) {
1233 let Some(value) = attr.value_str() else {
1234 validate_attr::emit_fatal_malformed_builtin_attribute(&sess.psess, attr, name)
1235 };
1236 result.get_or_insert((value, attr.span));
1238 }
1239 result
1240}