rustc_interface/
interface.rs

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::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::parse::ParseSess;
22use rustc_session::{CompilerIO, EarlyDiagCtxt, Session, lint};
23use rustc_span::source_map::{FileLoader, RealFileLoader, SourceMapInputs};
24use rustc_span::{FileName, sym};
25use tracing::trace;
26
27use crate::util;
28
29pub type Result<T> = result::Result<T, ErrorGuaranteed>;
30
31/// Represents a compiler session. Note that every `Compiler` contains a
32/// `Session`, but `Compiler` also contains some things that cannot be in
33/// `Session`, due to `Session` being in a crate that has many fewer
34/// dependencies than this crate.
35///
36/// Can be used to run `rustc_interface` queries.
37/// Created by passing [`Config`] to [`run_compiler`].
38pub struct Compiler {
39    pub sess: Session,
40    pub codegen_backend: Box<dyn CodegenBackend>,
41    pub(crate) override_queries: Option<fn(&Session, &mut Providers)>,
42
43    /// A reference to the current `GlobalCtxt` which we pass on to `GlobalCtxt`.
44    pub(crate) current_gcx: CurrentGcx,
45
46    /// A jobserver reference which we pass on to `GlobalCtxt`.
47    pub(crate) jobserver_proxy: Arc<Proxy>,
48}
49
50/// Converts strings provided as `--cfg [cfgspec]` into a `Cfg`.
51pub(crate) fn parse_cfg(dcx: DiagCtxtHandle<'_>, cfgs: Vec<String>) -> Cfg {
52    cfgs.into_iter()
53        .map(|s| {
54            let psess = ParseSess::with_fatal_emitter(
55                vec![crate::DEFAULT_LOCALE_RESOURCE, rustc_parse::DEFAULT_LOCALE_RESOURCE],
56                format!("this error occurred on the command line: `--cfg={s}`"),
57            );
58            let filename = FileName::cfg_spec_source_code(&s);
59
60            macro_rules! error {
61                ($reason: expr) => {
62                    #[allow(rustc::untranslatable_diagnostic)]
63                    #[allow(rustc::diagnostic_outside_of_impl)]
64                    dcx.fatal(format!(
65                        concat!("invalid `--cfg` argument: `{}` (", $reason, ")"),
66                        s
67                    ));
68                };
69            }
70
71            match new_parser_from_source_str(&psess, filename, s.to_string()) {
72                Ok(mut parser) => match parser.parse_meta_item(AllowLeadingUnsafe::No) {
73                    Ok(meta_item) if parser.token == token::Eof => {
74                        if meta_item.path.segments.len() != 1 {
75                            error!("argument key must be an identifier");
76                        }
77                        match &meta_item.kind {
78                            MetaItemKind::List(..) => {}
79                            MetaItemKind::NameValue(lit) if !lit.kind.is_str() => {
80                                error!("argument value must be a string");
81                            }
82                            MetaItemKind::NameValue(..) | MetaItemKind::Word => {
83                                let ident = meta_item.ident().expect("multi-segment cfg key");
84                                return (ident.name, meta_item.value_str());
85                            }
86                        }
87                    }
88                    Ok(..) => {}
89                    Err(err) => err.cancel(),
90                },
91                Err(errs) => errs.into_iter().for_each(|err| err.cancel()),
92            }
93
94            // If the user tried to use a key="value" flag, but is missing the quotes, provide
95            // a hint about how to resolve this.
96            if s.contains('=') && !s.contains("=\"") && !s.ends_with('"') {
97                error!(concat!(
98                    r#"expected `key` or `key="value"`, ensure escaping is appropriate"#,
99                    r#" for your shell, try 'key="value"' or key=\"value\""#
100                ));
101            } else {
102                error!(r#"expected `key` or `key="value"`"#);
103            }
104        })
105        .collect::<Cfg>()
106}
107
108/// Converts strings provided as `--check-cfg [specs]` into a `CheckCfg`.
109pub(crate) fn parse_check_cfg(dcx: DiagCtxtHandle<'_>, specs: Vec<String>) -> CheckCfg {
110    // If any --check-cfg is passed then exhaustive_values and exhaustive_names
111    // are enabled by default.
112    let exhaustive_names = !specs.is_empty();
113    let exhaustive_values = !specs.is_empty();
114    let mut check_cfg = CheckCfg { exhaustive_names, exhaustive_values, ..CheckCfg::default() };
115
116    for s in specs {
117        let psess = ParseSess::with_fatal_emitter(
118            vec![crate::DEFAULT_LOCALE_RESOURCE, rustc_parse::DEFAULT_LOCALE_RESOURCE],
119            format!("this error occurred on the command line: `--check-cfg={s}`"),
120        );
121        let filename = FileName::cfg_spec_source_code(&s);
122
123        const VISIT: &str =
124            "visit <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more details";
125
126        macro_rules! error {
127            ($reason:expr) => {
128                #[allow(rustc::untranslatable_diagnostic)]
129                #[allow(rustc::diagnostic_outside_of_impl)]
130                {
131                    let mut diag =
132                        dcx.struct_fatal(format!("invalid `--check-cfg` argument: `{s}`"));
133                    diag.note($reason);
134                    diag.note(VISIT);
135                    diag.emit()
136                }
137            };
138            (in $arg:expr, $reason:expr) => {
139                #[allow(rustc::untranslatable_diagnostic)]
140                #[allow(rustc::diagnostic_outside_of_impl)]
141                {
142                    let mut diag =
143                        dcx.struct_fatal(format!("invalid `--check-cfg` argument: `{s}`"));
144
145                    let pparg = rustc_ast_pretty::pprust::meta_list_item_to_string($arg);
146                    if let Some(lit) = $arg.lit() {
147                        let (lit_kind_article, lit_kind_descr) = {
148                            let lit_kind = lit.as_token_lit().kind;
149                            (lit_kind.article(), lit_kind.descr())
150                        };
151                        diag.note(format!(
152                            "`{pparg}` is {lit_kind_article} {lit_kind_descr} literal"
153                        ));
154                    } else {
155                        diag.note(format!("`{pparg}` is invalid"));
156                    }
157
158                    diag.note($reason);
159                    diag.note(VISIT);
160                    diag.emit()
161                }
162            };
163        }
164
165        let expected_error = || -> ! {
166            error!("expected `cfg(name, values(\"value1\", \"value2\", ... \"valueN\"))`")
167        };
168
169        let mut parser = match new_parser_from_source_str(&psess, filename, s.to_string()) {
170            Ok(parser) => parser,
171            Err(errs) => {
172                errs.into_iter().for_each(|err| err.cancel());
173                expected_error();
174            }
175        };
176
177        let meta_item = match parser.parse_meta_item(AllowLeadingUnsafe::No) {
178            Ok(meta_item) if parser.token == token::Eof => meta_item,
179            Ok(..) => expected_error(),
180            Err(err) => {
181                err.cancel();
182                expected_error();
183            }
184        };
185
186        let Some(args) = meta_item.meta_item_list() else {
187            expected_error();
188        };
189
190        if !meta_item.has_name(sym::cfg) {
191            expected_error();
192        }
193
194        let mut names = Vec::new();
195        let mut values: FxHashSet<_> = Default::default();
196
197        let mut any_specified = false;
198        let mut values_specified = false;
199        let mut values_any_specified = false;
200
201        for arg in args {
202            if arg.is_word()
203                && let Some(ident) = arg.ident()
204            {
205                if values_specified {
206                    error!("`cfg()` names cannot be after values");
207                }
208                names.push(ident);
209            } else if let Some(boolean) = arg.boolean_literal() {
210                if values_specified {
211                    error!("`cfg()` names cannot be after values");
212                }
213                names.push(rustc_span::Ident::new(
214                    if boolean { rustc_span::kw::True } else { rustc_span::kw::False },
215                    arg.span(),
216                ));
217            } else if arg.has_name(sym::any)
218                && let Some(args) = arg.meta_item_list()
219            {
220                if any_specified {
221                    error!("`any()` cannot be specified multiple times");
222                }
223                any_specified = true;
224                if !args.is_empty() {
225                    error!(in arg, "`any()` takes no argument");
226                }
227            } else if arg.has_name(sym::values)
228                && let Some(args) = arg.meta_item_list()
229            {
230                if names.is_empty() {
231                    error!("`values()` cannot be specified before the names");
232                } else if values_specified {
233                    error!("`values()` cannot be specified multiple times");
234                }
235                values_specified = true;
236
237                for arg in args {
238                    if let Some(LitKind::Str(s, _)) = arg.lit().map(|lit| &lit.kind) {
239                        values.insert(Some(*s));
240                    } else if arg.has_name(sym::any)
241                        && let Some(args) = arg.meta_item_list()
242                    {
243                        if values_any_specified {
244                            error!(in arg, "`any()` in `values()` cannot be specified multiple times");
245                        }
246                        values_any_specified = true;
247                        if !args.is_empty() {
248                            error!(in arg, "`any()` in `values()` takes no argument");
249                        }
250                    } else if arg.has_name(sym::none)
251                        && let Some(args) = arg.meta_item_list()
252                    {
253                        values.insert(None);
254                        if !args.is_empty() {
255                            error!(in arg, "`none()` in `values()` takes no argument");
256                        }
257                    } else {
258                        error!(in arg, "`values()` arguments must be string literals, `none()` or `any()`");
259                    }
260                }
261            } else {
262                error!(in arg, "`cfg()` arguments must be simple identifiers, `any()` or `values(...)`");
263            }
264        }
265
266        if !values_specified && !any_specified {
267            // `cfg(name)` is equivalent to `cfg(name, values(none()))` so add
268            // an implicit `none()`
269            values.insert(None);
270        } else if !values.is_empty() && values_any_specified {
271            error!(
272                "`values()` arguments cannot specify string literals and `any()` at the same time"
273            );
274        }
275
276        if any_specified {
277            if names.is_empty() && values.is_empty() && !values_specified && !values_any_specified {
278                check_cfg.exhaustive_names = false;
279            } else {
280                error!("`cfg(any())` can only be provided in isolation");
281            }
282        } else {
283            for name in names {
284                check_cfg
285                    .expecteds
286                    .entry(name.name)
287                    .and_modify(|v| match v {
288                        ExpectedValues::Some(v) if !values_any_specified =>
289                        {
290                            #[allow(rustc::potential_query_instability)]
291                            v.extend(values.clone())
292                        }
293                        ExpectedValues::Some(_) => *v = ExpectedValues::Any,
294                        ExpectedValues::Any => {}
295                    })
296                    .or_insert_with(|| {
297                        if values_any_specified {
298                            ExpectedValues::Any
299                        } else {
300                            ExpectedValues::Some(values.clone())
301                        }
302                    });
303            }
304        }
305    }
306
307    check_cfg
308}
309
310/// The compiler configuration
311pub struct Config {
312    /// Command line options
313    pub opts: config::Options,
314
315    /// Unparsed cfg! configuration in addition to the default ones.
316    pub crate_cfg: Vec<String>,
317    pub crate_check_cfg: Vec<String>,
318
319    pub input: Input,
320    pub output_dir: Option<PathBuf>,
321    pub output_file: Option<OutFileName>,
322    pub ice_file: Option<PathBuf>,
323    /// Load files from sources other than the file system.
324    ///
325    /// Has no uses within this repository, but may be used in the future by
326    /// bjorn3 for "hooking rust-analyzer's VFS into rustc at some point for
327    /// running rustc without having to save". (See #102759.)
328    pub file_loader: Option<Box<dyn FileLoader + Send + Sync>>,
329    /// The list of fluent resources, used for lints declared with
330    /// [`Diagnostic`](rustc_errors::Diagnostic) and [`LintDiagnostic`](rustc_errors::LintDiagnostic).
331    pub locale_resources: Vec<&'static str>,
332
333    pub lint_caps: FxHashMap<lint::LintId, lint::Level>,
334
335    /// This is a callback from the driver that is called when [`ParseSess`] is created.
336    pub psess_created: Option<Box<dyn FnOnce(&mut ParseSess) + Send>>,
337
338    /// This is a callback to hash otherwise untracked state used by the caller, if the
339    /// hash changes between runs the incremental cache will be cleared.
340    ///
341    /// e.g. used by Clippy to hash its config file
342    pub hash_untracked_state: Option<Box<dyn FnOnce(&Session, &mut StableHasher) + Send>>,
343
344    /// This is a callback from the driver that is called when we're registering lints;
345    /// it is called during lint loading when we have the LintStore in a non-shared state.
346    ///
347    /// Note that if you find a Some here you probably want to call that function in the new
348    /// function being registered.
349    pub register_lints: Option<Box<dyn Fn(&Session, &mut LintStore) + Send + Sync>>,
350
351    /// This is a callback from the driver that is called just after we have populated
352    /// the list of queries.
353    pub override_queries: Option<fn(&Session, &mut Providers)>,
354
355    /// An extra set of symbols to add to the symbol interner, the symbol indices
356    /// will start at [`PREDEFINED_SYMBOLS_COUNT`](rustc_span::symbol::PREDEFINED_SYMBOLS_COUNT)
357    pub extra_symbols: Vec<&'static str>,
358
359    /// This is a callback from the driver that is called to create a codegen backend.
360    ///
361    /// Has no uses within this repository, but is used by bjorn3 for "the
362    /// hotswapping branch of cg_clif" for "setting the codegen backend from a
363    /// custom driver where the custom codegen backend has arbitrary data."
364    /// (See #102759.)
365    pub make_codegen_backend:
366        Option<Box<dyn FnOnce(&config::Options) -> Box<dyn CodegenBackend> + Send>>,
367
368    /// Registry of diagnostics codes.
369    pub registry: Registry,
370
371    /// The inner atomic value is set to true when a feature marked as `internal` is
372    /// enabled. Makes it so that "please report a bug" is hidden, as ICEs with
373    /// internal features are wontfix, and they are usually the cause of the ICEs.
374    pub using_internal_features: &'static std::sync::atomic::AtomicBool,
375
376    /// All commandline args used to invoke the compiler, with @file args fully expanded.
377    /// This will only be used within debug info, e.g. in the pdb file on windows
378    /// This is mainly useful for other tools that reads that debuginfo to figure out
379    /// how to call the compiler with the same arguments.
380    pub expanded_args: Vec<String>,
381}
382
383/// Initialize jobserver before getting `jobserver::client` and `build_session`.
384pub(crate) fn initialize_checked_jobserver(early_dcx: &EarlyDiagCtxt) {
385    jobserver::initialize_checked(|err| {
386        #[allow(rustc::untranslatable_diagnostic)]
387        #[allow(rustc::diagnostic_outside_of_impl)]
388        early_dcx
389            .early_struct_warn(err)
390            .with_note("the build environment is likely misconfigured")
391            .emit()
392    });
393}
394
395// JUSTIFICATION: before session exists, only config
396#[allow(rustc::bad_opt_access)]
397pub fn run_compiler<R: Send>(config: Config, f: impl FnOnce(&Compiler) -> R + Send) -> R {
398    trace!("run_compiler");
399
400    // Set parallel mode before thread pool creation, which will create `Lock`s.
401    rustc_data_structures::sync::set_dyn_thread_safe_mode(config.opts.unstable_opts.threads > 1);
402
403    // Check jobserver before run_in_thread_pool_with_globals, which call jobserver::acquire_thread
404    let early_dcx = EarlyDiagCtxt::new(config.opts.error_format);
405    initialize_checked_jobserver(&early_dcx);
406
407    crate::callbacks::setup_callbacks();
408
409    let target = config::build_target_config(
410        &early_dcx,
411        &config.opts.target_triple,
412        config.opts.sysroot.path(),
413    );
414    let file_loader = config.file_loader.unwrap_or_else(|| Box::new(RealFileLoader));
415    let path_mapping = config.opts.file_path_mapping();
416    let hash_kind = config.opts.unstable_opts.src_hash_algorithm(&target);
417    let checksum_hash_kind = config.opts.unstable_opts.checksum_hash_algorithm();
418
419    util::run_in_thread_pool_with_globals(
420        &early_dcx,
421        config.opts.edition,
422        config.opts.unstable_opts.threads,
423        &config.extra_symbols,
424        SourceMapInputs { file_loader, path_mapping, hash_kind, checksum_hash_kind },
425        |current_gcx, jobserver_proxy| {
426            // The previous `early_dcx` can't be reused here because it doesn't
427            // impl `Send`. Creating a new one is fine.
428            let early_dcx = EarlyDiagCtxt::new(config.opts.error_format);
429
430            let codegen_backend = match config.make_codegen_backend {
431                None => util::get_codegen_backend(
432                    &early_dcx,
433                    &config.opts.sysroot,
434                    config.opts.unstable_opts.codegen_backend.as_deref(),
435                    &target,
436                ),
437                Some(make_codegen_backend) => {
438                    // N.B. `make_codegen_backend` takes precedence over
439                    // `target.default_codegen_backend`, which is ignored in this case.
440                    make_codegen_backend(&config.opts)
441                }
442            };
443
444            let temps_dir = config.opts.unstable_opts.temps_dir.as_deref().map(PathBuf::from);
445
446            let bundle = match rustc_errors::fluent_bundle(
447                &config.opts.sysroot.all_paths().collect::<Vec<_>>(),
448                config.opts.unstable_opts.translate_lang.clone(),
449                config.opts.unstable_opts.translate_additional_ftl.as_deref(),
450                config.opts.unstable_opts.translate_directionality_markers,
451            ) {
452                Ok(bundle) => bundle,
453                Err(e) => {
454                    // We can't translate anything if we failed to load translations
455                    #[allow(rustc::untranslatable_diagnostic)]
456                    early_dcx.early_fatal(format!("failed to load fluent bundle: {e}"))
457                }
458            };
459
460            let mut locale_resources = config.locale_resources;
461            locale_resources.push(codegen_backend.locale_resource());
462
463            let mut sess = rustc_session::build_session(
464                config.opts,
465                CompilerIO {
466                    input: config.input,
467                    output_dir: config.output_dir,
468                    output_file: config.output_file,
469                    temps_dir,
470                },
471                bundle,
472                config.registry,
473                locale_resources,
474                config.lint_caps,
475                target,
476                util::rustc_version_str().unwrap_or("unknown"),
477                config.ice_file,
478                config.using_internal_features,
479                config.expanded_args,
480            );
481
482            codegen_backend.init(&sess);
483
484            let cfg = parse_cfg(sess.dcx(), config.crate_cfg);
485            let mut cfg = config::build_configuration(&sess, cfg);
486            util::add_configuration(&mut cfg, &mut sess, &*codegen_backend);
487            sess.psess.config = cfg;
488
489            let mut check_cfg = parse_check_cfg(sess.dcx(), config.crate_check_cfg);
490            check_cfg.fill_well_known(&sess.target);
491            sess.psess.check_config = check_cfg;
492
493            if let Some(psess_created) = config.psess_created {
494                psess_created(&mut sess.psess);
495            }
496
497            if let Some(hash_untracked_state) = config.hash_untracked_state {
498                let mut hasher = StableHasher::new();
499                hash_untracked_state(&sess, &mut hasher);
500                sess.opts.untracked_state_hash = hasher.finish()
501            }
502
503            // Even though the session holds the lint store, we can't build the
504            // lint store until after the session exists. And we wait until now
505            // so that `register_lints` sees the fully initialized session.
506            let mut lint_store = rustc_lint::new_lint_store(sess.enable_internal_lints());
507            if let Some(register_lints) = config.register_lints.as_deref() {
508                register_lints(&sess, &mut lint_store);
509            }
510            sess.lint_store = Some(Arc::new(lint_store));
511
512            util::check_abi_required_features(&sess);
513
514            let compiler = Compiler {
515                sess,
516                codegen_backend,
517                override_queries: config.override_queries,
518                current_gcx,
519                jobserver_proxy,
520            };
521
522            // There are two paths out of `f`.
523            // - Normal exit.
524            // - Panic, e.g. triggered by `abort_if_errors` or a fatal error.
525            //
526            // We must run `finish_diagnostics` in both cases.
527            let res = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| f(&compiler)));
528
529            compiler.sess.finish_diagnostics();
530
531            // If error diagnostics have been emitted, we can't return an
532            // error directly, because the return type of this function
533            // is `R`, not `Result<R, E>`. But we need to communicate the
534            // errors' existence to the caller, otherwise the caller might
535            // mistakenly think that no errors occurred and return a zero
536            // exit code. So we abort (panic) instead, similar to if `f`
537            // had panicked.
538            if res.is_ok() {
539                compiler.sess.dcx().abort_if_errors();
540            }
541
542            // Also make sure to flush delayed bugs as if we panicked, the
543            // bugs would be flushed by the Drop impl of DiagCtxt while
544            // unwinding, which would result in an abort with
545            // "panic in a destructor during cleanup".
546            compiler.sess.dcx().flush_delayed();
547
548            let res = match res {
549                Ok(res) => res,
550                // Resume unwinding if a panic happened.
551                Err(err) => std::panic::resume_unwind(err),
552            };
553
554            let prof = compiler.sess.prof.clone();
555            prof.generic_activity("drop_compiler").run(move || drop(compiler));
556
557            res
558        },
559    )
560}
561
562pub fn try_print_query_stack(
563    dcx: DiagCtxtHandle<'_>,
564    limit_frames: Option<usize>,
565    file: Option<std::fs::File>,
566) {
567    eprintln!("query stack during panic:");
568
569    // Be careful relying on global state here: this code is called from
570    // a panic hook, which means that the global `DiagCtxt` may be in a weird
571    // state if it was responsible for triggering the panic.
572    let all_frames = ty::tls::with_context_opt(|icx| {
573        if let Some(icx) = icx {
574            ty::print::with_no_queries!(print_query_stack(
575                QueryCtxt::new(icx.tcx),
576                icx.query,
577                dcx,
578                limit_frames,
579                file,
580            ))
581        } else {
582            0
583        }
584    });
585
586    if let Some(limit_frames) = limit_frames
587        && all_frames > limit_frames
588    {
589        eprintln!(
590            "... and {} other queries... use `env RUST_BACKTRACE=1` to see the full query stack",
591            all_frames - limit_frames
592        );
593    } else {
594        eprintln!("end of query stack");
595    }
596}