1use std::path::PathBuf;
2use std::result;
3use std::sync::Arc;
4
5use rustc_ast::{LitKind, MetaItemKind, token};
6use rustc_codegen_ssa::traits::CodegenBackend;
7use rustc_data_structures::fx::{FxHashMap, FxHashSet};
8use rustc_data_structures::jobserver;
9use rustc_data_structures::stable_hasher::StableHasher;
10use rustc_errors::registry::Registry;
11use rustc_errors::{DiagCtxtHandle, ErrorGuaranteed};
12use rustc_lint::LintStore;
13use rustc_middle::ty;
14use rustc_middle::ty::CurrentGcx;
15use rustc_middle::util::Providers;
16use rustc_parse::new_parser_from_source_str;
17use rustc_parse::parser::attr::AllowLeadingUnsafe;
18use rustc_query_impl::QueryCtxt;
19use rustc_query_system::query::print_query_stack;
20use rustc_session::config::{self, Cfg, CheckCfg, ExpectedValues, Input, OutFileName};
21use rustc_session::filesearch::sysroot_candidates;
22use rustc_session::parse::ParseSess;
23use rustc_session::{CompilerIO, EarlyDiagCtxt, Session, lint};
24use rustc_span::source_map::{FileLoader, RealFileLoader, SourceMapInputs};
25use rustc_span::{FileName, sym};
26use tracing::trace;
27
28use crate::util;
29
30pub type Result<T> = result::Result<T, ErrorGuaranteed>;
31
32pub struct Compiler {
40 pub sess: Session,
41 pub codegen_backend: Box<dyn CodegenBackend>,
42 pub(crate) override_queries: Option<fn(&Session, &mut Providers)>,
43 pub(crate) current_gcx: CurrentGcx,
44}
45
46pub(crate) fn parse_cfg(dcx: DiagCtxtHandle<'_>, cfgs: Vec<String>) -> Cfg {
48 cfgs.into_iter()
49 .map(|s| {
50 let psess = ParseSess::with_silent_emitter(
51 vec![crate::DEFAULT_LOCALE_RESOURCE, rustc_parse::DEFAULT_LOCALE_RESOURCE],
52 format!("this error occurred on the command line: `--cfg={s}`"),
53 true,
54 );
55 let filename = FileName::cfg_spec_source_code(&s);
56
57 macro_rules! error {
58 ($reason: expr) => {
59 #[allow(rustc::untranslatable_diagnostic)]
60 #[allow(rustc::diagnostic_outside_of_impl)]
61 dcx.fatal(format!(
62 concat!("invalid `--cfg` argument: `{}` (", $reason, ")"),
63 s
64 ));
65 };
66 }
67
68 match new_parser_from_source_str(&psess, filename, s.to_string()) {
69 Ok(mut parser) => match parser.parse_meta_item(AllowLeadingUnsafe::No) {
70 Ok(meta_item) if parser.token == token::Eof => {
71 if meta_item.path.segments.len() != 1 {
72 error!("argument key must be an identifier");
73 }
74 match &meta_item.kind {
75 MetaItemKind::List(..) => {}
76 MetaItemKind::NameValue(lit) if !lit.kind.is_str() => {
77 error!("argument value must be a string");
78 }
79 MetaItemKind::NameValue(..) | MetaItemKind::Word => {
80 let ident = meta_item.ident().expect("multi-segment cfg key");
81 return (ident.name, meta_item.value_str());
82 }
83 }
84 }
85 Ok(..) => {}
86 Err(err) => err.cancel(),
87 },
88 Err(errs) => errs.into_iter().for_each(|err| err.cancel()),
89 }
90
91 if s.contains('=') && !s.contains("=\"") && !s.ends_with('"') {
94 error!(concat!(
95 r#"expected `key` or `key="value"`, ensure escaping is appropriate"#,
96 r#" for your shell, try 'key="value"' or key=\"value\""#
97 ));
98 } else {
99 error!(r#"expected `key` or `key="value"`"#);
100 }
101 })
102 .collect::<Cfg>()
103}
104
105pub(crate) fn parse_check_cfg(dcx: DiagCtxtHandle<'_>, specs: Vec<String>) -> CheckCfg {
107 let exhaustive_names = !specs.is_empty();
110 let exhaustive_values = !specs.is_empty();
111 let mut check_cfg = CheckCfg { exhaustive_names, exhaustive_values, ..CheckCfg::default() };
112
113 for s in specs {
114 let psess = ParseSess::with_silent_emitter(
115 vec![crate::DEFAULT_LOCALE_RESOURCE, rustc_parse::DEFAULT_LOCALE_RESOURCE],
116 format!("this error occurred on the command line: `--check-cfg={s}`"),
117 true,
118 );
119 let filename = FileName::cfg_spec_source_code(&s);
120
121 const VISIT: &str =
122 "visit <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more details";
123
124 macro_rules! error {
125 ($reason:expr) => {
126 #[allow(rustc::untranslatable_diagnostic)]
127 #[allow(rustc::diagnostic_outside_of_impl)]
128 {
129 let mut diag =
130 dcx.struct_fatal(format!("invalid `--check-cfg` argument: `{s}`"));
131 diag.note($reason);
132 diag.note(VISIT);
133 diag.emit()
134 }
135 };
136 (in $arg:expr, $reason:expr) => {
137 #[allow(rustc::untranslatable_diagnostic)]
138 #[allow(rustc::diagnostic_outside_of_impl)]
139 {
140 let mut diag =
141 dcx.struct_fatal(format!("invalid `--check-cfg` argument: `{s}`"));
142
143 let pparg = rustc_ast_pretty::pprust::meta_list_item_to_string($arg);
144 if let Some(lit) = $arg.lit() {
145 let (lit_kind_article, lit_kind_descr) = {
146 let lit_kind = lit.as_token_lit().kind;
147 (lit_kind.article(), lit_kind.descr())
148 };
149 diag.note(format!(
150 "`{pparg}` is {lit_kind_article} {lit_kind_descr} literal"
151 ));
152 } else {
153 diag.note(format!("`{pparg}` is invalid"));
154 }
155
156 diag.note($reason);
157 diag.note(VISIT);
158 diag.emit()
159 }
160 };
161 }
162
163 let expected_error = || -> ! {
164 error!("expected `cfg(name, values(\"value1\", \"value2\", ... \"valueN\"))`")
165 };
166
167 let mut parser = match new_parser_from_source_str(&psess, filename, s.to_string()) {
168 Ok(parser) => parser,
169 Err(errs) => {
170 errs.into_iter().for_each(|err| err.cancel());
171 expected_error();
172 }
173 };
174
175 let meta_item = match parser.parse_meta_item(AllowLeadingUnsafe::No) {
176 Ok(meta_item) if parser.token == token::Eof => meta_item,
177 Ok(..) => expected_error(),
178 Err(err) => {
179 err.cancel();
180 expected_error();
181 }
182 };
183
184 let Some(args) = meta_item.meta_item_list() else {
185 expected_error();
186 };
187
188 if !meta_item.has_name(sym::cfg) {
189 expected_error();
190 }
191
192 let mut names = Vec::new();
193 let mut values: FxHashSet<_> = Default::default();
194
195 let mut any_specified = false;
196 let mut values_specified = false;
197 let mut values_any_specified = false;
198
199 for arg in args {
200 if arg.is_word()
201 && let Some(ident) = arg.ident()
202 {
203 if values_specified {
204 error!("`cfg()` names cannot be after values");
205 }
206 names.push(ident);
207 } else if let Some(boolean) = arg.boolean_literal() {
208 if values_specified {
209 error!("`cfg()` names cannot be after values");
210 }
211 names.push(rustc_span::Ident::new(
212 if boolean { rustc_span::kw::True } else { rustc_span::kw::False },
213 arg.span(),
214 ));
215 } else if arg.has_name(sym::any)
216 && let Some(args) = arg.meta_item_list()
217 {
218 if any_specified {
219 error!("`any()` cannot be specified multiple times");
220 }
221 any_specified = true;
222 if !args.is_empty() {
223 error!(in arg, "`any()` takes no argument");
224 }
225 } else if arg.has_name(sym::values)
226 && let Some(args) = arg.meta_item_list()
227 {
228 if names.is_empty() {
229 error!("`values()` cannot be specified before the names");
230 } else if values_specified {
231 error!("`values()` cannot be specified multiple times");
232 }
233 values_specified = true;
234
235 for arg in args {
236 if let Some(LitKind::Str(s, _)) = arg.lit().map(|lit| &lit.kind) {
237 values.insert(Some(*s));
238 } else if arg.has_name(sym::any)
239 && let Some(args) = arg.meta_item_list()
240 {
241 if values_any_specified {
242 error!(in arg, "`any()` in `values()` cannot be specified multiple times");
243 }
244 values_any_specified = true;
245 if !args.is_empty() {
246 error!(in arg, "`any()` in `values()` takes no argument");
247 }
248 } else if arg.has_name(sym::none)
249 && let Some(args) = arg.meta_item_list()
250 {
251 values.insert(None);
252 if !args.is_empty() {
253 error!(in arg, "`none()` in `values()` takes no argument");
254 }
255 } else {
256 error!(in arg, "`values()` arguments must be string literals, `none()` or `any()`");
257 }
258 }
259 } else {
260 error!(in arg, "`cfg()` arguments must be simple identifiers, `any()` or `values(...)`");
261 }
262 }
263
264 if !values_specified && !any_specified {
265 values.insert(None);
268 } else if !values.is_empty() && values_any_specified {
269 error!(
270 "`values()` arguments cannot specify string literals and `any()` at the same time"
271 );
272 }
273
274 if any_specified {
275 if names.is_empty() && values.is_empty() && !values_specified && !values_any_specified {
276 check_cfg.exhaustive_names = false;
277 } else {
278 error!("`cfg(any())` can only be provided in isolation");
279 }
280 } else {
281 for name in names {
282 check_cfg
283 .expecteds
284 .entry(name.name)
285 .and_modify(|v| match v {
286 ExpectedValues::Some(v) if !values_any_specified => {
287 v.extend(values.clone())
288 }
289 ExpectedValues::Some(_) => *v = ExpectedValues::Any,
290 ExpectedValues::Any => {}
291 })
292 .or_insert_with(|| {
293 if values_any_specified {
294 ExpectedValues::Any
295 } else {
296 ExpectedValues::Some(values.clone())
297 }
298 });
299 }
300 }
301 }
302
303 check_cfg
304}
305
306pub struct Config {
308 pub opts: config::Options,
310
311 pub crate_cfg: Vec<String>,
313 pub crate_check_cfg: Vec<String>,
314
315 pub input: Input,
316 pub output_dir: Option<PathBuf>,
317 pub output_file: Option<OutFileName>,
318 pub ice_file: Option<PathBuf>,
319 pub file_loader: Option<Box<dyn FileLoader + Send + Sync>>,
325 pub locale_resources: Vec<&'static str>,
328
329 pub lint_caps: FxHashMap<lint::LintId, lint::Level>,
330
331 pub psess_created: Option<Box<dyn FnOnce(&mut ParseSess) + Send>>,
333
334 pub hash_untracked_state: Option<Box<dyn FnOnce(&Session, &mut StableHasher) + Send>>,
339
340 pub register_lints: Option<Box<dyn Fn(&Session, &mut LintStore) + Send + Sync>>,
346
347 pub override_queries: Option<fn(&Session, &mut Providers)>,
350
351 pub extra_symbols: Vec<&'static str>,
354
355 pub make_codegen_backend:
362 Option<Box<dyn FnOnce(&config::Options) -> Box<dyn CodegenBackend> + Send>>,
363
364 pub registry: Registry,
366
367 pub using_internal_features: &'static std::sync::atomic::AtomicBool,
371
372 pub expanded_args: Vec<String>,
377}
378
379pub(crate) fn initialize_checked_jobserver(early_dcx: &EarlyDiagCtxt) {
381 jobserver::initialize_checked(|err| {
382 #[allow(rustc::untranslatable_diagnostic)]
383 #[allow(rustc::diagnostic_outside_of_impl)]
384 early_dcx
385 .early_struct_warn(err)
386 .with_note("the build environment is likely misconfigured")
387 .emit()
388 });
389}
390
391#[allow(rustc::bad_opt_access)]
393pub fn run_compiler<R: Send>(config: Config, f: impl FnOnce(&Compiler) -> R + Send) -> R {
394 trace!("run_compiler");
395
396 rustc_data_structures::sync::set_dyn_thread_safe_mode(config.opts.unstable_opts.threads > 1);
398
399 let early_dcx = EarlyDiagCtxt::new(config.opts.error_format);
401 initialize_checked_jobserver(&early_dcx);
402
403 crate::callbacks::setup_callbacks();
404
405 let sysroot = config.opts.sysroot.clone();
406 let target = config::build_target_config(&early_dcx, &config.opts.target_triple, &sysroot);
407 let file_loader = config.file_loader.unwrap_or_else(|| Box::new(RealFileLoader));
408 let path_mapping = config.opts.file_path_mapping();
409 let hash_kind = config.opts.unstable_opts.src_hash_algorithm(&target);
410 let checksum_hash_kind = config.opts.unstable_opts.checksum_hash_algorithm();
411
412 util::run_in_thread_pool_with_globals(
413 &early_dcx,
414 config.opts.edition,
415 config.opts.unstable_opts.threads,
416 &config.extra_symbols,
417 SourceMapInputs { file_loader, path_mapping, hash_kind, checksum_hash_kind },
418 |current_gcx| {
419 let early_dcx = EarlyDiagCtxt::new(config.opts.error_format);
422
423 let codegen_backend = match config.make_codegen_backend {
424 None => util::get_codegen_backend(
425 &early_dcx,
426 &sysroot,
427 config.opts.unstable_opts.codegen_backend.as_deref(),
428 &target,
429 ),
430 Some(make_codegen_backend) => {
431 make_codegen_backend(&config.opts)
434 }
435 };
436
437 let temps_dir = config.opts.unstable_opts.temps_dir.as_deref().map(PathBuf::from);
438
439 let bundle = match rustc_errors::fluent_bundle(
440 config.opts.sysroot.clone(),
441 sysroot_candidates().to_vec(),
442 config.opts.unstable_opts.translate_lang.clone(),
443 config.opts.unstable_opts.translate_additional_ftl.as_deref(),
444 config.opts.unstable_opts.translate_directionality_markers,
445 ) {
446 Ok(bundle) => bundle,
447 Err(e) => {
448 #[allow(rustc::untranslatable_diagnostic)]
450 early_dcx.early_fatal(format!("failed to load fluent bundle: {e}"))
451 }
452 };
453
454 let mut locale_resources = config.locale_resources;
455 locale_resources.push(codegen_backend.locale_resource());
456
457 let mut sess = rustc_session::build_session(
458 config.opts,
459 CompilerIO {
460 input: config.input,
461 output_dir: config.output_dir,
462 output_file: config.output_file,
463 temps_dir,
464 },
465 bundle,
466 config.registry,
467 locale_resources,
468 config.lint_caps,
469 target,
470 sysroot,
471 util::rustc_version_str().unwrap_or("unknown"),
472 config.ice_file,
473 config.using_internal_features,
474 config.expanded_args,
475 );
476
477 codegen_backend.init(&sess);
478
479 let cfg = parse_cfg(sess.dcx(), config.crate_cfg);
480 let mut cfg = config::build_configuration(&sess, cfg);
481 util::add_configuration(&mut cfg, &mut sess, &*codegen_backend);
482 sess.psess.config = cfg;
483
484 let mut check_cfg = parse_check_cfg(sess.dcx(), config.crate_check_cfg);
485 check_cfg.fill_well_known(&sess.target);
486 sess.psess.check_config = check_cfg;
487
488 if let Some(psess_created) = config.psess_created {
489 psess_created(&mut sess.psess);
490 }
491
492 if let Some(hash_untracked_state) = config.hash_untracked_state {
493 let mut hasher = StableHasher::new();
494 hash_untracked_state(&sess, &mut hasher);
495 sess.opts.untracked_state_hash = hasher.finish()
496 }
497
498 let mut lint_store = rustc_lint::new_lint_store(sess.enable_internal_lints());
502 if let Some(register_lints) = config.register_lints.as_deref() {
503 register_lints(&sess, &mut lint_store);
504 }
505 sess.lint_store = Some(Arc::new(lint_store));
506
507 util::check_abi_required_features(&sess);
508
509 let compiler = Compiler {
510 sess,
511 codegen_backend,
512 override_queries: config.override_queries,
513 current_gcx,
514 };
515
516 let res = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| f(&compiler)));
522
523 compiler.sess.finish_diagnostics();
524
525 if res.is_ok() {
533 compiler.sess.dcx().abort_if_errors();
534 }
535
536 compiler.sess.dcx().flush_delayed();
541
542 let res = match res {
543 Ok(res) => res,
544 Err(err) => std::panic::resume_unwind(err),
546 };
547
548 let prof = compiler.sess.prof.clone();
549 prof.generic_activity("drop_compiler").run(move || drop(compiler));
550
551 res
552 },
553 )
554}
555
556pub fn try_print_query_stack(
557 dcx: DiagCtxtHandle<'_>,
558 limit_frames: Option<usize>,
559 file: Option<std::fs::File>,
560) {
561 eprintln!("query stack during panic:");
562
563 let all_frames = ty::tls::with_context_opt(|icx| {
567 if let Some(icx) = icx {
568 ty::print::with_no_queries!(print_query_stack(
569 QueryCtxt::new(icx.tcx),
570 icx.query,
571 dcx,
572 limit_frames,
573 file,
574 ))
575 } else {
576 0
577 }
578 });
579
580 if let Some(limit_frames) = limit_frames
581 && all_frames > limit_frames
582 {
583 eprintln!(
584 "... and {} other queries... use `env RUST_BACKTRACE=1` to see the full query stack",
585 all_frames - limit_frames
586 );
587 } else {
588 eprintln!("end of query stack");
589 }
590}