]> git.proxmox.com Git - rustc.git/blob - src/librustc_session/session.rs
New upstream version 1.43.0+dfsg1
[rustc.git] / src / librustc_session / session.rs
1 use crate::code_stats::CodeStats;
2 pub use crate::code_stats::{DataTypeKind, FieldInfo, SizeKind, VariantInfo};
3
4 use crate::cgu_reuse_tracker::CguReuseTracker;
5 use rustc_data_structures::fingerprint::Fingerprint;
6 use rustc_data_structures::fx::{FxHashMap, FxHashSet};
7
8 use crate::config::{self, OutputType, PrintRequest, Sanitizer, SwitchWithOptPath};
9 use crate::filesearch;
10 use crate::lint;
11 use crate::search_paths::{PathKind, SearchPath};
12 use rustc_data_structures::profiling::duration_to_secs_str;
13 use rustc_errors::ErrorReported;
14
15 use rustc_data_structures::base_n;
16 use rustc_data_structures::impl_stable_hash_via_hash;
17 use rustc_data_structures::sync::{
18 self, AtomicU64, AtomicUsize, Lock, Lrc, Once, OneThread, Ordering, Ordering::SeqCst,
19 };
20
21 use crate::parse::ParseSess;
22 use rustc_errors::annotate_snippet_emitter_writer::AnnotateSnippetEmitterWriter;
23 use rustc_errors::emitter::HumanReadableErrorType;
24 use rustc_errors::emitter::{Emitter, EmitterWriter};
25 use rustc_errors::json::JsonEmitter;
26 use rustc_errors::{Applicability, DiagnosticBuilder, DiagnosticId};
27 use rustc_span::edition::Edition;
28 use rustc_span::source_map;
29 use rustc_span::{MultiSpan, Span};
30
31 use rustc_data_structures::flock;
32 use rustc_data_structures::jobserver::{self, Client};
33 use rustc_data_structures::profiling::{SelfProfiler, SelfProfilerRef};
34 use rustc_target::spec::{PanicStrategy, RelroLevel, Target, TargetTriple};
35
36 use std::cell::{self, RefCell};
37 use std::env;
38 use std::fmt;
39 use std::io::Write;
40 use std::num::NonZeroU32;
41 use std::path::PathBuf;
42 use std::sync::Arc;
43 use std::time::Duration;
44
45 pub struct OptimizationFuel {
46 /// If `-zfuel=crate=n` is specified, initially set to `n`, otherwise `0`.
47 remaining: u64,
48 /// We're rejecting all further optimizations.
49 out_of_fuel: bool,
50 }
51
52 /// Represents the data associated with a compilation
53 /// session for a single crate.
54 pub struct Session {
55 pub target: config::Config,
56 pub host: Target,
57 pub opts: config::Options,
58 pub host_tlib_path: SearchPath,
59 /// `None` if the host and target are the same.
60 pub target_tlib_path: Option<SearchPath>,
61 pub parse_sess: ParseSess,
62 pub sysroot: PathBuf,
63 /// The name of the root source file of the crate, in the local file system.
64 /// `None` means that there is no source file.
65 pub local_crate_source_file: Option<PathBuf>,
66 /// The directory the compiler has been executed in plus a flag indicating
67 /// if the value stored here has been affected by path remapping.
68 pub working_dir: (PathBuf, bool),
69
70 /// Set of `(DiagnosticId, Option<Span>, message)` tuples tracking
71 /// (sub)diagnostics that have been set once, but should not be set again,
72 /// in order to avoid redundantly verbose output (Issue #24690, #44953).
73 pub one_time_diagnostics: Lock<FxHashSet<(DiagnosticMessageId, Option<Span>, String)>>,
74 pub crate_types: Once<Vec<config::CrateType>>,
75 /// The `crate_disambiguator` is constructed out of all the `-C metadata`
76 /// arguments passed to the compiler. Its value together with the crate-name
77 /// forms a unique global identifier for the crate. It is used to allow
78 /// multiple crates with the same name to coexist. See the
79 /// `rustc_codegen_llvm::back::symbol_names` module for more information.
80 pub crate_disambiguator: Once<CrateDisambiguator>,
81
82 features: Once<rustc_feature::Features>,
83
84 /// The maximum recursion limit for potentially infinitely recursive
85 /// operations such as auto-dereference and monomorphization.
86 pub recursion_limit: Once<usize>,
87
88 /// The maximum length of types during monomorphization.
89 pub type_length_limit: Once<usize>,
90
91 /// The maximum blocks a const expression can evaluate.
92 pub const_eval_limit: Once<usize>,
93
94 /// Map from imported macro spans (which consist of
95 /// the localized span for the macro body) to the
96 /// macro name and definition span in the source crate.
97 pub imported_macro_spans: OneThread<RefCell<FxHashMap<Span, (String, Span)>>>,
98
99 incr_comp_session: OneThread<RefCell<IncrCompSession>>,
100 /// Used for incremental compilation tests. Will only be populated if
101 /// `-Zquery-dep-graph` is specified.
102 pub cgu_reuse_tracker: CguReuseTracker,
103
104 /// Used by `-Z self-profile`.
105 pub prof: SelfProfilerRef,
106
107 /// Some measurements that are being gathered during compilation.
108 pub perf_stats: PerfStats,
109
110 /// Data about code being compiled, gathered during compilation.
111 pub code_stats: CodeStats,
112
113 /// If `-zfuel=crate=n` is specified, `Some(crate)`.
114 optimization_fuel_crate: Option<String>,
115
116 /// Tracks fuel info if `-zfuel=crate=n` is specified.
117 optimization_fuel: Lock<OptimizationFuel>,
118
119 // The next two are public because the driver needs to read them.
120 /// If `-zprint-fuel=crate`, `Some(crate)`.
121 pub print_fuel_crate: Option<String>,
122 /// Always set to zero and incremented so that we can print fuel expended by a crate.
123 pub print_fuel: AtomicU64,
124
125 /// Loaded up early on in the initialization of this `Session` to avoid
126 /// false positives about a job server in our environment.
127 pub jobserver: Client,
128
129 /// Cap lint level specified by a driver specifically.
130 pub driver_lint_caps: FxHashMap<lint::LintId, lint::Level>,
131
132 /// `Span`s of trait methods that weren't found to avoid emitting object safety errors
133 pub trait_methods_not_found: Lock<FxHashSet<Span>>,
134
135 /// Mapping from ident span to path span for paths that don't exist as written, but that
136 /// exist under `std`. For example, wrote `str::from_utf8` instead of `std::str::from_utf8`.
137 pub confused_type_with_std_module: Lock<FxHashMap<Span, Span>>,
138
139 /// Path for libraries that will take preference over libraries shipped by Rust.
140 /// Used by windows-gnu targets to priortize system mingw-w64 libraries.
141 pub system_library_path: OneThread<RefCell<Option<Option<PathBuf>>>>,
142 }
143
144 pub struct PerfStats {
145 /// The accumulated time spent on computing symbol hashes.
146 pub symbol_hash_time: Lock<Duration>,
147 /// The accumulated time spent decoding def path tables from metadata.
148 pub decode_def_path_tables_time: Lock<Duration>,
149 /// Total number of values canonicalized queries constructed.
150 pub queries_canonicalized: AtomicUsize,
151 /// Number of times this query is invoked.
152 pub normalize_ty_after_erasing_regions: AtomicUsize,
153 /// Number of times this query is invoked.
154 pub normalize_projection_ty: AtomicUsize,
155 }
156
157 /// Enum to support dispatch of one-time diagnostics (in `Session.diag_once`).
158 enum DiagnosticBuilderMethod {
159 Note,
160 SpanNote,
161 SpanSuggestion(String), // suggestion
162 // Add more variants as needed to support one-time diagnostics.
163 }
164
165 /// Diagnostic message ID, used by `Session.one_time_diagnostics` to avoid
166 /// emitting the same message more than once.
167 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
168 pub enum DiagnosticMessageId {
169 ErrorId(u16), // EXXXX error code as integer
170 LintId(lint::LintId),
171 StabilityId(Option<NonZeroU32>), // issue number
172 }
173
174 impl From<&'static lint::Lint> for DiagnosticMessageId {
175 fn from(lint: &'static lint::Lint) -> Self {
176 DiagnosticMessageId::LintId(lint::LintId::of(lint))
177 }
178 }
179
180 impl Session {
181 pub fn local_crate_disambiguator(&self) -> CrateDisambiguator {
182 *self.crate_disambiguator.get()
183 }
184
185 pub fn struct_span_warn<S: Into<MultiSpan>>(&self, sp: S, msg: &str) -> DiagnosticBuilder<'_> {
186 self.diagnostic().struct_span_warn(sp, msg)
187 }
188 pub fn struct_span_warn_with_code<S: Into<MultiSpan>>(
189 &self,
190 sp: S,
191 msg: &str,
192 code: DiagnosticId,
193 ) -> DiagnosticBuilder<'_> {
194 self.diagnostic().struct_span_warn_with_code(sp, msg, code)
195 }
196 pub fn struct_warn(&self, msg: &str) -> DiagnosticBuilder<'_> {
197 self.diagnostic().struct_warn(msg)
198 }
199 pub fn struct_span_err<S: Into<MultiSpan>>(&self, sp: S, msg: &str) -> DiagnosticBuilder<'_> {
200 self.diagnostic().struct_span_err(sp, msg)
201 }
202 pub fn struct_span_err_with_code<S: Into<MultiSpan>>(
203 &self,
204 sp: S,
205 msg: &str,
206 code: DiagnosticId,
207 ) -> DiagnosticBuilder<'_> {
208 self.diagnostic().struct_span_err_with_code(sp, msg, code)
209 }
210 // FIXME: This method should be removed (every error should have an associated error code).
211 pub fn struct_err(&self, msg: &str) -> DiagnosticBuilder<'_> {
212 self.diagnostic().struct_err(msg)
213 }
214 pub fn struct_err_with_code(&self, msg: &str, code: DiagnosticId) -> DiagnosticBuilder<'_> {
215 self.diagnostic().struct_err_with_code(msg, code)
216 }
217 pub fn struct_span_fatal<S: Into<MultiSpan>>(&self, sp: S, msg: &str) -> DiagnosticBuilder<'_> {
218 self.diagnostic().struct_span_fatal(sp, msg)
219 }
220 pub fn struct_span_fatal_with_code<S: Into<MultiSpan>>(
221 &self,
222 sp: S,
223 msg: &str,
224 code: DiagnosticId,
225 ) -> DiagnosticBuilder<'_> {
226 self.diagnostic().struct_span_fatal_with_code(sp, msg, code)
227 }
228 pub fn struct_fatal(&self, msg: &str) -> DiagnosticBuilder<'_> {
229 self.diagnostic().struct_fatal(msg)
230 }
231
232 pub fn span_fatal<S: Into<MultiSpan>>(&self, sp: S, msg: &str) -> ! {
233 self.diagnostic().span_fatal(sp, msg).raise()
234 }
235 pub fn span_fatal_with_code<S: Into<MultiSpan>>(
236 &self,
237 sp: S,
238 msg: &str,
239 code: DiagnosticId,
240 ) -> ! {
241 self.diagnostic().span_fatal_with_code(sp, msg, code).raise()
242 }
243 pub fn fatal(&self, msg: &str) -> ! {
244 self.diagnostic().fatal(msg).raise()
245 }
246 pub fn span_err_or_warn<S: Into<MultiSpan>>(&self, is_warning: bool, sp: S, msg: &str) {
247 if is_warning {
248 self.span_warn(sp, msg);
249 } else {
250 self.span_err(sp, msg);
251 }
252 }
253 pub fn span_err<S: Into<MultiSpan>>(&self, sp: S, msg: &str) {
254 self.diagnostic().span_err(sp, msg)
255 }
256 pub fn span_err_with_code<S: Into<MultiSpan>>(&self, sp: S, msg: &str, code: DiagnosticId) {
257 self.diagnostic().span_err_with_code(sp, &msg, code)
258 }
259 pub fn err(&self, msg: &str) {
260 self.diagnostic().err(msg)
261 }
262 pub fn err_count(&self) -> usize {
263 self.diagnostic().err_count()
264 }
265 pub fn has_errors(&self) -> bool {
266 self.diagnostic().has_errors()
267 }
268 pub fn has_errors_or_delayed_span_bugs(&self) -> bool {
269 self.diagnostic().has_errors_or_delayed_span_bugs()
270 }
271 pub fn abort_if_errors(&self) {
272 self.diagnostic().abort_if_errors();
273 }
274 pub fn compile_status(&self) -> Result<(), ErrorReported> {
275 if self.has_errors() {
276 self.diagnostic().emit_stashed_diagnostics();
277 Err(ErrorReported)
278 } else {
279 Ok(())
280 }
281 }
282 // FIXME(matthewjasper) Remove this method, it should never be needed.
283 pub fn track_errors<F, T>(&self, f: F) -> Result<T, ErrorReported>
284 where
285 F: FnOnce() -> T,
286 {
287 let old_count = self.err_count();
288 let result = f();
289 let errors = self.err_count() - old_count;
290 if errors == 0 { Ok(result) } else { Err(ErrorReported) }
291 }
292 pub fn span_warn<S: Into<MultiSpan>>(&self, sp: S, msg: &str) {
293 self.diagnostic().span_warn(sp, msg)
294 }
295 pub fn span_warn_with_code<S: Into<MultiSpan>>(&self, sp: S, msg: &str, code: DiagnosticId) {
296 self.diagnostic().span_warn_with_code(sp, msg, code)
297 }
298 pub fn warn(&self, msg: &str) {
299 self.diagnostic().warn(msg)
300 }
301 pub fn opt_span_warn<S: Into<MultiSpan>>(&self, opt_sp: Option<S>, msg: &str) {
302 match opt_sp {
303 Some(sp) => self.span_warn(sp, msg),
304 None => self.warn(msg),
305 }
306 }
307 /// Delay a span_bug() call until abort_if_errors()
308 pub fn delay_span_bug<S: Into<MultiSpan>>(&self, sp: S, msg: &str) {
309 self.diagnostic().delay_span_bug(sp, msg)
310 }
311 pub fn note_without_error(&self, msg: &str) {
312 self.diagnostic().note_without_error(msg)
313 }
314 pub fn span_note_without_error<S: Into<MultiSpan>>(&self, sp: S, msg: &str) {
315 self.diagnostic().span_note_without_error(sp, msg)
316 }
317
318 pub fn diagnostic(&self) -> &rustc_errors::Handler {
319 &self.parse_sess.span_diagnostic
320 }
321
322 /// Analogous to calling methods on the given `DiagnosticBuilder`, but
323 /// deduplicates on lint ID, span (if any), and message for this `Session`
324 fn diag_once<'a, 'b>(
325 &'a self,
326 diag_builder: &'b mut DiagnosticBuilder<'a>,
327 method: DiagnosticBuilderMethod,
328 msg_id: DiagnosticMessageId,
329 message: &str,
330 span_maybe: Option<Span>,
331 ) {
332 let id_span_message = (msg_id, span_maybe, message.to_owned());
333 let fresh = self.one_time_diagnostics.borrow_mut().insert(id_span_message);
334 if fresh {
335 match method {
336 DiagnosticBuilderMethod::Note => {
337 diag_builder.note(message);
338 }
339 DiagnosticBuilderMethod::SpanNote => {
340 let span = span_maybe.expect("`span_note` needs a span");
341 diag_builder.span_note(span, message);
342 }
343 DiagnosticBuilderMethod::SpanSuggestion(suggestion) => {
344 let span = span_maybe.expect("`span_suggestion_*` needs a span");
345 diag_builder.span_suggestion(
346 span,
347 message,
348 suggestion,
349 Applicability::Unspecified,
350 );
351 }
352 }
353 }
354 }
355
356 pub fn diag_span_note_once<'a, 'b>(
357 &'a self,
358 diag_builder: &'b mut DiagnosticBuilder<'a>,
359 msg_id: DiagnosticMessageId,
360 span: Span,
361 message: &str,
362 ) {
363 self.diag_once(
364 diag_builder,
365 DiagnosticBuilderMethod::SpanNote,
366 msg_id,
367 message,
368 Some(span),
369 );
370 }
371
372 pub fn diag_note_once<'a, 'b>(
373 &'a self,
374 diag_builder: &'b mut DiagnosticBuilder<'a>,
375 msg_id: DiagnosticMessageId,
376 message: &str,
377 ) {
378 self.diag_once(diag_builder, DiagnosticBuilderMethod::Note, msg_id, message, None);
379 }
380
381 pub fn diag_span_suggestion_once<'a, 'b>(
382 &'a self,
383 diag_builder: &'b mut DiagnosticBuilder<'a>,
384 msg_id: DiagnosticMessageId,
385 span: Span,
386 message: &str,
387 suggestion: String,
388 ) {
389 self.diag_once(
390 diag_builder,
391 DiagnosticBuilderMethod::SpanSuggestion(suggestion),
392 msg_id,
393 message,
394 Some(span),
395 );
396 }
397
398 #[inline]
399 pub fn source_map(&self) -> &source_map::SourceMap {
400 self.parse_sess.source_map()
401 }
402 pub fn verbose(&self) -> bool {
403 self.opts.debugging_opts.verbose
404 }
405 pub fn time_passes(&self) -> bool {
406 self.opts.debugging_opts.time_passes || self.opts.debugging_opts.time
407 }
408 pub fn instrument_mcount(&self) -> bool {
409 self.opts.debugging_opts.instrument_mcount
410 }
411 pub fn time_llvm_passes(&self) -> bool {
412 self.opts.debugging_opts.time_llvm_passes
413 }
414 pub fn meta_stats(&self) -> bool {
415 self.opts.debugging_opts.meta_stats
416 }
417 pub fn asm_comments(&self) -> bool {
418 self.opts.debugging_opts.asm_comments
419 }
420 pub fn verify_llvm_ir(&self) -> bool {
421 self.opts.debugging_opts.verify_llvm_ir || cfg!(always_verify_llvm_ir)
422 }
423 pub fn borrowck_stats(&self) -> bool {
424 self.opts.debugging_opts.borrowck_stats
425 }
426 pub fn print_llvm_passes(&self) -> bool {
427 self.opts.debugging_opts.print_llvm_passes
428 }
429 pub fn binary_dep_depinfo(&self) -> bool {
430 self.opts.debugging_opts.binary_dep_depinfo
431 }
432
433 /// Gets the features enabled for the current compilation session.
434 /// DO NOT USE THIS METHOD if there is a TyCtxt available, as it circumvents
435 /// dependency tracking. Use tcx.features() instead.
436 #[inline]
437 pub fn features_untracked(&self) -> &rustc_feature::Features {
438 self.features.get()
439 }
440
441 pub fn init_features(&self, features: rustc_feature::Features) {
442 self.features.set(features);
443 }
444
445 /// Calculates the flavor of LTO to use for this compilation.
446 pub fn lto(&self) -> config::Lto {
447 // If our target has codegen requirements ignore the command line
448 if self.target.target.options.requires_lto {
449 return config::Lto::Fat;
450 }
451
452 // If the user specified something, return that. If they only said `-C
453 // lto` and we've for whatever reason forced off ThinLTO via the CLI,
454 // then ensure we can't use a ThinLTO.
455 match self.opts.cg.lto {
456 config::LtoCli::Unspecified => {
457 // The compiler was invoked without the `-Clto` flag. Fall
458 // through to the default handling
459 }
460 config::LtoCli::No => {
461 // The user explicitly opted out of any kind of LTO
462 return config::Lto::No;
463 }
464 config::LtoCli::Yes | config::LtoCli::Fat | config::LtoCli::NoParam => {
465 // All of these mean fat LTO
466 return config::Lto::Fat;
467 }
468 config::LtoCli::Thin => {
469 return if self.opts.cli_forced_thinlto_off {
470 config::Lto::Fat
471 } else {
472 config::Lto::Thin
473 };
474 }
475 }
476
477 // Ok at this point the target doesn't require anything and the user
478 // hasn't asked for anything. Our next decision is whether or not
479 // we enable "auto" ThinLTO where we use multiple codegen units and
480 // then do ThinLTO over those codegen units. The logic below will
481 // either return `No` or `ThinLocal`.
482
483 // If processing command line options determined that we're incompatible
484 // with ThinLTO (e.g., `-C lto --emit llvm-ir`) then return that option.
485 if self.opts.cli_forced_thinlto_off {
486 return config::Lto::No;
487 }
488
489 // If `-Z thinlto` specified process that, but note that this is mostly
490 // a deprecated option now that `-C lto=thin` exists.
491 if let Some(enabled) = self.opts.debugging_opts.thinlto {
492 if enabled {
493 return config::Lto::ThinLocal;
494 } else {
495 return config::Lto::No;
496 }
497 }
498
499 // If there's only one codegen unit and LTO isn't enabled then there's
500 // no need for ThinLTO so just return false.
501 if self.codegen_units() == 1 {
502 return config::Lto::No;
503 }
504
505 // Now we're in "defaults" territory. By default we enable ThinLTO for
506 // optimized compiles (anything greater than O0).
507 match self.opts.optimize {
508 config::OptLevel::No => config::Lto::No,
509 _ => config::Lto::ThinLocal,
510 }
511 }
512
513 /// Returns the panic strategy for this compile session. If the user explicitly selected one
514 /// using '-C panic', use that, otherwise use the panic strategy defined by the target.
515 pub fn panic_strategy(&self) -> PanicStrategy {
516 self.opts.cg.panic.unwrap_or(self.target.target.options.panic_strategy)
517 }
518 pub fn fewer_names(&self) -> bool {
519 let more_names = self.opts.output_types.contains_key(&OutputType::LlvmAssembly)
520 || self.opts.output_types.contains_key(&OutputType::Bitcode);
521
522 // Address sanitizer and memory sanitizer use alloca name when reporting an issue.
523 let more_names = match self.opts.debugging_opts.sanitizer {
524 Some(Sanitizer::Address) => true,
525 Some(Sanitizer::Memory) => true,
526 _ => more_names,
527 };
528
529 self.opts.debugging_opts.fewer_names || !more_names
530 }
531
532 pub fn no_landing_pads(&self) -> bool {
533 self.opts.debugging_opts.no_landing_pads || self.panic_strategy() == PanicStrategy::Abort
534 }
535 pub fn unstable_options(&self) -> bool {
536 self.opts.debugging_opts.unstable_options
537 }
538 pub fn overflow_checks(&self) -> bool {
539 self.opts
540 .cg
541 .overflow_checks
542 .or(self.opts.debugging_opts.force_overflow_checks)
543 .unwrap_or(self.opts.debug_assertions)
544 }
545
546 pub fn crt_static(&self) -> bool {
547 // If the target does not opt in to crt-static support, use its default.
548 if self.target.target.options.crt_static_respected {
549 self.crt_static_feature()
550 } else {
551 self.target.target.options.crt_static_default
552 }
553 }
554
555 pub fn crt_static_feature(&self) -> bool {
556 let requested_features = self.opts.cg.target_feature.split(',');
557 let found_negative = requested_features.clone().any(|r| r == "-crt-static");
558 let found_positive = requested_features.clone().any(|r| r == "+crt-static");
559
560 // If the target we're compiling for requests a static crt by default,
561 // then see if the `-crt-static` feature was passed to disable that.
562 // Otherwise if we don't have a static crt by default then see if the
563 // `+crt-static` feature was passed.
564 if self.target.target.options.crt_static_default { !found_negative } else { found_positive }
565 }
566
567 pub fn must_not_eliminate_frame_pointers(&self) -> bool {
568 // "mcount" function relies on stack pointer.
569 // See <https://sourceware.org/binutils/docs/gprof/Implementation.html>.
570 if self.instrument_mcount() {
571 true
572 } else if let Some(x) = self.opts.cg.force_frame_pointers {
573 x
574 } else {
575 !self.target.target.options.eliminate_frame_pointer
576 }
577 }
578
579 /// Returns the symbol name for the registrar function,
580 /// given the crate `Svh` and the function `DefIndex`.
581 pub fn generate_plugin_registrar_symbol(&self, disambiguator: CrateDisambiguator) -> String {
582 format!("__rustc_plugin_registrar_{}__", disambiguator.to_fingerprint().to_hex())
583 }
584
585 pub fn generate_proc_macro_decls_symbol(&self, disambiguator: CrateDisambiguator) -> String {
586 format!("__rustc_proc_macro_decls_{}__", disambiguator.to_fingerprint().to_hex())
587 }
588
589 pub fn target_filesearch(&self, kind: PathKind) -> filesearch::FileSearch<'_> {
590 filesearch::FileSearch::new(
591 &self.sysroot,
592 self.opts.target_triple.triple(),
593 &self.opts.search_paths,
594 // `target_tlib_path == None` means it's the same as `host_tlib_path`.
595 self.target_tlib_path.as_ref().unwrap_or(&self.host_tlib_path),
596 kind,
597 )
598 }
599 pub fn host_filesearch(&self, kind: PathKind) -> filesearch::FileSearch<'_> {
600 filesearch::FileSearch::new(
601 &self.sysroot,
602 config::host_triple(),
603 &self.opts.search_paths,
604 &self.host_tlib_path,
605 kind,
606 )
607 }
608
609 pub fn set_incr_session_load_dep_graph(&self, load: bool) {
610 let mut incr_comp_session = self.incr_comp_session.borrow_mut();
611
612 if let IncrCompSession::Active { ref mut load_dep_graph, .. } = *incr_comp_session {
613 *load_dep_graph = load;
614 }
615 }
616
617 pub fn incr_session_load_dep_graph(&self) -> bool {
618 let incr_comp_session = self.incr_comp_session.borrow();
619 match *incr_comp_session {
620 IncrCompSession::Active { load_dep_graph, .. } => load_dep_graph,
621 _ => false,
622 }
623 }
624
625 pub fn init_incr_comp_session(
626 &self,
627 session_dir: PathBuf,
628 lock_file: flock::Lock,
629 load_dep_graph: bool,
630 ) {
631 let mut incr_comp_session = self.incr_comp_session.borrow_mut();
632
633 if let IncrCompSession::NotInitialized = *incr_comp_session {
634 } else {
635 panic!("Trying to initialize IncrCompSession `{:?}`", *incr_comp_session)
636 }
637
638 *incr_comp_session =
639 IncrCompSession::Active { session_directory: session_dir, lock_file, load_dep_graph };
640 }
641
642 pub fn finalize_incr_comp_session(&self, new_directory_path: PathBuf) {
643 let mut incr_comp_session = self.incr_comp_session.borrow_mut();
644
645 if let IncrCompSession::Active { .. } = *incr_comp_session {
646 } else {
647 panic!("trying to finalize `IncrCompSession` `{:?}`", *incr_comp_session);
648 }
649
650 // Note: this will also drop the lock file, thus unlocking the directory.
651 *incr_comp_session = IncrCompSession::Finalized { session_directory: new_directory_path };
652 }
653
654 pub fn mark_incr_comp_session_as_invalid(&self) {
655 let mut incr_comp_session = self.incr_comp_session.borrow_mut();
656
657 let session_directory = match *incr_comp_session {
658 IncrCompSession::Active { ref session_directory, .. } => session_directory.clone(),
659 IncrCompSession::InvalidBecauseOfErrors { .. } => return,
660 _ => panic!("trying to invalidate `IncrCompSession` `{:?}`", *incr_comp_session),
661 };
662
663 // Note: this will also drop the lock file, thus unlocking the directory.
664 *incr_comp_session = IncrCompSession::InvalidBecauseOfErrors { session_directory };
665 }
666
667 pub fn incr_comp_session_dir(&self) -> cell::Ref<'_, PathBuf> {
668 let incr_comp_session = self.incr_comp_session.borrow();
669 cell::Ref::map(incr_comp_session, |incr_comp_session| match *incr_comp_session {
670 IncrCompSession::NotInitialized => panic!(
671 "trying to get session directory from `IncrCompSession`: {:?}",
672 *incr_comp_session,
673 ),
674 IncrCompSession::Active { ref session_directory, .. }
675 | IncrCompSession::Finalized { ref session_directory }
676 | IncrCompSession::InvalidBecauseOfErrors { ref session_directory } => {
677 session_directory
678 }
679 })
680 }
681
682 pub fn incr_comp_session_dir_opt(&self) -> Option<cell::Ref<'_, PathBuf>> {
683 self.opts.incremental.as_ref().map(|_| self.incr_comp_session_dir())
684 }
685
686 pub fn print_perf_stats(&self) {
687 println!(
688 "Total time spent computing symbol hashes: {}",
689 duration_to_secs_str(*self.perf_stats.symbol_hash_time.lock())
690 );
691 println!(
692 "Total time spent decoding DefPath tables: {}",
693 duration_to_secs_str(*self.perf_stats.decode_def_path_tables_time.lock())
694 );
695 println!(
696 "Total queries canonicalized: {}",
697 self.perf_stats.queries_canonicalized.load(Ordering::Relaxed)
698 );
699 println!(
700 "normalize_ty_after_erasing_regions: {}",
701 self.perf_stats.normalize_ty_after_erasing_regions.load(Ordering::Relaxed)
702 );
703 println!(
704 "normalize_projection_ty: {}",
705 self.perf_stats.normalize_projection_ty.load(Ordering::Relaxed)
706 );
707 }
708
709 /// We want to know if we're allowed to do an optimization for crate foo from -z fuel=foo=n.
710 /// This expends fuel if applicable, and records fuel if applicable.
711 pub fn consider_optimizing<T: Fn() -> String>(&self, crate_name: &str, msg: T) -> bool {
712 let mut ret = true;
713 if let Some(ref c) = self.optimization_fuel_crate {
714 if c == crate_name {
715 assert_eq!(self.threads(), 1);
716 let mut fuel = self.optimization_fuel.lock();
717 ret = fuel.remaining != 0;
718 if fuel.remaining == 0 && !fuel.out_of_fuel {
719 eprintln!("optimization-fuel-exhausted: {}", msg());
720 fuel.out_of_fuel = true;
721 } else if fuel.remaining > 0 {
722 fuel.remaining -= 1;
723 }
724 }
725 }
726 if let Some(ref c) = self.print_fuel_crate {
727 if c == crate_name {
728 assert_eq!(self.threads(), 1);
729 self.print_fuel.fetch_add(1, SeqCst);
730 }
731 }
732 ret
733 }
734
735 /// Returns the number of query threads that should be used for this
736 /// compilation
737 pub fn threads(&self) -> usize {
738 self.opts.debugging_opts.threads
739 }
740
741 /// Returns the number of codegen units that should be used for this
742 /// compilation
743 pub fn codegen_units(&self) -> usize {
744 if let Some(n) = self.opts.cli_forced_codegen_units {
745 return n;
746 }
747 if let Some(n) = self.target.target.options.default_codegen_units {
748 return n as usize;
749 }
750
751 // Why is 16 codegen units the default all the time?
752 //
753 // The main reason for enabling multiple codegen units by default is to
754 // leverage the ability for the codegen backend to do codegen and
755 // optimization in parallel. This allows us, especially for large crates, to
756 // make good use of all available resources on the machine once we've
757 // hit that stage of compilation. Large crates especially then often
758 // take a long time in codegen/optimization and this helps us amortize that
759 // cost.
760 //
761 // Note that a high number here doesn't mean that we'll be spawning a
762 // large number of threads in parallel. The backend of rustc contains
763 // global rate limiting through the `jobserver` crate so we'll never
764 // overload the system with too much work, but rather we'll only be
765 // optimizing when we're otherwise cooperating with other instances of
766 // rustc.
767 //
768 // Rather a high number here means that we should be able to keep a lot
769 // of idle cpus busy. By ensuring that no codegen unit takes *too* long
770 // to build we'll be guaranteed that all cpus will finish pretty closely
771 // to one another and we should make relatively optimal use of system
772 // resources
773 //
774 // Note that the main cost of codegen units is that it prevents LLVM
775 // from inlining across codegen units. Users in general don't have a lot
776 // of control over how codegen units are split up so it's our job in the
777 // compiler to ensure that undue performance isn't lost when using
778 // codegen units (aka we can't require everyone to slap `#[inline]` on
779 // everything).
780 //
781 // If we're compiling at `-O0` then the number doesn't really matter too
782 // much because performance doesn't matter and inlining is ok to lose.
783 // In debug mode we just want to try to guarantee that no cpu is stuck
784 // doing work that could otherwise be farmed to others.
785 //
786 // In release mode, however (O1 and above) performance does indeed
787 // matter! To recover the loss in performance due to inlining we'll be
788 // enabling ThinLTO by default (the function for which is just below).
789 // This will ensure that we recover any inlining wins we otherwise lost
790 // through codegen unit partitioning.
791 //
792 // ---
793 //
794 // Ok that's a lot of words but the basic tl;dr; is that we want a high
795 // number here -- but not too high. Additionally we're "safe" to have it
796 // always at the same number at all optimization levels.
797 //
798 // As a result 16 was chosen here! Mostly because it was a power of 2
799 // and most benchmarks agreed it was roughly a local optimum. Not very
800 // scientific.
801 16
802 }
803
804 pub fn teach(&self, code: &DiagnosticId) -> bool {
805 self.opts.debugging_opts.teach && self.diagnostic().must_teach(code)
806 }
807
808 pub fn rust_2015(&self) -> bool {
809 self.opts.edition == Edition::Edition2015
810 }
811
812 /// Are we allowed to use features from the Rust 2018 edition?
813 pub fn rust_2018(&self) -> bool {
814 self.opts.edition >= Edition::Edition2018
815 }
816
817 pub fn edition(&self) -> Edition {
818 self.opts.edition
819 }
820
821 /// Returns `true` if we cannot skip the PLT for shared library calls.
822 pub fn needs_plt(&self) -> bool {
823 // Check if the current target usually needs PLT to be enabled.
824 // The user can use the command line flag to override it.
825 let needs_plt = self.target.target.options.needs_plt;
826
827 let dbg_opts = &self.opts.debugging_opts;
828
829 let relro_level = dbg_opts.relro_level.unwrap_or(self.target.target.options.relro_level);
830
831 // Only enable this optimization by default if full relro is also enabled.
832 // In this case, lazy binding was already unavailable, so nothing is lost.
833 // This also ensures `-Wl,-z,now` is supported by the linker.
834 let full_relro = RelroLevel::Full == relro_level;
835
836 // If user didn't explicitly forced us to use / skip the PLT,
837 // then try to skip it where possible.
838 dbg_opts.plt.unwrap_or(needs_plt || !full_relro)
839 }
840 }
841
842 pub fn build_session(
843 sopts: config::Options,
844 local_crate_source_file: Option<PathBuf>,
845 registry: rustc_errors::registry::Registry,
846 ) -> Session {
847 let file_path_mapping = sopts.file_path_mapping();
848
849 build_session_with_source_map(
850 sopts,
851 local_crate_source_file,
852 registry,
853 Lrc::new(source_map::SourceMap::new(file_path_mapping)),
854 DiagnosticOutput::Default,
855 Default::default(),
856 )
857 }
858
859 fn default_emitter(
860 sopts: &config::Options,
861 registry: rustc_errors::registry::Registry,
862 source_map: &Lrc<source_map::SourceMap>,
863 emitter_dest: Option<Box<dyn Write + Send>>,
864 ) -> Box<dyn Emitter + sync::Send> {
865 let macro_backtrace = sopts.debugging_opts.macro_backtrace;
866 match (sopts.error_format, emitter_dest) {
867 (config::ErrorOutputType::HumanReadable(kind), dst) => {
868 let (short, color_config) = kind.unzip();
869
870 if let HumanReadableErrorType::AnnotateSnippet(_) = kind {
871 let emitter = AnnotateSnippetEmitterWriter::new(
872 Some(source_map.clone()),
873 short,
874 macro_backtrace,
875 );
876 Box::new(emitter.ui_testing(sopts.debugging_opts.ui_testing()))
877 } else {
878 let emitter = match dst {
879 None => EmitterWriter::stderr(
880 color_config,
881 Some(source_map.clone()),
882 short,
883 sopts.debugging_opts.teach,
884 sopts.debugging_opts.terminal_width,
885 macro_backtrace,
886 ),
887 Some(dst) => EmitterWriter::new(
888 dst,
889 Some(source_map.clone()),
890 short,
891 false, // no teach messages when writing to a buffer
892 false, // no colors when writing to a buffer
893 None, // no terminal width
894 macro_backtrace,
895 ),
896 };
897 Box::new(emitter.ui_testing(sopts.debugging_opts.ui_testing()))
898 }
899 }
900 (config::ErrorOutputType::Json { pretty, json_rendered }, None) => Box::new(
901 JsonEmitter::stderr(
902 Some(registry),
903 source_map.clone(),
904 pretty,
905 json_rendered,
906 macro_backtrace,
907 )
908 .ui_testing(sopts.debugging_opts.ui_testing()),
909 ),
910 (config::ErrorOutputType::Json { pretty, json_rendered }, Some(dst)) => Box::new(
911 JsonEmitter::new(
912 dst,
913 Some(registry),
914 source_map.clone(),
915 pretty,
916 json_rendered,
917 macro_backtrace,
918 )
919 .ui_testing(sopts.debugging_opts.ui_testing()),
920 ),
921 }
922 }
923
924 pub enum DiagnosticOutput {
925 Default,
926 Raw(Box<dyn Write + Send>),
927 }
928
929 pub fn build_session_with_source_map(
930 sopts: config::Options,
931 local_crate_source_file: Option<PathBuf>,
932 registry: rustc_errors::registry::Registry,
933 source_map: Lrc<source_map::SourceMap>,
934 diagnostics_output: DiagnosticOutput,
935 lint_caps: FxHashMap<lint::LintId, lint::Level>,
936 ) -> Session {
937 // FIXME: This is not general enough to make the warning lint completely override
938 // normal diagnostic warnings, since the warning lint can also be denied and changed
939 // later via the source code.
940 let warnings_allow = sopts
941 .lint_opts
942 .iter()
943 .filter(|&&(ref key, _)| *key == "warnings")
944 .map(|&(_, ref level)| *level == lint::Allow)
945 .last()
946 .unwrap_or(false);
947 let cap_lints_allow = sopts.lint_cap.map_or(false, |cap| cap == lint::Allow);
948 let can_emit_warnings = !(warnings_allow || cap_lints_allow);
949
950 let write_dest = match diagnostics_output {
951 DiagnosticOutput::Default => None,
952 DiagnosticOutput::Raw(write) => Some(write),
953 };
954 let emitter = default_emitter(&sopts, registry, &source_map, write_dest);
955
956 let diagnostic_handler = rustc_errors::Handler::with_emitter_and_flags(
957 emitter,
958 sopts.debugging_opts.diagnostic_handler_flags(can_emit_warnings),
959 );
960
961 build_session_(sopts, local_crate_source_file, diagnostic_handler, source_map, lint_caps)
962 }
963
964 fn build_session_(
965 sopts: config::Options,
966 local_crate_source_file: Option<PathBuf>,
967 span_diagnostic: rustc_errors::Handler,
968 source_map: Lrc<source_map::SourceMap>,
969 driver_lint_caps: FxHashMap<lint::LintId, lint::Level>,
970 ) -> Session {
971 let self_profiler = if let SwitchWithOptPath::Enabled(ref d) = sopts.debugging_opts.self_profile
972 {
973 let directory =
974 if let Some(ref directory) = d { directory } else { std::path::Path::new(".") };
975
976 let profiler = SelfProfiler::new(
977 directory,
978 sopts.crate_name.as_ref().map(|s| &s[..]),
979 &sopts.debugging_opts.self_profile_events,
980 );
981 match profiler {
982 Ok(profiler) => Some(Arc::new(profiler)),
983 Err(e) => {
984 early_warn(sopts.error_format, &format!("failed to create profiler: {}", e));
985 None
986 }
987 }
988 } else {
989 None
990 };
991
992 let host_triple = TargetTriple::from_triple(config::host_triple());
993 let host = Target::search(&host_triple).unwrap_or_else(|e| {
994 span_diagnostic.fatal(&format!("Error loading host specification: {}", e)).raise()
995 });
996 let target_cfg = config::build_target_config(&sopts, &span_diagnostic);
997
998 let parse_sess = ParseSess::with_span_handler(span_diagnostic, source_map);
999 let sysroot = match &sopts.maybe_sysroot {
1000 Some(sysroot) => sysroot.clone(),
1001 None => filesearch::get_or_default_sysroot(),
1002 };
1003
1004 let host_triple = config::host_triple();
1005 let target_triple = sopts.target_triple.triple();
1006 let host_tlib_path = SearchPath::from_sysroot_and_triple(&sysroot, host_triple);
1007 let target_tlib_path = if host_triple == target_triple {
1008 None
1009 } else {
1010 Some(SearchPath::from_sysroot_and_triple(&sysroot, target_triple))
1011 };
1012
1013 let file_path_mapping = sopts.file_path_mapping();
1014
1015 let local_crate_source_file =
1016 local_crate_source_file.map(|path| file_path_mapping.map_prefix(path).0);
1017
1018 let optimization_fuel_crate = sopts.debugging_opts.fuel.as_ref().map(|i| i.0.clone());
1019 let optimization_fuel = Lock::new(OptimizationFuel {
1020 remaining: sopts.debugging_opts.fuel.as_ref().map(|i| i.1).unwrap_or(0),
1021 out_of_fuel: false,
1022 });
1023 let print_fuel_crate = sopts.debugging_opts.print_fuel.clone();
1024 let print_fuel = AtomicU64::new(0);
1025
1026 let working_dir = env::current_dir().unwrap_or_else(|e| {
1027 parse_sess.span_diagnostic.fatal(&format!("Current directory is invalid: {}", e)).raise()
1028 });
1029 let working_dir = file_path_mapping.map_prefix(working_dir);
1030
1031 let cgu_reuse_tracker = if sopts.debugging_opts.query_dep_graph {
1032 CguReuseTracker::new()
1033 } else {
1034 CguReuseTracker::new_disabled()
1035 };
1036
1037 let prof = SelfProfilerRef::new(
1038 self_profiler,
1039 sopts.debugging_opts.time_passes || sopts.debugging_opts.time,
1040 sopts.debugging_opts.time_passes,
1041 );
1042
1043 let sess = Session {
1044 target: target_cfg,
1045 host,
1046 opts: sopts,
1047 host_tlib_path,
1048 target_tlib_path,
1049 parse_sess,
1050 sysroot,
1051 local_crate_source_file,
1052 working_dir,
1053 one_time_diagnostics: Default::default(),
1054 crate_types: Once::new(),
1055 crate_disambiguator: Once::new(),
1056 features: Once::new(),
1057 recursion_limit: Once::new(),
1058 type_length_limit: Once::new(),
1059 const_eval_limit: Once::new(),
1060 imported_macro_spans: OneThread::new(RefCell::new(FxHashMap::default())),
1061 incr_comp_session: OneThread::new(RefCell::new(IncrCompSession::NotInitialized)),
1062 cgu_reuse_tracker,
1063 prof,
1064 perf_stats: PerfStats {
1065 symbol_hash_time: Lock::new(Duration::from_secs(0)),
1066 decode_def_path_tables_time: Lock::new(Duration::from_secs(0)),
1067 queries_canonicalized: AtomicUsize::new(0),
1068 normalize_ty_after_erasing_regions: AtomicUsize::new(0),
1069 normalize_projection_ty: AtomicUsize::new(0),
1070 },
1071 code_stats: Default::default(),
1072 optimization_fuel_crate,
1073 optimization_fuel,
1074 print_fuel_crate,
1075 print_fuel,
1076 jobserver: jobserver::client(),
1077 driver_lint_caps,
1078 trait_methods_not_found: Lock::new(Default::default()),
1079 confused_type_with_std_module: Lock::new(Default::default()),
1080 system_library_path: OneThread::new(RefCell::new(Default::default())),
1081 };
1082
1083 validate_commandline_args_with_session_available(&sess);
1084
1085 sess
1086 }
1087
1088 // If it is useful to have a Session available already for validating a
1089 // commandline argument, you can do so here.
1090 fn validate_commandline_args_with_session_available(sess: &Session) {
1091 // Since we don't know if code in an rlib will be linked to statically or
1092 // dynamically downstream, rustc generates `__imp_` symbols that help the
1093 // MSVC linker deal with this lack of knowledge (#27438). Unfortunately,
1094 // these manually generated symbols confuse LLD when it tries to merge
1095 // bitcode during ThinLTO. Therefore we disallow dynamic linking on MSVC
1096 // when compiling for LLD ThinLTO. This way we can validly just not generate
1097 // the `dllimport` attributes and `__imp_` symbols in that case.
1098 if sess.opts.cg.linker_plugin_lto.enabled()
1099 && sess.opts.cg.prefer_dynamic
1100 && sess.target.target.options.is_like_msvc
1101 {
1102 sess.err(
1103 "Linker plugin based LTO is not supported together with \
1104 `-C prefer-dynamic` when targeting MSVC",
1105 );
1106 }
1107
1108 // Make sure that any given profiling data actually exists so LLVM can't
1109 // decide to silently skip PGO.
1110 if let Some(ref path) = sess.opts.cg.profile_use {
1111 if !path.exists() {
1112 sess.err(&format!(
1113 "File `{}` passed to `-C profile-use` does not exist.",
1114 path.display()
1115 ));
1116 }
1117 }
1118
1119 // PGO does not work reliably with panic=unwind on Windows. Let's make it
1120 // an error to combine the two for now. It always runs into an assertions
1121 // if LLVM is built with assertions, but without assertions it sometimes
1122 // does not crash and will probably generate a corrupted binary.
1123 // We should only display this error if we're actually going to run PGO.
1124 // If we're just supposed to print out some data, don't show the error (#61002).
1125 if sess.opts.cg.profile_generate.enabled()
1126 && sess.target.target.options.is_like_msvc
1127 && sess.panic_strategy() == PanicStrategy::Unwind
1128 && sess.opts.prints.iter().all(|&p| p == PrintRequest::NativeStaticLibs)
1129 {
1130 sess.err(
1131 "Profile-guided optimization does not yet work in conjunction \
1132 with `-Cpanic=unwind` on Windows when targeting MSVC. \
1133 See issue #61002 <https://github.com/rust-lang/rust/issues/61002> \
1134 for more information.",
1135 );
1136 }
1137
1138 // Sanitizers can only be used on some tested platforms.
1139 if let Some(ref sanitizer) = sess.opts.debugging_opts.sanitizer {
1140 const ASAN_SUPPORTED_TARGETS: &[&str] = &[
1141 "x86_64-unknown-linux-gnu",
1142 "x86_64-apple-darwin",
1143 "x86_64-fuchsia",
1144 "aarch64-fuchsia",
1145 ];
1146 const TSAN_SUPPORTED_TARGETS: &[&str] =
1147 &["x86_64-unknown-linux-gnu", "x86_64-apple-darwin"];
1148 const LSAN_SUPPORTED_TARGETS: &[&str] =
1149 &["x86_64-unknown-linux-gnu", "x86_64-apple-darwin"];
1150 const MSAN_SUPPORTED_TARGETS: &[&str] = &["x86_64-unknown-linux-gnu"];
1151
1152 let supported_targets = match *sanitizer {
1153 Sanitizer::Address => ASAN_SUPPORTED_TARGETS,
1154 Sanitizer::Thread => TSAN_SUPPORTED_TARGETS,
1155 Sanitizer::Leak => LSAN_SUPPORTED_TARGETS,
1156 Sanitizer::Memory => MSAN_SUPPORTED_TARGETS,
1157 };
1158
1159 if !supported_targets.contains(&&*sess.opts.target_triple.triple()) {
1160 sess.err(&format!(
1161 "{:?}Sanitizer only works with the `{}` target",
1162 sanitizer,
1163 supported_targets.join("` or `")
1164 ));
1165 }
1166 }
1167 }
1168
1169 /// Hash value constructed out of all the `-C metadata` arguments passed to the
1170 /// compiler. Together with the crate-name forms a unique global identifier for
1171 /// the crate.
1172 #[derive(Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Clone, Copy, RustcEncodable, RustcDecodable)]
1173 pub struct CrateDisambiguator(Fingerprint);
1174
1175 impl CrateDisambiguator {
1176 pub fn to_fingerprint(self) -> Fingerprint {
1177 self.0
1178 }
1179 }
1180
1181 impl fmt::Display for CrateDisambiguator {
1182 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
1183 let (a, b) = self.0.as_value();
1184 let as_u128 = a as u128 | ((b as u128) << 64);
1185 f.write_str(&base_n::encode(as_u128, base_n::CASE_INSENSITIVE))
1186 }
1187 }
1188
1189 impl From<Fingerprint> for CrateDisambiguator {
1190 fn from(fingerprint: Fingerprint) -> CrateDisambiguator {
1191 CrateDisambiguator(fingerprint)
1192 }
1193 }
1194
1195 impl_stable_hash_via_hash!(CrateDisambiguator);
1196
1197 /// Holds data on the current incremental compilation session, if there is one.
1198 #[derive(Debug)]
1199 pub enum IncrCompSession {
1200 /// This is the state the session will be in until the incr. comp. dir is
1201 /// needed.
1202 NotInitialized,
1203 /// This is the state during which the session directory is private and can
1204 /// be modified.
1205 Active { session_directory: PathBuf, lock_file: flock::Lock, load_dep_graph: bool },
1206 /// This is the state after the session directory has been finalized. In this
1207 /// state, the contents of the directory must not be modified any more.
1208 Finalized { session_directory: PathBuf },
1209 /// This is an error state that is reached when some compilation error has
1210 /// occurred. It indicates that the contents of the session directory must
1211 /// not be used, since they might be invalid.
1212 InvalidBecauseOfErrors { session_directory: PathBuf },
1213 }
1214
1215 pub fn early_error(output: config::ErrorOutputType, msg: &str) -> ! {
1216 let emitter: Box<dyn Emitter + sync::Send> = match output {
1217 config::ErrorOutputType::HumanReadable(kind) => {
1218 let (short, color_config) = kind.unzip();
1219 Box::new(EmitterWriter::stderr(color_config, None, short, false, None, false))
1220 }
1221 config::ErrorOutputType::Json { pretty, json_rendered } => {
1222 Box::new(JsonEmitter::basic(pretty, json_rendered, false))
1223 }
1224 };
1225 let handler = rustc_errors::Handler::with_emitter(true, None, emitter);
1226 handler.struct_fatal(msg).emit();
1227 rustc_errors::FatalError.raise();
1228 }
1229
1230 pub fn early_warn(output: config::ErrorOutputType, msg: &str) {
1231 let emitter: Box<dyn Emitter + sync::Send> = match output {
1232 config::ErrorOutputType::HumanReadable(kind) => {
1233 let (short, color_config) = kind.unzip();
1234 Box::new(EmitterWriter::stderr(color_config, None, short, false, None, false))
1235 }
1236 config::ErrorOutputType::Json { pretty, json_rendered } => {
1237 Box::new(JsonEmitter::basic(pretty, json_rendered, false))
1238 }
1239 };
1240 let handler = rustc_errors::Handler::with_emitter(true, None, emitter);
1241 handler.struct_warn(msg).emit();
1242 }
1243
1244 pub type CompileResult = Result<(), ErrorReported>;