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