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::Recovery;
19use rustc_parse::parser::attr::AllowLeadingUnsafe;
20use rustc_query_impl::QueryCtxt;
21use rustc_query_system::query::print_query_stack;
22use rustc_session::config::{self, Cfg, CheckCfg, ExpectedValues, Input, OutFileName};
23use rustc_session::parse::ParseSess;
24use rustc_session::{CompilerIO, EarlyDiagCtxt, Session, lint};
25use rustc_span::source_map::{FileLoader, RealFileLoader, SourceMapInputs};
26use rustc_span::{FileName, sym};
27use rustc_target::spec::Target;
28use tracing::trace;
29
30use crate::util;
31
32pub type Result<T> = result::Result<T, ErrorGuaranteed>;
33
34pub struct Compiler {
42 pub sess: Session,
43 pub codegen_backend: Box<dyn CodegenBackend>,
44 pub(crate) override_queries: Option<fn(&Session, &mut Providers)>,
45
46 pub(crate) current_gcx: CurrentGcx,
48
49 pub(crate) jobserver_proxy: Arc<Proxy>,
51}
52
53pub(crate) fn parse_cfg(dcx: DiagCtxtHandle<'_>, cfgs: Vec<String>) -> Cfg {
55 cfgs.into_iter()
56 .map(|s| {
57 let psess = ParseSess::emitter_with_note(
58 <[_]>::into_vec(::alloc::boxed::box_new([crate::DEFAULT_LOCALE_RESOURCE,
rustc_parse::DEFAULT_LOCALE_RESOURCE,
rustc_session::DEFAULT_LOCALE_RESOURCE]))vec![
59 crate::DEFAULT_LOCALE_RESOURCE,
60 rustc_parse::DEFAULT_LOCALE_RESOURCE,
61 rustc_session::DEFAULT_LOCALE_RESOURCE,
62 ],
63 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("this occurred on the command line: `--cfg={0}`",
s))
})format!("this occurred on the command line: `--cfg={s}`"),
64 );
65 let filename = FileName::cfg_spec_source_code(&s);
66
67 macro_rules! error {
68 ($reason: expr) => {
69 #[allow(rustc::untranslatable_diagnostic)]
70 #[allow(rustc::diagnostic_outside_of_impl)]
71 dcx.fatal(format!("invalid `--cfg` argument: `{s}` ({})", $reason));
72 };
73 }
74
75 match new_parser_from_source_str(&psess, filename, s.to_string(), StripTokens::Nothing)
76 {
77 Ok(mut parser) => {
78 parser = parser.recovery(Recovery::Forbidden);
79 match parser.parse_meta_item(AllowLeadingUnsafe::No) {
80 Ok(meta_item)
81 if parser.token == token::Eof
82 && parser.dcx().has_errors().is_none() =>
83 {
84 if meta_item.path.segments.len() != 1 {
85 #[allow(rustc :: untranslatable_diagnostic)]
#[allow(rustc :: diagnostic_outside_of_impl)]
dcx.fatal(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("invalid `--cfg` argument: `{1}` ({0})",
"argument key must be an identifier", s))
}));error!("argument key must be an identifier");
86 }
87 match &meta_item.kind {
88 MetaItemKind::List(..) => {}
89 MetaItemKind::NameValue(lit) if !lit.kind.is_str() => {
90 #[allow(rustc :: untranslatable_diagnostic)]
#[allow(rustc :: diagnostic_outside_of_impl)]
dcx.fatal(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("invalid `--cfg` argument: `{1}` ({0})",
"argument value must be a string", s))
}));error!("argument value must be a string");
91 }
92 MetaItemKind::NameValue(..) | MetaItemKind::Word => {
93 let ident = meta_item.ident().expect("multi-segment cfg key");
94
95 if ident.is_path_segment_keyword() {
96 #[allow(rustc :: untranslatable_diagnostic)]
#[allow(rustc :: diagnostic_outside_of_impl)]
dcx.fatal(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("invalid `--cfg` argument: `{1}` ({0})",
"malformed `cfg` input, expected a valid identifier", s))
}));error!(
97 "malformed `cfg` input, expected a valid identifier"
98 );
99 }
100
101 return (ident.name, meta_item.value_str());
102 }
103 }
104 }
105 Ok(..) => {}
106 Err(err) => err.cancel(),
107 }
108 }
109 Err(errs) => errs.into_iter().for_each(|err| err.cancel()),
110 };
111
112 if s.contains('=') && !s.contains("=\"") && !s.ends_with('"') {
115 #[allow(rustc :: untranslatable_diagnostic)]
#[allow(rustc :: diagnostic_outside_of_impl)]
dcx.fatal(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("invalid `--cfg` argument: `{1}` ({0})",
"expected `key` or `key=\"value\"`, ensure escaping is appropriate for your shell, try \'key=\"value\"\' or key=\\\"value\\\"",
s))
}));error!(concat!(
116 r#"expected `key` or `key="value"`, ensure escaping is appropriate"#,
117 r#" for your shell, try 'key="value"' or key=\"value\""#
118 ));
119 } else {
120 #[allow(rustc :: untranslatable_diagnostic)]
#[allow(rustc :: diagnostic_outside_of_impl)]
dcx.fatal(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("invalid `--cfg` argument: `{1}` ({0})",
r#"expected `key` or `key="value"`"#, s))
}));error!(r#"expected `key` or `key="value"`"#);
121 }
122 })
123 .collect::<Cfg>()
124}
125
126pub(crate) fn parse_check_cfg(dcx: DiagCtxtHandle<'_>, specs: Vec<String>) -> CheckCfg {
128 let exhaustive_names = !specs.is_empty();
131 let exhaustive_values = !specs.is_empty();
132 let mut check_cfg = CheckCfg { exhaustive_names, exhaustive_values, ..CheckCfg::default() };
133
134 for s in specs {
135 let psess = ParseSess::emitter_with_note(
136 <[_]>::into_vec(::alloc::boxed::box_new([crate::DEFAULT_LOCALE_RESOURCE,
rustc_parse::DEFAULT_LOCALE_RESOURCE,
rustc_session::DEFAULT_LOCALE_RESOURCE]))vec![
137 crate::DEFAULT_LOCALE_RESOURCE,
138 rustc_parse::DEFAULT_LOCALE_RESOURCE,
139 rustc_session::DEFAULT_LOCALE_RESOURCE,
140 ],
141 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("this occurred on the command line: `--check-cfg={0}`",
s))
})format!("this occurred on the command line: `--check-cfg={s}`"),
142 );
143 let filename = FileName::cfg_spec_source_code(&s);
144
145 const VISIT: &str =
146 "visit <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more details";
147
148 macro_rules! error {
149 ($reason:expr) => {
150 #[allow(rustc::untranslatable_diagnostic)]
151 #[allow(rustc::diagnostic_outside_of_impl)]
152 {
153 let mut diag =
154 dcx.struct_fatal(format!("invalid `--check-cfg` argument: `{s}`"));
155 diag.note($reason);
156 diag.note(VISIT);
157 diag.emit()
158 }
159 };
160 (in $arg:expr, $reason:expr) => {
161 #[allow(rustc::untranslatable_diagnostic)]
162 #[allow(rustc::diagnostic_outside_of_impl)]
163 {
164 let mut diag =
165 dcx.struct_fatal(format!("invalid `--check-cfg` argument: `{s}`"));
166
167 let pparg = rustc_ast_pretty::pprust::meta_list_item_to_string($arg);
168 if let Some(lit) = $arg.lit() {
169 let (lit_kind_article, lit_kind_descr) = {
170 let lit_kind = lit.as_token_lit().kind;
171 (lit_kind.article(), lit_kind.descr())
172 };
173 diag.note(format!(
174 "`{pparg}` is {lit_kind_article} {lit_kind_descr} literal"
175 ));
176 } else {
177 diag.note(format!("`{pparg}` is invalid"));
178 }
179
180 diag.note($reason);
181 diag.note(VISIT);
182 diag.emit()
183 }
184 };
185 }
186
187 let expected_error = || -> ! {
188 #[allow(rustc ::
untranslatable_diagnostic)] #[allow(rustc ::
diagnostic_outside_of_impl)] {
let mut diag =
dcx.struct_fatal(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("invalid `--check-cfg` argument: `{0}`",
s))
}));
diag.note("expected `cfg(name, values(\"value1\", \"value2\", ... \"valueN\"))`");
diag.note(VISIT);
diag.emit()
}error!("expected `cfg(name, values(\"value1\", \"value2\", ... \"valueN\"))`")
189 };
190
191 let mut parser =
192 match new_parser_from_source_str(&psess, filename, s.to_string(), StripTokens::Nothing)
193 {
194 Ok(parser) => parser.recovery(Recovery::Forbidden),
195 Err(errs) => {
196 errs.into_iter().for_each(|err| err.cancel());
197 expected_error();
198 }
199 };
200
201 let meta_item = match parser.parse_meta_item(AllowLeadingUnsafe::No) {
202 Ok(meta_item) if parser.token == token::Eof && parser.dcx().has_errors().is_none() => {
203 meta_item
204 }
205 Ok(..) => expected_error(),
206 Err(err) => {
207 err.cancel();
208 expected_error();
209 }
210 };
211
212 let Some(args) = meta_item.meta_item_list() else {
213 expected_error();
214 };
215
216 if !meta_item.has_name(sym::cfg) {
217 expected_error();
218 }
219
220 let mut names = Vec::new();
221 let mut values: FxHashSet<_> = Default::default();
222
223 let mut any_specified = false;
224 let mut values_specified = false;
225 let mut values_any_specified = false;
226
227 for arg in args {
228 if arg.is_word()
229 && let Some(ident) = arg.ident()
230 {
231 if values_specified {
232 #[allow(rustc :: untranslatable_diagnostic)]
#[allow(rustc :: diagnostic_outside_of_impl)]
{
let mut diag =
dcx.struct_fatal(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("invalid `--check-cfg` argument: `{0}`",
s))
}));
diag.note("`cfg()` names cannot be after values");
diag.note(VISIT);
diag.emit()
};error!("`cfg()` names cannot be after values");
233 }
234
235 if ident.is_path_segment_keyword() {
236 #[allow(rustc :: untranslatable_diagnostic)]
#[allow(rustc :: diagnostic_outside_of_impl)]
{
let mut diag =
dcx.struct_fatal(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("invalid `--check-cfg` argument: `{0}`",
s))
}));
diag.note("malformed `cfg` input, expected a valid identifier");
diag.note(VISIT);
diag.emit()
};error!("malformed `cfg` input, expected a valid identifier");
237 }
238
239 names.push(ident);
240 } else if let Some(boolean) = arg.boolean_literal() {
241 if values_specified {
242 #[allow(rustc :: untranslatable_diagnostic)]
#[allow(rustc :: diagnostic_outside_of_impl)]
{
let mut diag =
dcx.struct_fatal(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("invalid `--check-cfg` argument: `{0}`",
s))
}));
diag.note("`cfg()` names cannot be after values");
diag.note(VISIT);
diag.emit()
};error!("`cfg()` names cannot be after values");
243 }
244 names.push(rustc_span::Ident::new(
245 if boolean { rustc_span::kw::True } else { rustc_span::kw::False },
246 arg.span(),
247 ));
248 } else if arg.has_name(sym::any)
249 && let Some(args) = arg.meta_item_list()
250 {
251 if any_specified {
252 #[allow(rustc :: untranslatable_diagnostic)]
#[allow(rustc :: diagnostic_outside_of_impl)]
{
let mut diag =
dcx.struct_fatal(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("invalid `--check-cfg` argument: `{0}`",
s))
}));
diag.note("`any()` cannot be specified multiple times");
diag.note(VISIT);
diag.emit()
};error!("`any()` cannot be specified multiple times");
253 }
254 any_specified = true;
255 if !args.is_empty() {
256 #[allow(rustc :: untranslatable_diagnostic)]
#[allow(rustc :: diagnostic_outside_of_impl)]
{
let mut diag =
dcx.struct_fatal(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("invalid `--check-cfg` argument: `{0}`",
s))
}));
let pparg = rustc_ast_pretty::pprust::meta_list_item_to_string(arg);
if let Some(lit) = arg.lit() {
let (lit_kind_article, lit_kind_descr) =
{
let lit_kind = lit.as_token_lit().kind;
(lit_kind.article(), lit_kind.descr())
};
diag.note(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}` is {1} {2} literal",
pparg, lit_kind_article, lit_kind_descr))
}));
} else {
diag.note(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}` is invalid",
pparg))
}));
}
diag.note("`any()` takes no argument");
diag.note(VISIT);
diag.emit()
};error!(in arg, "`any()` takes no argument");
257 }
258 } else if arg.has_name(sym::values)
259 && let Some(args) = arg.meta_item_list()
260 {
261 if names.is_empty() {
262 #[allow(rustc :: untranslatable_diagnostic)]
#[allow(rustc :: diagnostic_outside_of_impl)]
{
let mut diag =
dcx.struct_fatal(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("invalid `--check-cfg` argument: `{0}`",
s))
}));
diag.note("`values()` cannot be specified before the names");
diag.note(VISIT);
diag.emit()
};error!("`values()` cannot be specified before the names");
263 } else if values_specified {
264 #[allow(rustc :: untranslatable_diagnostic)]
#[allow(rustc :: diagnostic_outside_of_impl)]
{
let mut diag =
dcx.struct_fatal(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("invalid `--check-cfg` argument: `{0}`",
s))
}));
diag.note("`values()` cannot be specified multiple times");
diag.note(VISIT);
diag.emit()
};error!("`values()` cannot be specified multiple times");
265 }
266 values_specified = true;
267
268 for arg in args {
269 if let Some(LitKind::Str(s, _)) = arg.lit().map(|lit| &lit.kind) {
270 values.insert(Some(*s));
271 } else if arg.has_name(sym::any)
272 && let Some(args) = arg.meta_item_list()
273 {
274 if values_any_specified {
275 #[allow(rustc :: untranslatable_diagnostic)]
#[allow(rustc :: diagnostic_outside_of_impl)]
{
let mut diag =
dcx.struct_fatal(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("invalid `--check-cfg` argument: `{0}`",
s))
}));
let pparg = rustc_ast_pretty::pprust::meta_list_item_to_string(arg);
if let Some(lit) = arg.lit() {
let (lit_kind_article, lit_kind_descr) =
{
let lit_kind = lit.as_token_lit().kind;
(lit_kind.article(), lit_kind.descr())
};
diag.note(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}` is {1} {2} literal",
pparg, lit_kind_article, lit_kind_descr))
}));
} else {
diag.note(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}` is invalid",
pparg))
}));
}
diag.note("`any()` in `values()` cannot be specified multiple times");
diag.note(VISIT);
diag.emit()
};error!(in arg, "`any()` in `values()` cannot be specified multiple times");
276 }
277 values_any_specified = true;
278 if !args.is_empty() {
279 #[allow(rustc :: untranslatable_diagnostic)]
#[allow(rustc :: diagnostic_outside_of_impl)]
{
let mut diag =
dcx.struct_fatal(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("invalid `--check-cfg` argument: `{0}`",
s))
}));
let pparg = rustc_ast_pretty::pprust::meta_list_item_to_string(arg);
if let Some(lit) = arg.lit() {
let (lit_kind_article, lit_kind_descr) =
{
let lit_kind = lit.as_token_lit().kind;
(lit_kind.article(), lit_kind.descr())
};
diag.note(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}` is {1} {2} literal",
pparg, lit_kind_article, lit_kind_descr))
}));
} else {
diag.note(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}` is invalid",
pparg))
}));
}
diag.note("`any()` in `values()` takes no argument");
diag.note(VISIT);
diag.emit()
};error!(in arg, "`any()` in `values()` takes no argument");
280 }
281 } else if arg.has_name(sym::none)
282 && let Some(args) = arg.meta_item_list()
283 {
284 values.insert(None);
285 if !args.is_empty() {
286 #[allow(rustc :: untranslatable_diagnostic)]
#[allow(rustc :: diagnostic_outside_of_impl)]
{
let mut diag =
dcx.struct_fatal(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("invalid `--check-cfg` argument: `{0}`",
s))
}));
let pparg = rustc_ast_pretty::pprust::meta_list_item_to_string(arg);
if let Some(lit) = arg.lit() {
let (lit_kind_article, lit_kind_descr) =
{
let lit_kind = lit.as_token_lit().kind;
(lit_kind.article(), lit_kind.descr())
};
diag.note(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}` is {1} {2} literal",
pparg, lit_kind_article, lit_kind_descr))
}));
} else {
diag.note(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}` is invalid",
pparg))
}));
}
diag.note("`none()` in `values()` takes no argument");
diag.note(VISIT);
diag.emit()
};error!(in arg, "`none()` in `values()` takes no argument");
287 }
288 } else {
289 #[allow(rustc :: untranslatable_diagnostic)]
#[allow(rustc :: diagnostic_outside_of_impl)]
{
let mut diag =
dcx.struct_fatal(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("invalid `--check-cfg` argument: `{0}`",
s))
}));
let pparg = rustc_ast_pretty::pprust::meta_list_item_to_string(arg);
if let Some(lit) = arg.lit() {
let (lit_kind_article, lit_kind_descr) =
{
let lit_kind = lit.as_token_lit().kind;
(lit_kind.article(), lit_kind.descr())
};
diag.note(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}` is {1} {2} literal",
pparg, lit_kind_article, lit_kind_descr))
}));
} else {
diag.note(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}` is invalid",
pparg))
}));
}
diag.note("`values()` arguments must be string literals, `none()` or `any()`");
diag.note(VISIT);
diag.emit()
};error!(in arg, "`values()` arguments must be string literals, `none()` or `any()`");
290 }
291 }
292 } else {
293 #[allow(rustc :: untranslatable_diagnostic)]
#[allow(rustc :: diagnostic_outside_of_impl)]
{
let mut diag =
dcx.struct_fatal(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("invalid `--check-cfg` argument: `{0}`",
s))
}));
let pparg = rustc_ast_pretty::pprust::meta_list_item_to_string(arg);
if let Some(lit) = arg.lit() {
let (lit_kind_article, lit_kind_descr) =
{
let lit_kind = lit.as_token_lit().kind;
(lit_kind.article(), lit_kind.descr())
};
diag.note(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}` is {1} {2} literal",
pparg, lit_kind_article, lit_kind_descr))
}));
} else {
diag.note(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}` is invalid",
pparg))
}));
}
diag.note("`cfg()` arguments must be simple identifiers, `any()` or `values(...)`");
diag.note(VISIT);
diag.emit()
};error!(in arg, "`cfg()` arguments must be simple identifiers, `any()` or `values(...)`");
294 }
295 }
296
297 if !values_specified && !any_specified {
298 values.insert(None);
301 } else if !values.is_empty() && values_any_specified {
302 #[allow(rustc :: untranslatable_diagnostic)]
#[allow(rustc :: diagnostic_outside_of_impl)]
{
let mut diag =
dcx.struct_fatal(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("invalid `--check-cfg` argument: `{0}`",
s))
}));
diag.note("`values()` arguments cannot specify string literals and `any()` at the same time");
diag.note(VISIT);
diag.emit()
};error!(
303 "`values()` arguments cannot specify string literals and `any()` at the same time"
304 );
305 }
306
307 if any_specified {
308 if names.is_empty() && values.is_empty() && !values_specified && !values_any_specified {
309 check_cfg.exhaustive_names = false;
310 } else {
311 #[allow(rustc :: untranslatable_diagnostic)]
#[allow(rustc :: diagnostic_outside_of_impl)]
{
let mut diag =
dcx.struct_fatal(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("invalid `--check-cfg` argument: `{0}`",
s))
}));
diag.note("`cfg(any())` can only be provided in isolation");
diag.note(VISIT);
diag.emit()
};error!("`cfg(any())` can only be provided in isolation");
312 }
313 } else {
314 for name in names {
315 check_cfg
316 .expecteds
317 .entry(name.name)
318 .and_modify(|v| match v {
319 ExpectedValues::Some(v) if !values_any_specified =>
320 {
321 #[allow(rustc::potential_query_instability)]
322 v.extend(values.clone())
323 }
324 ExpectedValues::Some(_) => *v = ExpectedValues::Any,
325 ExpectedValues::Any => {}
326 })
327 .or_insert_with(|| {
328 if values_any_specified {
329 ExpectedValues::Any
330 } else {
331 ExpectedValues::Some(values.clone())
332 }
333 });
334 }
335 }
336 }
337
338 check_cfg
339}
340
341pub struct Config {
343 pub opts: config::Options,
345
346 pub crate_cfg: Vec<String>,
348 pub crate_check_cfg: Vec<String>,
349
350 pub input: Input,
351 pub output_dir: Option<PathBuf>,
352 pub output_file: Option<OutFileName>,
353 pub ice_file: Option<PathBuf>,
354 pub file_loader: Option<Box<dyn FileLoader + Send + Sync>>,
360 pub locale_resources: Vec<&'static str>,
363
364 pub lint_caps: FxHashMap<lint::LintId, lint::Level>,
365
366 pub psess_created: Option<Box<dyn FnOnce(&mut ParseSess) + Send>>,
368
369 pub hash_untracked_state: Option<Box<dyn FnOnce(&Session, &mut StableHasher) + Send>>,
374
375 pub register_lints: Option<Box<dyn Fn(&Session, &mut LintStore) + Send + Sync>>,
381
382 pub override_queries: Option<fn(&Session, &mut Providers)>,
385
386 pub extra_symbols: Vec<&'static str>,
389
390 pub make_codegen_backend:
397 Option<Box<dyn FnOnce(&config::Options, &Target) -> Box<dyn CodegenBackend> + Send>>,
398
399 pub registry: Registry,
401
402 pub using_internal_features: &'static std::sync::atomic::AtomicBool,
406}
407
408pub(crate) fn initialize_checked_jobserver(early_dcx: &EarlyDiagCtxt) {
410 jobserver::initialize_checked(|err| {
411 #[allow(rustc::untranslatable_diagnostic)]
412 #[allow(rustc::diagnostic_outside_of_impl)]
413 early_dcx
414 .early_struct_warn(err)
415 .with_note("the build environment is likely misconfigured")
416 .emit()
417 });
418}
419
420#[allow(rustc::bad_opt_access)]
422pub fn run_compiler<R: Send>(config: Config, f: impl FnOnce(&Compiler) -> R + Send) -> R {
423 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_interface/src/interface.rs:423",
"rustc_interface::interface", ::tracing::Level::TRACE,
::tracing_core::__macro_support::Option::Some("compiler/rustc_interface/src/interface.rs"),
::tracing_core::__macro_support::Option::Some(423u32),
::tracing_core::__macro_support::Option::Some("rustc_interface::interface"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::TRACE <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::TRACE <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("run_compiler")
as &dyn Value))])
});
} else { ; }
};trace!("run_compiler");
424
425 rustc_data_structures::sync::set_dyn_thread_safe_mode(config.opts.unstable_opts.threads > 1);
427
428 let early_dcx = EarlyDiagCtxt::new(config.opts.error_format);
430 initialize_checked_jobserver(&early_dcx);
431
432 crate::callbacks::setup_callbacks();
433
434 let target = config::build_target_config(
435 &early_dcx,
436 &config.opts.target_triple,
437 config.opts.sysroot.path(),
438 );
439 let file_loader = config.file_loader.unwrap_or_else(|| Box::new(RealFileLoader));
440 let path_mapping = config.opts.file_path_mapping();
441 let hash_kind = config.opts.unstable_opts.src_hash_algorithm(&target);
442 let checksum_hash_kind = config.opts.unstable_opts.checksum_hash_algorithm();
443
444 util::run_in_thread_pool_with_globals(
445 &early_dcx,
446 config.opts.edition,
447 config.opts.unstable_opts.threads,
448 &config.extra_symbols,
449 SourceMapInputs { file_loader, path_mapping, hash_kind, checksum_hash_kind },
450 |current_gcx, jobserver_proxy| {
451 let early_dcx = EarlyDiagCtxt::new(config.opts.error_format);
454
455 let codegen_backend = match config.make_codegen_backend {
456 None => util::get_codegen_backend(
457 &early_dcx,
458 &config.opts.sysroot,
459 config.opts.unstable_opts.codegen_backend.as_deref(),
460 &target,
461 ),
462 Some(make_codegen_backend) => {
463 make_codegen_backend(&config.opts, &target)
466 }
467 };
468
469 let temps_dir = config.opts.unstable_opts.temps_dir.as_deref().map(PathBuf::from);
470
471 let bundle = match rustc_errors::fluent_bundle(
472 &config.opts.sysroot.all_paths().collect::<Vec<_>>(),
473 config.opts.unstable_opts.translate_lang.clone(),
474 config.opts.unstable_opts.translate_additional_ftl.as_deref(),
475 config.opts.unstable_opts.translate_directionality_markers,
476 ) {
477 Ok(bundle) => bundle,
478 Err(e) => {
479 #[allow(rustc::untranslatable_diagnostic)]
481 early_dcx.early_fatal(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("failed to load fluent bundle: {0}",
e))
})format!("failed to load fluent bundle: {e}"))
482 }
483 };
484
485 let mut locale_resources = config.locale_resources;
486 locale_resources.push(codegen_backend.locale_resource());
487
488 let mut sess = rustc_session::build_session(
489 config.opts,
490 CompilerIO {
491 input: config.input,
492 output_dir: config.output_dir,
493 output_file: config.output_file,
494 temps_dir,
495 },
496 bundle,
497 config.registry,
498 locale_resources,
499 config.lint_caps,
500 target,
501 util::rustc_version_str().unwrap_or("unknown"),
502 config.ice_file,
503 config.using_internal_features,
504 );
505
506 codegen_backend.init(&sess);
507
508 let cfg = parse_cfg(sess.dcx(), config.crate_cfg);
509 let mut cfg = config::build_configuration(&sess, cfg);
510 util::add_configuration(&mut cfg, &mut sess, &*codegen_backend);
511 sess.psess.config = cfg;
512
513 let mut check_cfg = parse_check_cfg(sess.dcx(), config.crate_check_cfg);
514 check_cfg.fill_well_known(&sess.target);
515 sess.psess.check_config = check_cfg;
516
517 if let Some(psess_created) = config.psess_created {
518 psess_created(&mut sess.psess);
519 }
520
521 if let Some(hash_untracked_state) = config.hash_untracked_state {
522 let mut hasher = StableHasher::new();
523 hash_untracked_state(&sess, &mut hasher);
524 sess.opts.untracked_state_hash = hasher.finish()
525 }
526
527 let mut lint_store = rustc_lint::new_lint_store(sess.enable_internal_lints());
531 if let Some(register_lints) = config.register_lints.as_deref() {
532 register_lints(&sess, &mut lint_store);
533 }
534 sess.lint_store = Some(Arc::new(lint_store));
535
536 util::check_abi_required_features(&sess);
537
538 let compiler = Compiler {
539 sess,
540 codegen_backend,
541 override_queries: config.override_queries,
542 current_gcx,
543 jobserver_proxy,
544 };
545
546 let res = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| f(&compiler)));
552
553 compiler.sess.finish_diagnostics();
554
555 if res.is_ok() {
563 compiler.sess.dcx().abort_if_errors();
564 }
565
566 compiler.sess.dcx().flush_delayed();
571
572 let res = match res {
573 Ok(res) => res,
574 Err(err) => std::panic::resume_unwind(err),
576 };
577
578 let prof = compiler.sess.prof.clone();
579 prof.generic_activity("drop_compiler").run(move || drop(compiler));
580
581 res
582 },
583 )
584}
585
586pub fn try_print_query_stack(
587 dcx: DiagCtxtHandle<'_>,
588 limit_frames: Option<usize>,
589 file: Option<std::fs::File>,
590) {
591 { ::std::io::_eprint(format_args!("query stack during panic:\n")); };eprintln!("query stack during panic:");
592
593 let all_frames = ty::tls::with_context_opt(|icx| {
597 if let Some(icx) = icx {
598 {
{
let _guard = ReducedQueriesGuard::new();
{
let _guard = ForcedImplGuard::new();
{
let _guard = NoTrimmedGuard::new();
{
let _guard = NoVisibleGuard::new();
{
let _guard = ForcedImplGuard::new();
print_query_stack(QueryCtxt::new(icx.tcx), icx.query, dcx,
limit_frames, file)
}
}
}
}
}
}ty::print::with_no_queries!(print_query_stack(
599 QueryCtxt::new(icx.tcx),
600 icx.query,
601 dcx,
602 limit_frames,
603 file,
604 ))
605 } else {
606 0
607 }
608 });
609
610 if let Some(limit_frames) = limit_frames
611 && all_frames > limit_frames
612 {
613 {
::std::io::_eprint(format_args!("... and {0} other queries... use `env RUST_BACKTRACE=1` to see the full query stack\n",
all_frames - limit_frames));
};eprintln!(
614 "... and {} other queries... use `env RUST_BACKTRACE=1` to see the full query stack",
615 all_frames - limit_frames
616 );
617 } else {
618 { ::std::io::_eprint(format_args!("end of query stack\n")); };eprintln!("end of query stack");
619 }
620}