2019-03-26 18:07:13 +00:00
|
|
|
use crate::interface::{Compiler, Result};
|
|
|
|
use crate::proc_macro_decls;
|
2019-12-22 22:42:04 +00:00
|
|
|
use crate::util;
|
2018-12-08 19:30:23 +00:00
|
|
|
|
2020-10-31 19:47:57 +00:00
|
|
|
use rustc_ast::mut_visit::MutVisitor;
|
2021-12-07 10:28:12 +00:00
|
|
|
use rustc_ast::{self as ast, visit};
|
2020-12-30 17:48:40 +00:00
|
|
|
use rustc_borrowck as mir_borrowck;
|
2019-04-30 05:53:30 +00:00
|
|
|
use rustc_codegen_ssa::back::link::emit_metadata;
|
2020-03-12 23:07:58 +00:00
|
|
|
use rustc_codegen_ssa::traits::CodegenBackend;
|
2021-05-31 13:17:04 +00:00
|
|
|
use rustc_data_structures::parallel;
|
2021-07-18 16:12:17 +00:00
|
|
|
use rustc_data_structures::sync::{Lrc, OnceCell, WorkerLocal};
|
2020-08-09 01:05:50 +00:00
|
|
|
use rustc_data_structures::temp_dir::MaybeTempDir;
|
2021-12-03 00:20:25 +00:00
|
|
|
use rustc_errors::{Applicability, ErrorReported, PResult};
|
2021-12-11 07:32:48 +00:00
|
|
|
use rustc_expand::base::{ExtCtxt, LintStoreExpand, ResolverExpand};
|
2021-06-08 16:36:30 +00:00
|
|
|
use rustc_hir::def_id::{StableCrateId, LOCAL_CRATE};
|
2020-02-06 12:41:37 +00:00
|
|
|
use rustc_hir::Crate;
|
2021-12-07 10:28:12 +00:00
|
|
|
use rustc_lint::{EarlyCheckNode, LintStore};
|
2020-08-09 23:57:35 +00:00
|
|
|
use rustc_metadata::creader::CStore;
|
2021-09-24 16:15:36 +00:00
|
|
|
use rustc_metadata::{encode_metadata, EncodedMetadata};
|
2020-03-29 14:41:09 +00:00
|
|
|
use rustc_middle::arena::Arena;
|
|
|
|
use rustc_middle::dep_graph::DepGraph;
|
2021-05-30 15:24:54 +00:00
|
|
|
use rustc_middle::ty::query::{ExternProviders, Providers};
|
2021-09-28 22:17:54 +00:00
|
|
|
use rustc_middle::ty::{self, GlobalCtxt, RegisteredTools, ResolverOutputs, TyCtxt};
|
2020-01-05 15:46:44 +00:00
|
|
|
use rustc_mir_build as mir_build;
|
2021-09-17 20:08:56 +00:00
|
|
|
use rustc_parse::{parse_crate_from_file, parse_crate_from_source_str, validate_attr};
|
2020-01-05 09:58:44 +00:00
|
|
|
use rustc_passes::{self, hir_stats, layout_test};
|
2019-11-16 20:35:27 +00:00
|
|
|
use rustc_plugin_impl as plugin;
|
2021-06-28 19:12:01 +00:00
|
|
|
use rustc_query_impl::{OnDiskCache, Queries as TcxQueries};
|
2018-12-08 19:30:23 +00:00
|
|
|
use rustc_resolve::{Resolver, ResolverArenas};
|
2021-04-09 14:35:40 +00:00
|
|
|
use rustc_serialize::json;
|
2020-05-01 22:30:23 +00:00
|
|
|
use rustc_session::config::{CrateType, Input, OutputFilenames, OutputType, PpMode, PpSourceMode};
|
2020-11-14 02:02:03 +00:00
|
|
|
use rustc_session::cstore::{MetadataLoader, MetadataLoaderDyn};
|
2020-12-19 21:30:56 +00:00
|
|
|
use rustc_session::lint;
|
2020-03-12 23:07:58 +00:00
|
|
|
use rustc_session::output::{filename_for_input, filename_for_metadata};
|
2020-03-11 11:49:08 +00:00
|
|
|
use rustc_session::search_paths::PathKind;
|
2021-09-17 20:08:56 +00:00
|
|
|
use rustc_session::{Limit, Session};
|
2021-12-07 10:28:12 +00:00
|
|
|
use rustc_span::symbol::{sym, Symbol};
|
2021-08-29 08:34:23 +00:00
|
|
|
use rustc_span::{FileName, MultiSpan};
|
2020-02-11 20:19:40 +00:00
|
|
|
use rustc_trait_selection::traits;
|
2018-12-08 19:30:23 +00:00
|
|
|
use rustc_typeck as typeck;
|
2019-04-30 05:53:30 +00:00
|
|
|
use tempfile::Builder as TempFileBuilder;
|
2021-04-09 14:35:40 +00:00
|
|
|
use tracing::{info, warn};
|
2018-12-08 19:30:23 +00:00
|
|
|
|
|
|
|
use std::any::Any;
|
2019-12-22 22:42:04 +00:00
|
|
|
use std::cell::RefCell;
|
2018-12-08 19:30:23 +00:00
|
|
|
use std::ffi::OsString;
|
2020-01-22 15:22:46 +00:00
|
|
|
use std::io::{self, BufWriter, Write};
|
2020-08-30 10:41:36 +00:00
|
|
|
use std::lazy::SyncLazy;
|
2021-06-01 08:47:39 +00:00
|
|
|
use std::marker::PhantomPinned;
|
2021-11-07 09:33:27 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
2021-05-31 13:20:50 +00:00
|
|
|
use std::pin::Pin;
|
2018-12-08 19:30:23 +00:00
|
|
|
use std::rc::Rc;
|
2021-03-30 16:17:14 +00:00
|
|
|
use std::{env, fs, iter};
|
2018-12-08 19:30:23 +00:00
|
|
|
|
2018-12-08 19:30:23 +00:00
|
|
|
pub fn parse<'a>(sess: &'a Session, input: &Input) -> PResult<'a, ast::Crate> {
|
2020-01-07 20:34:08 +00:00
|
|
|
let krate = sess.time("parse_crate", || match input {
|
2020-01-01 01:24:05 +00:00
|
|
|
Input::File(file) => parse_crate_from_file(file, &sess.parse_sess),
|
|
|
|
Input::Str { input, name } => {
|
|
|
|
parse_crate_from_source_str(name.clone(), input.clone(), &sess.parse_sess)
|
2019-09-27 12:04:36 +00:00
|
|
|
}
|
2018-12-08 19:30:23 +00:00
|
|
|
})?;
|
|
|
|
|
|
|
|
if sess.opts.debugging_opts.ast_json_noexpand {
|
|
|
|
println!("{}", json::as_json(&krate));
|
|
|
|
}
|
|
|
|
|
|
|
|
if sess.opts.debugging_opts.input_stats {
|
2021-02-18 12:13:38 +00:00
|
|
|
eprintln!("Lines of code: {}", sess.source_map().count_lines());
|
|
|
|
eprintln!("Pre-expansion node count: {}", count_nodes(&krate));
|
2018-12-08 19:30:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(ref s) = sess.opts.debugging_opts.show_span {
|
2020-01-05 10:21:26 +00:00
|
|
|
rustc_ast_passes::show_span::run(sess.diagnostic(), s, &krate);
|
2018-12-08 19:30:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if sess.opts.debugging_opts.hir_stats {
|
|
|
|
hir_stats::print_ast_stats(&krate, "PRE EXPANSION AST STATS");
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(krate)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn count_nodes(krate: &ast::Crate) -> usize {
|
2020-01-11 08:48:57 +00:00
|
|
|
let mut counter = rustc_ast_passes::node_count::NodeCounter::new();
|
2018-12-08 19:30:23 +00:00
|
|
|
visit::walk_crate(&mut counter, krate);
|
|
|
|
counter.count
|
|
|
|
}
|
|
|
|
|
2021-06-01 08:49:42 +00:00
|
|
|
pub use boxed_resolver::BoxedResolver;
|
|
|
|
mod boxed_resolver {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
pub struct BoxedResolver(Pin<Box<BoxedResolverInner>>);
|
|
|
|
|
|
|
|
struct BoxedResolverInner {
|
|
|
|
session: Lrc<Session>,
|
2021-06-09 12:47:01 +00:00
|
|
|
resolver_arenas: Option<ResolverArenas<'static>>,
|
|
|
|
resolver: Option<Resolver<'static>>,
|
2021-06-01 08:49:42 +00:00
|
|
|
_pin: PhantomPinned,
|
|
|
|
}
|
2018-12-08 19:30:23 +00:00
|
|
|
|
2021-06-09 12:47:01 +00:00
|
|
|
// Note: Drop order is important to prevent dangling references. Resolver must be dropped first,
|
2021-06-30 18:52:00 +00:00
|
|
|
// then resolver_arenas and session.
|
2021-06-09 12:47:01 +00:00
|
|
|
impl Drop for BoxedResolverInner {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
self.resolver.take();
|
|
|
|
self.resolver_arenas.take();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-01 08:49:42 +00:00
|
|
|
impl BoxedResolver {
|
2021-06-30 18:52:00 +00:00
|
|
|
pub(super) fn new(
|
|
|
|
session: Lrc<Session>,
|
|
|
|
make_resolver: impl for<'a> FnOnce(&'a Session, &'a ResolverArenas<'a>) -> Resolver<'a>,
|
|
|
|
) -> BoxedResolver {
|
2021-06-01 08:49:42 +00:00
|
|
|
let mut boxed_resolver = Box::new(BoxedResolverInner {
|
|
|
|
session,
|
2021-06-09 12:47:01 +00:00
|
|
|
resolver_arenas: Some(Resolver::arenas()),
|
2021-06-01 08:49:42 +00:00
|
|
|
resolver: None,
|
|
|
|
_pin: PhantomPinned,
|
|
|
|
});
|
2021-06-09 12:51:42 +00:00
|
|
|
// SAFETY: `make_resolver` takes a resolver arena with an arbitrary lifetime and
|
|
|
|
// returns a resolver with the same lifetime as the arena. We ensure that the arena
|
|
|
|
// outlives the resolver in the drop impl and elsewhere so these transmutes are sound.
|
2021-06-01 08:49:42 +00:00
|
|
|
unsafe {
|
2021-05-24 16:45:21 +00:00
|
|
|
let resolver = make_resolver(
|
2021-06-01 08:49:42 +00:00
|
|
|
std::mem::transmute::<&Session, &Session>(&boxed_resolver.session),
|
|
|
|
std::mem::transmute::<&ResolverArenas<'_>, &ResolverArenas<'_>>(
|
2021-06-09 12:47:01 +00:00
|
|
|
boxed_resolver.resolver_arenas.as_ref().unwrap(),
|
2021-06-01 08:49:42 +00:00
|
|
|
),
|
2021-05-24 16:45:21 +00:00
|
|
|
);
|
2021-06-10 17:36:27 +00:00
|
|
|
boxed_resolver.resolver = Some(resolver);
|
2021-05-24 16:45:21 +00:00
|
|
|
BoxedResolver(Pin::new_unchecked(boxed_resolver))
|
2021-06-01 08:49:42 +00:00
|
|
|
}
|
2021-06-01 08:47:39 +00:00
|
|
|
}
|
2021-05-31 13:17:04 +00:00
|
|
|
|
2021-06-01 08:49:42 +00:00
|
|
|
pub fn access<F: for<'a> FnOnce(&mut Resolver<'a>) -> R, R>(&mut self, f: F) -> R {
|
2021-06-09 12:51:42 +00:00
|
|
|
// SAFETY: The resolver doesn't need to be pinned.
|
2021-06-01 08:49:42 +00:00
|
|
|
let mut resolver = unsafe {
|
|
|
|
self.0.as_mut().map_unchecked_mut(|boxed_resolver| &mut boxed_resolver.resolver)
|
|
|
|
};
|
|
|
|
f((&mut *resolver).as_mut().unwrap())
|
|
|
|
}
|
2021-05-31 13:17:04 +00:00
|
|
|
|
2021-06-01 08:49:42 +00:00
|
|
|
pub fn to_resolver_outputs(resolver: Rc<RefCell<BoxedResolver>>) -> ResolverOutputs {
|
|
|
|
match Rc::try_unwrap(resolver) {
|
|
|
|
Ok(resolver) => {
|
|
|
|
let mut resolver = resolver.into_inner();
|
2021-06-09 12:51:42 +00:00
|
|
|
// SAFETY: The resolver doesn't need to be pinned.
|
2021-06-01 08:49:42 +00:00
|
|
|
let mut resolver = unsafe {
|
|
|
|
resolver
|
|
|
|
.0
|
|
|
|
.as_mut()
|
|
|
|
.map_unchecked_mut(|boxed_resolver| &mut boxed_resolver.resolver)
|
|
|
|
};
|
|
|
|
resolver.take().unwrap().into_outputs()
|
|
|
|
}
|
|
|
|
Err(resolver) => resolver.borrow_mut().access(|resolver| resolver.clone_outputs()),
|
2021-06-01 08:04:13 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-31 13:17:04 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-08 19:30:23 +00:00
|
|
|
|
2021-05-24 16:45:21 +00:00
|
|
|
pub fn create_resolver(
|
2018-12-08 19:30:23 +00:00
|
|
|
sess: Lrc<Session>,
|
2019-10-20 00:28:36 +00:00
|
|
|
metadata_loader: Box<MetadataLoaderDyn>,
|
2021-05-24 16:45:21 +00:00
|
|
|
krate: &ast::Crate,
|
2018-12-08 19:30:23 +00:00
|
|
|
crate_name: &str,
|
2021-05-24 16:45:21 +00:00
|
|
|
) -> BoxedResolver {
|
|
|
|
tracing::trace!("create_resolver");
|
2021-06-01 08:47:39 +00:00
|
|
|
BoxedResolver::new(sess, move |sess, resolver_arenas| {
|
2021-09-30 17:38:50 +00:00
|
|
|
Resolver::new(sess, krate, crate_name, metadata_loader, resolver_arenas)
|
2021-06-01 08:15:28 +00:00
|
|
|
})
|
2018-12-08 19:30:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn register_plugins<'a>(
|
|
|
|
sess: &'a Session,
|
2019-10-16 16:57:10 +00:00
|
|
|
metadata_loader: &'a dyn MetadataLoader,
|
2020-01-09 06:52:01 +00:00
|
|
|
register_lints: impl Fn(&Session, &mut LintStore),
|
2018-12-08 19:30:23 +00:00
|
|
|
mut krate: ast::Crate,
|
|
|
|
crate_name: &str,
|
2021-09-28 08:53:33 +00:00
|
|
|
) -> Result<(ast::Crate, LintStore)> {
|
2020-01-07 20:34:08 +00:00
|
|
|
krate = sess.time("attributes_injection", || {
|
2019-12-29 14:23:55 +00:00
|
|
|
rustc_builtin_macros::cmdline_attrs::inject(
|
2019-12-22 22:42:04 +00:00
|
|
|
krate,
|
|
|
|
&sess.parse_sess,
|
|
|
|
&sess.opts.debugging_opts.crate_attr,
|
2019-09-06 20:41:54 +00:00
|
|
|
)
|
2018-12-08 19:30:23 +00:00
|
|
|
});
|
|
|
|
|
2020-07-30 01:27:50 +00:00
|
|
|
let (krate, features) = rustc_expand::config::features(sess, krate);
|
2018-12-08 19:30:23 +00:00
|
|
|
// these need to be set "early" so that expansion sees `quote` if enabled.
|
|
|
|
sess.init_features(features);
|
|
|
|
|
|
|
|
let crate_types = util::collect_crate_types(sess, &krate.attrs);
|
2020-05-16 04:44:28 +00:00
|
|
|
sess.init_crate_types(crate_types);
|
2018-12-08 19:30:23 +00:00
|
|
|
|
2021-06-08 16:36:30 +00:00
|
|
|
let stable_crate_id = StableCrateId::new(
|
|
|
|
crate_name,
|
|
|
|
sess.crate_types().contains(&CrateType::Executable),
|
|
|
|
sess.opts.cg.metadata.clone(),
|
|
|
|
);
|
|
|
|
sess.stable_crate_id.set(stable_crate_id).expect("not yet initialized");
|
2021-09-30 17:38:50 +00:00
|
|
|
rustc_incremental::prepare_session_directory(sess, crate_name, stable_crate_id)?;
|
2018-12-08 19:30:23 +00:00
|
|
|
|
|
|
|
if sess.opts.incremental.is_some() {
|
2020-01-07 20:34:08 +00:00
|
|
|
sess.time("incr_comp_garbage_collect_session_directories", || {
|
2018-12-08 19:30:23 +00:00
|
|
|
if let Err(e) = rustc_incremental::garbage_collect_session_directories(sess) {
|
|
|
|
warn!(
|
|
|
|
"Error while trying to garbage collect incremental \
|
|
|
|
compilation cache directory: {}",
|
|
|
|
e
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-10-09 13:53:13 +00:00
|
|
|
let mut lint_store = rustc_lint::new_lint_store(
|
|
|
|
sess.opts.debugging_opts.no_interleave_lints,
|
|
|
|
sess.unstable_options(),
|
|
|
|
);
|
2021-09-30 17:38:50 +00:00
|
|
|
register_lints(sess, &mut lint_store);
|
2019-10-09 13:53:13 +00:00
|
|
|
|
2019-12-22 22:42:04 +00:00
|
|
|
let registrars =
|
2020-01-07 20:34:08 +00:00
|
|
|
sess.time("plugin_loading", || plugin::load::load_plugins(sess, metadata_loader, &krate));
|
|
|
|
sess.time("plugin_registration", || {
|
2019-11-30 12:35:25 +00:00
|
|
|
let mut registry = plugin::Registry { lint_store: &mut lint_store };
|
2018-12-08 19:30:23 +00:00
|
|
|
for registrar in registrars {
|
2019-11-29 22:20:06 +00:00
|
|
|
registrar(&mut registry);
|
2018-12-08 19:30:23 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-08-13 19:41:52 +00:00
|
|
|
Ok((krate, lint_store))
|
2018-12-08 19:30:23 +00:00
|
|
|
}
|
|
|
|
|
2021-12-07 10:28:12 +00:00
|
|
|
fn pre_expansion_lint<'a>(
|
2021-01-22 19:33:21 +00:00
|
|
|
sess: &Session,
|
|
|
|
lint_store: &LintStore,
|
2021-09-28 22:17:54 +00:00
|
|
|
registered_tools: &RegisteredTools,
|
2021-12-07 10:28:12 +00:00
|
|
|
check_node: impl EarlyCheckNode<'a>,
|
2021-09-27 21:28:49 +00:00
|
|
|
node_name: &str,
|
2021-01-22 19:33:21 +00:00
|
|
|
) {
|
2021-09-27 21:28:49 +00:00
|
|
|
sess.prof.generic_activity_with_arg("pre_AST_expansion_lint_checks", node_name).run(|| {
|
|
|
|
rustc_lint::check_ast_node(
|
2018-12-08 19:30:23 +00:00
|
|
|
sess,
|
2021-09-27 21:28:49 +00:00
|
|
|
true,
|
2019-10-09 13:53:13 +00:00
|
|
|
lint_store,
|
2021-09-28 22:17:54 +00:00
|
|
|
registered_tools,
|
2019-10-25 17:41:51 +00:00
|
|
|
None,
|
2019-12-22 22:42:04 +00:00
|
|
|
rustc_lint::BuiltinCombinedPreExpansionLintPass::new(),
|
2021-09-27 21:28:49 +00:00
|
|
|
check_node,
|
2019-12-22 22:42:04 +00:00
|
|
|
);
|
2018-12-08 19:30:23 +00:00
|
|
|
});
|
2020-03-15 23:43:37 +00:00
|
|
|
}
|
|
|
|
|
2021-12-11 07:32:48 +00:00
|
|
|
// Cannot implement directly for `LintStore` due to trait coherence.
|
|
|
|
struct LintStoreExpandImpl<'a>(&'a LintStore);
|
|
|
|
|
|
|
|
impl LintStoreExpand for LintStoreExpandImpl<'_> {
|
|
|
|
fn pre_expansion_lint(
|
|
|
|
&self,
|
|
|
|
sess: &Session,
|
|
|
|
registered_tools: &RegisteredTools,
|
|
|
|
node_id: ast::NodeId,
|
|
|
|
attrs: &[ast::Attribute],
|
|
|
|
items: &[rustc_ast::ptr::P<ast::Item>],
|
|
|
|
name: &str,
|
|
|
|
) {
|
|
|
|
pre_expansion_lint(sess, self.0, registered_tools, (node_id, attrs, items), name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-24 16:45:21 +00:00
|
|
|
/// Runs the "early phases" of the compiler: initial `cfg` processing, loading compiler plugins,
|
|
|
|
/// syntax expansion, secondary `cfg` expansion, synthesis of a test
|
|
|
|
/// harness if one is to be provided, injection of a dependency on the
|
|
|
|
/// standard library and prelude, and name resolution.
|
|
|
|
pub fn configure_and_expand(
|
|
|
|
sess: &Session,
|
2021-06-01 09:52:44 +00:00
|
|
|
lint_store: &LintStore,
|
2020-03-15 23:43:37 +00:00
|
|
|
mut krate: ast::Crate,
|
|
|
|
crate_name: &str,
|
2021-05-24 16:45:21 +00:00
|
|
|
resolver: &mut Resolver<'_>,
|
|
|
|
) -> Result<ast::Crate> {
|
|
|
|
tracing::trace!("configure_and_expand");
|
2021-09-28 22:17:54 +00:00
|
|
|
pre_expansion_lint(sess, lint_store, resolver.registered_tools(), &krate, crate_name);
|
2021-05-24 16:45:21 +00:00
|
|
|
rustc_builtin_macros::register_builtin_macros(resolver);
|
2019-08-25 20:03:24 +00:00
|
|
|
|
2020-01-07 20:34:08 +00:00
|
|
|
krate = sess.time("crate_injection", || {
|
2022-01-14 13:25:52 +00:00
|
|
|
rustc_builtin_macros::standard_library_imports::inject(krate, resolver, sess)
|
2019-08-25 20:03:24 +00:00
|
|
|
});
|
|
|
|
|
2021-09-30 17:38:50 +00:00
|
|
|
util::check_attr_crate_type(sess, &krate.attrs, &mut resolver.lint_buffer());
|
2019-10-25 17:23:18 +00:00
|
|
|
|
2018-12-08 19:30:23 +00:00
|
|
|
// Expand all macros
|
2020-01-07 20:34:08 +00:00
|
|
|
krate = sess.time("macro_expand_crate", || {
|
2018-12-08 19:30:23 +00:00
|
|
|
// Windows dlls do not have rpaths, so they don't know how to find their
|
|
|
|
// dependencies. It's up to us to tell the system where to find all the
|
|
|
|
// dependent dlls. Note that this uses cfg!(windows) as opposed to
|
|
|
|
// targ_cfg because syntax extensions are always loaded for the host
|
|
|
|
// compiler, not for the target.
|
|
|
|
//
|
|
|
|
// This is somewhat of an inherently racy operation, however, as
|
|
|
|
// multiple threads calling this function could possibly continue
|
|
|
|
// extending PATH far beyond what it should. To solve this for now we
|
|
|
|
// just don't add any new elements to PATH which are already there
|
|
|
|
// within PATH. This is basically a targeted fix at #17360 for rustdoc
|
|
|
|
// which runs rustc in parallel but has been seen (#33844) to cause
|
|
|
|
// problems with PATH becoming too long.
|
|
|
|
let mut old_path = OsString::new();
|
|
|
|
if cfg!(windows) {
|
|
|
|
old_path = env::var_os("PATH").unwrap_or(old_path);
|
|
|
|
let mut new_path = sess.host_filesearch(PathKind::All).search_path_dirs();
|
|
|
|
for path in env::split_paths(&old_path) {
|
|
|
|
if !new_path.contains(&path) {
|
|
|
|
new_path.push(path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
env::set_var(
|
|
|
|
"PATH",
|
|
|
|
&env::join_paths(
|
2019-12-22 22:42:04 +00:00
|
|
|
new_path.iter().filter(|p| env::join_paths(iter::once(p)).is_ok()),
|
|
|
|
)
|
|
|
|
.unwrap(),
|
2018-12-08 19:30:23 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the config for macro expansion
|
|
|
|
let features = sess.features_untracked();
|
2021-09-30 17:38:50 +00:00
|
|
|
let recursion_limit = get_recursion_limit(&krate.attrs, sess);
|
2019-12-29 14:23:55 +00:00
|
|
|
let cfg = rustc_expand::expand::ExpansionConfig {
|
2021-09-30 17:38:50 +00:00
|
|
|
features: Some(features),
|
2021-06-25 23:48:26 +00:00
|
|
|
recursion_limit,
|
2018-12-08 19:30:23 +00:00
|
|
|
trace_mac: sess.opts.debugging_opts.trace_macros,
|
|
|
|
should_test: sess.opts.test,
|
2020-05-31 20:20:50 +00:00
|
|
|
span_debug: sess.opts.debugging_opts.span_debug,
|
2020-08-31 02:17:24 +00:00
|
|
|
proc_macro_backtrace: sess.opts.debugging_opts.proc_macro_backtrace,
|
2019-12-29 14:23:55 +00:00
|
|
|
..rustc_expand::expand::ExpansionConfig::default(crate_name.to_string())
|
2018-12-08 19:30:23 +00:00
|
|
|
};
|
|
|
|
|
2021-12-11 07:32:48 +00:00
|
|
|
let lint_store = LintStoreExpandImpl(lint_store);
|
|
|
|
let mut ecx = ExtCtxt::new(sess, cfg, resolver, Some(&lint_store));
|
2018-12-08 19:30:23 +00:00
|
|
|
|
|
|
|
// Expand macros now!
|
2020-01-07 20:34:08 +00:00
|
|
|
let krate = sess.time("expand_crate", || ecx.monotonic_expander().expand_crate(krate));
|
2018-12-08 19:30:23 +00:00
|
|
|
|
|
|
|
// The rest is error reporting
|
|
|
|
|
2020-01-07 20:34:08 +00:00
|
|
|
sess.time("check_unused_macros", || {
|
2018-12-08 19:30:23 +00:00
|
|
|
ecx.check_unused_macros();
|
|
|
|
});
|
|
|
|
|
2020-12-19 21:30:56 +00:00
|
|
|
let mut missing_fragment_specifiers: Vec<_> = ecx
|
|
|
|
.sess
|
|
|
|
.parse_sess
|
|
|
|
.missing_fragment_specifiers
|
|
|
|
.borrow()
|
|
|
|
.iter()
|
|
|
|
.map(|(span, node_id)| (*span, *node_id))
|
|
|
|
.collect();
|
|
|
|
missing_fragment_specifiers.sort_unstable_by_key(|(span, _)| *span);
|
|
|
|
|
|
|
|
let recursion_limit_hit = ecx.reduced_recursion_limit.is_some();
|
|
|
|
|
|
|
|
for (span, node_id) in missing_fragment_specifiers {
|
|
|
|
let lint = lint::builtin::MISSING_FRAGMENT_SPECIFIER;
|
|
|
|
let msg = "missing fragment specifier";
|
|
|
|
resolver.lint_buffer().buffer_lint(lint, node_id, span, msg);
|
|
|
|
}
|
2018-12-08 19:30:23 +00:00
|
|
|
if cfg!(windows) {
|
|
|
|
env::set_var("PATH", &old_path);
|
|
|
|
}
|
2020-02-26 22:43:49 +00:00
|
|
|
|
|
|
|
if recursion_limit_hit {
|
|
|
|
// If we hit a recursion limit, exit early to avoid later passes getting overwhelmed
|
|
|
|
// with a large AST
|
|
|
|
Err(ErrorReported)
|
|
|
|
} else {
|
|
|
|
Ok(krate)
|
|
|
|
}
|
|
|
|
})?;
|
2018-12-08 19:30:23 +00:00
|
|
|
|
2020-01-07 20:34:08 +00:00
|
|
|
sess.time("maybe_building_test_harness", || {
|
2021-09-30 17:38:50 +00:00
|
|
|
rustc_builtin_macros::test_harness::inject(sess, resolver, &mut krate)
|
2018-12-08 19:30:23 +00:00
|
|
|
});
|
|
|
|
|
2021-02-18 19:21:18 +00:00
|
|
|
if let Some(PpMode::Source(PpSourceMode::EveryBodyLoops)) = sess.opts.pretty {
|
2020-08-14 06:05:01 +00:00
|
|
|
tracing::debug!("replacing bodies with loop {{}}");
|
2021-05-24 16:45:21 +00:00
|
|
|
util::ReplaceBodyWithLoop::new(resolver).visit_crate(&mut krate);
|
2018-12-08 19:30:23 +00:00
|
|
|
}
|
|
|
|
|
2020-01-07 20:34:08 +00:00
|
|
|
let has_proc_macro_decls = sess.time("AST_validation", || {
|
2021-05-24 16:45:21 +00:00
|
|
|
rustc_ast_passes::ast_validation::check_crate(sess, &krate, resolver.lint_buffer())
|
2018-12-08 19:30:23 +00:00
|
|
|
});
|
|
|
|
|
2020-05-16 04:44:28 +00:00
|
|
|
let crate_types = sess.crate_types();
|
2022-01-15 13:23:45 +00:00
|
|
|
let is_executable_crate = crate_types.contains(&CrateType::Executable);
|
2020-05-01 22:30:23 +00:00
|
|
|
let is_proc_macro_crate = crate_types.contains(&CrateType::ProcMacro);
|
2019-08-28 22:00:36 +00:00
|
|
|
|
2022-01-15 13:23:45 +00:00
|
|
|
if crate_types.len() > 1 {
|
|
|
|
if is_executable_crate {
|
|
|
|
sess.err("cannot mix `bin` crate type with others");
|
|
|
|
}
|
|
|
|
if is_proc_macro_crate {
|
|
|
|
sess.err("cannot mix `proc-macro` crate type with others");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-28 22:00:36 +00:00
|
|
|
// For backwards compatibility, we don't try to run proc macro injection
|
|
|
|
// if rustdoc is run on a proc macro crate without '--crate-type proc-macro' being
|
|
|
|
// specified. This should only affect users who manually invoke 'rustdoc', as
|
|
|
|
// 'cargo doc' will automatically pass the proper '--crate-type' flags.
|
|
|
|
// However, we do emit a warning, to let such users know that they should
|
|
|
|
// start passing '--crate-type proc-macro'
|
|
|
|
if has_proc_macro_decls && sess.opts.actually_rustdoc && !is_proc_macro_crate {
|
2019-12-22 22:42:04 +00:00
|
|
|
let mut msg = sess.diagnostic().struct_warn(
|
2021-09-30 17:38:50 +00:00
|
|
|
"Trying to document proc macro crate \
|
|
|
|
without passing '--crate-type proc-macro to rustdoc",
|
2019-12-22 22:42:04 +00:00
|
|
|
);
|
2019-08-28 22:00:36 +00:00
|
|
|
|
|
|
|
msg.warn("The generated documentation may be incorrect");
|
|
|
|
msg.emit()
|
|
|
|
} else {
|
2020-01-07 20:34:08 +00:00
|
|
|
krate = sess.time("maybe_create_a_macro_crate", || {
|
2019-08-28 22:00:36 +00:00
|
|
|
let is_test_crate = sess.opts.test;
|
2019-12-29 14:23:55 +00:00
|
|
|
rustc_builtin_macros::proc_macro_harness::inject(
|
2021-09-30 17:38:50 +00:00
|
|
|
sess,
|
2021-05-24 16:45:21 +00:00
|
|
|
resolver,
|
2019-08-28 22:00:36 +00:00
|
|
|
krate,
|
|
|
|
is_proc_macro_crate,
|
|
|
|
has_proc_macro_decls,
|
|
|
|
is_test_crate,
|
|
|
|
sess.diagnostic(),
|
|
|
|
)
|
|
|
|
});
|
|
|
|
}
|
2018-12-08 19:30:23 +00:00
|
|
|
|
|
|
|
// Done with macro expansion!
|
|
|
|
|
|
|
|
if sess.opts.debugging_opts.input_stats {
|
2021-02-18 12:13:38 +00:00
|
|
|
eprintln!("Post-expansion node count: {}", count_nodes(&krate));
|
2018-12-08 19:30:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if sess.opts.debugging_opts.hir_stats {
|
|
|
|
hir_stats::print_ast_stats(&krate, "POST EXPANSION AST STATS");
|
|
|
|
}
|
|
|
|
|
|
|
|
if sess.opts.debugging_opts.ast_json {
|
|
|
|
println!("{}", json::as_json(&krate));
|
|
|
|
}
|
|
|
|
|
2020-01-07 20:34:08 +00:00
|
|
|
resolver.resolve_crate(&krate);
|
2018-12-08 19:30:23 +00:00
|
|
|
|
|
|
|
// Needs to go *after* expansion to be able to check the results of macro expansion.
|
2020-01-07 20:34:08 +00:00
|
|
|
sess.time("complete_gated_feature_checking", || {
|
2020-07-30 01:27:50 +00:00
|
|
|
rustc_ast_passes::feature_gate::check_crate(&krate, sess);
|
2018-12-08 19:30:23 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Add all buffered lints from the `ParseSess` to the `Session`.
|
2021-10-03 19:04:36 +00:00
|
|
|
// The ReplaceBodyWithLoop pass may have deleted some AST nodes, potentially
|
|
|
|
// causing a delay_span_bug later if a buffered lint refers to such a deleted
|
|
|
|
// AST node (issue #87308). Since everybody_loops is for pretty-printing only,
|
|
|
|
// anyway, we simply skip all buffered lints here.
|
|
|
|
if !matches!(sess.opts.pretty, Some(PpMode::Source(PpSourceMode::EveryBodyLoops))) {
|
|
|
|
sess.parse_sess.buffered_lints.with_lock(|buffered_lints| {
|
|
|
|
info!("{} parse sess buffered_lints", buffered_lints.len());
|
|
|
|
for early_lint in buffered_lints.drain(..) {
|
|
|
|
resolver.lint_buffer().add_early_lint(early_lint);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2018-12-08 19:30:23 +00:00
|
|
|
|
2021-08-29 08:34:23 +00:00
|
|
|
// Gate identifiers containing invalid Unicode codepoints that were recovered during lexing.
|
|
|
|
sess.parse_sess.bad_unicode_identifiers.with_lock(|identifiers| {
|
2021-09-14 11:39:49 +00:00
|
|
|
let mut identifiers: Vec<_> = identifiers.drain().collect();
|
|
|
|
identifiers.sort_by_key(|&(key, _)| key);
|
|
|
|
for (ident, mut spans) in identifiers.into_iter() {
|
|
|
|
spans.sort();
|
2021-12-03 00:20:25 +00:00
|
|
|
if ident == sym::ferris {
|
|
|
|
let first_span = spans[0];
|
|
|
|
sess.diagnostic()
|
|
|
|
.struct_span_err(
|
|
|
|
MultiSpan::from(spans),
|
|
|
|
"Ferris cannot be used as an identifier",
|
|
|
|
)
|
|
|
|
.span_suggestion(
|
|
|
|
first_span,
|
|
|
|
"try using their name instead",
|
|
|
|
"ferris".to_string(),
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
)
|
|
|
|
.emit();
|
|
|
|
} else {
|
|
|
|
sess.diagnostic().span_err(
|
|
|
|
MultiSpan::from(spans),
|
|
|
|
&format!("identifiers cannot contain emoji: `{}`", ident),
|
|
|
|
);
|
|
|
|
}
|
2021-08-29 08:34:23 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-05-24 16:45:21 +00:00
|
|
|
Ok(krate)
|
2018-12-08 19:30:23 +00:00
|
|
|
}
|
|
|
|
|
2019-11-28 10:49:29 +00:00
|
|
|
pub fn lower_to_hir<'res, 'tcx>(
|
|
|
|
sess: &'tcx Session,
|
2020-01-09 06:52:01 +00:00
|
|
|
lint_store: &LintStore,
|
2019-11-28 10:49:29 +00:00
|
|
|
resolver: &'res mut Resolver<'_>,
|
2021-06-20 16:52:35 +00:00
|
|
|
krate: Rc<ast::Crate>,
|
2020-03-21 01:21:21 +00:00
|
|
|
arena: &'tcx rustc_ast_lowering::Arena<'tcx>,
|
2021-06-11 16:55:14 +00:00
|
|
|
) -> &'tcx Crate<'tcx> {
|
2019-09-30 00:03:41 +00:00
|
|
|
// Lower AST to HIR.
|
2020-01-07 20:34:08 +00:00
|
|
|
let hir_crate = rustc_ast_lowering::lower_crate(
|
|
|
|
sess,
|
2021-06-20 16:52:35 +00:00
|
|
|
&*krate,
|
2020-01-07 20:34:08 +00:00
|
|
|
resolver,
|
|
|
|
rustc_parse::nt_to_tokenstream,
|
|
|
|
arena,
|
|
|
|
);
|
2018-12-08 19:30:23 +00:00
|
|
|
|
2020-01-07 20:34:08 +00:00
|
|
|
sess.time("early_lint_checks", || {
|
2021-09-28 22:17:54 +00:00
|
|
|
let lint_buffer = Some(std::mem::take(resolver.lint_buffer()));
|
2021-09-27 21:28:49 +00:00
|
|
|
rustc_lint::check_ast_node(
|
2019-10-09 12:46:11 +00:00
|
|
|
sess,
|
2021-09-27 21:28:49 +00:00
|
|
|
false,
|
2019-10-09 13:53:13 +00:00
|
|
|
lint_store,
|
2021-09-28 22:17:54 +00:00
|
|
|
resolver.registered_tools(),
|
|
|
|
lint_buffer,
|
2019-10-09 12:46:11 +00:00
|
|
|
rustc_lint::BuiltinCombinedEarlyLintPass::new(),
|
2021-12-07 10:28:12 +00:00
|
|
|
&*krate,
|
2019-10-09 12:46:11 +00:00
|
|
|
)
|
2018-12-08 19:30:23 +00:00
|
|
|
});
|
|
|
|
|
2021-06-11 16:55:30 +00:00
|
|
|
// Drop AST to free memory
|
|
|
|
sess.time("drop_ast", || std::mem::drop(krate));
|
|
|
|
|
2018-12-08 19:30:23 +00:00
|
|
|
// Discard hygiene data, which isn't required after lowering to HIR.
|
|
|
|
if !sess.opts.debugging_opts.keep_hygiene_data {
|
2019-12-31 17:15:40 +00:00
|
|
|
rustc_span::hygiene::clear_syntax_context_map();
|
2018-12-08 19:30:23 +00:00
|
|
|
}
|
|
|
|
|
2020-02-06 12:41:37 +00:00
|
|
|
hir_crate
|
2018-12-08 19:30:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns all the paths that correspond to generated files.
|
|
|
|
fn generated_output_paths(
|
2018-12-08 19:30:23 +00:00
|
|
|
sess: &Session,
|
|
|
|
outputs: &OutputFilenames,
|
|
|
|
exact_name: bool,
|
|
|
|
crate_name: &str,
|
|
|
|
) -> Vec<PathBuf> {
|
|
|
|
let mut out_filenames = Vec::new();
|
|
|
|
for output_type in sess.opts.output_types.keys() {
|
|
|
|
let file = outputs.path(*output_type);
|
|
|
|
match *output_type {
|
|
|
|
// If the filename has been overridden using `-o`, it will not be modified
|
|
|
|
// by appending `.rlib`, `.exe`, etc., so we can skip this transformation.
|
2019-12-22 22:42:04 +00:00
|
|
|
OutputType::Exe if !exact_name => {
|
2020-05-16 04:44:28 +00:00
|
|
|
for crate_type in sess.crate_types().iter() {
|
2020-03-12 23:07:58 +00:00
|
|
|
let p = filename_for_input(sess, *crate_type, crate_name, outputs);
|
2019-12-22 22:42:04 +00:00
|
|
|
out_filenames.push(p);
|
|
|
|
}
|
|
|
|
}
|
2018-12-08 19:30:23 +00:00
|
|
|
OutputType::DepInfo if sess.opts.debugging_opts.dep_info_omit_d_target => {
|
|
|
|
// Don't add the dep-info output when omitting it from dep-info targets
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
out_filenames.push(file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out_filenames
|
|
|
|
}
|
|
|
|
|
|
|
|
// Runs `f` on every output file path and returns the first non-None result, or None if `f`
|
|
|
|
// returns None for every file path.
|
|
|
|
fn check_output<F, T>(output_paths: &[PathBuf], f: F) -> Option<T>
|
|
|
|
where
|
|
|
|
F: Fn(&PathBuf) -> Option<T>,
|
|
|
|
{
|
|
|
|
for output_path in output_paths {
|
|
|
|
if let Some(result) = f(output_path) {
|
|
|
|
return Some(result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2021-11-07 09:33:27 +00:00
|
|
|
fn output_contains_path(output_paths: &[PathBuf], input_path: &Path) -> bool {
|
2018-12-08 19:30:23 +00:00
|
|
|
let input_path = input_path.canonicalize().ok();
|
|
|
|
if input_path.is_none() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
let check = |output_path: &PathBuf| {
|
2019-12-22 22:42:04 +00:00
|
|
|
if output_path.canonicalize().ok() == input_path { Some(()) } else { None }
|
2018-12-08 19:30:23 +00:00
|
|
|
};
|
|
|
|
check_output(output_paths, check).is_some()
|
|
|
|
}
|
|
|
|
|
2018-12-08 19:30:23 +00:00
|
|
|
fn output_conflicts_with_dir(output_paths: &[PathBuf]) -> Option<PathBuf> {
|
2019-12-06 12:18:32 +00:00
|
|
|
let check = |output_path: &PathBuf| output_path.is_dir().then(|| output_path.clone());
|
2018-12-08 19:30:23 +00:00
|
|
|
check_output(output_paths, check)
|
|
|
|
}
|
|
|
|
|
2021-11-07 09:33:27 +00:00
|
|
|
fn escape_dep_filename(filename: &str) -> String {
|
2018-12-08 19:30:23 +00:00
|
|
|
// Apparently clang and gcc *only* escape spaces:
|
2021-06-23 20:26:46 +00:00
|
|
|
// https://llvm.org/klaus/clang/commit/9d50634cfc268ecc9a7250226dd5ca0e945240d4
|
2021-12-13 21:58:58 +00:00
|
|
|
filename.replace(' ', "\\ ")
|
2018-12-08 19:30:23 +00:00
|
|
|
}
|
|
|
|
|
2020-05-03 17:47:51 +00:00
|
|
|
// Makefile comments only need escaping newlines and `\`.
|
|
|
|
// The result can be unescaped by anything that can unescape `escape_default` and friends.
|
|
|
|
fn escape_dep_env(symbol: Symbol) -> String {
|
|
|
|
let s = symbol.as_str();
|
|
|
|
let mut escaped = String::with_capacity(s.len());
|
|
|
|
for c in s.chars() {
|
|
|
|
match c {
|
|
|
|
'\n' => escaped.push_str(r"\n"),
|
|
|
|
'\r' => escaped.push_str(r"\r"),
|
|
|
|
'\\' => escaped.push_str(r"\\"),
|
|
|
|
_ => escaped.push(c),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
escaped
|
|
|
|
}
|
|
|
|
|
2019-10-20 00:28:36 +00:00
|
|
|
fn write_out_deps(
|
|
|
|
sess: &Session,
|
2021-06-11 16:55:30 +00:00
|
|
|
boxed_resolver: &RefCell<BoxedResolver>,
|
2019-10-20 00:28:36 +00:00
|
|
|
outputs: &OutputFilenames,
|
|
|
|
out_filenames: &[PathBuf],
|
|
|
|
) {
|
2018-12-08 19:30:23 +00:00
|
|
|
// Write out dependency rules to the dep-info file if requested
|
|
|
|
if !sess.opts.output_types.contains_key(&OutputType::DepInfo) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let deps_filename = outputs.path(OutputType::DepInfo);
|
|
|
|
|
|
|
|
let result = (|| -> io::Result<()> {
|
|
|
|
// Build a list of files used to compile the output and
|
|
|
|
// write Makefile-compatible dependency rules
|
2019-12-22 22:42:04 +00:00
|
|
|
let mut files: Vec<String> = sess
|
|
|
|
.source_map()
|
2018-12-08 19:30:23 +00:00
|
|
|
.files()
|
|
|
|
.iter()
|
|
|
|
.filter(|fmap| fmap.is_real_file())
|
|
|
|
.filter(|fmap| !fmap.is_imported())
|
2021-04-19 22:27:02 +00:00
|
|
|
.map(|fmap| escape_dep_filename(&fmap.name.prefer_local().to_string()))
|
2018-12-08 19:30:23 +00:00
|
|
|
.collect();
|
2019-06-10 16:18:53 +00:00
|
|
|
|
2021-04-09 14:35:40 +00:00
|
|
|
// Account for explicitly marked-to-track files
|
|
|
|
// (e.g. accessed in proc macros).
|
|
|
|
let file_depinfo = sess.parse_sess.file_depinfo.borrow();
|
|
|
|
let extra_tracked_files = file_depinfo.iter().map(|path_sym| {
|
2021-12-15 03:39:23 +00:00
|
|
|
let path = PathBuf::from(path_sym.as_str());
|
2021-04-09 14:35:40 +00:00
|
|
|
let file = FileName::from(path);
|
|
|
|
escape_dep_filename(&file.prefer_local().to_string())
|
|
|
|
});
|
|
|
|
files.extend(extra_tracked_files);
|
|
|
|
|
2020-10-05 07:46:03 +00:00
|
|
|
if let Some(ref backend) = sess.opts.debugging_opts.codegen_backend {
|
|
|
|
files.push(backend.to_string());
|
|
|
|
}
|
|
|
|
|
2019-07-24 15:00:09 +00:00
|
|
|
if sess.binary_dep_depinfo() {
|
2021-06-11 16:55:30 +00:00
|
|
|
boxed_resolver.borrow_mut().access(|resolver| {
|
2019-10-20 00:28:36 +00:00
|
|
|
for cnum in resolver.cstore().crates_untracked() {
|
|
|
|
let source = resolver.cstore().crate_source_untracked(cnum);
|
2022-01-31 18:55:34 +00:00
|
|
|
if let Some((path, _)) = &source.dylib {
|
2021-04-19 19:07:27 +00:00
|
|
|
files.push(escape_dep_filename(&path.display().to_string()));
|
2019-10-20 00:28:36 +00:00
|
|
|
}
|
2022-01-31 18:55:34 +00:00
|
|
|
if let Some((path, _)) = &source.rlib {
|
2021-04-19 19:07:27 +00:00
|
|
|
files.push(escape_dep_filename(&path.display().to_string()));
|
2019-10-20 00:28:36 +00:00
|
|
|
}
|
2022-01-31 18:55:34 +00:00
|
|
|
if let Some((path, _)) = &source.rmeta {
|
2021-04-19 19:07:27 +00:00
|
|
|
files.push(escape_dep_filename(&path.display().to_string()));
|
2019-10-20 00:28:36 +00:00
|
|
|
}
|
2019-07-24 15:00:09 +00:00
|
|
|
}
|
2019-10-20 00:28:36 +00:00
|
|
|
});
|
2019-06-10 16:18:53 +00:00
|
|
|
}
|
|
|
|
|
2020-01-22 15:22:46 +00:00
|
|
|
let mut file = BufWriter::new(fs::File::create(&deps_filename)?);
|
2018-12-08 19:30:23 +00:00
|
|
|
for path in out_filenames {
|
|
|
|
writeln!(file, "{}: {}\n", path.display(), files.join(" "))?;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Emit a fake target for each input file to the compilation. This
|
|
|
|
// prevents `make` from spitting out an error if a file is later
|
|
|
|
// deleted. For more info see #28735
|
|
|
|
for path in files {
|
|
|
|
writeln!(file, "{}:", path)?;
|
|
|
|
}
|
2020-05-03 17:47:51 +00:00
|
|
|
|
|
|
|
// Emit special comments with information about accessed environment variables.
|
|
|
|
let env_depinfo = sess.parse_sess.env_depinfo.borrow();
|
|
|
|
if !env_depinfo.is_empty() {
|
|
|
|
let mut envs: Vec<_> = env_depinfo
|
|
|
|
.iter()
|
|
|
|
.map(|(k, v)| (escape_dep_env(*k), v.map(escape_dep_env)))
|
|
|
|
.collect();
|
|
|
|
envs.sort_unstable();
|
|
|
|
writeln!(file)?;
|
|
|
|
for (k, v) in envs {
|
|
|
|
write!(file, "# env-dep:{}", k)?;
|
|
|
|
if let Some(v) = v {
|
|
|
|
write!(file, "={}", v)?;
|
|
|
|
}
|
|
|
|
writeln!(file)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-08 19:30:23 +00:00
|
|
|
Ok(())
|
|
|
|
})();
|
|
|
|
|
2019-07-18 08:02:22 +00:00
|
|
|
match result {
|
|
|
|
Ok(_) => {
|
2019-07-17 19:52:56 +00:00
|
|
|
if sess.opts.json_artifact_notifications {
|
2019-12-22 22:42:04 +00:00
|
|
|
sess.parse_sess
|
|
|
|
.span_diagnostic
|
2019-07-18 08:02:22 +00:00
|
|
|
.emit_artifact_notification(&deps_filename, "dep-info");
|
|
|
|
}
|
|
|
|
}
|
2019-12-22 22:42:04 +00:00
|
|
|
Err(e) => sess.fatal(&format!(
|
|
|
|
"error writing dependencies to `{}`: {}",
|
|
|
|
deps_filename.display(),
|
|
|
|
e
|
|
|
|
)),
|
2018-12-08 19:30:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-08 19:30:23 +00:00
|
|
|
pub fn prepare_outputs(
|
|
|
|
sess: &Session,
|
|
|
|
compiler: &Compiler,
|
|
|
|
krate: &ast::Crate,
|
2021-06-11 16:55:30 +00:00
|
|
|
boxed_resolver: &RefCell<BoxedResolver>,
|
2019-12-22 22:42:04 +00:00
|
|
|
crate_name: &str,
|
2018-12-08 19:30:23 +00:00
|
|
|
) -> Result<OutputFilenames> {
|
2020-01-09 02:48:00 +00:00
|
|
|
let _timer = sess.timer("prepare_outputs");
|
|
|
|
|
2018-12-08 19:30:23 +00:00
|
|
|
// FIXME: rustdoc passes &[] instead of &krate.attrs here
|
|
|
|
let outputs = util::build_output_filenames(
|
|
|
|
&compiler.input,
|
|
|
|
&compiler.output_dir,
|
|
|
|
&compiler.output_file,
|
2021-11-02 21:41:34 +00:00
|
|
|
&compiler.temps_dir,
|
2018-12-08 19:30:23 +00:00
|
|
|
&krate.attrs,
|
|
|
|
sess,
|
|
|
|
);
|
|
|
|
|
2019-12-22 22:42:04 +00:00
|
|
|
let output_paths =
|
2021-09-30 17:38:50 +00:00
|
|
|
generated_output_paths(sess, &outputs, compiler.output_file.is_some(), crate_name);
|
2019-12-22 22:42:04 +00:00
|
|
|
|
2018-12-08 19:30:23 +00:00
|
|
|
// Ensure the source file isn't accidentally overwritten during compilation.
|
|
|
|
if let Some(ref input_path) = compiler.input_path {
|
|
|
|
if sess.opts.will_create_output_file() {
|
|
|
|
if output_contains_path(&output_paths, input_path) {
|
|
|
|
sess.err(&format!(
|
|
|
|
"the input file \"{}\" would be overwritten by the generated \
|
|
|
|
executable",
|
|
|
|
input_path.display()
|
|
|
|
));
|
|
|
|
return Err(ErrorReported);
|
|
|
|
}
|
|
|
|
if let Some(dir_path) = output_conflicts_with_dir(&output_paths) {
|
|
|
|
sess.err(&format!(
|
|
|
|
"the generated executable for the input file \"{}\" conflicts with the \
|
|
|
|
existing directory \"{}\"",
|
|
|
|
input_path.display(),
|
|
|
|
dir_path.display()
|
|
|
|
));
|
|
|
|
return Err(ErrorReported);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-04 11:33:33 +00:00
|
|
|
if let Some(ref dir) = compiler.temps_dir {
|
|
|
|
if fs::create_dir_all(dir).is_err() {
|
|
|
|
sess.err("failed to find or create the directory specified by `--temps-dir`");
|
|
|
|
return Err(ErrorReported);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-20 00:28:36 +00:00
|
|
|
write_out_deps(sess, boxed_resolver, &outputs, &output_paths);
|
2018-12-08 19:30:23 +00:00
|
|
|
|
|
|
|
let only_dep_info = sess.opts.output_types.contains_key(&OutputType::DepInfo)
|
|
|
|
&& sess.opts.output_types.len() == 1;
|
|
|
|
|
|
|
|
if !only_dep_info {
|
|
|
|
if let Some(ref dir) = compiler.output_dir {
|
|
|
|
if fs::create_dir_all(dir).is_err() {
|
2019-09-30 00:03:41 +00:00
|
|
|
sess.err("failed to find or create the directory specified by `--out-dir`");
|
2018-12-08 19:30:23 +00:00
|
|
|
return Err(ErrorReported);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(outputs)
|
|
|
|
}
|
|
|
|
|
2020-08-30 10:41:36 +00:00
|
|
|
pub static DEFAULT_QUERY_PROVIDERS: SyncLazy<Providers> = SyncLazy::new(|| {
|
2020-07-15 00:23:35 +00:00
|
|
|
let providers = &mut Providers::default();
|
2018-12-08 19:30:23 +00:00
|
|
|
providers.analysis = analysis;
|
|
|
|
proc_macro_decls::provide(providers);
|
2021-01-05 19:08:11 +00:00
|
|
|
rustc_const_eval::provide(providers);
|
2020-03-29 14:41:09 +00:00
|
|
|
rustc_middle::hir::provide(providers);
|
2020-12-30 17:48:40 +00:00
|
|
|
mir_borrowck::provide(providers);
|
2020-01-05 15:46:44 +00:00
|
|
|
mir_build::provide(providers);
|
2021-01-01 00:53:25 +00:00
|
|
|
rustc_mir_transform::provide(providers);
|
2021-01-02 13:42:15 +00:00
|
|
|
rustc_monomorphize::provide(providers);
|
2018-12-08 19:30:23 +00:00
|
|
|
rustc_privacy::provide(providers);
|
|
|
|
typeck::provide(providers);
|
|
|
|
ty::provide(providers);
|
|
|
|
traits::provide(providers);
|
|
|
|
rustc_passes::provide(providers);
|
2019-12-29 09:48:52 +00:00
|
|
|
rustc_resolve::provide(providers);
|
2018-12-08 19:30:23 +00:00
|
|
|
rustc_traits::provide(providers);
|
2020-11-19 20:32:37 +00:00
|
|
|
rustc_ty_utils::provide(providers);
|
2019-11-23 22:10:12 +00:00
|
|
|
rustc_metadata::provide(providers);
|
2019-01-31 00:36:11 +00:00
|
|
|
rustc_lint::provide(providers);
|
2020-03-12 23:07:58 +00:00
|
|
|
rustc_symbol_mangling::provide(providers);
|
2019-10-13 10:11:27 +00:00
|
|
|
rustc_codegen_ssa::provide(providers);
|
2020-07-15 00:23:35 +00:00
|
|
|
*providers
|
|
|
|
});
|
2018-12-08 19:30:23 +00:00
|
|
|
|
2021-05-30 15:24:54 +00:00
|
|
|
pub static DEFAULT_EXTERN_QUERY_PROVIDERS: SyncLazy<ExternProviders> = SyncLazy::new(|| {
|
|
|
|
let mut extern_providers = ExternProviders::default();
|
2020-07-15 00:23:35 +00:00
|
|
|
rustc_metadata::provide_extern(&mut extern_providers);
|
|
|
|
rustc_codegen_ssa::provide_extern(&mut extern_providers);
|
|
|
|
extern_providers
|
|
|
|
});
|
2018-12-08 19:30:23 +00:00
|
|
|
|
2020-10-11 08:34:13 +00:00
|
|
|
pub struct QueryContext<'tcx> {
|
|
|
|
gcx: &'tcx GlobalCtxt<'tcx>,
|
|
|
|
}
|
2018-12-08 19:30:23 +00:00
|
|
|
|
2019-11-27 12:17:58 +00:00
|
|
|
impl<'tcx> QueryContext<'tcx> {
|
2018-12-08 19:30:23 +00:00
|
|
|
pub fn enter<F, R>(&mut self, f: F) -> R
|
|
|
|
where
|
2019-11-27 12:13:57 +00:00
|
|
|
F: FnOnce(TyCtxt<'tcx>) -> R,
|
2018-12-08 19:30:23 +00:00
|
|
|
{
|
2020-10-11 08:34:13 +00:00
|
|
|
let icx = ty::tls::ImplicitCtxt::new(self.gcx);
|
2020-07-31 03:01:29 +00:00
|
|
|
ty::tls::enter_context(&icx, |_| f(icx.tcx))
|
2019-11-26 21:51:02 +00:00
|
|
|
}
|
2018-12-08 19:30:23 +00:00
|
|
|
}
|
|
|
|
|
2019-11-27 12:13:57 +00:00
|
|
|
pub fn create_global_ctxt<'tcx>(
|
|
|
|
compiler: &'tcx Compiler,
|
2020-01-09 06:52:01 +00:00
|
|
|
lint_store: Lrc<LintStore>,
|
2021-06-20 16:52:35 +00:00
|
|
|
krate: Rc<ast::Crate>,
|
2020-02-06 12:41:37 +00:00
|
|
|
dep_graph: DepGraph,
|
2021-05-23 19:42:16 +00:00
|
|
|
resolver: Rc<RefCell<BoxedResolver>>,
|
2018-12-08 19:30:23 +00:00
|
|
|
outputs: OutputFilenames,
|
2019-06-13 22:32:15 +00:00
|
|
|
crate_name: &str,
|
2021-01-19 19:40:16 +00:00
|
|
|
queries: &'tcx OnceCell<TcxQueries<'tcx>>,
|
2020-05-16 04:44:28 +00:00
|
|
|
global_ctxt: &'tcx OnceCell<GlobalCtxt<'tcx>>,
|
2019-11-27 12:24:19 +00:00
|
|
|
arena: &'tcx WorkerLocal<Arena<'tcx>>,
|
2021-05-23 19:42:16 +00:00
|
|
|
hir_arena: &'tcx WorkerLocal<rustc_ast_lowering::Arena<'tcx>>,
|
2019-11-27 12:17:58 +00:00
|
|
|
) -> QueryContext<'tcx> {
|
2021-05-23 19:42:16 +00:00
|
|
|
// We're constructing the HIR here; we don't care what we will
|
|
|
|
// read, since we haven't even constructed the *input* to
|
|
|
|
// incr. comp. yet.
|
|
|
|
dep_graph.assert_ignored();
|
|
|
|
|
2019-11-26 21:51:02 +00:00
|
|
|
let sess = &compiler.session();
|
2021-05-23 19:42:16 +00:00
|
|
|
let krate = resolver
|
|
|
|
.borrow_mut()
|
|
|
|
.access(|resolver| lower_to_hir(sess, &lint_store, resolver, krate, hir_arena));
|
|
|
|
let resolver_outputs = BoxedResolver::to_resolver_outputs(resolver);
|
2018-12-08 19:30:23 +00:00
|
|
|
|
2021-06-08 20:18:53 +00:00
|
|
|
let query_result_on_disk_cache = rustc_incremental::load_query_result_cache(sess);
|
2018-12-08 19:30:23 +00:00
|
|
|
|
2019-11-26 21:56:05 +00:00
|
|
|
let codegen_backend = compiler.codegen_backend();
|
2020-07-15 00:23:35 +00:00
|
|
|
let mut local_providers = *DEFAULT_QUERY_PROVIDERS;
|
2019-11-26 21:56:05 +00:00
|
|
|
codegen_backend.provide(&mut local_providers);
|
2019-11-11 15:09:03 +00:00
|
|
|
|
2020-07-15 00:23:35 +00:00
|
|
|
let mut extern_providers = *DEFAULT_EXTERN_QUERY_PROVIDERS;
|
2019-11-26 21:56:05 +00:00
|
|
|
codegen_backend.provide_extern(&mut extern_providers);
|
2018-12-08 19:30:23 +00:00
|
|
|
|
2019-11-27 12:24:19 +00:00
|
|
|
if let Some(callback) = compiler.override_queries {
|
2019-11-26 21:56:05 +00:00
|
|
|
callback(sess, &mut local_providers, &mut extern_providers);
|
|
|
|
}
|
2018-12-08 19:30:23 +00:00
|
|
|
|
2021-06-28 19:12:01 +00:00
|
|
|
let queries = queries.get_or_init(|| {
|
|
|
|
TcxQueries::new(local_providers, extern_providers, query_result_on_disk_cache)
|
|
|
|
});
|
2020-10-11 08:34:13 +00:00
|
|
|
|
2020-01-09 02:48:00 +00:00
|
|
|
let gcx = sess.time("setup_global_ctxt", || {
|
2021-05-23 19:42:16 +00:00
|
|
|
global_ctxt.get_or_init(move || {
|
2020-01-09 02:48:00 +00:00
|
|
|
TyCtxt::create_global_ctxt(
|
|
|
|
sess,
|
|
|
|
lint_store,
|
|
|
|
arena,
|
|
|
|
resolver_outputs,
|
2020-02-09 14:32:00 +00:00
|
|
|
krate,
|
2020-02-08 04:18:34 +00:00
|
|
|
dep_graph,
|
2021-06-28 19:12:01 +00:00
|
|
|
queries.on_disk_cache.as_ref().map(OnDiskCache::as_dyn),
|
2021-01-19 19:40:16 +00:00
|
|
|
queries.as_dyn(),
|
2021-10-16 19:12:34 +00:00
|
|
|
rustc_query_impl::query_callbacks(arena),
|
2021-09-30 17:38:50 +00:00
|
|
|
crate_name,
|
2021-05-11 13:03:53 +00:00
|
|
|
outputs,
|
2020-01-09 02:48:00 +00:00
|
|
|
)
|
|
|
|
})
|
2019-12-22 22:42:04 +00:00
|
|
|
});
|
2018-12-08 19:30:23 +00:00
|
|
|
|
2020-10-11 08:34:13 +00:00
|
|
|
QueryContext { gcx }
|
2018-12-08 19:30:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Runs the resolution, type-checking, region checking and other
|
|
|
|
/// miscellaneous analysis passes on the crate.
|
2021-05-11 12:50:54 +00:00
|
|
|
fn analysis(tcx: TyCtxt<'_>, (): ()) -> Result<()> {
|
2020-03-24 03:59:39 +00:00
|
|
|
rustc_passes::hir_id_validator::check_crate(tcx);
|
2020-02-09 16:11:02 +00:00
|
|
|
|
2018-12-08 19:30:23 +00:00
|
|
|
let sess = tcx.sess;
|
2019-04-12 03:01:19 +00:00
|
|
|
let mut entry_point = None;
|
2018-12-08 19:30:23 +00:00
|
|
|
|
2020-01-07 20:34:08 +00:00
|
|
|
sess.time("misc_checking_1", || {
|
2019-12-22 22:42:04 +00:00
|
|
|
parallel!(
|
|
|
|
{
|
2021-05-11 10:00:59 +00:00
|
|
|
entry_point = sess.time("looking_for_entry_point", || tcx.entry_fn(()));
|
2019-02-23 17:32:45 +00:00
|
|
|
|
2021-05-11 10:12:52 +00:00
|
|
|
sess.time("looking_for_derive_registrar", || {
|
|
|
|
tcx.ensure().proc_macro_decls_static(())
|
|
|
|
});
|
2020-08-09 23:57:35 +00:00
|
|
|
|
2021-07-12 19:20:16 +00:00
|
|
|
CStore::from_tcx(tcx).report_unused_deps(tcx);
|
2019-12-22 22:42:04 +00:00
|
|
|
},
|
|
|
|
{
|
2021-07-18 16:12:17 +00:00
|
|
|
tcx.hir().par_for_each_module(|module| {
|
2021-01-31 16:58:57 +00:00
|
|
|
tcx.ensure().check_mod_loops(module);
|
|
|
|
tcx.ensure().check_mod_attrs(module);
|
|
|
|
tcx.ensure().check_mod_naked_functions(module);
|
|
|
|
tcx.ensure().check_mod_unstable_api_usage(module);
|
|
|
|
tcx.ensure().check_mod_const_bodies(module);
|
2019-12-22 22:42:04 +00:00
|
|
|
});
|
2021-06-25 23:48:26 +00:00
|
|
|
},
|
|
|
|
{
|
2021-07-04 18:02:51 +00:00
|
|
|
// We force these querie to run,
|
|
|
|
// since they might not otherwise get called.
|
|
|
|
// This marks the corresponding crate-level attributes
|
|
|
|
// as used, and ensures that their values are valid.
|
|
|
|
tcx.ensure().limits(());
|
2019-12-22 22:42:04 +00:00
|
|
|
}
|
|
|
|
);
|
2018-12-08 19:30:23 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// passes are timed inside typeck
|
|
|
|
typeck::check_crate(tcx)?;
|
|
|
|
|
2020-01-07 20:34:08 +00:00
|
|
|
sess.time("misc_checking_2", || {
|
2019-12-22 22:42:04 +00:00
|
|
|
parallel!(
|
|
|
|
{
|
2020-01-07 20:34:08 +00:00
|
|
|
sess.time("match_checking", || {
|
2021-09-12 09:33:16 +00:00
|
|
|
tcx.hir().par_body_owners(|def_id| tcx.ensure().check_match(def_id.to_def_id()))
|
2019-02-23 17:12:38 +00:00
|
|
|
});
|
2019-12-22 22:42:04 +00:00
|
|
|
},
|
|
|
|
{
|
2020-01-07 20:34:08 +00:00
|
|
|
sess.time("liveness_and_intrinsic_checking", || {
|
2021-07-18 16:12:17 +00:00
|
|
|
tcx.hir().par_for_each_module(|module| {
|
2019-12-22 22:42:04 +00:00
|
|
|
// this must run before MIR dump, because
|
|
|
|
// "not all control paths return a value" is reported here.
|
|
|
|
//
|
|
|
|
// maybe move the check to a MIR pass?
|
2021-01-31 16:58:57 +00:00
|
|
|
tcx.ensure().check_mod_liveness(module);
|
|
|
|
tcx.ensure().check_mod_intrinsics(module);
|
2019-12-22 22:42:04 +00:00
|
|
|
});
|
2019-02-23 17:12:38 +00:00
|
|
|
});
|
2019-12-22 22:42:04 +00:00
|
|
|
}
|
|
|
|
);
|
2018-12-08 19:30:23 +00:00
|
|
|
});
|
|
|
|
|
2020-01-07 20:34:08 +00:00
|
|
|
sess.time("MIR_borrow_checking", || {
|
2021-09-12 09:33:16 +00:00
|
|
|
tcx.hir().par_body_owners(|def_id| tcx.ensure().mir_borrowck(def_id));
|
2018-12-08 19:30:23 +00:00
|
|
|
});
|
2018-12-08 19:30:23 +00:00
|
|
|
|
2020-01-07 20:34:08 +00:00
|
|
|
sess.time("MIR_effect_checking", || {
|
2021-09-12 09:33:16 +00:00
|
|
|
for def_id in tcx.hir().body_owners() {
|
2021-05-24 13:09:33 +00:00
|
|
|
tcx.ensure().thir_check_unsafety(def_id);
|
|
|
|
if !tcx.sess.opts.debugging_opts.thir_unsafeck {
|
2021-01-01 00:53:25 +00:00
|
|
|
rustc_mir_transform::check_unsafety::check_unsafety(tcx, def_id);
|
2021-03-14 19:10:22 +00:00
|
|
|
}
|
2020-05-03 04:16:55 +00:00
|
|
|
|
|
|
|
if tcx.hir().body_const_context(def_id).is_some() {
|
2020-07-03 20:15:27 +00:00
|
|
|
tcx.ensure()
|
2020-07-15 08:55:41 +00:00
|
|
|
.mir_drops_elaborated_and_const_checked(ty::WithOptConstParam::unknown(def_id));
|
2020-05-03 04:16:55 +00:00
|
|
|
}
|
2018-12-08 19:30:23 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-01-07 20:34:08 +00:00
|
|
|
sess.time("layout_testing", || layout_test::test_layout(tcx));
|
2018-12-08 19:30:23 +00:00
|
|
|
|
|
|
|
// Avoid overwhelming user with errors if borrow checking failed.
|
2019-04-19 22:37:34 +00:00
|
|
|
// I'm not sure how helpful this is, to be honest, but it avoids a
|
2020-12-28 17:15:16 +00:00
|
|
|
// lot of annoying errors in the ui tests (basically,
|
2018-12-08 19:30:23 +00:00
|
|
|
// lint warnings and so on -- kindck used to do this abort, but
|
|
|
|
// kindck is gone now). -nmatsakis
|
2019-06-22 11:44:03 +00:00
|
|
|
if sess.has_errors() {
|
2018-12-08 19:30:23 +00:00
|
|
|
return Err(ErrorReported);
|
|
|
|
}
|
|
|
|
|
2020-01-07 20:34:08 +00:00
|
|
|
sess.time("misc_checking_3", || {
|
2019-12-22 22:42:04 +00:00
|
|
|
parallel!(
|
|
|
|
{
|
2021-05-11 11:49:00 +00:00
|
|
|
tcx.ensure().privacy_access_levels(());
|
2020-01-07 20:34:08 +00:00
|
|
|
|
2019-12-22 22:42:04 +00:00
|
|
|
parallel!(
|
|
|
|
{
|
2021-05-11 11:49:00 +00:00
|
|
|
tcx.ensure().check_private_in_public(());
|
2019-12-22 22:42:04 +00:00
|
|
|
},
|
|
|
|
{
|
2022-01-29 20:10:41 +00:00
|
|
|
tcx.hir()
|
|
|
|
.par_for_each_module(|module| tcx.ensure().check_mod_deathness(module));
|
2019-12-22 22:42:04 +00:00
|
|
|
},
|
|
|
|
{
|
2020-01-07 20:34:08 +00:00
|
|
|
sess.time("unused_lib_feature_checking", || {
|
2019-12-29 10:20:20 +00:00
|
|
|
rustc_passes::stability::check_unused_or_stable_features(tcx)
|
2019-12-22 22:42:04 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
{
|
2020-01-07 20:34:08 +00:00
|
|
|
sess.time("lint_checking", || {
|
2019-12-30 13:22:46 +00:00
|
|
|
rustc_lint::check_crate(tcx, || {
|
2019-12-22 22:42:04 +00:00
|
|
|
rustc_lint::BuiltinCombinedLateLintPass::new()
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
{
|
2020-01-07 20:34:08 +00:00
|
|
|
sess.time("privacy_checking_modules", || {
|
2021-07-18 16:12:17 +00:00
|
|
|
tcx.hir().par_for_each_module(|module| {
|
2021-01-31 16:58:57 +00:00
|
|
|
tcx.ensure().check_mod_privacy(module);
|
2019-12-22 22:42:04 +00:00
|
|
|
});
|
2019-02-23 15:40:15 +00:00
|
|
|
});
|
2019-12-22 22:42:04 +00:00
|
|
|
}
|
|
|
|
);
|
2018-12-08 19:30:23 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2018-12-08 19:30:23 +00:00
|
|
|
|
2019-06-21 18:27:44 +00:00
|
|
|
fn encode_and_write_metadata(
|
|
|
|
tcx: TyCtxt<'_>,
|
2019-04-30 05:53:30 +00:00
|
|
|
outputs: &OutputFilenames,
|
2021-09-24 16:15:36 +00:00
|
|
|
) -> (EncodedMetadata, bool) {
|
2019-04-26 07:22:36 +00:00
|
|
|
#[derive(PartialEq, Eq, PartialOrd, Ord)]
|
|
|
|
enum MetadataKind {
|
|
|
|
None,
|
|
|
|
Uncompressed,
|
2019-12-22 22:42:04 +00:00
|
|
|
Compressed,
|
2019-04-26 07:22:36 +00:00
|
|
|
}
|
|
|
|
|
2019-12-22 22:42:04 +00:00
|
|
|
let metadata_kind = tcx
|
|
|
|
.sess
|
2020-05-16 04:44:28 +00:00
|
|
|
.crate_types()
|
2019-12-22 22:42:04 +00:00
|
|
|
.iter()
|
|
|
|
.map(|ty| match *ty {
|
|
|
|
CrateType::Executable | CrateType::Staticlib | CrateType::Cdylib => MetadataKind::None,
|
2019-04-26 07:22:36 +00:00
|
|
|
|
|
|
|
CrateType::Rlib => MetadataKind::Uncompressed,
|
|
|
|
|
2019-12-22 22:42:04 +00:00
|
|
|
CrateType::Dylib | CrateType::ProcMacro => MetadataKind::Compressed,
|
|
|
|
})
|
|
|
|
.max()
|
|
|
|
.unwrap_or(MetadataKind::None);
|
2019-04-26 07:22:36 +00:00
|
|
|
|
|
|
|
let metadata = match metadata_kind {
|
2021-09-24 16:15:36 +00:00
|
|
|
MetadataKind::None => EncodedMetadata::new(),
|
2020-11-14 00:59:00 +00:00
|
|
|
MetadataKind::Uncompressed | MetadataKind::Compressed => encode_metadata(tcx),
|
2019-04-26 07:22:36 +00:00
|
|
|
};
|
|
|
|
|
2020-01-07 20:34:08 +00:00
|
|
|
let _prof_timer = tcx.sess.prof.generic_activity("write_crate_metadata");
|
|
|
|
|
2019-04-30 05:53:30 +00:00
|
|
|
let need_metadata_file = tcx.sess.opts.output_types.contains_key(&OutputType::Metadata);
|
|
|
|
if need_metadata_file {
|
2021-12-14 21:32:21 +00:00
|
|
|
let crate_name = tcx.crate_name(LOCAL_CRATE);
|
|
|
|
let out_filename = filename_for_metadata(tcx.sess, crate_name.as_str(), outputs);
|
2019-04-30 05:53:30 +00:00
|
|
|
// To avoid races with another rustc process scanning the output directory,
|
|
|
|
// we need to write the file somewhere else and atomically move it to its
|
|
|
|
// final destination, with an `fs::rename` call. In order for the rename to
|
|
|
|
// always succeed, the temporary file needs to be on the same filesystem,
|
|
|
|
// which is why we create it inside the output directory specifically.
|
|
|
|
let metadata_tmpdir = TempFileBuilder::new()
|
|
|
|
.prefix("rmeta")
|
|
|
|
.tempdir_in(out_filename.parent().unwrap())
|
2019-12-22 22:42:04 +00:00
|
|
|
.unwrap_or_else(|err| tcx.sess.fatal(&format!("couldn't create a temp dir: {}", err)));
|
2020-08-09 01:05:50 +00:00
|
|
|
let metadata_tmpdir = MaybeTempDir::new(metadata_tmpdir, tcx.sess.opts.cg.save_temps);
|
2021-09-24 16:15:36 +00:00
|
|
|
let metadata_filename = emit_metadata(tcx.sess, metadata.raw_data(), &metadata_tmpdir);
|
2021-02-12 23:54:17 +00:00
|
|
|
if let Err(e) = util::non_durable_rename(&metadata_filename, &out_filename) {
|
2019-05-02 02:06:33 +00:00
|
|
|
tcx.sess.fatal(&format!("failed to write {}: {}", out_filename.display(), e));
|
|
|
|
}
|
2019-07-17 19:52:56 +00:00
|
|
|
if tcx.sess.opts.json_artifact_notifications {
|
2019-12-22 22:42:04 +00:00
|
|
|
tcx.sess
|
|
|
|
.parse_sess
|
|
|
|
.span_diagnostic
|
2019-05-21 19:38:46 +00:00
|
|
|
.emit_artifact_notification(&out_filename, "metadata");
|
2019-04-30 05:53:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let need_metadata_module = metadata_kind == MetadataKind::Compressed;
|
|
|
|
|
2019-04-26 07:22:36 +00:00
|
|
|
(metadata, need_metadata_module)
|
|
|
|
}
|
|
|
|
|
2018-12-08 19:30:23 +00:00
|
|
|
/// Runs the codegen backend, after which the AST and analysis can
|
|
|
|
/// be discarded.
|
|
|
|
pub fn start_codegen<'tcx>(
|
|
|
|
codegen_backend: &dyn CodegenBackend,
|
2019-06-13 21:48:52 +00:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2018-12-08 19:30:23 +00:00
|
|
|
outputs: &OutputFilenames,
|
|
|
|
) -> Box<dyn Any> {
|
2020-07-28 14:15:40 +00:00
|
|
|
info!("Pre-codegen\n{:?}", tcx.debug_stats());
|
2018-12-08 19:30:23 +00:00
|
|
|
|
2020-01-07 20:34:08 +00:00
|
|
|
let (metadata, need_metadata_module) = encode_and_write_metadata(tcx, outputs);
|
2018-12-08 19:30:23 +00:00
|
|
|
|
2020-01-07 20:34:08 +00:00
|
|
|
let codegen = tcx.sess.time("codegen_crate", move || {
|
2019-09-25 17:14:43 +00:00
|
|
|
codegen_backend.codegen_crate(tcx, metadata, need_metadata_module)
|
2019-04-26 07:22:36 +00:00
|
|
|
});
|
2018-12-08 19:30:23 +00:00
|
|
|
|
2020-12-17 09:05:39 +00:00
|
|
|
// Don't run these test assertions when not doing codegen. Compiletest tries to build
|
|
|
|
// build-fail tests in check mode first and expects it to not give an error in that case.
|
|
|
|
if tcx.sess.opts.output_types.should_codegen() {
|
|
|
|
rustc_incremental::assert_module_sources::assert_module_sources(tcx);
|
|
|
|
rustc_symbol_mangling::test::report_symbol_names(tcx);
|
|
|
|
}
|
2020-12-03 13:11:35 +00:00
|
|
|
|
2020-07-28 14:15:40 +00:00
|
|
|
info!("Post-codegen\n{:?}", tcx.debug_stats());
|
2018-12-08 19:30:23 +00:00
|
|
|
|
|
|
|
if tcx.sess.opts.output_types.contains_key(&OutputType::Mir) {
|
2021-01-01 00:53:25 +00:00
|
|
|
if let Err(e) = rustc_mir_transform::dump_mir::emit_mir(tcx, outputs) {
|
2018-12-08 19:30:23 +00:00
|
|
|
tcx.sess.err(&format!("could not emit MIR: {}", e));
|
|
|
|
tcx.sess.abort_if_errors();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
codegen
|
|
|
|
}
|
2021-09-17 20:08:56 +00:00
|
|
|
|
|
|
|
fn get_recursion_limit(krate_attrs: &[ast::Attribute], sess: &Session) -> Limit {
|
|
|
|
if let Some(attr) = krate_attrs
|
|
|
|
.iter()
|
|
|
|
.find(|attr| attr.has_name(sym::recursion_limit) && attr.value_str().is_none())
|
|
|
|
{
|
|
|
|
// This is here mainly to check for using a macro, such as
|
|
|
|
// #![recursion_limit = foo!()]. That is not supported since that
|
|
|
|
// would require expanding this while in the middle of expansion,
|
|
|
|
// which needs to know the limit before expanding. Otherwise,
|
|
|
|
// validation would normally be caught in AstValidator (via
|
|
|
|
// `check_builtin_attribute`), but by the time that runs the macro
|
|
|
|
// is expanded, and it doesn't give an error.
|
|
|
|
validate_attr::emit_fatal_malformed_builtin_attribute(
|
|
|
|
&sess.parse_sess,
|
|
|
|
attr,
|
|
|
|
sym::recursion_limit,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
rustc_middle::middle::limits::get_recursion_limit(krate_attrs, sess)
|
|
|
|
}
|