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 config.opts.unstable_opts.unstable_options,
439 );
440 let file_loader = config.file_loader.unwrap_or_else(|| Box::new(RealFileLoader));
441 let path_mapping = config.opts.file_path_mapping();
442 let hash_kind = config.opts.unstable_opts.src_hash_algorithm(&target);
443 let checksum_hash_kind = config.opts.unstable_opts.checksum_hash_algorithm();
444
445 util::run_in_thread_pool_with_globals(
446 &early_dcx,
447 config.opts.edition,
448 config.opts.unstable_opts.threads,
449 &config.extra_symbols,
450 SourceMapInputs { file_loader, path_mapping, hash_kind, checksum_hash_kind },
451 |current_gcx, jobserver_proxy| {
452 let early_dcx = EarlyDiagCtxt::new(config.opts.error_format);
455
456 let codegen_backend = match config.make_codegen_backend {
457 None => util::get_codegen_backend(
458 &early_dcx,
459 &config.opts.sysroot,
460 config.opts.unstable_opts.codegen_backend.as_deref(),
461 &target,
462 ),
463 Some(make_codegen_backend) => {
464 make_codegen_backend(&config.opts, &target)
467 }
468 };
469
470 let temps_dir = config.opts.unstable_opts.temps_dir.as_deref().map(PathBuf::from);
471
472 let bundle = match rustc_errors::fluent_bundle(
473 &config.opts.sysroot.all_paths().collect::<Vec<_>>(),
474 config.opts.unstable_opts.translate_lang.clone(),
475 config.opts.unstable_opts.translate_additional_ftl.as_deref(),
476 config.opts.unstable_opts.translate_directionality_markers,
477 ) {
478 Ok(bundle) => bundle,
479 Err(e) => {
480 #[allow(rustc::untranslatable_diagnostic)]
482 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}"))
483 }
484 };
485
486 let mut locale_resources = config.locale_resources;
487 locale_resources.push(codegen_backend.locale_resource());
488
489 let mut sess = rustc_session::build_session(
490 config.opts,
491 CompilerIO {
492 input: config.input,
493 output_dir: config.output_dir,
494 output_file: config.output_file,
495 temps_dir,
496 },
497 bundle,
498 config.registry,
499 locale_resources,
500 config.lint_caps,
501 target,
502 util::rustc_version_str().unwrap_or("unknown"),
503 config.ice_file,
504 config.using_internal_features,
505 );
506
507 codegen_backend.init(&sess);
508
509 let cfg = parse_cfg(sess.dcx(), config.crate_cfg);
510 let mut cfg = config::build_configuration(&sess, cfg);
511 util::add_configuration(&mut cfg, &mut sess, &*codegen_backend);
512 sess.psess.config = cfg;
513
514 let mut check_cfg = parse_check_cfg(sess.dcx(), config.crate_check_cfg);
515 check_cfg.fill_well_known(&sess.target);
516 sess.psess.check_config = check_cfg;
517
518 if let Some(psess_created) = config.psess_created {
519 psess_created(&mut sess.psess);
520 }
521
522 if let Some(hash_untracked_state) = config.hash_untracked_state {
523 let mut hasher = StableHasher::new();
524 hash_untracked_state(&sess, &mut hasher);
525 sess.opts.untracked_state_hash = hasher.finish()
526 }
527
528 let mut lint_store = rustc_lint::new_lint_store(sess.enable_internal_lints());
532 if let Some(register_lints) = config.register_lints.as_deref() {
533 register_lints(&sess, &mut lint_store);
534 }
535 sess.lint_store = Some(Arc::new(lint_store));
536
537 util::check_abi_required_features(&sess);
538
539 let compiler = Compiler {
540 sess,
541 codegen_backend,
542 override_queries: config.override_queries,
543 current_gcx,
544 jobserver_proxy,
545 };
546
547 let res = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| f(&compiler)));
553
554 compiler.sess.finish_diagnostics();
555
556 if res.is_ok() {
564 compiler.sess.dcx().abort_if_errors();
565 }
566
567 compiler.sess.dcx().flush_delayed();
572
573 let res = match res {
574 Ok(res) => res,
575 Err(err) => std::panic::resume_unwind(err),
577 };
578
579 let prof = compiler.sess.prof.clone();
580 prof.generic_activity("drop_compiler").run(move || drop(compiler));
581
582 res
583 },
584 )
585}
586
587pub fn try_print_query_stack(
588 dcx: DiagCtxtHandle<'_>,
589 limit_frames: Option<usize>,
590 file: Option<std::fs::File>,
591) {
592 { ::std::io::_eprint(format_args!("query stack during panic:\n")); };eprintln!("query stack during panic:");
593
594 let all_frames = ty::tls::with_context_opt(|icx| {
598 if let Some(icx) = icx {
599 {
{
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(
600 QueryCtxt::new(icx.tcx),
601 icx.query,
602 dcx,
603 limit_frames,
604 file,
605 ))
606 } else {
607 0
608 }
609 });
610
611 if let Some(limit_frames) = limit_frames
612 && all_frames > limit_frames
613 {
614 {
::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!(
615 "... and {} other queries... use `env RUST_BACKTRACE=1` to see the full query stack",
616 all_frames - limit_frames
617 );
618 } else {
619 { ::std::io::_eprint(format_args!("end of query stack\n")); };eprintln!("end of query stack");
620 }
621}