]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_driver_impl/src/lib.rs
Rollup merge of #107761 - oli-obk:miri_🪵, r=TaKO8Ki
[rust.git] / compiler / rustc_driver_impl / src / lib.rs
1 //! The Rust compiler.
2 //!
3 //! # Note
4 //!
5 //! This API is completely unstable and subject to change.
6
7 #![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
8 #![feature(is_terminal)]
9 #![feature(once_cell)]
10 #![feature(decl_macro)]
11 #![recursion_limit = "256"]
12 #![allow(rustc::potential_query_instability)]
13 #![deny(rustc::untranslatable_diagnostic)]
14 #![deny(rustc::diagnostic_outside_of_impl)]
15
16 #[macro_use]
17 extern crate tracing;
18
19 pub extern crate rustc_plugin_impl as plugin;
20
21 use rustc_ast as ast;
22 use rustc_codegen_ssa::{traits::CodegenBackend, CodegenErrors, CodegenResults};
23 use rustc_data_structures::profiling::{get_resident_set_size, print_time_passes_entry};
24 use rustc_data_structures::sync::SeqCst;
25 use rustc_errors::registry::{InvalidErrorCode, Registry};
26 use rustc_errors::{ErrorGuaranteed, PResult};
27 use rustc_feature::find_gated_cfg;
28 use rustc_hir::def_id::LOCAL_CRATE;
29 use rustc_interface::util::{self, collect_crate_types, get_codegen_backend};
30 use rustc_interface::{interface, Queries};
31 use rustc_lint::LintStore;
32 use rustc_metadata::locator;
33 use rustc_save_analysis as save;
34 use rustc_save_analysis::DumpHandler;
35 use rustc_session::config::{nightly_options, CG_OPTIONS, Z_OPTIONS};
36 use rustc_session::config::{ErrorOutputType, Input, OutputType, PrintRequest, TrimmedDefPaths};
37 use rustc_session::cstore::MetadataLoader;
38 use rustc_session::getopts;
39 use rustc_session::lint::{Lint, LintId};
40 use rustc_session::{config, Session};
41 use rustc_session::{early_error, early_error_no_abort, early_warn};
42 use rustc_span::source_map::{FileLoader, FileName};
43 use rustc_span::symbol::sym;
44 use rustc_target::json::ToJson;
45
46 use std::cmp::max;
47 use std::env;
48 use std::ffi::OsString;
49 use std::fs;
50 use std::io::{self, IsTerminal, Read, Write};
51 use std::panic::{self, catch_unwind};
52 use std::path::PathBuf;
53 use std::process::{self, Command, Stdio};
54 use std::str;
55 use std::sync::LazyLock;
56 use std::time::Instant;
57
58 pub mod args;
59 pub mod pretty;
60 mod session_diagnostics;
61
62 use crate::session_diagnostics::{
63     RLinkEmptyVersionNumber, RLinkEncodingVersionMismatch, RLinkRustcVersionMismatch,
64     RLinkWrongFileType, RlinkNotAFile, RlinkUnableToRead,
65 };
66
67 /// Exit status code used for successful compilation and help output.
68 pub const EXIT_SUCCESS: i32 = 0;
69
70 /// Exit status code used for compilation failures and invalid flags.
71 pub const EXIT_FAILURE: i32 = 1;
72
73 const BUG_REPORT_URL: &str = "https://github.com/rust-lang/rust/issues/new\
74     ?labels=C-bug%2C+I-ICE%2C+T-compiler&template=ice.md";
75
76 const ICE_REPORT_COMPILER_FLAGS: &[&str] = &["-Z", "-C", "--crate-type"];
77
78 const ICE_REPORT_COMPILER_FLAGS_EXCLUDE: &[&str] = &["metadata", "extra-filename"];
79
80 const ICE_REPORT_COMPILER_FLAGS_STRIP_VALUE: &[&str] = &["incremental"];
81
82 pub fn abort_on_err<T>(result: Result<T, ErrorGuaranteed>, sess: &Session) -> T {
83     match result {
84         Err(..) => {
85             sess.abort_if_errors();
86             panic!("error reported but abort_if_errors didn't abort???");
87         }
88         Ok(x) => x,
89     }
90 }
91
92 pub trait Callbacks {
93     /// Called before creating the compiler instance
94     fn config(&mut self, _config: &mut interface::Config) {}
95     /// Called after parsing. Return value instructs the compiler whether to
96     /// continue the compilation afterwards (defaults to `Compilation::Continue`)
97     fn after_parsing<'tcx>(
98         &mut self,
99         _compiler: &interface::Compiler,
100         _queries: &'tcx Queries<'tcx>,
101     ) -> Compilation {
102         Compilation::Continue
103     }
104     /// Called after expansion. Return value instructs the compiler whether to
105     /// continue the compilation afterwards (defaults to `Compilation::Continue`)
106     fn after_expansion<'tcx>(
107         &mut self,
108         _compiler: &interface::Compiler,
109         _queries: &'tcx Queries<'tcx>,
110     ) -> Compilation {
111         Compilation::Continue
112     }
113     /// Called after analysis. Return value instructs the compiler whether to
114     /// continue the compilation afterwards (defaults to `Compilation::Continue`)
115     fn after_analysis<'tcx>(
116         &mut self,
117         _compiler: &interface::Compiler,
118         _queries: &'tcx Queries<'tcx>,
119     ) -> Compilation {
120         Compilation::Continue
121     }
122 }
123
124 #[derive(Default)]
125 pub struct TimePassesCallbacks {
126     time_passes: bool,
127 }
128
129 impl Callbacks for TimePassesCallbacks {
130     // JUSTIFICATION: the session doesn't exist at this point.
131     #[allow(rustc::bad_opt_access)]
132     fn config(&mut self, config: &mut interface::Config) {
133         // If a --print=... option has been given, we don't print the "total"
134         // time because it will mess up the --print output. See #64339.
135         //
136         self.time_passes = config.opts.prints.is_empty() && config.opts.unstable_opts.time_passes;
137         config.opts.trimmed_def_paths = TrimmedDefPaths::GoodPath;
138     }
139 }
140
141 pub fn diagnostics_registry() -> Registry {
142     Registry::new(rustc_error_codes::DIAGNOSTICS)
143 }
144
145 /// This is the primary entry point for rustc.
146 pub struct RunCompiler<'a, 'b> {
147     at_args: &'a [String],
148     callbacks: &'b mut (dyn Callbacks + Send),
149     file_loader: Option<Box<dyn FileLoader + Send + Sync>>,
150     make_codegen_backend:
151         Option<Box<dyn FnOnce(&config::Options) -> Box<dyn CodegenBackend> + Send>>,
152 }
153
154 impl<'a, 'b> RunCompiler<'a, 'b> {
155     pub fn new(at_args: &'a [String], callbacks: &'b mut (dyn Callbacks + Send)) -> Self {
156         Self { at_args, callbacks, file_loader: None, make_codegen_backend: None }
157     }
158
159     /// Set a custom codegen backend.
160     ///
161     /// Has no uses within this repository, but is used by bjorn3 for "the
162     /// hotswapping branch of cg_clif" for "setting the codegen backend from a
163     /// custom driver where the custom codegen backend has arbitrary data."
164     /// (See #102759.)
165     pub fn set_make_codegen_backend(
166         &mut self,
167         make_codegen_backend: Option<
168             Box<dyn FnOnce(&config::Options) -> Box<dyn CodegenBackend> + Send>,
169         >,
170     ) -> &mut Self {
171         self.make_codegen_backend = make_codegen_backend;
172         self
173     }
174
175     /// Load files from sources other than the file system.
176     ///
177     /// Has no uses within this repository, but may be used in the future by
178     /// bjorn3 for "hooking rust-analyzer's VFS into rustc at some point for
179     /// running rustc without having to save". (See #102759.)
180     pub fn set_file_loader(
181         &mut self,
182         file_loader: Option<Box<dyn FileLoader + Send + Sync>>,
183     ) -> &mut Self {
184         self.file_loader = file_loader;
185         self
186     }
187
188     /// Parse args and run the compiler.
189     pub fn run(self) -> interface::Result<()> {
190         run_compiler(self.at_args, self.callbacks, self.file_loader, self.make_codegen_backend)
191     }
192 }
193
194 fn run_compiler(
195     at_args: &[String],
196     callbacks: &mut (dyn Callbacks + Send),
197     file_loader: Option<Box<dyn FileLoader + Send + Sync>>,
198     make_codegen_backend: Option<
199         Box<dyn FnOnce(&config::Options) -> Box<dyn CodegenBackend> + Send>,
200     >,
201 ) -> interface::Result<()> {
202     let args = args::arg_expand_all(at_args);
203
204     let Some(matches) = handle_options(&args) else { return Ok(()) };
205
206     let sopts = config::build_session_options(&matches);
207
208     if let Some(ref code) = matches.opt_str("explain") {
209         handle_explain(diagnostics_registry(), code, sopts.error_format);
210         return Ok(());
211     }
212
213     let cfg = interface::parse_cfgspecs(matches.opt_strs("cfg"));
214     let check_cfg = interface::parse_check_cfg(matches.opt_strs("check-cfg"));
215     let (odir, ofile) = make_output(&matches);
216     let mut config = interface::Config {
217         opts: sopts,
218         crate_cfg: cfg,
219         crate_check_cfg: check_cfg,
220         input: Input::File(PathBuf::new()),
221         output_file: ofile,
222         output_dir: odir,
223         file_loader,
224         lint_caps: Default::default(),
225         parse_sess_created: None,
226         register_lints: None,
227         override_queries: None,
228         make_codegen_backend,
229         registry: diagnostics_registry(),
230     };
231
232     match make_input(config.opts.error_format, &matches.free) {
233         Err(reported) => return Err(reported),
234         Ok(Some(input)) => {
235             config.input = input;
236
237             callbacks.config(&mut config);
238         }
239         Ok(None) => match matches.free.len() {
240             0 => {
241                 callbacks.config(&mut config);
242                 interface::run_compiler(config, |compiler| {
243                     let sopts = &compiler.session().opts;
244                     if sopts.describe_lints {
245                         let mut lint_store =
246                             rustc_lint::new_lint_store(compiler.session().enable_internal_lints());
247                         let registered_lints =
248                             if let Some(register_lints) = compiler.register_lints() {
249                                 register_lints(compiler.session(), &mut lint_store);
250                                 true
251                             } else {
252                                 false
253                             };
254                         describe_lints(compiler.session(), &lint_store, registered_lints);
255                         return;
256                     }
257                     let should_stop =
258                         print_crate_info(&***compiler.codegen_backend(), compiler.session(), false);
259
260                     if should_stop == Compilation::Stop {
261                         return;
262                     }
263                     early_error(sopts.error_format, "no input filename given")
264                 });
265                 return Ok(());
266             }
267             1 => panic!("make_input should have provided valid inputs"),
268             _ => early_error(
269                 config.opts.error_format,
270                 &format!(
271                     "multiple input filenames provided (first two filenames are `{}` and `{}`)",
272                     matches.free[0], matches.free[1],
273                 ),
274             ),
275         },
276     };
277
278     interface::run_compiler(config, |compiler| {
279         let sess = compiler.session();
280         let should_stop = print_crate_info(&***compiler.codegen_backend(), sess, true)
281             .and_then(|| list_metadata(sess, &*compiler.codegen_backend().metadata_loader()))
282             .and_then(|| try_process_rlink(sess, compiler));
283
284         if should_stop == Compilation::Stop {
285             return sess.compile_status();
286         }
287
288         let linker = compiler.enter(|queries| {
289             let early_exit = || sess.compile_status().map(|_| None);
290             queries.parse()?;
291
292             if let Some(ppm) = &sess.opts.pretty {
293                 if ppm.needs_ast_map() {
294                     queries.global_ctxt()?.enter(|tcx| {
295                         pretty::print_after_hir_lowering(tcx, *ppm);
296                         Ok(())
297                     })?;
298                 } else {
299                     let krate = queries.parse()?.steal();
300                     pretty::print_after_parsing(sess, &krate, *ppm);
301                 }
302                 trace!("finished pretty-printing");
303                 return early_exit();
304             }
305
306             if callbacks.after_parsing(compiler, queries) == Compilation::Stop {
307                 return early_exit();
308             }
309
310             if sess.opts.unstable_opts.parse_only || sess.opts.unstable_opts.show_span.is_some() {
311                 return early_exit();
312             }
313
314             {
315                 let plugins = queries.register_plugins()?;
316                 let (_, lint_store) = &*plugins.borrow();
317
318                 // Lint plugins are registered; now we can process command line flags.
319                 if sess.opts.describe_lints {
320                     describe_lints(sess, lint_store, true);
321                     return early_exit();
322                 }
323             }
324
325             // Make sure name resolution and macro expansion is run.
326             queries.global_ctxt()?;
327
328             if callbacks.after_expansion(compiler, queries) == Compilation::Stop {
329                 return early_exit();
330             }
331
332             // Make sure the `output_filenames` query is run for its side
333             // effects of writing the dep-info and reporting errors.
334             queries.global_ctxt()?.enter(|tcx| tcx.output_filenames(()));
335
336             if sess.opts.output_types.contains_key(&OutputType::DepInfo)
337                 && sess.opts.output_types.len() == 1
338             {
339                 return early_exit();
340             }
341
342             if sess.opts.unstable_opts.no_analysis {
343                 return early_exit();
344             }
345
346             queries.global_ctxt()?.enter(|tcx| {
347                 let result = tcx.analysis(());
348                 if sess.opts.unstable_opts.save_analysis {
349                     let crate_name = tcx.crate_name(LOCAL_CRATE);
350                     sess.time("save_analysis", || {
351                         save::process_crate(
352                             tcx,
353                             crate_name,
354                             &sess.io.input,
355                             None,
356                             DumpHandler::new(sess.io.output_dir.as_deref(), crate_name),
357                         )
358                     });
359                 }
360                 result
361             })?;
362
363             if callbacks.after_analysis(compiler, queries) == Compilation::Stop {
364                 return early_exit();
365             }
366
367             queries.ongoing_codegen()?;
368
369             if sess.opts.unstable_opts.print_type_sizes {
370                 sess.code_stats.print_type_sizes();
371             }
372
373             let linker = queries.linker()?;
374             Ok(Some(linker))
375         })?;
376
377         if let Some(linker) = linker {
378             let _timer = sess.timer("link");
379             linker.link()?
380         }
381
382         if sess.opts.unstable_opts.perf_stats {
383             sess.print_perf_stats();
384         }
385
386         if sess.opts.unstable_opts.print_fuel.is_some() {
387             eprintln!(
388                 "Fuel used by {}: {}",
389                 sess.opts.unstable_opts.print_fuel.as_ref().unwrap(),
390                 sess.print_fuel.load(SeqCst)
391             );
392         }
393
394         Ok(())
395     })
396 }
397
398 // Extract output directory and file from matches.
399 fn make_output(matches: &getopts::Matches) -> (Option<PathBuf>, Option<PathBuf>) {
400     let odir = matches.opt_str("out-dir").map(|o| PathBuf::from(&o));
401     let ofile = matches.opt_str("o").map(|o| PathBuf::from(&o));
402     (odir, ofile)
403 }
404
405 // Extract input (string or file and optional path) from matches.
406 fn make_input(
407     error_format: ErrorOutputType,
408     free_matches: &[String],
409 ) -> Result<Option<Input>, ErrorGuaranteed> {
410     if free_matches.len() == 1 {
411         let ifile = &free_matches[0];
412         if ifile == "-" {
413             let mut src = String::new();
414             if io::stdin().read_to_string(&mut src).is_err() {
415                 // Immediately stop compilation if there was an issue reading
416                 // the input (for example if the input stream is not UTF-8).
417                 let reported = early_error_no_abort(
418                     error_format,
419                     "couldn't read from stdin, as it did not contain valid UTF-8",
420                 );
421                 return Err(reported);
422             }
423             if let Ok(path) = env::var("UNSTABLE_RUSTDOC_TEST_PATH") {
424                 let line = env::var("UNSTABLE_RUSTDOC_TEST_LINE").expect(
425                     "when UNSTABLE_RUSTDOC_TEST_PATH is set \
426                                     UNSTABLE_RUSTDOC_TEST_LINE also needs to be set",
427                 );
428                 let line = isize::from_str_radix(&line, 10)
429                     .expect("UNSTABLE_RUSTDOC_TEST_LINE needs to be an number");
430                 let file_name = FileName::doc_test_source_code(PathBuf::from(path), line);
431                 Ok(Some(Input::Str { name: file_name, input: src }))
432             } else {
433                 Ok(Some(Input::Str { name: FileName::anon_source_code(&src), input: src }))
434             }
435         } else {
436             Ok(Some(Input::File(PathBuf::from(ifile))))
437         }
438     } else {
439         Ok(None)
440     }
441 }
442
443 /// Whether to stop or continue compilation.
444 #[derive(Copy, Clone, Debug, Eq, PartialEq)]
445 pub enum Compilation {
446     Stop,
447     Continue,
448 }
449
450 impl Compilation {
451     pub fn and_then<F: FnOnce() -> Compilation>(self, next: F) -> Compilation {
452         match self {
453             Compilation::Stop => Compilation::Stop,
454             Compilation::Continue => next(),
455         }
456     }
457 }
458
459 fn handle_explain(registry: Registry, code: &str, output: ErrorOutputType) {
460     let upper_cased_code = code.to_ascii_uppercase();
461     let normalised =
462         if upper_cased_code.starts_with('E') { upper_cased_code } else { format!("E{code:0>4}") };
463     match registry.try_find_description(&normalised) {
464         Ok(Some(description)) => {
465             let mut is_in_code_block = false;
466             let mut text = String::new();
467             // Slice off the leading newline and print.
468             for line in description.lines() {
469                 let indent_level =
470                     line.find(|c: char| !c.is_whitespace()).unwrap_or_else(|| line.len());
471                 let dedented_line = &line[indent_level..];
472                 if dedented_line.starts_with("```") {
473                     is_in_code_block = !is_in_code_block;
474                     text.push_str(&line[..(indent_level + 3)]);
475                 } else if is_in_code_block && dedented_line.starts_with("# ") {
476                     continue;
477                 } else {
478                     text.push_str(line);
479                 }
480                 text.push('\n');
481             }
482             if io::stdout().is_terminal() {
483                 show_content_with_pager(&text);
484             } else {
485                 print!("{text}");
486             }
487         }
488         Ok(None) => {
489             early_error(output, &format!("no extended information for {code}"));
490         }
491         Err(InvalidErrorCode) => {
492             early_error(output, &format!("{code} is not a valid error code"));
493         }
494     }
495 }
496
497 fn show_content_with_pager(content: &str) {
498     let pager_name = env::var_os("PAGER").unwrap_or_else(|| {
499         if cfg!(windows) { OsString::from("more.com") } else { OsString::from("less") }
500     });
501
502     let mut fallback_to_println = false;
503
504     match Command::new(pager_name).stdin(Stdio::piped()).spawn() {
505         Ok(mut pager) => {
506             if let Some(pipe) = pager.stdin.as_mut() {
507                 if pipe.write_all(content.as_bytes()).is_err() {
508                     fallback_to_println = true;
509                 }
510             }
511
512             if pager.wait().is_err() {
513                 fallback_to_println = true;
514             }
515         }
516         Err(_) => {
517             fallback_to_println = true;
518         }
519     }
520
521     // If pager fails for whatever reason, we should still print the content
522     // to standard output
523     if fallback_to_println {
524         print!("{content}");
525     }
526 }
527
528 pub fn try_process_rlink(sess: &Session, compiler: &interface::Compiler) -> Compilation {
529     if sess.opts.unstable_opts.link_only {
530         if let Input::File(file) = &sess.io.input {
531             // FIXME: #![crate_type] and #![crate_name] support not implemented yet
532             sess.init_crate_types(collect_crate_types(sess, &[]));
533             let outputs = compiler.build_output_filenames(sess, &[]);
534             let rlink_data = fs::read(file).unwrap_or_else(|err| {
535                 sess.emit_fatal(RlinkUnableToRead { err });
536             });
537             let codegen_results = match CodegenResults::deserialize_rlink(rlink_data) {
538                 Ok(codegen) => codegen,
539                 Err(err) => {
540                     match err {
541                         CodegenErrors::WrongFileType => sess.emit_fatal(RLinkWrongFileType),
542                         CodegenErrors::EmptyVersionNumber => {
543                             sess.emit_fatal(RLinkEmptyVersionNumber)
544                         }
545                         CodegenErrors::EncodingVersionMismatch { version_array, rlink_version } => {
546                             sess.emit_fatal(RLinkEncodingVersionMismatch {
547                                 version_array,
548                                 rlink_version,
549                             })
550                         }
551                         CodegenErrors::RustcVersionMismatch { rustc_version, current_version } => {
552                             sess.emit_fatal(RLinkRustcVersionMismatch {
553                                 rustc_version,
554                                 current_version,
555                             })
556                         }
557                     };
558                 }
559             };
560             let result = compiler.codegen_backend().link(sess, codegen_results, &outputs);
561             abort_on_err(result, sess);
562         } else {
563             sess.emit_fatal(RlinkNotAFile {})
564         }
565         Compilation::Stop
566     } else {
567         Compilation::Continue
568     }
569 }
570
571 pub fn list_metadata(sess: &Session, metadata_loader: &dyn MetadataLoader) -> Compilation {
572     if sess.opts.unstable_opts.ls {
573         match sess.io.input {
574             Input::File(ref ifile) => {
575                 let path = &(*ifile);
576                 let mut v = Vec::new();
577                 locator::list_file_metadata(&sess.target, path, metadata_loader, &mut v).unwrap();
578                 println!("{}", String::from_utf8(v).unwrap());
579             }
580             Input::Str { .. } => {
581                 early_error(ErrorOutputType::default(), "cannot list metadata for stdin");
582             }
583         }
584         return Compilation::Stop;
585     }
586
587     Compilation::Continue
588 }
589
590 fn print_crate_info(
591     codegen_backend: &dyn CodegenBackend,
592     sess: &Session,
593     parse_attrs: bool,
594 ) -> Compilation {
595     use rustc_session::config::PrintRequest::*;
596     // NativeStaticLibs and LinkArgs are special - printed during linking
597     // (empty iterator returns true)
598     if sess.opts.prints.iter().all(|&p| p == NativeStaticLibs || p == LinkArgs) {
599         return Compilation::Continue;
600     }
601
602     let attrs = if parse_attrs {
603         let result = parse_crate_attrs(sess);
604         match result {
605             Ok(attrs) => Some(attrs),
606             Err(mut parse_error) => {
607                 parse_error.emit();
608                 return Compilation::Stop;
609             }
610         }
611     } else {
612         None
613     };
614     for req in &sess.opts.prints {
615         match *req {
616             TargetList => {
617                 let mut targets = rustc_target::spec::TARGETS.to_vec();
618                 targets.sort_unstable();
619                 println!("{}", targets.join("\n"));
620             }
621             Sysroot => println!("{}", sess.sysroot.display()),
622             TargetLibdir => println!("{}", sess.target_tlib_path.dir.display()),
623             TargetSpec => {
624                 println!("{}", serde_json::to_string_pretty(&sess.target.to_json()).unwrap());
625             }
626             FileNames | CrateName => {
627                 let attrs = attrs.as_ref().unwrap();
628                 let t_outputs = rustc_interface::util::build_output_filenames(attrs, sess);
629                 let id = rustc_session::output::find_crate_name(sess, attrs);
630                 if *req == PrintRequest::CrateName {
631                     println!("{id}");
632                     continue;
633                 }
634                 let crate_types = collect_crate_types(sess, attrs);
635                 for &style in &crate_types {
636                     let fname =
637                         rustc_session::output::filename_for_input(sess, style, id, &t_outputs);
638                     println!("{}", fname.file_name().unwrap().to_string_lossy());
639                 }
640             }
641             Cfg => {
642                 let mut cfgs = sess
643                     .parse_sess
644                     .config
645                     .iter()
646                     .filter_map(|&(name, value)| {
647                         // Note that crt-static is a specially recognized cfg
648                         // directive that's printed out here as part of
649                         // rust-lang/rust#37406, but in general the
650                         // `target_feature` cfg is gated under
651                         // rust-lang/rust#29717. For now this is just
652                         // specifically allowing the crt-static cfg and that's
653                         // it, this is intended to get into Cargo and then go
654                         // through to build scripts.
655                         if (name != sym::target_feature || value != Some(sym::crt_dash_static))
656                             && !sess.is_nightly_build()
657                             && find_gated_cfg(|cfg_sym| cfg_sym == name).is_some()
658                         {
659                             return None;
660                         }
661
662                         if let Some(value) = value {
663                             Some(format!("{name}=\"{value}\""))
664                         } else {
665                             Some(name.to_string())
666                         }
667                     })
668                     .collect::<Vec<String>>();
669
670                 cfgs.sort();
671                 for cfg in cfgs {
672                     println!("{cfg}");
673                 }
674             }
675             CallingConventions => {
676                 let mut calling_conventions = rustc_target::spec::abi::all_names();
677                 calling_conventions.sort_unstable();
678                 println!("{}", calling_conventions.join("\n"));
679             }
680             RelocationModels
681             | CodeModels
682             | TlsModels
683             | TargetCPUs
684             | StackProtectorStrategies
685             | TargetFeatures => {
686                 codegen_backend.print(*req, sess);
687             }
688             // Any output here interferes with Cargo's parsing of other printed output
689             NativeStaticLibs => {}
690             LinkArgs => {}
691             SplitDebuginfo => {
692                 use rustc_target::spec::SplitDebuginfo::{Off, Packed, Unpacked};
693
694                 for split in &[Off, Packed, Unpacked] {
695                     let stable = sess.target.options.supported_split_debuginfo.contains(split);
696                     let unstable_ok = sess.unstable_options();
697                     if stable || unstable_ok {
698                         println!("{split}");
699                     }
700                 }
701             }
702         }
703     }
704     Compilation::Stop
705 }
706
707 /// Prints version information
708 ///
709 /// NOTE: this is a macro to support drivers built at a different time than the main `rustc_driver` crate.
710 pub macro version($binary: literal, $matches: expr) {
711     fn unw(x: Option<&str>) -> &str {
712         x.unwrap_or("unknown")
713     }
714     $crate::version_at_macro_invocation(
715         $binary,
716         $matches,
717         unw(option_env!("CFG_VERSION")),
718         unw(option_env!("CFG_VER_HASH")),
719         unw(option_env!("CFG_VER_DATE")),
720         unw(option_env!("CFG_RELEASE")),
721     )
722 }
723
724 #[doc(hidden)] // use the macro instead
725 pub fn version_at_macro_invocation(
726     binary: &str,
727     matches: &getopts::Matches,
728     version: &str,
729     commit_hash: &str,
730     commit_date: &str,
731     release: &str,
732 ) {
733     let verbose = matches.opt_present("verbose");
734
735     println!("{binary} {version}");
736
737     if verbose {
738         println!("binary: {binary}");
739         println!("commit-hash: {commit_hash}");
740         println!("commit-date: {commit_date}");
741         println!("host: {}", config::host_triple());
742         println!("release: {release}");
743
744         let debug_flags = matches.opt_strs("Z");
745         let backend_name = debug_flags.iter().find_map(|x| x.strip_prefix("codegen-backend="));
746         get_codegen_backend(&None, backend_name).print_version();
747     }
748 }
749
750 fn usage(verbose: bool, include_unstable_options: bool, nightly_build: bool) {
751     let groups = if verbose { config::rustc_optgroups() } else { config::rustc_short_optgroups() };
752     let mut options = getopts::Options::new();
753     for option in groups.iter().filter(|x| include_unstable_options || x.is_stable()) {
754         (option.apply)(&mut options);
755     }
756     let message = "Usage: rustc [OPTIONS] INPUT";
757     let nightly_help = if nightly_build {
758         "\n    -Z help             Print unstable compiler options"
759     } else {
760         ""
761     };
762     let verbose_help = if verbose {
763         ""
764     } else {
765         "\n    --help -v           Print the full set of options rustc accepts"
766     };
767     let at_path = if verbose {
768         "    @path               Read newline separated options from `path`\n"
769     } else {
770         ""
771     };
772     println!(
773         "{options}{at_path}\nAdditional help:
774     -C help             Print codegen options
775     -W help             \
776               Print 'lint' options and default settings{nightly}{verbose}\n",
777         options = options.usage(message),
778         at_path = at_path,
779         nightly = nightly_help,
780         verbose = verbose_help
781     );
782 }
783
784 fn print_wall_help() {
785     println!(
786         "
787 The flag `-Wall` does not exist in `rustc`. Most useful lints are enabled by
788 default. Use `rustc -W help` to see all available lints. It's more common to put
789 warning settings in the crate root using `#![warn(LINT_NAME)]` instead of using
790 the command line flag directly.
791 "
792     );
793 }
794
795 /// Write to stdout lint command options, together with a list of all available lints
796 pub fn describe_lints(sess: &Session, lint_store: &LintStore, loaded_plugins: bool) {
797     println!(
798         "
799 Available lint options:
800     -W <foo>           Warn about <foo>
801     -A <foo>           \
802               Allow <foo>
803     -D <foo>           Deny <foo>
804     -F <foo>           Forbid <foo> \
805               (deny <foo> and all attempts to override)
806
807 "
808     );
809
810     fn sort_lints(sess: &Session, mut lints: Vec<&'static Lint>) -> Vec<&'static Lint> {
811         // The sort doesn't case-fold but it's doubtful we care.
812         lints.sort_by_cached_key(|x: &&Lint| (x.default_level(sess.edition()), x.name));
813         lints
814     }
815
816     fn sort_lint_groups(
817         lints: Vec<(&'static str, Vec<LintId>, bool)>,
818     ) -> Vec<(&'static str, Vec<LintId>)> {
819         let mut lints: Vec<_> = lints.into_iter().map(|(x, y, _)| (x, y)).collect();
820         lints.sort_by_key(|l| l.0);
821         lints
822     }
823
824     let (plugin, builtin): (Vec<_>, _) =
825         lint_store.get_lints().iter().cloned().partition(|&lint| lint.is_plugin);
826     let plugin = sort_lints(sess, plugin);
827     let builtin = sort_lints(sess, builtin);
828
829     let (plugin_groups, builtin_groups): (Vec<_>, _) =
830         lint_store.get_lint_groups().partition(|&(.., p)| p);
831     let plugin_groups = sort_lint_groups(plugin_groups);
832     let builtin_groups = sort_lint_groups(builtin_groups);
833
834     let max_name_len =
835         plugin.iter().chain(&builtin).map(|&s| s.name.chars().count()).max().unwrap_or(0);
836     let padded = |x: &str| {
837         let mut s = " ".repeat(max_name_len - x.chars().count());
838         s.push_str(x);
839         s
840     };
841
842     println!("Lint checks provided by rustc:\n");
843
844     let print_lints = |lints: Vec<&Lint>| {
845         println!("    {}  {:7.7}  {}", padded("name"), "default", "meaning");
846         println!("    {}  {:7.7}  {}", padded("----"), "-------", "-------");
847         for lint in lints {
848             let name = lint.name_lower().replace('_', "-");
849             println!(
850                 "    {}  {:7.7}  {}",
851                 padded(&name),
852                 lint.default_level(sess.edition()).as_str(),
853                 lint.desc
854             );
855         }
856         println!("\n");
857     };
858
859     print_lints(builtin);
860
861     let max_name_len = max(
862         "warnings".len(),
863         plugin_groups
864             .iter()
865             .chain(&builtin_groups)
866             .map(|&(s, _)| s.chars().count())
867             .max()
868             .unwrap_or(0),
869     );
870
871     let padded = |x: &str| {
872         let mut s = " ".repeat(max_name_len - x.chars().count());
873         s.push_str(x);
874         s
875     };
876
877     println!("Lint groups provided by rustc:\n");
878
879     let print_lint_groups = |lints: Vec<(&'static str, Vec<LintId>)>, all_warnings| {
880         println!("    {}  sub-lints", padded("name"));
881         println!("    {}  ---------", padded("----"));
882
883         if all_warnings {
884             println!("    {}  all lints that are set to issue warnings", padded("warnings"));
885         }
886
887         for (name, to) in lints {
888             let name = name.to_lowercase().replace('_', "-");
889             let desc = to
890                 .into_iter()
891                 .map(|x| x.to_string().replace('_', "-"))
892                 .collect::<Vec<String>>()
893                 .join(", ");
894             println!("    {}  {}", padded(&name), desc);
895         }
896         println!("\n");
897     };
898
899     print_lint_groups(builtin_groups, true);
900
901     match (loaded_plugins, plugin.len(), plugin_groups.len()) {
902         (false, 0, _) | (false, _, 0) => {
903             println!("Lint tools like Clippy can provide additional lints and lint groups.");
904         }
905         (false, ..) => panic!("didn't load lint plugins but got them anyway!"),
906         (true, 0, 0) => println!("This crate does not load any lint plugins or lint groups."),
907         (true, l, g) => {
908             if l > 0 {
909                 println!("Lint checks provided by plugins loaded by this crate:\n");
910                 print_lints(plugin);
911             }
912             if g > 0 {
913                 println!("Lint groups provided by plugins loaded by this crate:\n");
914                 print_lint_groups(plugin_groups, false);
915             }
916         }
917     }
918 }
919
920 fn describe_debug_flags() {
921     println!("\nAvailable options:\n");
922     print_flag_list("-Z", config::Z_OPTIONS);
923 }
924
925 fn describe_codegen_flags() {
926     println!("\nAvailable codegen options:\n");
927     print_flag_list("-C", config::CG_OPTIONS);
928 }
929
930 pub fn print_flag_list<T>(
931     cmdline_opt: &str,
932     flag_list: &[(&'static str, T, &'static str, &'static str)],
933 ) {
934     let max_len = flag_list.iter().map(|&(name, _, _, _)| name.chars().count()).max().unwrap_or(0);
935
936     for &(name, _, _, desc) in flag_list {
937         println!(
938             "    {} {:>width$}=val -- {}",
939             cmdline_opt,
940             name.replace('_', "-"),
941             desc,
942             width = max_len
943         );
944     }
945 }
946
947 /// Process command line options. Emits messages as appropriate. If compilation
948 /// should continue, returns a getopts::Matches object parsed from args,
949 /// otherwise returns `None`.
950 ///
951 /// The compiler's handling of options is a little complicated as it ties into
952 /// our stability story. The current intention of each compiler option is to
953 /// have one of two modes:
954 ///
955 /// 1. An option is stable and can be used everywhere.
956 /// 2. An option is unstable, and can only be used on nightly.
957 ///
958 /// Like unstable library and language features, however, unstable options have
959 /// always required a form of "opt in" to indicate that you're using them. This
960 /// provides the easy ability to scan a code base to check to see if anything
961 /// unstable is being used. Currently, this "opt in" is the `-Z` "zed" flag.
962 ///
963 /// All options behind `-Z` are considered unstable by default. Other top-level
964 /// options can also be considered unstable, and they were unlocked through the
965 /// `-Z unstable-options` flag. Note that `-Z` remains to be the root of
966 /// instability in both cases, though.
967 ///
968 /// So with all that in mind, the comments below have some more detail about the
969 /// contortions done here to get things to work out correctly.
970 pub fn handle_options(args: &[String]) -> Option<getopts::Matches> {
971     // Throw away the first argument, the name of the binary
972     let args = &args[1..];
973
974     if args.is_empty() {
975         // user did not write `-v` nor `-Z unstable-options`, so do not
976         // include that extra information.
977         let nightly_build =
978             rustc_feature::UnstableFeatures::from_environment(None).is_nightly_build();
979         usage(false, false, nightly_build);
980         return None;
981     }
982
983     // Parse with *all* options defined in the compiler, we don't worry about
984     // option stability here we just want to parse as much as possible.
985     let mut options = getopts::Options::new();
986     for option in config::rustc_optgroups() {
987         (option.apply)(&mut options);
988     }
989     let matches = options.parse(args).unwrap_or_else(|e| {
990         let msg = match e {
991             getopts::Fail::UnrecognizedOption(ref opt) => CG_OPTIONS
992                 .iter()
993                 .map(|&(name, ..)| ('C', name))
994                 .chain(Z_OPTIONS.iter().map(|&(name, ..)| ('Z', name)))
995                 .find(|&(_, name)| *opt == name.replace('_', "-"))
996                 .map(|(flag, _)| format!("{e}. Did you mean `-{flag} {opt}`?")),
997             _ => None,
998         };
999         early_error(ErrorOutputType::default(), &msg.unwrap_or_else(|| e.to_string()));
1000     });
1001
1002     // For all options we just parsed, we check a few aspects:
1003     //
1004     // * If the option is stable, we're all good
1005     // * If the option wasn't passed, we're all good
1006     // * If `-Z unstable-options` wasn't passed (and we're not a -Z option
1007     //   ourselves), then we require the `-Z unstable-options` flag to unlock
1008     //   this option that was passed.
1009     // * If we're a nightly compiler, then unstable options are now unlocked, so
1010     //   we're good to go.
1011     // * Otherwise, if we're an unstable option then we generate an error
1012     //   (unstable option being used on stable)
1013     nightly_options::check_nightly_options(&matches, &config::rustc_optgroups());
1014
1015     if matches.opt_present("h") || matches.opt_present("help") {
1016         // Only show unstable options in --help if we accept unstable options.
1017         let unstable_enabled = nightly_options::is_unstable_enabled(&matches);
1018         let nightly_build = nightly_options::match_is_nightly_build(&matches);
1019         usage(matches.opt_present("verbose"), unstable_enabled, nightly_build);
1020         return None;
1021     }
1022
1023     // Handle the special case of -Wall.
1024     let wall = matches.opt_strs("W");
1025     if wall.iter().any(|x| *x == "all") {
1026         print_wall_help();
1027         rustc_errors::FatalError.raise();
1028     }
1029
1030     // Don't handle -W help here, because we might first load plugins.
1031     let debug_flags = matches.opt_strs("Z");
1032     if debug_flags.iter().any(|x| *x == "help") {
1033         describe_debug_flags();
1034         return None;
1035     }
1036
1037     let cg_flags = matches.opt_strs("C");
1038
1039     if cg_flags.iter().any(|x| *x == "help") {
1040         describe_codegen_flags();
1041         return None;
1042     }
1043
1044     if cg_flags.iter().any(|x| *x == "no-stack-check") {
1045         early_warn(
1046             ErrorOutputType::default(),
1047             "the --no-stack-check flag is deprecated and does nothing",
1048         );
1049     }
1050
1051     if cg_flags.iter().any(|x| *x == "passes=list") {
1052         let backend_name = debug_flags.iter().find_map(|x| x.strip_prefix("codegen-backend="));
1053         get_codegen_backend(&None, backend_name).print_passes();
1054         return None;
1055     }
1056
1057     if matches.opt_present("version") {
1058         version!("rustc", &matches);
1059         return None;
1060     }
1061
1062     Some(matches)
1063 }
1064
1065 fn parse_crate_attrs<'a>(sess: &'a Session) -> PResult<'a, ast::AttrVec> {
1066     match &sess.io.input {
1067         Input::File(ifile) => rustc_parse::parse_crate_attrs_from_file(ifile, &sess.parse_sess),
1068         Input::Str { name, input } => rustc_parse::parse_crate_attrs_from_source_str(
1069             name.clone(),
1070             input.clone(),
1071             &sess.parse_sess,
1072         ),
1073     }
1074 }
1075
1076 /// Gets a list of extra command-line flags provided by the user, as strings.
1077 ///
1078 /// This function is used during ICEs to show more information useful for
1079 /// debugging, since some ICEs only happens with non-default compiler flags
1080 /// (and the users don't always report them).
1081 fn extra_compiler_flags() -> Option<(Vec<String>, bool)> {
1082     let mut args = env::args_os().map(|arg| arg.to_string_lossy().to_string()).peekable();
1083
1084     let mut result = Vec::new();
1085     let mut excluded_cargo_defaults = false;
1086     while let Some(arg) = args.next() {
1087         if let Some(a) = ICE_REPORT_COMPILER_FLAGS.iter().find(|a| arg.starts_with(*a)) {
1088             let content = if arg.len() == a.len() {
1089                 // A space-separated option, like `-C incremental=foo` or `--crate-type rlib`
1090                 match args.next() {
1091                     Some(arg) => arg.to_string(),
1092                     None => continue,
1093                 }
1094             } else if arg.get(a.len()..a.len() + 1) == Some("=") {
1095                 // An equals option, like `--crate-type=rlib`
1096                 arg[a.len() + 1..].to_string()
1097             } else {
1098                 // A non-space option, like `-Cincremental=foo`
1099                 arg[a.len()..].to_string()
1100             };
1101             let option = content.split_once('=').map(|s| s.0).unwrap_or(&content);
1102             if ICE_REPORT_COMPILER_FLAGS_EXCLUDE.iter().any(|exc| option == *exc) {
1103                 excluded_cargo_defaults = true;
1104             } else {
1105                 result.push(a.to_string());
1106                 match ICE_REPORT_COMPILER_FLAGS_STRIP_VALUE.iter().find(|s| option == **s) {
1107                     Some(s) => result.push(format!("{s}=[REDACTED]")),
1108                     None => result.push(content),
1109                 }
1110             }
1111         }
1112     }
1113
1114     if !result.is_empty() { Some((result, excluded_cargo_defaults)) } else { None }
1115 }
1116
1117 /// Runs a closure and catches unwinds triggered by fatal errors.
1118 ///
1119 /// The compiler currently unwinds with a special sentinel value to abort
1120 /// compilation on fatal errors. This function catches that sentinel and turns
1121 /// the panic into a `Result` instead.
1122 pub fn catch_fatal_errors<F: FnOnce() -> R, R>(f: F) -> Result<R, ErrorGuaranteed> {
1123     catch_unwind(panic::AssertUnwindSafe(f)).map_err(|value| {
1124         if value.is::<rustc_errors::FatalErrorMarker>() {
1125             ErrorGuaranteed::unchecked_claim_error_was_emitted()
1126         } else {
1127             panic::resume_unwind(value);
1128         }
1129     })
1130 }
1131
1132 /// Variant of `catch_fatal_errors` for the `interface::Result` return type
1133 /// that also computes the exit code.
1134 pub fn catch_with_exit_code(f: impl FnOnce() -> interface::Result<()>) -> i32 {
1135     let result = catch_fatal_errors(f).and_then(|result| result);
1136     match result {
1137         Ok(()) => EXIT_SUCCESS,
1138         Err(_) => EXIT_FAILURE,
1139     }
1140 }
1141
1142 static DEFAULT_HOOK: LazyLock<Box<dyn Fn(&panic::PanicInfo<'_>) + Sync + Send + 'static>> =
1143     LazyLock::new(|| {
1144         let hook = panic::take_hook();
1145         panic::set_hook(Box::new(|info| {
1146             // If the error was caused by a broken pipe then this is not a bug.
1147             // Write the error and return immediately. See #98700.
1148             #[cfg(windows)]
1149             if let Some(msg) = info.payload().downcast_ref::<String>() {
1150                 if msg.starts_with("failed printing to stdout: ") && msg.ends_with("(os error 232)")
1151                 {
1152                     early_error_no_abort(ErrorOutputType::default(), &msg);
1153                     return;
1154                 }
1155             };
1156
1157             // Invoke the default handler, which prints the actual panic message and optionally a backtrace
1158             // Don't do this for delayed bugs, which already emit their own more useful backtrace.
1159             if !info.payload().is::<rustc_errors::DelayedBugPanic>() {
1160                 (*DEFAULT_HOOK)(info);
1161
1162                 // Separate the output with an empty line
1163                 eprintln!();
1164             }
1165
1166             // Print the ICE message
1167             report_ice(info, BUG_REPORT_URL);
1168         }));
1169         hook
1170     });
1171
1172 /// Prints the ICE message, including query stack, but without backtrace.
1173 ///
1174 /// The message will point the user at `bug_report_url` to report the ICE.
1175 ///
1176 /// When `install_ice_hook` is called, this function will be called as the panic
1177 /// hook.
1178 pub fn report_ice(info: &panic::PanicInfo<'_>, bug_report_url: &str) {
1179     let fallback_bundle =
1180         rustc_errors::fallback_fluent_bundle(rustc_errors::DEFAULT_LOCALE_RESOURCES, false);
1181     let emitter = Box::new(rustc_errors::emitter::EmitterWriter::stderr(
1182         rustc_errors::ColorConfig::Auto,
1183         None,
1184         None,
1185         fallback_bundle,
1186         false,
1187         false,
1188         None,
1189         false,
1190         false,
1191     ));
1192     let handler = rustc_errors::Handler::with_emitter(true, None, emitter);
1193
1194     // a .span_bug or .bug call has already printed what
1195     // it wants to print.
1196     if !info.payload().is::<rustc_errors::ExplicitBug>()
1197         && !info.payload().is::<rustc_errors::DelayedBugPanic>()
1198     {
1199         handler.emit_err(session_diagnostics::Ice);
1200     }
1201
1202     handler.emit_note(session_diagnostics::IceBugReport { bug_report_url });
1203     handler.emit_note(session_diagnostics::IceVersion {
1204         version: util::version_str!().unwrap_or("unknown_version"),
1205         triple: config::host_triple(),
1206     });
1207
1208     if let Some((flags, excluded_cargo_defaults)) = extra_compiler_flags() {
1209         handler.emit_note(session_diagnostics::IceFlags { flags: flags.join(" ") });
1210         if excluded_cargo_defaults {
1211             handler.emit_note(session_diagnostics::IceExcludeCargoDefaults);
1212         }
1213     }
1214
1215     // If backtraces are enabled, also print the query stack
1216     let backtrace = env::var_os("RUST_BACKTRACE").map_or(false, |x| &x != "0");
1217
1218     let num_frames = if backtrace { None } else { Some(2) };
1219
1220     interface::try_print_query_stack(&handler, num_frames);
1221
1222     #[cfg(windows)]
1223     unsafe {
1224         if env::var("RUSTC_BREAK_ON_ICE").is_ok() {
1225             // Trigger a debugger if we crashed during bootstrap
1226             winapi::um::debugapi::DebugBreak();
1227         }
1228     }
1229 }
1230
1231 /// Installs a panic hook that will print the ICE message on unexpected panics.
1232 ///
1233 /// A custom rustc driver can skip calling this to set up a custom ICE hook.
1234 pub fn install_ice_hook() {
1235     // If the user has not explicitly overridden "RUST_BACKTRACE", then produce
1236     // full backtraces. When a compiler ICE happens, we want to gather
1237     // as much information as possible to present in the issue opened
1238     // by the user. Compiler developers and other rustc users can
1239     // opt in to less-verbose backtraces by manually setting "RUST_BACKTRACE"
1240     // (e.g. `RUST_BACKTRACE=1`)
1241     if std::env::var("RUST_BACKTRACE").is_err() {
1242         std::env::set_var("RUST_BACKTRACE", "full");
1243     }
1244     LazyLock::force(&DEFAULT_HOOK);
1245 }
1246
1247 /// This allows tools to enable rust logging without having to magically match rustc's
1248 /// tracing crate version.
1249 pub fn init_rustc_env_logger() {
1250     init_env_logger("RUSTC_LOG");
1251 }
1252
1253 /// This allows tools to enable rust logging without having to magically match rustc's
1254 /// tracing crate version. In contrast to `init_rustc_env_logger` it allows you to choose an env var
1255 /// other than `RUSTC_LOG`.
1256 pub fn init_env_logger(env: &str) {
1257     if let Err(error) = rustc_log::init_env_logger(env) {
1258         early_error(ErrorOutputType::default(), &error.to_string());
1259     }
1260 }
1261
1262 #[cfg(all(unix, any(target_env = "gnu", target_os = "macos")))]
1263 mod signal_handler {
1264     extern "C" {
1265         fn backtrace_symbols_fd(
1266             buffer: *const *mut libc::c_void,
1267             size: libc::c_int,
1268             fd: libc::c_int,
1269         );
1270     }
1271
1272     extern "C" fn print_stack_trace(_: libc::c_int) {
1273         const MAX_FRAMES: usize = 256;
1274         static mut STACK_TRACE: [*mut libc::c_void; MAX_FRAMES] =
1275             [std::ptr::null_mut(); MAX_FRAMES];
1276         unsafe {
1277             let depth = libc::backtrace(STACK_TRACE.as_mut_ptr(), MAX_FRAMES as i32);
1278             if depth == 0 {
1279                 return;
1280             }
1281             backtrace_symbols_fd(STACK_TRACE.as_ptr(), depth, 2);
1282         }
1283     }
1284
1285     /// When an error signal (such as SIGABRT or SIGSEGV) is delivered to the
1286     /// process, print a stack trace and then exit.
1287     pub(super) fn install() {
1288         unsafe {
1289             const ALT_STACK_SIZE: usize = libc::MINSIGSTKSZ + 64 * 1024;
1290             let mut alt_stack: libc::stack_t = std::mem::zeroed();
1291             alt_stack.ss_sp =
1292                 std::alloc::alloc(std::alloc::Layout::from_size_align(ALT_STACK_SIZE, 1).unwrap())
1293                     as *mut libc::c_void;
1294             alt_stack.ss_size = ALT_STACK_SIZE;
1295             libc::sigaltstack(&alt_stack, std::ptr::null_mut());
1296
1297             let mut sa: libc::sigaction = std::mem::zeroed();
1298             sa.sa_sigaction = print_stack_trace as libc::sighandler_t;
1299             sa.sa_flags = libc::SA_NODEFER | libc::SA_RESETHAND | libc::SA_ONSTACK;
1300             libc::sigemptyset(&mut sa.sa_mask);
1301             libc::sigaction(libc::SIGSEGV, &sa, std::ptr::null_mut());
1302         }
1303     }
1304 }
1305
1306 #[cfg(not(all(unix, any(target_env = "gnu", target_os = "macos"))))]
1307 mod signal_handler {
1308     pub(super) fn install() {}
1309 }
1310
1311 pub fn main() -> ! {
1312     let start_time = Instant::now();
1313     let start_rss = get_resident_set_size();
1314     init_rustc_env_logger();
1315     signal_handler::install();
1316     let mut callbacks = TimePassesCallbacks::default();
1317     install_ice_hook();
1318     let exit_code = catch_with_exit_code(|| {
1319         let args = env::args_os()
1320             .enumerate()
1321             .map(|(i, arg)| {
1322                 arg.into_string().unwrap_or_else(|arg| {
1323                     early_error(
1324                         ErrorOutputType::default(),
1325                         &format!("argument {i} is not valid Unicode: {arg:?}"),
1326                     )
1327                 })
1328             })
1329             .collect::<Vec<_>>();
1330         RunCompiler::new(&args, &mut callbacks).run()
1331     });
1332
1333     if callbacks.time_passes {
1334         let end_rss = get_resident_set_size();
1335         print_time_passes_entry("total", start_time.elapsed(), start_rss, end_rss);
1336     }
1337
1338     process::exit(exit_code)
1339 }