1 // Copyright 2012-2015 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 use rustc
::dep_graph
::DepGraph
;
12 use rustc
::hir
::{self, map as hir_map}
;
13 use rustc
::hir
::lowering
::lower_crate
;
14 use rustc
::ich
::Fingerprint
;
15 use rustc_data_structures
::stable_hasher
::StableHasher
;
17 use rustc
::session
::{Session, CompileResult, CrateDisambiguator}
;
18 use rustc
::session
::CompileIncomplete
;
19 use rustc
::session
::config
::{self, Input, OutputFilenames, OutputType}
;
20 use rustc
::session
::search_paths
::PathKind
;
22 use rustc
::middle
::{self, stability, reachable}
;
23 use rustc
::middle
::cstore
::CrateStore
;
24 use rustc
::middle
::privacy
::AccessLevels
;
25 use rustc
::ty
::{self, TyCtxt, Resolutions, GlobalArenas}
;
27 use rustc
::util
::common
::{ErrorReported, time}
;
28 use rustc_allocator
as allocator
;
29 use rustc_borrowck
as borrowck
;
30 use rustc_incremental
;
31 use rustc_resolve
::{MakeGlobMap, Resolver}
;
32 use rustc_metadata
::creader
::CrateLoader
;
33 use rustc_metadata
::cstore
::{self, CStore}
;
34 use rustc_trans
as trans
;
35 use rustc_trans_utils
::trans_crate
::TransCrate
;
36 use rustc_typeck
as typeck
;
38 use rustc_plugin
::registry
::Registry
;
39 use rustc_plugin
as plugin
;
40 use rustc_passes
::{self, ast_validation, no_asm, loops, consts, static_recursion, hir_stats}
;
41 use rustc_const_eval
::{self, check_match}
;
42 use super::Compilation
;
43 use ::DefaultTransCrate
;
49 use std
::ffi
::{OsString, OsStr}
;
51 use std
::io
::{self, Write}
;
53 use std
::path
::{Path, PathBuf}
;
56 use syntax
::{ast, diagnostics, visit}
;
58 use syntax
::ext
::base
::ExtCtxt
;
59 use syntax
::parse
::{self, PResult}
;
60 use syntax
::util
::node_count
::NodeCounter
;
63 use arena
::DroplessArena
;
69 pub fn compile_input(sess
: &Session
,
72 outdir
: &Option
<PathBuf
>,
73 output
: &Option
<PathBuf
>,
74 addl_plugins
: Option
<Vec
<String
>>,
75 control
: &CompileController
) -> CompileResult
{
76 use rustc
::session
::config
::CrateType
;
78 macro_rules
! controller_entry_point
{
79 ($point
: ident
, $tsess
: expr
, $make_state
: expr
, $phase_result
: expr
) => {{
80 let state
= &mut $make_state
;
81 let phase_result
: &CompileResult
= &$phase_result
;
82 if phase_result
.is_ok() || control
.$point
.run_callback_on_error
{
83 (control
.$point
.callback
)(state
);
86 if control
.$point
.stop
== Compilation
::Stop
{
87 // FIXME: shouldn't this return Err(CompileIncomplete::Stopped)
88 // if there are no errors?
89 return $tsess
.compile_status();
94 if cfg
!(not(feature
="llvm")) {
95 for cty
in sess
.opts
.crate_types
.iter() {
97 CrateType
::CrateTypeRlib
| CrateType
::CrateTypeDylib
|
98 CrateType
::CrateTypeExecutable
=> {}
,
100 sess
.parse_sess
.span_diagnostic
.warn(
101 &format
!("LLVM unsupported, so output type {} is not supported", cty
)
107 sess
.abort_if_errors();
110 if sess
.profile_queries() {
114 // We need nested scopes here, because the intermediate results can keep
115 // large chunks of memory alive and we want to free them as soon as
116 // possible to keep the peak memory usage low
117 let (outputs
, trans
, dep_graph
) = {
118 let krate
= match phase_1_parse_input(control
, sess
, input
) {
120 Err(mut parse_error
) => {
122 return Err(CompileIncomplete
::Errored(ErrorReported
));
126 let (krate
, registry
) = {
127 let mut compile_state
= CompileState
::state_after_parse(input
,
133 controller_entry_point
!(after_parse
,
138 (compile_state
.krate
.unwrap(), compile_state
.registry
)
141 let outputs
= build_output_filenames(input
, outdir
, output
, &krate
.attrs
, sess
);
143 ::rustc_trans_utils
::link
::find_crate_name(Some(sess
), &krate
.attrs
, input
);
144 let ExpansionResult { expanded_crate, defs, analysis, resolutions, mut hir_forest }
= {
145 phase_2_configure_and_expand(
152 control
.make_glob_map
,
154 let mut state
= CompileState
::state_after_expand(
155 input
, sess
, outdir
, output
, &cstore
, expanded_crate
, &crate_name
,
157 controller_entry_point
!(after_expand
, sess
, state
, Ok(()));
163 write_out_deps(sess
, &outputs
, &crate_name
);
164 if sess
.opts
.output_types
.contains_key(&OutputType
::DepInfo
) &&
165 sess
.opts
.output_types
.keys().count() == 1 {
169 let arena
= DroplessArena
::new();
170 let arenas
= GlobalArenas
::new();
172 // Construct the HIR map
173 let hir_map
= time(sess
.time_passes(),
175 || hir_map
::map_crate(sess
, cstore
, &mut hir_forest
, &defs
));
178 let _ignore
= hir_map
.dep_graph
.in_ignore();
179 controller_entry_point
!(after_hir_lowering
,
181 CompileState
::state_after_hir_lowering(input
,
198 time(sess
.time_passes(), "attribute checking", || {
199 hir
::check_attr
::check_crate(sess
, &expanded_crate
);
202 let opt_crate
= if control
.keep_ast
{
203 Some(&expanded_crate
)
205 drop(expanded_crate
);
209 phase_3_run_analysis_passes(control
,
219 |tcx
, analysis
, rx
, result
| {
221 // Eventually, we will want to track plugins.
222 let _ignore
= tcx
.dep_graph
.in_ignore();
224 let mut state
= CompileState
::state_after_analysis(input
,
233 (control
.after_analysis
.callback
)(&mut state
);
235 if control
.after_analysis
.stop
== Compilation
::Stop
{
236 return result
.and_then(|_
| Err(CompileIncomplete
::Stopped
));
242 if log_enabled
!(::log
::LogLevel
::Info
) {
243 println
!("Pre-trans");
244 tcx
.print_debug_stats();
247 let trans
= phase_4_translate_to_llvm
::<DefaultTransCrate
>(tcx
, rx
);
249 if log_enabled
!(::log
::LogLevel
::Info
) {
250 println
!("Post-trans");
251 tcx
.print_debug_stats();
254 if tcx
.sess
.opts
.output_types
.contains_key(&OutputType
::Mir
) {
255 if let Err(e
) = mir
::transform
::dump_mir
::emit_mir(tcx
, &outputs
) {
256 sess
.err(&format
!("could not emit MIR: {}", e
));
257 sess
.abort_if_errors();
261 Ok((outputs
.clone(), trans
, tcx
.dep_graph
.clone()))
265 if sess
.opts
.debugging_opts
.print_type_sizes
{
266 sess
.code_stats
.borrow().print_type_sizes();
269 let (phase5_result
, trans
) =
270 phase_5_run_llvm_passes
::<DefaultTransCrate
>(sess
, &dep_graph
, trans
);
272 controller_entry_point
!(after_llvm
,
274 CompileState
::state_after_llvm(input
, sess
, outdir
, output
, &trans
),
278 // Run the linker on any artifacts that resulted from the LLVM run.
279 // This should produce either a finished executable or library.
280 time(sess
.time_passes(), "linking", || {
281 DefaultTransCrate
::link_binary(sess
, &trans
, &outputs
)
284 // Now that we won't touch anything in the incremental compilation directory
285 // any more, we can finalize it (which involves renaming it)
286 #[cfg(feature="llvm")]
287 rustc_incremental
::finalize_session_directory(sess
, trans
.link
.crate_hash
);
289 if sess
.opts
.debugging_opts
.perf_stats
{
290 sess
.print_perf_stats();
293 controller_entry_point
!(
296 CompileState
::state_when_compilation_done(input
, sess
, outdir
, output
),
303 fn keep_hygiene_data(sess
: &Session
) -> bool
{
304 sess
.opts
.debugging_opts
.keep_hygiene_data
308 /// The name used for source code that doesn't originate in a file
309 /// (e.g. source from stdin or a string)
310 pub fn anon_src() -> String
{
314 pub fn source_name(input
: &Input
) -> String
{
316 // FIXME (#9639): This needs to handle non-utf8 paths
317 Input
::File(ref ifile
) => ifile
.to_str().unwrap().to_string(),
318 Input
::Str { ref name, .. }
=> name
.clone(),
322 /// CompileController is used to customize compilation, it allows compilation to
323 /// be stopped and/or to call arbitrary code at various points in compilation.
324 /// It also allows for various flags to be set to influence what information gets
325 /// collected during compilation.
327 /// This is a somewhat higher level controller than a Session - the Session
328 /// controls what happens in each phase, whereas the CompileController controls
329 /// whether a phase is run at all and whether other code (from outside the
330 /// compiler) is run between phases.
332 /// Note that if compilation is set to stop and a callback is provided for a
333 /// given entry point, the callback is called before compilation is stopped.
335 /// Expect more entry points to be added in the future.
336 pub struct CompileController
<'a
> {
337 pub after_parse
: PhaseController
<'a
>,
338 pub after_expand
: PhaseController
<'a
>,
339 pub after_hir_lowering
: PhaseController
<'a
>,
340 pub after_analysis
: PhaseController
<'a
>,
341 pub after_llvm
: PhaseController
<'a
>,
342 pub compilation_done
: PhaseController
<'a
>,
344 // FIXME we probably want to group the below options together and offer a
345 // better API, rather than this ad-hoc approach.
346 pub make_glob_map
: MakeGlobMap
,
347 // Whether the compiler should keep the ast beyond parsing.
349 // -Zcontinue-parse-after-error
350 pub continue_parse_after_error
: bool
,
352 /// Allows overriding default rustc query providers,
353 /// after `default_provide` has installed them.
354 pub provide
: Box
<Fn(&mut ty
::maps
::Providers
) + 'a
>,
355 /// Same as `provide`, but only for non-local crates,
356 /// applied after `default_provide_extern`.
357 pub provide_extern
: Box
<Fn(&mut ty
::maps
::Providers
) + 'a
>,
360 impl<'a
> CompileController
<'a
> {
361 pub fn basic() -> CompileController
<'a
> {
363 after_parse
: PhaseController
::basic(),
364 after_expand
: PhaseController
::basic(),
365 after_hir_lowering
: PhaseController
::basic(),
366 after_analysis
: PhaseController
::basic(),
367 after_llvm
: PhaseController
::basic(),
368 compilation_done
: PhaseController
::basic(),
369 make_glob_map
: MakeGlobMap
::No
,
371 continue_parse_after_error
: false,
373 provide_extern
: box |_
| {}
,
378 pub struct PhaseController
<'a
> {
379 pub stop
: Compilation
,
380 // If true then the compiler will try to run the callback even if the phase
381 // ends with an error. Note that this is not always possible.
382 pub run_callback_on_error
: bool
,
383 pub callback
: Box
<Fn(&mut CompileState
) + 'a
>,
386 impl<'a
> PhaseController
<'a
> {
387 pub fn basic() -> PhaseController
<'a
> {
389 stop
: Compilation
::Continue
,
390 run_callback_on_error
: false,
391 callback
: box |_
| {}
,
396 /// State that is passed to a callback. What state is available depends on when
397 /// during compilation the callback is made. See the various constructor methods
398 /// (`state_*`) in the impl to see which data is provided for any given entry point.
399 pub struct CompileState
<'a
, 'tcx
: 'a
> {
400 pub input
: &'a Input
,
401 pub session
: &'tcx Session
,
402 pub krate
: Option
<ast
::Crate
>,
403 pub registry
: Option
<Registry
<'a
>>,
404 pub cstore
: Option
<&'tcx CStore
>,
405 pub crate_name
: Option
<&'a
str>,
406 pub output_filenames
: Option
<&'a OutputFilenames
>,
407 pub out_dir
: Option
<&'a Path
>,
408 pub out_file
: Option
<&'a Path
>,
409 pub arena
: Option
<&'tcx DroplessArena
>,
410 pub arenas
: Option
<&'tcx GlobalArenas
<'tcx
>>,
411 pub expanded_crate
: Option
<&'a ast
::Crate
>,
412 pub hir_crate
: Option
<&'a hir
::Crate
>,
413 pub hir_map
: Option
<&'a hir_map
::Map
<'tcx
>>,
414 pub resolutions
: Option
<&'a Resolutions
>,
415 pub analysis
: Option
<&'a ty
::CrateAnalysis
>,
416 pub tcx
: Option
<TyCtxt
<'a
, 'tcx
, 'tcx
>>,
417 pub trans
: Option
<&'a trans
::CrateTranslation
>,
420 impl<'a
, 'tcx
> CompileState
<'a
, 'tcx
> {
421 fn empty(input
: &'a Input
,
422 session
: &'tcx Session
,
423 out_dir
: &'a Option
<PathBuf
>)
428 out_dir
: out_dir
.as_ref().map(|s
| &**s
),
436 output_filenames
: None
,
437 expanded_crate
: None
,
447 fn state_after_parse(input
: &'a Input
,
448 session
: &'tcx Session
,
449 out_dir
: &'a Option
<PathBuf
>,
450 out_file
: &'a Option
<PathBuf
>,
452 cstore
: &'tcx CStore
)
455 // Initialize the registry before moving `krate`
456 registry
: Some(Registry
::new(&session
, krate
.span
)),
458 cstore
: Some(cstore
),
459 out_file
: out_file
.as_ref().map(|s
| &**s
),
460 ..CompileState
::empty(input
, session
, out_dir
)
464 fn state_after_expand(input
: &'a Input
,
465 session
: &'tcx Session
,
466 out_dir
: &'a Option
<PathBuf
>,
467 out_file
: &'a Option
<PathBuf
>,
468 cstore
: &'tcx CStore
,
469 expanded_crate
: &'a ast
::Crate
,
473 crate_name
: Some(crate_name
),
474 cstore
: Some(cstore
),
475 expanded_crate
: Some(expanded_crate
),
476 out_file
: out_file
.as_ref().map(|s
| &**s
),
477 ..CompileState
::empty(input
, session
, out_dir
)
481 fn state_after_hir_lowering(input
: &'a Input
,
482 session
: &'tcx Session
,
483 out_dir
: &'a Option
<PathBuf
>,
484 out_file
: &'a Option
<PathBuf
>,
485 arena
: &'tcx DroplessArena
,
486 arenas
: &'tcx GlobalArenas
<'tcx
>,
487 cstore
: &'tcx CStore
,
488 hir_map
: &'a hir_map
::Map
<'tcx
>,
489 analysis
: &'a ty
::CrateAnalysis
,
490 resolutions
: &'a Resolutions
,
491 krate
: &'a ast
::Crate
,
492 hir_crate
: &'a hir
::Crate
,
493 output_filenames
: &'a OutputFilenames
,
497 crate_name
: Some(crate_name
),
499 arenas
: Some(arenas
),
500 cstore
: Some(cstore
),
501 hir_map
: Some(hir_map
),
502 analysis
: Some(analysis
),
503 resolutions
: Some(resolutions
),
504 expanded_crate
: Some(krate
),
505 hir_crate
: Some(hir_crate
),
506 output_filenames
: Some(output_filenames
),
507 out_file
: out_file
.as_ref().map(|s
| &**s
),
508 ..CompileState
::empty(input
, session
, out_dir
)
512 fn state_after_analysis(input
: &'a Input
,
513 session
: &'tcx Session
,
514 out_dir
: &'a Option
<PathBuf
>,
515 out_file
: &'a Option
<PathBuf
>,
516 krate
: Option
<&'a ast
::Crate
>,
517 hir_crate
: &'a hir
::Crate
,
518 analysis
: &'a ty
::CrateAnalysis
,
519 tcx
: TyCtxt
<'a
, 'tcx
, 'tcx
>,
523 analysis
: Some(analysis
),
525 expanded_crate
: krate
,
526 hir_crate
: Some(hir_crate
),
527 crate_name
: Some(crate_name
),
528 out_file
: out_file
.as_ref().map(|s
| &**s
),
529 ..CompileState
::empty(input
, session
, out_dir
)
533 fn state_after_llvm(input
: &'a Input
,
534 session
: &'tcx Session
,
535 out_dir
: &'a Option
<PathBuf
>,
536 out_file
: &'a Option
<PathBuf
>,
537 trans
: &'a trans
::CrateTranslation
)
541 out_file
: out_file
.as_ref().map(|s
| &**s
),
542 ..CompileState
::empty(input
, session
, out_dir
)
546 fn state_when_compilation_done(input
: &'a Input
,
547 session
: &'tcx Session
,
548 out_dir
: &'a Option
<PathBuf
>,
549 out_file
: &'a Option
<PathBuf
>)
552 out_file
: out_file
.as_ref().map(|s
| &**s
),
553 ..CompileState
::empty(input
, session
, out_dir
)
558 pub fn phase_1_parse_input
<'a
>(control
: &CompileController
,
561 -> PResult
<'a
, ast
::Crate
> {
562 sess
.diagnostic().set_continue_after_error(control
.continue_parse_after_error
);
564 if sess
.profile_queries() {
568 let krate
= time(sess
.time_passes(), "parsing", || {
570 Input
::File(ref file
) => {
571 parse
::parse_crate_from_file(file
, &sess
.parse_sess
)
573 Input
::Str { ref input, ref name }
=> {
574 parse
::parse_crate_from_source_str(name
.clone(), input
.clone(), &sess
.parse_sess
)
579 sess
.diagnostic().set_continue_after_error(true);
581 if sess
.opts
.debugging_opts
.ast_json_noexpand
{
582 println
!("{}", json
::as_json(&krate
));
585 if sess
.opts
.debugging_opts
.input_stats
{
586 println
!("Lines of code: {}", sess
.codemap().count_lines());
587 println
!("Pre-expansion node count: {}", count_nodes(&krate
));
590 if let Some(ref s
) = sess
.opts
.debugging_opts
.show_span
{
591 syntax
::show_span
::run(sess
.diagnostic(), s
, &krate
);
594 if sess
.opts
.debugging_opts
.hir_stats
{
595 hir_stats
::print_ast_stats(&krate
, "PRE EXPANSION AST STATS");
601 fn count_nodes(krate
: &ast
::Crate
) -> usize {
602 let mut counter
= NodeCounter
::new();
603 visit
::walk_crate(&mut counter
, krate
);
607 // For continuing compilation after a parsed crate has been
610 pub struct ExpansionResult
{
611 pub expanded_crate
: ast
::Crate
,
612 pub defs
: hir_map
::Definitions
,
613 pub analysis
: ty
::CrateAnalysis
,
614 pub resolutions
: Resolutions
,
615 pub hir_forest
: hir_map
::Forest
,
618 /// Run the "early phases" of the compiler: initial `cfg` processing,
619 /// loading compiler plugins (including those from `addl_plugins`),
620 /// syntax expansion, secondary `cfg` expansion, synthesis of a test
621 /// harness if one is to be provided, injection of a dependency on the
622 /// standard library and prelude, and name resolution.
624 /// Returns `None` if we're aborting after handling -W help.
625 pub fn phase_2_configure_and_expand
<F
>(sess
: &Session
,
628 registry
: Option
<Registry
>,
630 addl_plugins
: Option
<Vec
<String
>>,
631 make_glob_map
: MakeGlobMap
,
633 -> Result
<ExpansionResult
, CompileIncomplete
>
634 where F
: FnOnce(&ast
::Crate
) -> CompileResult
,
636 let time_passes
= sess
.time_passes();
638 let (mut krate
, features
) = syntax
::config
::features(krate
, &sess
.parse_sess
, sess
.opts
.test
);
639 // these need to be set "early" so that expansion sees `quote` if enabled.
640 *sess
.features
.borrow_mut() = features
;
642 *sess
.crate_types
.borrow_mut() = collect_crate_types(sess
, &krate
.attrs
);
644 let disambiguator
= compute_crate_disambiguator(sess
);
645 *sess
.crate_disambiguator
.borrow_mut() = Some(disambiguator
);
646 rustc_incremental
::prepare_session_directory(
652 let dep_graph
= if sess
.opts
.build_dep_graph() {
653 let prev_dep_graph
= time(time_passes
, "load prev dep-graph", || {
654 rustc_incremental
::load_dep_graph(sess
)
657 DepGraph
::new(prev_dep_graph
)
659 DepGraph
::new_disabled()
662 time(time_passes
, "recursion limit", || {
663 middle
::recursion_limit
::update_limits(sess
, &krate
);
666 krate
= time(time_passes
, "crate injection", || {
667 let alt_std_name
= sess
.opts
.alt_std_name
.clone();
668 syntax
::std_inject
::maybe_inject_crates_ref(krate
, alt_std_name
)
671 let mut addl_plugins
= Some(addl_plugins
);
672 let registrars
= time(time_passes
, "plugin loading", || {
673 plugin
::load
::load_plugins(sess
,
677 addl_plugins
.take().unwrap())
680 let mut registry
= registry
.unwrap_or(Registry
::new(sess
, krate
.span
));
682 time(time_passes
, "plugin registration", || {
683 if sess
.features
.borrow().rustc_diagnostic_macros
{
684 registry
.register_macro("__diagnostic_used",
685 diagnostics
::plugin
::expand_diagnostic_used
);
686 registry
.register_macro("__register_diagnostic",
687 diagnostics
::plugin
::expand_register_diagnostic
);
688 registry
.register_macro("__build_diagnostic_array",
689 diagnostics
::plugin
::expand_build_diagnostic_array
);
692 for registrar
in registrars
{
693 registry
.args_hidden
= Some(registrar
.args
);
694 (registrar
.fun
)(&mut registry
);
698 let whitelisted_legacy_custom_derives
= registry
.take_whitelisted_custom_derives();
699 let Registry
{ syntax_exts
, early_lint_passes
, late_lint_passes
, lint_groups
,
700 llvm_passes
, attributes
, .. } = registry
;
702 sess
.track_errors(|| {
703 let mut ls
= sess
.lint_store
.borrow_mut();
704 for pass
in early_lint_passes
{
705 ls
.register_early_pass(Some(sess
), true, pass
);
707 for pass
in late_lint_passes
{
708 ls
.register_late_pass(Some(sess
), true, pass
);
711 for (name
, to
) in lint_groups
{
712 ls
.register_group(Some(sess
), true, name
, to
);
715 *sess
.plugin_llvm_passes
.borrow_mut() = llvm_passes
;
716 *sess
.plugin_attributes
.borrow_mut() = attributes
.clone();
719 // Lint plugins are registered; now we can process command line flags.
720 if sess
.opts
.describe_lints
{
721 super::describe_lints(&sess
.lint_store
.borrow(), true);
722 return Err(CompileIncomplete
::Stopped
);
725 // Currently, we ignore the name resolution data structures for the purposes of dependency
726 // tracking. Instead we will run name resolution and include its output in the hash of each
727 // item, much like we do for macro expansion. In other words, the hash reflects not just
728 // its contents but the results of name resolution on those contents. Hopefully we'll push
729 // this back at some point.
730 let mut crate_loader
= CrateLoader
::new(sess
, &cstore
, crate_name
);
731 let resolver_arenas
= Resolver
::arenas();
732 let mut resolver
= Resolver
::new(sess
,
739 resolver
.whitelisted_legacy_custom_derives
= whitelisted_legacy_custom_derives
;
740 syntax_ext
::register_builtins(&mut resolver
, syntax_exts
, sess
.features
.borrow().quote
);
742 krate
= time(time_passes
, "expansion", || {
743 // Windows dlls do not have rpaths, so they don't know how to find their
744 // dependencies. It's up to us to tell the system where to find all the
745 // dependent dlls. Note that this uses cfg!(windows) as opposed to
746 // targ_cfg because syntax extensions are always loaded for the host
747 // compiler, not for the target.
749 // This is somewhat of an inherently racy operation, however, as
750 // multiple threads calling this function could possibly continue
751 // extending PATH far beyond what it should. To solve this for now we
752 // just don't add any new elements to PATH which are already there
753 // within PATH. This is basically a targeted fix at #17360 for rustdoc
754 // which runs rustc in parallel but has been seen (#33844) to cause
755 // problems with PATH becoming too long.
756 let mut old_path
= OsString
::new();
758 old_path
= env
::var_os("PATH").unwrap_or(old_path
);
759 let mut new_path
= sess
.host_filesearch(PathKind
::All
)
760 .get_dylib_search_paths();
761 for path
in env
::split_paths(&old_path
) {
762 if !new_path
.contains(&path
) {
767 &env
::join_paths(new_path
.iter()
768 .filter(|p
| env
::join_paths(iter
::once(p
)).is_ok()))
771 let features
= sess
.features
.borrow();
772 let cfg
= syntax
::ext
::expand
::ExpansionConfig
{
773 features
: Some(&features
),
774 recursion_limit
: sess
.recursion_limit
.get(),
775 trace_mac
: sess
.opts
.debugging_opts
.trace_macros
,
776 should_test
: sess
.opts
.test
,
777 ..syntax
::ext
::expand
::ExpansionConfig
::default(crate_name
.to_string())
780 let mut ecx
= ExtCtxt
::new(&sess
.parse_sess
, cfg
, &mut resolver
);
781 let err_count
= ecx
.parse_sess
.span_diagnostic
.err_count();
783 let krate
= ecx
.monotonic_expander().expand_crate(krate
);
785 ecx
.check_unused_macros();
787 let mut missing_fragment_specifiers
: Vec
<_
> =
788 ecx
.parse_sess
.missing_fragment_specifiers
.borrow().iter().cloned().collect();
789 missing_fragment_specifiers
.sort();
790 for span
in missing_fragment_specifiers
{
791 let lint
= lint
::builtin
::MISSING_FRAGMENT_SPECIFIER
;
792 let msg
= "missing fragment specifier";
793 sess
.buffer_lint(lint
, ast
::CRATE_NODE_ID
, span
, msg
);
795 if ecx
.parse_sess
.span_diagnostic
.err_count() - ecx
.resolve_err_count
> err_count
{
796 ecx
.parse_sess
.span_diagnostic
.abort_if_errors();
799 env
::set_var("PATH", &old_path
);
804 krate
= time(time_passes
, "maybe building test harness", || {
805 syntax
::test
::modify_for_testing(&sess
.parse_sess
,
812 // If we're in rustdoc we're always compiling as an rlib, but that'll trip a
813 // bunch of checks in the `modify` function below. For now just skip this
814 // step entirely if we're rustdoc as it's not too useful anyway.
815 if !sess
.opts
.actually_rustdoc
{
816 krate
= time(time_passes
, "maybe creating a macro crate", || {
817 let crate_types
= sess
.crate_types
.borrow();
818 let num_crate_types
= crate_types
.len();
819 let is_proc_macro_crate
= crate_types
.contains(&config
::CrateTypeProcMacro
);
820 let is_test_crate
= sess
.opts
.test
;
821 syntax_ext
::proc_macro_registrar
::modify(&sess
.parse_sess
,
831 krate
= time(time_passes
, "creating allocators", || {
832 allocator
::expand
::modify(&sess
.parse_sess
,
838 after_expand(&krate
)?
;
840 if sess
.opts
.debugging_opts
.input_stats
{
841 println
!("Post-expansion node count: {}", count_nodes(&krate
));
844 if sess
.opts
.debugging_opts
.hir_stats
{
845 hir_stats
::print_ast_stats(&krate
, "POST EXPANSION AST STATS");
848 if sess
.opts
.debugging_opts
.ast_json
{
849 println
!("{}", json
::as_json(&krate
));
853 "checking for inline asm in case the target doesn't support it",
854 || no_asm
::check_crate(sess
, &krate
));
858 || ast_validation
::check_crate(sess
, &krate
));
860 time(time_passes
, "name resolution", || -> CompileResult
{
861 resolver
.resolve_crate(&krate
);
865 if resolver
.found_unresolved_macro
{
866 sess
.parse_sess
.span_diagnostic
.abort_if_errors();
869 // Needs to go *after* expansion to be able to check the results of macro expansion.
870 time(time_passes
, "complete gated feature checking", || {
871 sess
.track_errors(|| {
872 syntax
::feature_gate
::check_crate(&krate
,
874 &sess
.features
.borrow(),
876 sess
.opts
.unstable_features
);
881 let hir_forest
= time(time_passes
, "lowering ast -> hir", || {
882 let hir_crate
= lower_crate(sess
, cstore
, &dep_graph
, &krate
, &mut resolver
);
884 if sess
.opts
.debugging_opts
.hir_stats
{
885 hir_stats
::print_hir_stats(&hir_crate
);
888 hir_map
::Forest
::new(hir_crate
, &dep_graph
)
893 || lint
::check_ast_crate(sess
, &krate
));
895 // Discard hygiene data, which isn't required after lowering to HIR.
896 if !keep_hygiene_data(sess
) {
897 syntax
::ext
::hygiene
::clear_markings();
901 expanded_crate
: krate
,
902 defs
: resolver
.definitions
,
903 analysis
: ty
::CrateAnalysis
{
904 access_levels
: Rc
::new(AccessLevels
::default()),
905 name
: crate_name
.to_string(),
906 glob_map
: if resolver
.make_glob_map { Some(resolver.glob_map) }
else { None }
,
908 resolutions
: Resolutions
{
909 freevars
: resolver
.freevars
,
910 export_map
: resolver
.export_map
,
911 trait_map
: resolver
.trait_map
,
912 maybe_unused_trait_imports
: resolver
.maybe_unused_trait_imports
,
913 maybe_unused_extern_crates
: resolver
.maybe_unused_extern_crates
,
919 pub fn default_provide(providers
: &mut ty
::maps
::Providers
) {
920 borrowck
::provide(providers
);
921 mir
::provide(providers
);
922 reachable
::provide(providers
);
923 rustc_privacy
::provide(providers
);
924 DefaultTransCrate
::provide(providers
);
925 typeck
::provide(providers
);
926 ty
::provide(providers
);
927 traits
::provide(providers
);
928 reachable
::provide(providers
);
929 rustc_const_eval
::provide(providers
);
930 rustc_passes
::provide(providers
);
931 middle
::region
::provide(providers
);
932 cstore
::provide(providers
);
933 lint
::provide(providers
);
936 pub fn default_provide_extern(providers
: &mut ty
::maps
::Providers
) {
937 cstore
::provide_extern(providers
);
938 DefaultTransCrate
::provide_extern(providers
);
941 /// Run the resolution, typechecking, region checking and other
942 /// miscellaneous analysis passes on the crate. Return various
943 /// structures carrying the results of the analysis.
944 pub fn phase_3_run_analysis_passes
<'tcx
, F
, R
>(control
: &CompileController
,
946 cstore
: &'tcx CrateStore
,
947 hir_map
: hir_map
::Map
<'tcx
>,
948 mut analysis
: ty
::CrateAnalysis
,
949 resolutions
: Resolutions
,
950 arena
: &'tcx DroplessArena
,
951 arenas
: &'tcx GlobalArenas
<'tcx
>,
953 output_filenames
: &OutputFilenames
,
955 -> Result
<R
, CompileIncomplete
>
956 where F
: for<'a
> FnOnce(TyCtxt
<'a
, 'tcx
, 'tcx
>,
958 mpsc
::Receiver
<Box
<Any
+ Send
>>,
961 macro_rules
! try_with_f
{
962 ($e
: expr
, ($
($t
:tt
)*)) => {
973 let time_passes
= sess
.time_passes();
975 let query_result_on_disk_cache
= time(time_passes
,
976 "load query result cache",
977 || rustc_incremental
::load_query_result_cache(sess
));
979 let named_region_map
= time(time_passes
,
980 "lifetime resolution",
981 || middle
::resolve_lifetime
::krate(sess
, cstore
, &hir_map
))?
;
984 "looking for entry point",
985 || middle
::entry
::find_entry_point(sess
, &hir_map
));
987 sess
.plugin_registrar_fn
.set(time(time_passes
, "looking for plugin registrar", || {
988 plugin
::build
::find_plugin_registrar(sess
.diagnostic(), &hir_map
)
990 sess
.derive_registrar_fn
.set(derive_registrar
::find(&hir_map
));
994 || loops
::check_crate(sess
, &hir_map
));
997 "static item recursion checking",
998 || static_recursion
::check_crate(sess
, &hir_map
))?
;
1000 let mut local_providers
= ty
::maps
::Providers
::default();
1001 default_provide(&mut local_providers
);
1002 (control
.provide
)(&mut local_providers
);
1004 let mut extern_providers
= local_providers
;
1005 default_provide_extern(&mut extern_providers
);
1006 (control
.provide_extern
)(&mut extern_providers
);
1008 let (tx
, rx
) = mpsc
::channel();
1010 TyCtxt
::create_and_enter(sess
,
1019 query_result_on_disk_cache
,
1024 // Do some initialization of the DepGraph that can only be done with the
1026 rustc_incremental
::dep_graph_tcx_init(tcx
);
1029 "stability checking",
1030 || stability
::check_unstable_api_usage(tcx
));
1032 // passes are timed inside typeck
1033 try_with_f
!(typeck
::check_crate(tcx
), (tcx
, analysis
, rx
));
1037 || consts
::check_crate(tcx
));
1039 analysis
.access_levels
=
1040 time(time_passes
, "privacy checking", || rustc_privacy
::check_crate(tcx
));
1043 "intrinsic checking",
1044 || middle
::intrinsicck
::check_crate(tcx
));
1048 || check_match
::check_crate(tcx
));
1050 // this must run before MIR dump, because
1051 // "not all control paths return a value" is reported here.
1053 // maybe move the check to a MIR pass?
1055 "liveness checking",
1056 || middle
::liveness
::check_crate(tcx
));
1060 || borrowck
::check_crate(tcx
));
1063 "MIR borrow checking",
1064 || for def_id
in tcx
.body_owners() { tcx.mir_borrowck(def_id) }
);
1067 "MIR effect checking",
1068 || for def_id
in tcx
.body_owners() {
1069 mir
::transform
::check_unsafety
::check_unsafety(tcx
, def_id
)
1071 // Avoid overwhelming user with errors if type checking failed.
1072 // I'm not sure how helpful this is, to be honest, but it avoids
1074 // lot of annoying errors in the compile-fail tests (basically,
1075 // lint warnings and so on -- kindck used to do this abort, but
1076 // kindck is gone now). -nmatsakis
1077 if sess
.err_count() > 0 {
1078 return Ok(f(tcx
, analysis
, rx
, sess
.compile_status()));
1081 time(time_passes
, "death checking", || middle
::dead
::check_crate(tcx
));
1083 time(time_passes
, "unused lib feature checking", || {
1084 stability
::check_unused_or_stable_features(tcx
)
1087 time(time_passes
, "lint checking", || lint
::check_crate(tcx
));
1089 return Ok(f(tcx
, analysis
, rx
, tcx
.sess
.compile_status()));
1093 /// Run the translation phase to LLVM, after which the AST and analysis can
1095 pub fn phase_4_translate_to_llvm
<'a
, 'tcx
, Trans
: TransCrate
>(tcx
: TyCtxt
<'a
, 'tcx
, 'tcx
>,
1096 rx
: mpsc
::Receiver
<Box
<Any
+ Send
>>)
1097 -> <Trans
as TransCrate
>::OngoingCrateTranslation
{
1098 let time_passes
= tcx
.sess
.time_passes();
1101 "resolving dependency formats",
1102 || ::rustc
::middle
::dependency_format
::calculate(tcx
));
1105 time(time_passes
, "translation", move || {
1106 Trans
::trans_crate(tcx
, rx
)
1108 if tcx
.sess
.profile_queries() {
1109 profile
::dump("profile_queries".to_string())
1115 /// Run LLVM itself, producing a bitcode file, assembly file or object file
1116 /// as a side effect.
1117 pub fn phase_5_run_llvm_passes
<Trans
: TransCrate
>(sess
: &Session
,
1118 dep_graph
: &DepGraph
,
1119 trans
: <Trans
as TransCrate
>::OngoingCrateTranslation
)
1120 -> (CompileResult
, <Trans
as TransCrate
>::TranslatedCrate
) {
1121 let trans
= Trans
::join_trans(trans
, sess
, dep_graph
);
1123 if sess
.opts
.debugging_opts
.incremental_info
{
1124 Trans
::dump_incremental_data(&trans
);
1127 time(sess
.time_passes(),
1128 "serialize work products",
1129 move || rustc_incremental
::save_work_products(sess
, dep_graph
));
1131 (sess
.compile_status(), trans
)
1134 fn escape_dep_filename(filename
: &str) -> String
{
1135 // Apparently clang and gcc *only* escape spaces:
1136 // http://llvm.org/klaus/clang/commit/9d50634cfc268ecc9a7250226dd5ca0e945240d4
1137 filename
.replace(" ", "\\ ")
1140 fn write_out_deps(sess
: &Session
, outputs
: &OutputFilenames
, crate_name
: &str) {
1141 let mut out_filenames
= Vec
::new();
1142 for output_type
in sess
.opts
.output_types
.keys() {
1143 let file
= outputs
.path(*output_type
);
1144 match *output_type
{
1145 OutputType
::Exe
=> {
1146 for output
in sess
.crate_types
.borrow().iter() {
1147 let p
= ::rustc_trans_utils
::link
::filename_for_input(
1153 out_filenames
.push(p
);
1157 out_filenames
.push(file
);
1162 // Write out dependency rules to the dep-info file if requested
1163 if !sess
.opts
.output_types
.contains_key(&OutputType
::DepInfo
) {
1166 let deps_filename
= outputs
.path(OutputType
::DepInfo
);
1169 (|| -> io
::Result
<()> {
1170 // Build a list of files used to compile the output and
1171 // write Makefile-compatible dependency rules
1172 let files
: Vec
<String
> = sess
.codemap()
1175 .filter(|fmap
| fmap
.is_real_file())
1176 .filter(|fmap
| !fmap
.is_imported())
1177 .map(|fmap
| escape_dep_filename(&fmap
.name
))
1179 let mut file
= fs
::File
::create(&deps_filename
)?
;
1180 for path
in &out_filenames
{
1181 write
!(file
, "{}: {}\n\n", path
.display(), files
.join(" "))?
;
1184 // Emit a fake target for each input file to the compilation. This
1185 // prevents `make` from spitting out an error if a file is later
1186 // deleted. For more info see #28735
1188 writeln
!(file
, "{}:", path
)?
;
1196 sess
.fatal(&format
!("error writing dependencies to `{}`: {}",
1197 deps_filename
.display(),
1203 pub fn collect_crate_types(session
: &Session
, attrs
: &[ast
::Attribute
]) -> Vec
<config
::CrateType
> {
1204 // Unconditionally collect crate types from attributes to make them used
1205 let attr_types
: Vec
<config
::CrateType
> =
1208 if a
.check_name("crate_type") {
1209 match a
.value_str() {
1210 Some(ref n
) if *n
== "rlib" => {
1211 Some(config
::CrateTypeRlib
)
1213 Some(ref n
) if *n
== "dylib" => {
1214 Some(config
::CrateTypeDylib
)
1216 Some(ref n
) if *n
== "cdylib" => {
1217 Some(config
::CrateTypeCdylib
)
1219 Some(ref n
) if *n
== "lib" => {
1220 Some(config
::default_lib_output())
1222 Some(ref n
) if *n
== "staticlib" => {
1223 Some(config
::CrateTypeStaticlib
)
1225 Some(ref n
) if *n
== "proc-macro" => {
1226 Some(config
::CrateTypeProcMacro
)
1228 Some(ref n
) if *n
== "bin" => Some(config
::CrateTypeExecutable
),
1230 session
.buffer_lint(lint
::builtin
::UNKNOWN_CRATE_TYPES
,
1233 "invalid `crate_type` value");
1237 session
.struct_span_err(a
.span
, "`crate_type` requires a value")
1238 .note("for example: `#![crate_type=\"lib\"]`")
1249 // If we're generating a test executable, then ignore all other output
1250 // styles at all other locations
1251 if session
.opts
.test
{
1252 return vec
![config
::CrateTypeExecutable
];
1255 // Only check command line flags if present. If no types are specified by
1256 // command line, then reuse the empty `base` Vec to hold the types that
1257 // will be found in crate attributes.
1258 let mut base
= session
.opts
.crate_types
.clone();
1259 if base
.is_empty() {
1260 base
.extend(attr_types
);
1261 if base
.is_empty() {
1262 base
.push(::rustc_trans_utils
::link
::default_output_for_target(session
));
1269 .filter(|crate_type
| {
1270 let res
= !::rustc_trans_utils
::link
::invalid_output_for_target(session
, *crate_type
);
1273 session
.warn(&format
!("dropping unsupported crate type `{}` for target `{}`",
1275 session
.opts
.target_triple
));
1283 pub fn compute_crate_disambiguator(session
: &Session
) -> CrateDisambiguator
{
1284 use std
::hash
::Hasher
;
1286 // The crate_disambiguator is a 128 bit hash. The disambiguator is fed
1287 // into various other hashes quite a bit (symbol hashes, incr. comp. hashes,
1288 // debuginfo type IDs, etc), so we don't want it to be too wide. 128 bits
1289 // should still be safe enough to avoid collisions in practice.
1290 let mut hasher
= StableHasher
::<Fingerprint
>::new();
1292 let mut metadata
= session
.opts
.cg
.metadata
.clone();
1293 // We don't want the crate_disambiguator to dependent on the order
1294 // -C metadata arguments, so sort them:
1296 // Every distinct -C metadata value is only incorporated once:
1299 hasher
.write(b
"metadata");
1300 for s
in &metadata
{
1301 // Also incorporate the length of a metadata string, so that we generate
1302 // different values for `-Cmetadata=ab -Cmetadata=c` and
1303 // `-Cmetadata=a -Cmetadata=bc`
1304 hasher
.write_usize(s
.len());
1305 hasher
.write(s
.as_bytes());
1308 // Also incorporate crate type, so that we don't get symbol conflicts when
1309 // linking against a library of the same name, if this is an executable.
1310 let is_exe
= session
.crate_types
.borrow().contains(&config
::CrateTypeExecutable
);
1311 hasher
.write(if is_exe { b"exe" }
else { b"lib" }
);
1313 CrateDisambiguator
::from(hasher
.finish())
1317 pub fn build_output_filenames(input
: &Input
,
1318 odir
: &Option
<PathBuf
>,
1319 ofile
: &Option
<PathBuf
>,
1320 attrs
: &[ast
::Attribute
],
1322 -> OutputFilenames
{
1325 // "-" as input file will cause the parser to read from stdin so we
1326 // have to make up a name
1327 // We want to toss everything after the final '.'
1328 let dirpath
= match *odir
{
1329 Some(ref d
) => d
.clone(),
1330 None
=> PathBuf
::new(),
1333 // If a crate name is present, we use it as the link name
1334 let stem
= sess
.opts
1337 .or_else(|| attr
::find_crate_name(attrs
).map(|n
| n
.to_string()))
1338 .unwrap_or(input
.filestem());
1341 out_directory
: dirpath
,
1343 single_output_file
: None
,
1344 extra
: sess
.opts
.cg
.extra_filename
.clone(),
1345 outputs
: sess
.opts
.output_types
.clone(),
1349 Some(ref out_file
) => {
1350 let unnamed_output_types
= sess
.opts
1353 .filter(|a
| a
.is_none())
1355 let ofile
= if unnamed_output_types
> 1 {
1356 sess
.warn("due to multiple output types requested, the explicitly specified \
1357 output file name will be adapted for each output type");
1360 Some(out_file
.clone())
1363 sess
.warn("ignoring --out-dir flag due to -o flag.");
1366 let cur_dir
= Path
::new("");
1369 out_directory
: out_file
.parent().unwrap_or(cur_dir
).to_path_buf(),
1370 out_filestem
: out_file
.file_stem()
1371 .unwrap_or(OsStr
::new(""))
1375 single_output_file
: ofile
,
1376 extra
: sess
.opts
.cg
.extra_filename
.clone(),
1377 outputs
: sess
.opts
.output_types
.clone(),