1 // Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 pub use self::code_stats::{CodeStats, DataTypeKind, FieldInfo};
12 pub use self::code_stats::{SizeKind, TypeSizeInfo, VariantInfo};
14 use dep_graph::DepGraph;
15 use hir::def_id::{CrateNum, DefIndex};
18 use middle::allocator::AllocatorKind;
19 use middle::dependency_format;
20 use session::search_paths::PathKind;
21 use session::config::DebugInfoLevel;
23 use util::nodemap::{FxHashMap, FxHashSet};
24 use util::common::{duration_to_secs_str, ErrorReported};
26 use syntax::ast::NodeId;
27 use errors::{self, DiagnosticBuilder};
28 use errors::emitter::{Emitter, EmitterWriter};
29 use syntax::json::JsonEmitter;
30 use syntax::feature_gate;
32 use syntax::parse::ParseSess;
33 use syntax::symbol::Symbol;
34 use syntax::{ast, codemap};
35 use syntax::feature_gate::AttributeType;
36 use syntax_pos::{Span, MultiSpan};
38 use rustc_back::{LinkerFlavor, PanicStrategy};
39 use rustc_back::target::Target;
40 use rustc_data_structures::flock;
41 use jobserver::Client;
43 use std::cell::{self, Cell, RefCell};
44 use std::collections::HashMap;
48 use std::path::{Path, PathBuf};
50 use std::sync::{Once, ONCE_INIT};
51 use std::time::Duration;
58 // Represents the data associated with a compilation
59 // session for a single crate.
61 pub dep_graph: DepGraph,
62 pub target: config::Config,
64 pub opts: config::Options,
65 pub parse_sess: ParseSess,
66 // For a library crate, this is always none
67 pub entry_fn: RefCell<Option<(NodeId, Span)>>,
68 pub entry_type: Cell<Option<config::EntryFnType>>,
69 pub plugin_registrar_fn: Cell<Option<ast::NodeId>>,
70 pub derive_registrar_fn: Cell<Option<ast::NodeId>>,
71 pub default_sysroot: Option<PathBuf>,
72 // The name of the root source file of the crate, in the local file system.
73 // The path is always expected to be absolute. `None` means that there is no
75 pub local_crate_source_file: Option<String>,
76 // The directory the compiler has been executed in plus a flag indicating
77 // if the value stored here has been affected by path remapping.
78 pub working_dir: (String, bool),
79 pub lint_store: RefCell<lint::LintStore>,
80 pub buffered_lints: RefCell<Option<lint::LintBuffer>>,
81 /// Set of (LintId, Option<Span>, message) tuples tracking lint
82 /// (sub)diagnostics that have been set once, but should not be set again,
83 /// in order to avoid redundantly verbose output (Issue #24690).
84 pub one_time_diagnostics: RefCell<FxHashSet<(lint::LintId, Option<Span>, String)>>,
85 pub plugin_llvm_passes: RefCell<Vec<String>>,
86 pub plugin_attributes: RefCell<Vec<(String, AttributeType)>>,
87 pub crate_types: RefCell<Vec<config::CrateType>>,
88 pub dependency_formats: RefCell<dependency_format::Dependencies>,
89 // The crate_disambiguator is constructed out of all the `-C metadata`
90 // arguments passed to the compiler. Its value together with the crate-name
91 // forms a unique global identifier for the crate. It is used to allow
92 // multiple crates with the same name to coexist. See the
93 // trans::back::symbol_names module for more information.
94 pub crate_disambiguator: RefCell<Symbol>,
95 pub features: RefCell<feature_gate::Features>,
97 /// The maximum recursion limit for potentially infinitely recursive
98 /// operations such as auto-dereference and monomorphization.
99 pub recursion_limit: Cell<usize>,
101 /// The maximum length of types during monomorphization.
102 pub type_length_limit: Cell<usize>,
104 /// The metadata::creader module may inject an allocator/panic_runtime
105 /// dependency if it didn't already find one, and this tracks what was
107 pub injected_allocator: Cell<Option<CrateNum>>,
108 pub allocator_kind: Cell<Option<AllocatorKind>>,
109 pub injected_panic_runtime: Cell<Option<CrateNum>>,
111 /// Map from imported macro spans (which consist of
112 /// the localized span for the macro body) to the
113 /// macro name and definition span in the source crate.
114 pub imported_macro_spans: RefCell<HashMap<Span, (String, Span)>>,
116 incr_comp_session: RefCell<IncrCompSession>,
118 /// Some measurements that are being gathered during compilation.
119 pub perf_stats: PerfStats,
121 /// Data about code being compiled, gathered during compilation.
122 pub code_stats: RefCell<CodeStats>,
124 next_node_id: Cell<ast::NodeId>,
126 /// If -zfuel=crate=n is specified, Some(crate).
127 optimization_fuel_crate: Option<String>,
128 /// If -zfuel=crate=n is specified, initially set to n. Otherwise 0.
129 optimization_fuel_limit: Cell<u64>,
130 /// We're rejecting all further optimizations.
131 out_of_fuel: Cell<bool>,
133 // The next two are public because the driver needs to read them.
135 /// If -zprint-fuel=crate, Some(crate).
136 pub print_fuel_crate: Option<String>,
137 /// Always set to zero and incremented so that we can print fuel expended by a crate.
138 pub print_fuel: Cell<u64>,
140 /// Loaded up early on in the initialization of this `Session` to avoid
141 /// false positives about a job server in our environment.
142 pub jobserver_from_env: Option<Client>,
144 /// Metadata about the allocators for the current crate being compiled
145 pub has_global_allocator: Cell<bool>,
148 pub struct PerfStats {
149 // The accumulated time needed for computing the SVH of the crate
150 pub svh_time: Cell<Duration>,
151 // The accumulated time spent on computing incr. comp. hashes
152 pub incr_comp_hashes_time: Cell<Duration>,
153 // The number of incr. comp. hash computations performed
154 pub incr_comp_hashes_count: Cell<u64>,
155 // The number of bytes hashed when computing ICH values
156 pub incr_comp_bytes_hashed: Cell<u64>,
157 // The accumulated time spent on computing symbol hashes
158 pub symbol_hash_time: Cell<Duration>,
159 // The accumulated time spent decoding def path tables from metadata
160 pub decode_def_path_tables_time: Cell<Duration>,
163 /// Enum to support dispatch of one-time diagnostics (in Session.diag_once)
164 enum DiagnosticBuilderMethod {
167 // add more variants as needed to support one-time diagnostics
171 pub fn local_crate_disambiguator(&self) -> Symbol {
172 *self.crate_disambiguator.borrow()
174 pub fn struct_span_warn<'a, S: Into<MultiSpan>>(&'a self,
177 -> DiagnosticBuilder<'a> {
178 self.diagnostic().struct_span_warn(sp, msg)
180 pub fn struct_span_warn_with_code<'a, S: Into<MultiSpan>>(&'a self,
184 -> DiagnosticBuilder<'a> {
185 self.diagnostic().struct_span_warn_with_code(sp, msg, code)
187 pub fn struct_warn<'a>(&'a self, msg: &str) -> DiagnosticBuilder<'a> {
188 self.diagnostic().struct_warn(msg)
190 pub fn struct_span_err<'a, S: Into<MultiSpan>>(&'a self,
193 -> DiagnosticBuilder<'a> {
194 self.diagnostic().struct_span_err(sp, msg)
196 pub fn struct_span_err_with_code<'a, S: Into<MultiSpan>>(&'a self,
200 -> DiagnosticBuilder<'a> {
201 self.diagnostic().struct_span_err_with_code(sp, msg, code)
203 // FIXME: This method should be removed (every error should have an associated error code).
204 pub fn struct_err<'a>(&'a self, msg: &str) -> DiagnosticBuilder<'a> {
205 self.diagnostic().struct_err(msg)
207 pub fn struct_err_with_code<'a>(&'a self, msg: &str, code: &str) -> DiagnosticBuilder<'a> {
208 self.diagnostic().struct_err_with_code(msg, code)
210 pub fn struct_span_fatal<'a, S: Into<MultiSpan>>(&'a self,
213 -> DiagnosticBuilder<'a> {
214 self.diagnostic().struct_span_fatal(sp, msg)
216 pub fn struct_span_fatal_with_code<'a, S: Into<MultiSpan>>(&'a self,
220 -> DiagnosticBuilder<'a> {
221 self.diagnostic().struct_span_fatal_with_code(sp, msg, code)
223 pub fn struct_fatal<'a>(&'a self, msg: &str) -> DiagnosticBuilder<'a> {
224 self.diagnostic().struct_fatal(msg)
227 pub fn span_fatal<S: Into<MultiSpan>>(&self, sp: S, msg: &str) -> ! {
228 panic!(self.diagnostic().span_fatal(sp, msg))
230 pub fn span_fatal_with_code<S: Into<MultiSpan>>(&self, sp: S, msg: &str, code: &str) -> ! {
231 panic!(self.diagnostic().span_fatal_with_code(sp, msg, code))
233 pub fn fatal(&self, msg: &str) -> ! {
234 panic!(self.diagnostic().fatal(msg))
236 pub fn span_err_or_warn<S: Into<MultiSpan>>(&self, is_warning: bool, sp: S, msg: &str) {
238 self.span_warn(sp, msg);
240 self.span_err(sp, msg);
243 pub fn span_err<S: Into<MultiSpan>>(&self, sp: S, msg: &str) {
244 self.diagnostic().span_err(sp, msg)
246 pub fn span_err_with_code<S: Into<MultiSpan>>(&self, sp: S, msg: &str, code: &str) {
247 self.diagnostic().span_err_with_code(sp, &msg, code)
249 pub fn err(&self, msg: &str) {
250 self.diagnostic().err(msg)
252 pub fn err_count(&self) -> usize {
253 self.diagnostic().err_count()
255 pub fn has_errors(&self) -> bool {
256 self.diagnostic().has_errors()
258 pub fn abort_if_errors(&self) {
259 self.diagnostic().abort_if_errors();
261 pub fn compile_status(&self) -> Result<(), CompileIncomplete> {
262 compile_result_from_err_count(self.err_count())
264 pub fn track_errors<F, T>(&self, f: F) -> Result<T, ErrorReported>
265 where F: FnOnce() -> T
267 let old_count = self.err_count();
269 let errors = self.err_count() - old_count;
276 pub fn span_warn<S: Into<MultiSpan>>(&self, sp: S, msg: &str) {
277 self.diagnostic().span_warn(sp, msg)
279 pub fn span_warn_with_code<S: Into<MultiSpan>>(&self, sp: S, msg: &str, code: &str) {
280 self.diagnostic().span_warn_with_code(sp, msg, code)
282 pub fn warn(&self, msg: &str) {
283 self.diagnostic().warn(msg)
285 pub fn opt_span_warn<S: Into<MultiSpan>>(&self, opt_sp: Option<S>, msg: &str) {
287 Some(sp) => self.span_warn(sp, msg),
288 None => self.warn(msg),
291 /// Delay a span_bug() call until abort_if_errors()
292 pub fn delay_span_bug<S: Into<MultiSpan>>(&self, sp: S, msg: &str) {
293 self.diagnostic().delay_span_bug(sp, msg)
295 pub fn note_without_error(&self, msg: &str) {
296 self.diagnostic().note_without_error(msg)
298 pub fn span_note_without_error<S: Into<MultiSpan>>(&self, sp: S, msg: &str) {
299 self.diagnostic().span_note_without_error(sp, msg)
301 pub fn span_unimpl<S: Into<MultiSpan>>(&self, sp: S, msg: &str) -> ! {
302 self.diagnostic().span_unimpl(sp, msg)
304 pub fn unimpl(&self, msg: &str) -> ! {
305 self.diagnostic().unimpl(msg)
308 pub fn buffer_lint<S: Into<MultiSpan>>(&self,
309 lint: &'static lint::Lint,
313 match *self.buffered_lints.borrow_mut() {
314 Some(ref mut buffer) => buffer.add_lint(lint, id, sp.into(), msg),
315 None => bug!("can't buffer lints after HIR lowering"),
319 pub fn reserve_node_ids(&self, count: usize) -> ast::NodeId {
320 let id = self.next_node_id.get();
322 match id.as_usize().checked_add(count) {
324 self.next_node_id.set(ast::NodeId::new(next));
326 None => bug!("Input too large, ran out of node ids!")
331 pub fn next_node_id(&self) -> NodeId {
332 self.reserve_node_ids(1)
334 pub fn diagnostic<'a>(&'a self) -> &'a errors::Handler {
335 &self.parse_sess.span_diagnostic
338 /// Analogous to calling methods on the given `DiagnosticBuilder`, but
339 /// deduplicates on lint ID, span (if any), and message for this `Session`
340 /// if we're not outputting in JSON mode.
341 fn diag_once<'a, 'b>(&'a self,
342 diag_builder: &'b mut DiagnosticBuilder<'a>,
343 method: DiagnosticBuilderMethod,
344 lint: &'static lint::Lint, message: &str, span: Option<Span>) {
345 let mut do_method = || {
347 DiagnosticBuilderMethod::Note => {
348 diag_builder.note(message);
350 DiagnosticBuilderMethod::SpanNote => {
351 diag_builder.span_note(span.expect("span_note expects a span"), message);
356 match self.opts.error_format {
357 // when outputting JSON for tool consumption, the tool might want
359 config::ErrorOutputType::Json => {
363 let lint_id = lint::LintId::of(lint);
364 let id_span_message = (lint_id, span, message.to_owned());
365 let fresh = self.one_time_diagnostics.borrow_mut().insert(id_span_message);
373 pub fn diag_span_note_once<'a, 'b>(&'a self,
374 diag_builder: &'b mut DiagnosticBuilder<'a>,
375 lint: &'static lint::Lint, span: Span, message: &str) {
376 self.diag_once(diag_builder, DiagnosticBuilderMethod::SpanNote, lint, message, Some(span));
379 pub fn diag_note_once<'a, 'b>(&'a self,
380 diag_builder: &'b mut DiagnosticBuilder<'a>,
381 lint: &'static lint::Lint, message: &str) {
382 self.diag_once(diag_builder, DiagnosticBuilderMethod::Note, lint, message, None);
385 pub fn codemap<'a>(&'a self) -> &'a codemap::CodeMap {
386 self.parse_sess.codemap()
388 pub fn verbose(&self) -> bool { self.opts.debugging_opts.verbose }
389 pub fn time_passes(&self) -> bool { self.opts.debugging_opts.time_passes }
390 pub fn profile_queries(&self) -> bool {
391 self.opts.debugging_opts.profile_queries ||
392 self.opts.debugging_opts.profile_queries_and_keys
394 pub fn profile_queries_and_keys(&self) -> bool {
395 self.opts.debugging_opts.profile_queries_and_keys
397 pub fn count_llvm_insns(&self) -> bool {
398 self.opts.debugging_opts.count_llvm_insns
400 pub fn time_llvm_passes(&self) -> bool {
401 self.opts.debugging_opts.time_llvm_passes
403 pub fn trans_stats(&self) -> bool { self.opts.debugging_opts.trans_stats }
404 pub fn meta_stats(&self) -> bool { self.opts.debugging_opts.meta_stats }
405 pub fn asm_comments(&self) -> bool { self.opts.debugging_opts.asm_comments }
406 pub fn no_verify(&self) -> bool { self.opts.debugging_opts.no_verify }
407 pub fn borrowck_stats(&self) -> bool { self.opts.debugging_opts.borrowck_stats }
408 pub fn print_llvm_passes(&self) -> bool {
409 self.opts.debugging_opts.print_llvm_passes
411 pub fn emit_end_regions(&self) -> bool {
412 self.opts.debugging_opts.emit_end_regions ||
413 (self.opts.debugging_opts.mir_emit_validate > 0)
415 pub fn lto(&self) -> bool {
418 /// Returns the panic strategy for this compile session. If the user explicitly selected one
419 /// using '-C panic', use that, otherwise use the panic strategy defined by the target.
420 pub fn panic_strategy(&self) -> PanicStrategy {
421 self.opts.cg.panic.unwrap_or(self.target.target.options.panic_strategy)
423 pub fn linker_flavor(&self) -> LinkerFlavor {
424 self.opts.debugging_opts.linker_flavor.unwrap_or(self.target.target.linker_flavor)
426 pub fn no_landing_pads(&self) -> bool {
427 self.opts.debugging_opts.no_landing_pads || self.panic_strategy() == PanicStrategy::Abort
429 pub fn unstable_options(&self) -> bool {
430 self.opts.debugging_opts.unstable_options
432 pub fn nonzeroing_move_hints(&self) -> bool {
433 self.opts.debugging_opts.enable_nonzeroing_move_hints
435 pub fn overflow_checks(&self) -> bool {
436 self.opts.cg.overflow_checks
437 .or(self.opts.debugging_opts.force_overflow_checks)
438 .unwrap_or(self.opts.debug_assertions)
441 pub fn crt_static(&self) -> bool {
442 // If the target does not opt in to crt-static support, use its default.
443 if self.target.target.options.crt_static_respected {
444 self.crt_static_feature()
446 self.target.target.options.crt_static_default
450 pub fn crt_static_feature(&self) -> bool {
451 let requested_features = self.opts.cg.target_feature.split(',');
452 let found_negative = requested_features.clone().any(|r| r == "-crt-static");
453 let found_positive = requested_features.clone().any(|r| r == "+crt-static");
455 // If the target we're compiling for requests a static crt by default,
456 // then see if the `-crt-static` feature was passed to disable that.
457 // Otherwise if we don't have a static crt by default then see if the
458 // `+crt-static` feature was passed.
459 if self.target.target.options.crt_static_default {
466 pub fn must_not_eliminate_frame_pointers(&self) -> bool {
467 self.opts.debuginfo != DebugInfoLevel::NoDebugInfo ||
468 !self.target.target.options.eliminate_frame_pointer
471 /// Returns the symbol name for the registrar function,
472 /// given the crate Svh and the function DefIndex.
473 pub fn generate_plugin_registrar_symbol(&self, disambiguator: Symbol, index: DefIndex)
475 format!("__rustc_plugin_registrar__{}_{}", disambiguator, index.as_usize())
478 pub fn generate_derive_registrar_symbol(&self, disambiguator: Symbol, index: DefIndex)
480 format!("__rustc_derive_registrar__{}_{}", disambiguator, index.as_usize())
483 pub fn sysroot<'a>(&'a self) -> &'a Path {
484 match self.opts.maybe_sysroot {
485 Some (ref sysroot) => sysroot,
486 None => self.default_sysroot.as_ref()
487 .expect("missing sysroot and default_sysroot in Session")
490 pub fn target_filesearch(&self, kind: PathKind) -> filesearch::FileSearch {
491 filesearch::FileSearch::new(self.sysroot(),
492 &self.opts.target_triple,
493 &self.opts.search_paths,
496 pub fn host_filesearch(&self, kind: PathKind) -> filesearch::FileSearch {
497 filesearch::FileSearch::new(
499 config::host_triple(),
500 &self.opts.search_paths,
504 pub fn init_incr_comp_session(&self,
505 session_dir: PathBuf,
506 lock_file: flock::Lock) {
507 let mut incr_comp_session = self.incr_comp_session.borrow_mut();
509 if let IncrCompSession::NotInitialized = *incr_comp_session { } else {
510 bug!("Trying to initialize IncrCompSession `{:?}`", *incr_comp_session)
513 *incr_comp_session = IncrCompSession::Active {
514 session_directory: session_dir,
519 pub fn finalize_incr_comp_session(&self, new_directory_path: PathBuf) {
520 let mut incr_comp_session = self.incr_comp_session.borrow_mut();
522 if let IncrCompSession::Active { .. } = *incr_comp_session { } else {
523 bug!("Trying to finalize IncrCompSession `{:?}`", *incr_comp_session)
526 // Note: This will also drop the lock file, thus unlocking the directory
527 *incr_comp_session = IncrCompSession::Finalized {
528 session_directory: new_directory_path,
532 pub fn mark_incr_comp_session_as_invalid(&self) {
533 let mut incr_comp_session = self.incr_comp_session.borrow_mut();
535 let session_directory = match *incr_comp_session {
536 IncrCompSession::Active { ref session_directory, .. } => {
537 session_directory.clone()
539 _ => bug!("Trying to invalidate IncrCompSession `{:?}`",
543 // Note: This will also drop the lock file, thus unlocking the directory
544 *incr_comp_session = IncrCompSession::InvalidBecauseOfErrors {
549 pub fn incr_comp_session_dir(&self) -> cell::Ref<PathBuf> {
550 let incr_comp_session = self.incr_comp_session.borrow();
551 cell::Ref::map(incr_comp_session, |incr_comp_session| {
552 match *incr_comp_session {
553 IncrCompSession::NotInitialized => {
554 bug!("Trying to get session directory from IncrCompSession `{:?}`",
557 IncrCompSession::Active { ref session_directory, .. } |
558 IncrCompSession::Finalized { ref session_directory } |
559 IncrCompSession::InvalidBecauseOfErrors { ref session_directory } => {
566 pub fn incr_comp_session_dir_opt(&self) -> Option<cell::Ref<PathBuf>> {
567 if self.opts.incremental.is_some() {
568 Some(self.incr_comp_session_dir())
574 pub fn print_perf_stats(&self) {
575 println!("Total time spent computing SVHs: {}",
576 duration_to_secs_str(self.perf_stats.svh_time.get()));
577 println!("Total time spent computing incr. comp. hashes: {}",
578 duration_to_secs_str(self.perf_stats.incr_comp_hashes_time.get()));
579 println!("Total number of incr. comp. hashes computed: {}",
580 self.perf_stats.incr_comp_hashes_count.get());
581 println!("Total number of bytes hashed for incr. comp.: {}",
582 self.perf_stats.incr_comp_bytes_hashed.get());
583 println!("Average bytes hashed per incr. comp. HIR node: {}",
584 self.perf_stats.incr_comp_bytes_hashed.get() /
585 self.perf_stats.incr_comp_hashes_count.get());
586 println!("Total time spent computing symbol hashes: {}",
587 duration_to_secs_str(self.perf_stats.symbol_hash_time.get()));
588 println!("Total time spent decoding DefPath tables: {}",
589 duration_to_secs_str(self.perf_stats.decode_def_path_tables_time.get()));
592 /// We want to know if we're allowed to do an optimization for crate foo from -z fuel=foo=n.
593 /// This expends fuel if applicable, and records fuel if applicable.
594 pub fn consider_optimizing<T: Fn() -> String>(&self, crate_name: &str, msg: T) -> bool {
596 match self.optimization_fuel_crate {
597 Some(ref c) if c == crate_name => {
598 let fuel = self.optimization_fuel_limit.get();
600 if fuel == 0 && !self.out_of_fuel.get() {
601 println!("optimization-fuel-exhausted: {}", msg());
602 self.out_of_fuel.set(true);
604 self.optimization_fuel_limit.set(fuel-1);
609 match self.print_fuel_crate {
610 Some(ref c) if c == crate_name=> {
611 self.print_fuel.set(self.print_fuel.get()+1);
619 pub fn build_session(sopts: config::Options,
620 dep_graph: &DepGraph,
621 local_crate_source_file: Option<PathBuf>,
622 registry: errors::registry::Registry)
624 let file_path_mapping = sopts.file_path_mapping();
626 build_session_with_codemap(sopts,
628 local_crate_source_file,
630 Rc::new(codemap::CodeMap::new(file_path_mapping)),
634 pub fn build_session_with_codemap(sopts: config::Options,
635 dep_graph: &DepGraph,
636 local_crate_source_file: Option<PathBuf>,
637 registry: errors::registry::Registry,
638 codemap: Rc<codemap::CodeMap>,
639 emitter_dest: Option<Box<Write + Send>>)
641 // FIXME: This is not general enough to make the warning lint completely override
642 // normal diagnostic warnings, since the warning lint can also be denied and changed
643 // later via the source code.
644 let can_print_warnings = sopts.lint_opts
646 .filter(|&&(ref key, _)| *key == "warnings")
647 .map(|&(_, ref level)| *level != lint::Allow)
650 let treat_err_as_bug = sopts.debugging_opts.treat_err_as_bug;
652 let emitter: Box<Emitter> = match (sopts.error_format, emitter_dest) {
653 (config::ErrorOutputType::HumanReadable(color_config), None) => {
654 Box::new(EmitterWriter::stderr(color_config,
655 Some(codemap.clone())))
657 (config::ErrorOutputType::HumanReadable(_), Some(dst)) => {
658 Box::new(EmitterWriter::new(dst,
659 Some(codemap.clone())))
661 (config::ErrorOutputType::Json, None) => {
662 Box::new(JsonEmitter::stderr(Some(registry), codemap.clone()))
664 (config::ErrorOutputType::Json, Some(dst)) => {
665 Box::new(JsonEmitter::new(dst, Some(registry), codemap.clone()))
669 let diagnostic_handler =
670 errors::Handler::with_emitter(can_print_warnings,
674 build_session_(sopts,
676 local_crate_source_file,
681 pub fn build_session_(sopts: config::Options,
682 dep_graph: &DepGraph,
683 local_crate_source_file: Option<PathBuf>,
684 span_diagnostic: errors::Handler,
685 codemap: Rc<codemap::CodeMap>)
687 let host = match Target::search(config::host_triple()) {
690 panic!(span_diagnostic.fatal(&format!("Error loading host specification: {}", e)));
693 let target_cfg = config::build_target_config(&sopts, &span_diagnostic);
695 let p_s = parse::ParseSess::with_span_handler(span_diagnostic, codemap);
696 let default_sysroot = match sopts.maybe_sysroot {
698 None => Some(filesearch::get_or_default_sysroot())
701 let file_path_mapping = sopts.file_path_mapping();
703 // Make the path absolute, if necessary
704 let local_crate_source_file = local_crate_source_file.map(|path| {
705 file_path_mapping.map_prefix(path.to_string_lossy().into_owned()).0
708 let optimization_fuel_crate = sopts.debugging_opts.fuel.as_ref().map(|i| i.0.clone());
709 let optimization_fuel_limit = Cell::new(sopts.debugging_opts.fuel.as_ref()
710 .map(|i| i.1).unwrap_or(0));
711 let print_fuel_crate = sopts.debugging_opts.print_fuel.clone();
712 let print_fuel = Cell::new(0);
714 let working_dir = env::current_dir().unwrap().to_string_lossy().into_owned();
715 let working_dir = file_path_mapping.map_prefix(working_dir);
718 dep_graph: dep_graph.clone(),
723 // For a library crate, this is always none
724 entry_fn: RefCell::new(None),
725 entry_type: Cell::new(None),
726 plugin_registrar_fn: Cell::new(None),
727 derive_registrar_fn: Cell::new(None),
729 local_crate_source_file,
731 lint_store: RefCell::new(lint::LintStore::new()),
732 buffered_lints: RefCell::new(Some(lint::LintBuffer::new())),
733 one_time_diagnostics: RefCell::new(FxHashSet()),
734 plugin_llvm_passes: RefCell::new(Vec::new()),
735 plugin_attributes: RefCell::new(Vec::new()),
736 crate_types: RefCell::new(Vec::new()),
737 dependency_formats: RefCell::new(FxHashMap()),
738 crate_disambiguator: RefCell::new(Symbol::intern("")),
739 features: RefCell::new(feature_gate::Features::new()),
740 recursion_limit: Cell::new(64),
741 type_length_limit: Cell::new(1048576),
742 next_node_id: Cell::new(NodeId::new(1)),
743 injected_allocator: Cell::new(None),
744 allocator_kind: Cell::new(None),
745 injected_panic_runtime: Cell::new(None),
746 imported_macro_spans: RefCell::new(HashMap::new()),
747 incr_comp_session: RefCell::new(IncrCompSession::NotInitialized),
748 perf_stats: PerfStats {
749 svh_time: Cell::new(Duration::from_secs(0)),
750 incr_comp_hashes_time: Cell::new(Duration::from_secs(0)),
751 incr_comp_hashes_count: Cell::new(0),
752 incr_comp_bytes_hashed: Cell::new(0),
753 symbol_hash_time: Cell::new(Duration::from_secs(0)),
754 decode_def_path_tables_time: Cell::new(Duration::from_secs(0)),
756 code_stats: RefCell::new(CodeStats::new()),
757 optimization_fuel_crate,
758 optimization_fuel_limit,
761 out_of_fuel: Cell::new(false),
762 // Note that this is unsafe because it may misinterpret file descriptors
763 // on Unix as jobserver file descriptors. We hopefully execute this near
764 // the beginning of the process though to ensure we don't get false
765 // positives, or in other words we try to execute this before we open
766 // any file descriptors ourselves.
768 // Also note that we stick this in a global because there could be
769 // multiple `Session` instances in this process, and the jobserver is
771 jobserver_from_env: unsafe {
772 static mut GLOBAL_JOBSERVER: *mut Option<Client> = 0 as *mut _;
773 static INIT: Once = ONCE_INIT;
775 GLOBAL_JOBSERVER = Box::into_raw(Box::new(Client::from_env()));
777 (*GLOBAL_JOBSERVER).clone()
779 has_global_allocator: Cell::new(false),
785 /// Holds data on the current incremental compilation session, if there is one.
787 pub enum IncrCompSession {
788 // This is the state the session will be in until the incr. comp. dir is
791 // This is the state during which the session directory is private and can
794 session_directory: PathBuf,
795 lock_file: flock::Lock,
797 // This is the state after the session directory has been finalized. In this
798 // state, the contents of the directory must not be modified any more.
800 session_directory: PathBuf,
802 // This is an error state that is reached when some compilation error has
803 // occurred. It indicates that the contents of the session directory must
804 // not be used, since they might be invalid.
805 InvalidBecauseOfErrors {
806 session_directory: PathBuf,
810 pub fn early_error(output: config::ErrorOutputType, msg: &str) -> ! {
811 let emitter: Box<Emitter> = match output {
812 config::ErrorOutputType::HumanReadable(color_config) => {
813 Box::new(EmitterWriter::stderr(color_config,
816 config::ErrorOutputType::Json => Box::new(JsonEmitter::basic()),
818 let handler = errors::Handler::with_emitter(true, false, emitter);
819 handler.emit(&MultiSpan::new(), msg, errors::Level::Fatal);
820 panic!(errors::FatalError);
823 pub fn early_warn(output: config::ErrorOutputType, msg: &str) {
824 let emitter: Box<Emitter> = match output {
825 config::ErrorOutputType::HumanReadable(color_config) => {
826 Box::new(EmitterWriter::stderr(color_config,
829 config::ErrorOutputType::Json => Box::new(JsonEmitter::basic()),
831 let handler = errors::Handler::with_emitter(true, false, emitter);
832 handler.emit(&MultiSpan::new(), msg, errors::Level::Warning);
835 #[derive(Copy, Clone, Debug)]
836 pub enum CompileIncomplete {
838 Errored(ErrorReported)
840 impl From<ErrorReported> for CompileIncomplete {
841 fn from(err: ErrorReported) -> CompileIncomplete {
842 CompileIncomplete::Errored(err)
845 pub type CompileResult = Result<(), CompileIncomplete>;
847 pub fn compile_result_from_err_count(err_count: usize) -> CompileResult {
851 Err(CompileIncomplete::Errored(ErrorReported))
857 pub fn bug_fmt(file: &'static str, line: u32, args: fmt::Arguments) -> ! {
858 // this wrapper mostly exists so I don't have to write a fully
859 // qualified path of None::<Span> inside the bug!() macro definition
860 opt_span_bug_fmt(file, line, None::<Span>, args);
865 pub fn span_bug_fmt<S: Into<MultiSpan>>(file: &'static str,
868 args: fmt::Arguments) -> ! {
869 opt_span_bug_fmt(file, line, Some(span), args);
872 fn opt_span_bug_fmt<S: Into<MultiSpan>>(file: &'static str,
875 args: fmt::Arguments) -> ! {
876 tls::with_opt(move |tcx| {
877 let msg = format!("{}:{}: {}", file, line, args);
879 (Some(tcx), Some(span)) => tcx.sess.diagnostic().span_bug(span, &msg),
880 (Some(tcx), None) => tcx.sess.diagnostic().bug(&msg),
881 (None, _) => panic!(msg)