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;
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::{self, 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
32/// Represents a compiler session. Note that every `Compiler` contains a
33/// `Session`, but `Compiler` also contains some things that cannot be in
34/// `Session`, due to `Session` being in a crate that has many fewer
35/// dependencies than this crate.
36///
37/// Can be used to run `rustc_interface` queries.
38/// Created by passing [`Config`] to [`run_compiler`].
39pub 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
46/// Converts strings provided as `--cfg [cfgspec]` into a `Cfg`.
47pub(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 the user tried to use a key="value" flag, but is missing the quotes, provide
92            // a hint about how to resolve this.
93            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
105/// Converts strings provided as `--check-cfg [specs]` into a `CheckCfg`.
106pub(crate) fn parse_check_cfg(dcx: DiagCtxtHandle<'_>, specs: Vec<String>) -> CheckCfg {
107    // If any --check-cfg is passed then exhaustive_values and exhaustive_names
108    // are enabled by default.
109    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 arg.has_name(sym::any)
208                && let Some(args) = arg.meta_item_list()
209            {
210                if any_specified {
211                    error!("`any()` cannot be specified multiple times");
212                }
213                any_specified = true;
214                if !args.is_empty() {
215                    error!(in arg, "`any()` takes no argument");
216                }
217            } else if arg.has_name(sym::values)
218                && let Some(args) = arg.meta_item_list()
219            {
220                if names.is_empty() {
221                    error!("`values()` cannot be specified before the names");
222                } else if values_specified {
223                    error!("`values()` cannot be specified multiple times");
224                }
225                values_specified = true;
226
227                for arg in args {
228                    if let Some(LitKind::Str(s, _)) = arg.lit().map(|lit| &lit.kind) {
229                        values.insert(Some(*s));
230                    } else if arg.has_name(sym::any)
231                        && let Some(args) = arg.meta_item_list()
232                    {
233                        if values_any_specified {
234                            error!(in arg, "`any()` in `values()` cannot be specified multiple times");
235                        }
236                        values_any_specified = true;
237                        if !args.is_empty() {
238                            error!(in arg, "`any()` in `values()` takes no argument");
239                        }
240                    } else if arg.has_name(sym::none)
241                        && let Some(args) = arg.meta_item_list()
242                    {
243                        values.insert(None);
244                        if !args.is_empty() {
245                            error!(in arg, "`none()` in `values()` takes no argument");
246                        }
247                    } else {
248                        error!(in arg, "`values()` arguments must be string literals, `none()` or `any()`");
249                    }
250                }
251            } else {
252                error!(in arg, "`cfg()` arguments must be simple identifiers, `any()` or `values(...)`");
253            }
254        }
255
256        if !values_specified && !any_specified {
257            // `cfg(name)` is equivalent to `cfg(name, values(none()))` so add
258            // an implicit `none()`
259            values.insert(None);
260        } else if !values.is_empty() && values_any_specified {
261            error!(
262                "`values()` arguments cannot specify string literals and `any()` at the same time"
263            );
264        }
265
266        if any_specified {
267            if names.is_empty() && values.is_empty() && !values_specified && !values_any_specified {
268                check_cfg.exhaustive_names = false;
269            } else {
270                error!("`cfg(any())` can only be provided in isolation");
271            }
272        } else {
273            for name in names {
274                check_cfg
275                    .expecteds
276                    .entry(name.name)
277                    .and_modify(|v| match v {
278                        ExpectedValues::Some(v) if !values_any_specified => {
279                            v.extend(values.clone())
280                        }
281                        ExpectedValues::Some(_) => *v = ExpectedValues::Any,
282                        ExpectedValues::Any => {}
283                    })
284                    .or_insert_with(|| {
285                        if values_any_specified {
286                            ExpectedValues::Any
287                        } else {
288                            ExpectedValues::Some(values.clone())
289                        }
290                    });
291            }
292        }
293    }
294
295    check_cfg
296}
297
298/// The compiler configuration
299pub struct Config {
300    /// Command line options
301    pub opts: config::Options,
302
303    /// Unparsed cfg! configuration in addition to the default ones.
304    pub crate_cfg: Vec<String>,
305    pub crate_check_cfg: Vec<String>,
306
307    pub input: Input,
308    pub output_dir: Option<PathBuf>,
309    pub output_file: Option<OutFileName>,
310    pub ice_file: Option<PathBuf>,
311    /// Load files from sources other than the file system.
312    ///
313    /// Has no uses within this repository, but may be used in the future by
314    /// bjorn3 for "hooking rust-analyzer's VFS into rustc at some point for
315    /// running rustc without having to save". (See #102759.)
316    pub file_loader: Option<Box<dyn FileLoader + Send + Sync>>,
317    /// The list of fluent resources, used for lints declared with
318    /// [`Diagnostic`](rustc_errors::Diagnostic) and [`LintDiagnostic`](rustc_errors::LintDiagnostic).
319    pub locale_resources: Vec<&'static str>,
320
321    pub lint_caps: FxHashMap<lint::LintId, lint::Level>,
322
323    /// This is a callback from the driver that is called when [`ParseSess`] is created.
324    pub psess_created: Option<Box<dyn FnOnce(&mut ParseSess) + Send>>,
325
326    /// This is a callback to hash otherwise untracked state used by the caller, if the
327    /// hash changes between runs the incremental cache will be cleared.
328    ///
329    /// e.g. used by Clippy to hash its config file
330    pub hash_untracked_state: Option<Box<dyn FnOnce(&Session, &mut StableHasher) + Send>>,
331
332    /// This is a callback from the driver that is called when we're registering lints;
333    /// it is called during lint loading when we have the LintStore in a non-shared state.
334    ///
335    /// Note that if you find a Some here you probably want to call that function in the new
336    /// function being registered.
337    pub register_lints: Option<Box<dyn Fn(&Session, &mut LintStore) + Send + Sync>>,
338
339    /// This is a callback from the driver that is called just after we have populated
340    /// the list of queries.
341    pub override_queries: Option<fn(&Session, &mut Providers)>,
342
343    /// This is a callback from the driver that is called to create a codegen backend.
344    ///
345    /// Has no uses within this repository, but is used by bjorn3 for "the
346    /// hotswapping branch of cg_clif" for "setting the codegen backend from a
347    /// custom driver where the custom codegen backend has arbitrary data."
348    /// (See #102759.)
349    pub make_codegen_backend:
350        Option<Box<dyn FnOnce(&config::Options) -> Box<dyn CodegenBackend> + Send>>,
351
352    /// Registry of diagnostics codes.
353    pub registry: Registry,
354
355    /// The inner atomic value is set to true when a feature marked as `internal` is
356    /// enabled. Makes it so that "please report a bug" is hidden, as ICEs with
357    /// internal features are wontfix, and they are usually the cause of the ICEs.
358    pub using_internal_features: &'static std::sync::atomic::AtomicBool,
359
360    /// All commandline args used to invoke the compiler, with @file args fully expanded.
361    /// This will only be used within debug info, e.g. in the pdb file on windows
362    /// This is mainly useful for other tools that reads that debuginfo to figure out
363    /// how to call the compiler with the same arguments.
364    pub expanded_args: Vec<String>,
365}
366
367/// Initialize jobserver before getting `jobserver::client` and `build_session`.
368pub(crate) fn initialize_checked_jobserver(early_dcx: &EarlyDiagCtxt) {
369    jobserver::initialize_checked(|err| {
370        #[allow(rustc::untranslatable_diagnostic)]
371        #[allow(rustc::diagnostic_outside_of_impl)]
372        early_dcx
373            .early_struct_warn(err)
374            .with_note("the build environment is likely misconfigured")
375            .emit()
376    });
377}
378
379// JUSTIFICATION: before session exists, only config
380#[allow(rustc::bad_opt_access)]
381pub fn run_compiler<R: Send>(config: Config, f: impl FnOnce(&Compiler) -> R + Send) -> R {
382    trace!("run_compiler");
383
384    // Set parallel mode before thread pool creation, which will create `Lock`s.
385    rustc_data_structures::sync::set_dyn_thread_safe_mode(config.opts.unstable_opts.threads > 1);
386
387    // Check jobserver before run_in_thread_pool_with_globals, which call jobserver::acquire_thread
388    let early_dcx = EarlyDiagCtxt::new(config.opts.error_format);
389    initialize_checked_jobserver(&early_dcx);
390
391    crate::callbacks::setup_callbacks();
392
393    let sysroot = filesearch::materialize_sysroot(config.opts.maybe_sysroot.clone());
394    let target = config::build_target_config(&early_dcx, &config.opts.target_triple, &sysroot);
395    let file_loader = config.file_loader.unwrap_or_else(|| Box::new(RealFileLoader));
396    let path_mapping = config.opts.file_path_mapping();
397    let hash_kind = config.opts.unstable_opts.src_hash_algorithm(&target);
398    let checksum_hash_kind = config.opts.unstable_opts.checksum_hash_algorithm();
399
400    util::run_in_thread_pool_with_globals(
401        &early_dcx,
402        config.opts.edition,
403        config.opts.unstable_opts.threads,
404        SourceMapInputs { file_loader, path_mapping, hash_kind, checksum_hash_kind },
405        |current_gcx| {
406            // The previous `early_dcx` can't be reused here because it doesn't
407            // impl `Send`. Creating a new one is fine.
408            let early_dcx = EarlyDiagCtxt::new(config.opts.error_format);
409
410            let codegen_backend = match config.make_codegen_backend {
411                None => util::get_codegen_backend(
412                    &early_dcx,
413                    &sysroot,
414                    config.opts.unstable_opts.codegen_backend.as_deref(),
415                    &target,
416                ),
417                Some(make_codegen_backend) => {
418                    // N.B. `make_codegen_backend` takes precedence over
419                    // `target.default_codegen_backend`, which is ignored in this case.
420                    make_codegen_backend(&config.opts)
421                }
422            };
423
424            let temps_dir = config.opts.unstable_opts.temps_dir.as_deref().map(PathBuf::from);
425
426            let bundle = match rustc_errors::fluent_bundle(
427                config.opts.maybe_sysroot.clone(),
428                sysroot_candidates().to_vec(),
429                config.opts.unstable_opts.translate_lang.clone(),
430                config.opts.unstable_opts.translate_additional_ftl.as_deref(),
431                config.opts.unstable_opts.translate_directionality_markers,
432            ) {
433                Ok(bundle) => bundle,
434                Err(e) => {
435                    // We can't translate anything if we failed to load translations
436                    #[allow(rustc::untranslatable_diagnostic)]
437                    early_dcx.early_fatal(format!("failed to load fluent bundle: {e}"))
438                }
439            };
440
441            let mut locale_resources = config.locale_resources;
442            locale_resources.push(codegen_backend.locale_resource());
443
444            let mut sess = rustc_session::build_session(
445                config.opts,
446                CompilerIO {
447                    input: config.input,
448                    output_dir: config.output_dir,
449                    output_file: config.output_file,
450                    temps_dir,
451                },
452                bundle,
453                config.registry,
454                locale_resources,
455                config.lint_caps,
456                target,
457                sysroot,
458                util::rustc_version_str().unwrap_or("unknown"),
459                config.ice_file,
460                config.using_internal_features,
461                config.expanded_args,
462            );
463
464            codegen_backend.init(&sess);
465
466            let cfg = parse_cfg(sess.dcx(), config.crate_cfg);
467            let mut cfg = config::build_configuration(&sess, cfg);
468            util::add_configuration(&mut cfg, &mut sess, &*codegen_backend);
469            sess.psess.config = cfg;
470
471            let mut check_cfg = parse_check_cfg(sess.dcx(), config.crate_check_cfg);
472            check_cfg.fill_well_known(&sess.target);
473            sess.psess.check_config = check_cfg;
474
475            if let Some(psess_created) = config.psess_created {
476                psess_created(&mut sess.psess);
477            }
478
479            if let Some(hash_untracked_state) = config.hash_untracked_state {
480                let mut hasher = StableHasher::new();
481                hash_untracked_state(&sess, &mut hasher);
482                sess.opts.untracked_state_hash = hasher.finish()
483            }
484
485            // Even though the session holds the lint store, we can't build the
486            // lint store until after the session exists. And we wait until now
487            // so that `register_lints` sees the fully initialized session.
488            let mut lint_store = rustc_lint::new_lint_store(sess.enable_internal_lints());
489            if let Some(register_lints) = config.register_lints.as_deref() {
490                register_lints(&sess, &mut lint_store);
491            }
492            sess.lint_store = Some(Arc::new(lint_store));
493
494            util::check_abi_required_features(&sess);
495
496            let compiler = Compiler {
497                sess,
498                codegen_backend,
499                override_queries: config.override_queries,
500                current_gcx,
501            };
502
503            // There are two paths out of `f`.
504            // - Normal exit.
505            // - Panic, e.g. triggered by `abort_if_errors` or a fatal error.
506            //
507            // We must run `finish_diagnostics` in both cases.
508            let res = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| f(&compiler)));
509
510            compiler.sess.finish_diagnostics();
511
512            // If error diagnostics have been emitted, we can't return an
513            // error directly, because the return type of this function
514            // is `R`, not `Result<R, E>`. But we need to communicate the
515            // errors' existence to the caller, otherwise the caller might
516            // mistakenly think that no errors occurred and return a zero
517            // exit code. So we abort (panic) instead, similar to if `f`
518            // had panicked.
519            if res.is_ok() {
520                compiler.sess.dcx().abort_if_errors();
521            }
522
523            // Also make sure to flush delayed bugs as if we panicked, the
524            // bugs would be flushed by the Drop impl of DiagCtxt while
525            // unwinding, which would result in an abort with
526            // "panic in a destructor during cleanup".
527            compiler.sess.dcx().flush_delayed();
528
529            let res = match res {
530                Ok(res) => res,
531                // Resume unwinding if a panic happened.
532                Err(err) => std::panic::resume_unwind(err),
533            };
534
535            let prof = compiler.sess.prof.clone();
536            prof.generic_activity("drop_compiler").run(move || drop(compiler));
537
538            res
539        },
540    )
541}
542
543pub fn try_print_query_stack(
544    dcx: DiagCtxtHandle<'_>,
545    limit_frames: Option<usize>,
546    file: Option<std::fs::File>,
547) {
548    eprintln!("query stack during panic:");
549
550    // Be careful relying on global state here: this code is called from
551    // a panic hook, which means that the global `DiagCtxt` may be in a weird
552    // state if it was responsible for triggering the panic.
553    let all_frames = ty::tls::with_context_opt(|icx| {
554        if let Some(icx) = icx {
555            ty::print::with_no_queries!(print_query_stack(
556                QueryCtxt::new(icx.tcx),
557                icx.query,
558                dcx,
559                limit_frames,
560                file,
561            ))
562        } else {
563            0
564        }
565    });
566
567    if let Some(limit_frames) = limit_frames
568        && all_frames > limit_frames
569    {
570        eprintln!(
571            "... and {} other queries... use `env RUST_BACKTRACE=1` to see the full query stack",
572            all_frames - limit_frames
573        );
574    } else {
575        eprintln!("end of query stack");
576    }
577}