2023-02-05 02:20:06 +00:00
|
|
|
use crate::errors;
|
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
|
|
|
|
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;
|
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;
|
2023-02-16 14:07:42 +00:00
|
|
|
use rustc_data_structures::steal::Steal;
|
2023-08-31 23:14:33 +00:00
|
|
|
use rustc_data_structures::sync::{Lrc, OnceLock, WorkerLocal};
|
2023-02-16 14:03:31 +00:00
|
|
|
use rustc_errors::PResult;
|
2023-03-06 10:56:53 +00:00
|
|
|
use rustc_expand::base::{ExtCtxt, LintStoreExpand};
|
2023-08-09 12:28:00 +00:00
|
|
|
use rustc_feature::Features;
|
2023-03-16 20:42:31 +00:00
|
|
|
use rustc_fs_util::try_canonicalize;
|
2023-08-08 12:08:24 +00:00
|
|
|
use rustc_hir::def_id::{StableCrateId, LOCAL_CRATE};
|
2023-02-16 11:36:44 +00:00
|
|
|
use rustc_lint::{unerased_lint_store, BufferedEarlyLint, EarlyCheckNode, LintStore};
|
2020-08-09 23:57:35 +00:00
|
|
|
use rustc_metadata::creader::CStore;
|
2020-03-29 14:41:09 +00:00
|
|
|
use rustc_middle::arena::Arena;
|
|
|
|
use rustc_middle::dep_graph::DepGraph;
|
2022-06-15 17:42:43 +00:00
|
|
|
use rustc_middle::ty::{self, GlobalCtxt, RegisteredTools, TyCtxt};
|
2023-09-22 16:26:20 +00:00
|
|
|
use rustc_middle::util::Providers;
|
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};
|
2023-11-10 02:11:24 +00:00
|
|
|
use rustc_passes::{abi_test, hir_stats, layout_test};
|
2022-12-13 09:46:37 +00:00
|
|
|
use rustc_resolve::Resolver;
|
2023-05-17 12:28:04 +00:00
|
|
|
use rustc_session::code_stats::VTableSizeInfo;
|
2023-02-26 20:27:27 +00:00
|
|
|
use rustc_session::config::{CrateType, Input, OutFileName, OutputFilenames, OutputType};
|
2023-10-03 02:54:17 +00:00
|
|
|
use rustc_session::cstore::Untracked;
|
2022-04-24 14:49:04 +00:00
|
|
|
use rustc_session::output::filename_for_input;
|
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};
|
2022-08-20 16:30:49 +00:00
|
|
|
use rustc_span::FileName;
|
2023-01-10 12:57:42 +00:00
|
|
|
use rustc_target::spec::PanicStrategy;
|
2020-02-11 20:19:40 +00:00
|
|
|
use rustc_trait_selection::traits;
|
2018-12-08 19:30:23 +00:00
|
|
|
|
|
|
|
use std::any::Any;
|
|
|
|
use std::ffi::OsString;
|
2020-01-22 15:22:46 +00:00
|
|
|
use std::io::{self, BufWriter, Write};
|
2021-11-07 09:33:27 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
2023-11-04 15:59:29 +00:00
|
|
|
use std::sync::LazyLock;
|
2021-03-30 16:17:14 +00:00
|
|
|
use std::{env, fs, iter};
|
2018-12-08 19:30:23 +00:00
|
|
|
|
2022-12-07 09:24:00 +00:00
|
|
|
pub fn parse<'a>(sess: &'a Session) -> PResult<'a, ast::Crate> {
|
|
|
|
let krate = sess.time("parse_crate", || match &sess.io.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
|
|
|
})?;
|
|
|
|
|
2022-07-06 12:44:47 +00:00
|
|
|
if sess.opts.unstable_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
|
|
|
}
|
|
|
|
|
2022-07-06 12:44:47 +00:00
|
|
|
if let Some(ref s) = sess.opts.unstable_opts.show_span {
|
2023-12-17 19:21:26 +00:00
|
|
|
rustc_ast_passes::show_span::run(sess.dcx(), s, &krate);
|
2018-12-08 19:30:23 +00:00
|
|
|
}
|
|
|
|
|
2022-07-06 12:44:47 +00:00
|
|
|
if sess.opts.unstable_opts.hir_stats {
|
2022-08-26 03:52:41 +00:00
|
|
|
hir_stats::print_ast_stats(&krate, "PRE EXPANSION AST STATS", "ast-stats-1");
|
2018-12-08 19:30:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-12-07 10:28:12 +00:00
|
|
|
fn pre_expansion_lint<'a>(
|
2021-01-22 19:33:21 +00:00
|
|
|
sess: &Session,
|
2023-08-09 12:28:00 +00:00
|
|
|
features: &Features,
|
2021-01-22 19:33:21 +00:00
|
|
|
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>,
|
2022-12-06 12:46:10 +00:00
|
|
|
node_name: Symbol,
|
2021-01-22 19:33:21 +00:00
|
|
|
) {
|
2022-12-06 12:46:10 +00:00
|
|
|
sess.prof.generic_activity_with_arg("pre_AST_expansion_lint_checks", node_name.as_str()).run(
|
|
|
|
|| {
|
|
|
|
rustc_lint::check_ast_node(
|
|
|
|
sess,
|
2023-08-09 12:28:00 +00:00
|
|
|
features,
|
2022-12-06 12:46:10 +00:00
|
|
|
true,
|
|
|
|
lint_store,
|
|
|
|
registered_tools,
|
|
|
|
None,
|
|
|
|
rustc_lint::BuiltinCombinedPreExpansionLintPass::new(),
|
|
|
|
check_node,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
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,
|
2023-08-09 12:28:00 +00:00
|
|
|
features: &Features,
|
2021-12-11 07:32:48 +00:00
|
|
|
registered_tools: &RegisteredTools,
|
|
|
|
node_id: ast::NodeId,
|
|
|
|
attrs: &[ast::Attribute],
|
|
|
|
items: &[rustc_ast::ptr::P<ast::Item>],
|
2022-12-06 12:46:10 +00:00
|
|
|
name: Symbol,
|
2021-12-11 07:32:48 +00:00
|
|
|
) {
|
2023-08-09 12:28:00 +00:00
|
|
|
pre_expansion_lint(sess, features, self.0, registered_tools, (node_id, attrs, items), name);
|
2021-12-11 07:32:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-15 02:38:47 +00:00
|
|
|
/// Runs the "early phases" of the compiler: initial `cfg` processing,
|
2021-05-24 16:45:21 +00:00
|
|
|
/// 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.
|
2023-02-16 14:51:51 +00:00
|
|
|
#[instrument(level = "trace", skip(krate, resolver))]
|
2023-03-14 12:53:04 +00:00
|
|
|
fn configure_and_expand(
|
|
|
|
mut krate: ast::Crate,
|
|
|
|
pre_configured_attrs: &[ast::Attribute],
|
|
|
|
resolver: &mut Resolver<'_, '_>,
|
|
|
|
) -> ast::Crate {
|
2023-02-16 14:51:51 +00:00
|
|
|
let tcx = resolver.tcx();
|
2023-02-16 11:36:44 +00:00
|
|
|
let sess = tcx.sess;
|
2023-08-09 12:28:00 +00:00
|
|
|
let features = tcx.features();
|
2023-11-21 19:07:32 +00:00
|
|
|
let lint_store = unerased_lint_store(tcx.sess);
|
2023-02-16 11:36:44 +00:00
|
|
|
let crate_name = tcx.crate_name(LOCAL_CRATE);
|
2023-03-14 12:53:04 +00:00
|
|
|
let lint_check_node = (&krate, pre_configured_attrs);
|
2023-08-09 12:28:00 +00:00
|
|
|
pre_expansion_lint(
|
|
|
|
sess,
|
|
|
|
features,
|
|
|
|
lint_store,
|
|
|
|
tcx.registered_tools(()),
|
|
|
|
lint_check_node,
|
|
|
|
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
|
|
|
|
2023-03-14 12:53:04 +00:00
|
|
|
let num_standard_library_imports = sess.time("crate_injection", || {
|
|
|
|
rustc_builtin_macros::standard_library_imports::inject(
|
|
|
|
&mut krate,
|
|
|
|
pre_configured_attrs,
|
|
|
|
resolver,
|
|
|
|
sess,
|
2023-08-09 12:28:00 +00:00
|
|
|
features,
|
2023-03-14 12:53:04 +00:00
|
|
|
)
|
2019-08-25 20:03:24 +00:00
|
|
|
});
|
|
|
|
|
2023-11-21 19:07:32 +00:00
|
|
|
util::check_attr_crate_type(sess, pre_configured_attrs, 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
|
2023-03-14 12:53:04 +00:00
|
|
|
let recursion_limit = get_recursion_limit(pre_configured_attrs, sess);
|
2019-12-29 14:23:55 +00:00
|
|
|
let cfg = rustc_expand::expand::ExpansionConfig {
|
2023-08-09 12:28:00 +00:00
|
|
|
crate_name: crate_name.to_string(),
|
|
|
|
features,
|
2021-06-25 23:48:26 +00:00
|
|
|
recursion_limit,
|
2022-07-06 12:44:47 +00:00
|
|
|
trace_mac: sess.opts.unstable_opts.trace_macros,
|
2023-04-09 19:37:31 +00:00
|
|
|
should_test: sess.is_test_crate(),
|
2022-07-06 12:44:47 +00:00
|
|
|
span_debug: sess.opts.unstable_opts.span_debug,
|
|
|
|
proc_macro_backtrace: sess.opts.unstable_opts.proc_macro_backtrace,
|
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));
|
2023-03-14 12:53:04 +00:00
|
|
|
ecx.num_standard_library_imports = num_standard_library_imports;
|
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
|
|
|
|
|
2022-06-25 05:52:13 +00:00
|
|
|
sess.parse_sess.buffered_lints.with_lock(|buffered_lints: &mut Vec<BufferedEarlyLint>| {
|
|
|
|
buffered_lints.append(&mut ecx.buffered_early_lint);
|
|
|
|
});
|
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
|
2023-02-16 14:03:31 +00:00
|
|
|
// If we hit a recursion limit, exit early to avoid later passes getting overwhelmed
|
|
|
|
// with a large AST
|
|
|
|
if ecx.reduced_recursion_limit.is_some() {
|
2023-12-21 05:26:09 +00:00
|
|
|
sess.dcx().abort_if_errors();
|
2023-02-16 14:03:31 +00:00
|
|
|
unreachable!();
|
|
|
|
}
|
2020-12-19 21:30:56 +00:00
|
|
|
|
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
|
|
|
|
2023-02-16 14:03:31 +00:00
|
|
|
krate
|
|
|
|
});
|
2018-12-08 19:30:23 +00:00
|
|
|
|
2020-01-07 20:34:08 +00:00
|
|
|
sess.time("maybe_building_test_harness", || {
|
2023-08-09 12:28:00 +00:00
|
|
|
rustc_builtin_macros::test_harness::inject(&mut krate, sess, features, resolver)
|
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", || {
|
2023-08-09 12:28:00 +00:00
|
|
|
rustc_ast_passes::ast_validation::check_crate(
|
|
|
|
sess,
|
|
|
|
features,
|
|
|
|
&krate,
|
|
|
|
resolver.lint_buffer(),
|
|
|
|
)
|
2018-12-08 19:30:23 +00:00
|
|
|
});
|
|
|
|
|
2023-08-08 10:28:20 +00:00
|
|
|
let crate_types = tcx.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 {
|
2023-12-18 11:21:37 +00:00
|
|
|
sess.dcx().emit_err(errors::MixedBinCrate);
|
2022-01-15 13:23:45 +00:00
|
|
|
}
|
|
|
|
if is_proc_macro_crate {
|
2023-12-18 11:21:37 +00:00
|
|
|
sess.dcx().emit_err(errors::MixedProcMacroCrate);
|
2022-01-15 13:23:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-10 12:57:42 +00:00
|
|
|
if is_proc_macro_crate && sess.panic_strategy() == PanicStrategy::Abort {
|
2023-12-18 11:21:37 +00:00
|
|
|
sess.dcx().emit_warning(errors::ProcMacroCratePanicAbort);
|
2023-01-10 12:57:42 +00:00
|
|
|
}
|
|
|
|
|
2023-02-18 12:23:57 +00:00
|
|
|
sess.time("maybe_create_a_macro_crate", || {
|
2023-04-09 19:37:31 +00:00
|
|
|
let is_test_crate = sess.is_test_crate();
|
2023-01-25 10:47:09 +00:00
|
|
|
rustc_builtin_macros::proc_macro_harness::inject(
|
2023-02-18 12:23:57 +00:00
|
|
|
&mut krate,
|
2023-01-25 10:47:09 +00:00
|
|
|
sess,
|
2023-08-09 12:28:00 +00:00
|
|
|
features,
|
2023-01-25 10:47:09 +00:00
|
|
|
resolver,
|
|
|
|
is_proc_macro_crate,
|
|
|
|
has_proc_macro_decls,
|
|
|
|
is_test_crate,
|
2023-12-17 19:21:26 +00:00
|
|
|
sess.dcx(),
|
2023-01-25 10:47:09 +00:00
|
|
|
)
|
|
|
|
});
|
2018-12-08 19:30:23 +00:00
|
|
|
|
|
|
|
// Done with macro expansion!
|
|
|
|
|
2023-03-06 10:56:53 +00:00
|
|
|
resolver.resolve_crate(&krate);
|
|
|
|
|
|
|
|
krate
|
|
|
|
}
|
|
|
|
|
|
|
|
fn early_lint_checks(tcx: TyCtxt<'_>, (): ()) {
|
|
|
|
let sess = tcx.sess;
|
|
|
|
let (resolver, krate) = &*tcx.resolver_for_lowering(()).borrow();
|
|
|
|
let mut lint_buffer = resolver.lint_buffer.steal();
|
|
|
|
|
2022-07-06 12:44:47 +00:00
|
|
|
if sess.opts.unstable_opts.input_stats {
|
2023-11-21 19:07:32 +00:00
|
|
|
eprintln!("Post-expansion node count: {}", count_nodes(krate));
|
2018-12-08 19:30:23 +00:00
|
|
|
}
|
|
|
|
|
2022-07-06 12:44:47 +00:00
|
|
|
if sess.opts.unstable_opts.hir_stats {
|
2023-11-21 19:07:32 +00:00
|
|
|
hir_stats::print_ast_stats(krate, "POST EXPANSION AST STATS", "ast-stats-2");
|
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", || {
|
2023-11-21 19:07:32 +00:00
|
|
|
rustc_ast_passes::feature_gate::check_crate(krate, sess, tcx.features());
|
2018-12-08 19:30:23 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Add all buffered lints from the `ParseSess` to the `Session`.
|
2021-06-25 09:56:14 +00:00
|
|
|
sess.parse_sess.buffered_lints.with_lock(|buffered_lints| {
|
|
|
|
info!("{} parse sess buffered_lints", buffered_lints.len());
|
|
|
|
for early_lint in buffered_lints.drain(..) {
|
2023-03-06 10:56:53 +00:00
|
|
|
lint_buffer.add_early_lint(early_lint);
|
2021-06-25 09:56:14 +00:00
|
|
|
}
|
|
|
|
});
|
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| {
|
2023-12-05 05:32:39 +00:00
|
|
|
// We will soon sort, so the initial order does not matter.
|
|
|
|
#[allow(rustc::potential_query_instability)]
|
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];
|
2023-12-18 11:21:37 +00:00
|
|
|
sess.dcx().emit_err(errors::FerrisIdentifier { spans, first_span });
|
2021-12-03 00:20:25 +00:00
|
|
|
} else {
|
2023-12-18 11:21:37 +00:00
|
|
|
sess.dcx().emit_err(errors::EmojiIdentifier { spans, ident });
|
2021-12-03 00:20:25 +00:00
|
|
|
}
|
2021-08-29 08:34:23 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-11-21 19:07:32 +00:00
|
|
|
let lint_store = unerased_lint_store(tcx.sess);
|
2023-03-06 10:56:53 +00:00
|
|
|
rustc_lint::check_ast_node(
|
|
|
|
sess,
|
2023-08-09 12:28:00 +00:00
|
|
|
tcx.features(),
|
2023-03-06 10:56:53 +00:00
|
|
|
false,
|
|
|
|
lint_store,
|
|
|
|
tcx.registered_tools(()),
|
|
|
|
Some(lint_buffer),
|
|
|
|
rustc_lint::BuiltinCombinedEarlyLintPass::new(),
|
2023-03-14 12:53:04 +00:00
|
|
|
(&**krate, &*krate.attrs),
|
2023-03-06 10:56:53 +00:00
|
|
|
)
|
2018-12-08 19:30:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns all the paths that correspond to generated files.
|
|
|
|
fn generated_output_paths(
|
2023-08-08 10:28:20 +00:00
|
|
|
tcx: TyCtxt<'_>,
|
2018-12-08 19:30:23 +00:00
|
|
|
outputs: &OutputFilenames,
|
|
|
|
exact_name: bool,
|
2022-12-06 12:46:10 +00:00
|
|
|
crate_name: Symbol,
|
2018-12-08 19:30:23 +00:00
|
|
|
) -> Vec<PathBuf> {
|
2023-08-08 10:28:20 +00:00
|
|
|
let sess = tcx.sess;
|
2018-12-08 19:30:23 +00:00
|
|
|
let mut out_filenames = Vec::new();
|
|
|
|
for output_type in sess.opts.output_types.keys() {
|
2023-02-26 20:27:27 +00:00
|
|
|
let out_filename = outputs.path(*output_type);
|
|
|
|
let file = out_filename.as_path().to_path_buf();
|
2018-12-08 19:30:23 +00:00
|
|
|
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 => {
|
2023-08-08 10:28:20 +00:00
|
|
|
for crate_type in tcx.crate_types().iter() {
|
2020-03-12 23:07:58 +00:00
|
|
|
let p = filename_for_input(sess, *crate_type, crate_name, outputs);
|
2023-02-26 20:27:27 +00:00
|
|
|
out_filenames.push(p.as_path().to_path_buf());
|
2019-12-22 22:42:04 +00:00
|
|
|
}
|
|
|
|
}
|
2022-07-06 12:44:47 +00:00
|
|
|
OutputType::DepInfo if sess.opts.unstable_opts.dep_info_omit_d_target => {
|
2018-12-08 19:30:23 +00:00
|
|
|
// Don't add the dep-info output when omitting it from dep-info targets
|
|
|
|
}
|
2023-02-26 20:27:27 +00:00
|
|
|
OutputType::DepInfo if out_filename.is_stdout() => {
|
|
|
|
// Don't add the dep-info output when it goes to stdout
|
|
|
|
}
|
2018-12-08 19:30:23 +00:00
|
|
|
_ => {
|
|
|
|
out_filenames.push(file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out_filenames
|
|
|
|
}
|
|
|
|
|
2021-11-07 09:33:27 +00:00
|
|
|
fn output_contains_path(output_paths: &[PathBuf], input_path: &Path) -> bool {
|
2023-03-16 20:42:31 +00:00
|
|
|
let input_path = try_canonicalize(input_path).ok();
|
2018-12-08 19:30:23 +00:00
|
|
|
if input_path.is_none() {
|
|
|
|
return false;
|
|
|
|
}
|
2023-10-30 04:42:03 +00:00
|
|
|
output_paths.iter().any(|output_path| try_canonicalize(output_path).ok() == input_path)
|
2018-12-08 19:30:23 +00:00
|
|
|
}
|
|
|
|
|
2023-10-30 04:42:03 +00:00
|
|
|
fn output_conflicts_with_dir(output_paths: &[PathBuf]) -> Option<&PathBuf> {
|
|
|
|
output_paths.iter().find(|output_path| output_path.is_dir())
|
2018-12-08 19:30:23 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-02-22 14:08:44 +00:00
|
|
|
fn write_out_deps(tcx: TyCtxt<'_>, outputs: &OutputFilenames, out_filenames: &[PathBuf]) {
|
2018-12-08 19:30:23 +00:00
|
|
|
// Write out dependency rules to the dep-info file if requested
|
2023-02-22 14:08:44 +00:00
|
|
|
let sess = tcx.sess;
|
2018-12-08 19:30:23 +00:00
|
|
|
if !sess.opts.output_types.contains_key(&OutputType::DepInfo) {
|
|
|
|
return;
|
|
|
|
}
|
2023-02-26 20:27:27 +00:00
|
|
|
let deps_output = outputs.path(OutputType::DepInfo);
|
|
|
|
let deps_filename = deps_output.as_path();
|
2018-12-08 19:30:23 +00:00
|
|
|
|
2023-01-09 15:15:26 +00:00
|
|
|
let result: io::Result<()> = try {
|
2018-12-08 19:30:23 +00:00
|
|
|
// 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();
|
2022-08-20 11:40:18 +00:00
|
|
|
|
|
|
|
let normalize_path = |path: PathBuf| {
|
2021-04-09 14:35:40 +00:00
|
|
|
let file = FileName::from(path);
|
|
|
|
escape_dep_filename(&file.prefer_local().to_string())
|
2022-08-20 11:40:18 +00:00
|
|
|
};
|
|
|
|
|
2023-12-05 05:32:39 +00:00
|
|
|
// The entries will be used to declare dependencies beween files in a
|
|
|
|
// Makefile-like output, so the iteration order does not matter.
|
|
|
|
#[allow(rustc::potential_query_instability)]
|
2022-08-20 11:40:18 +00:00
|
|
|
let extra_tracked_files =
|
|
|
|
file_depinfo.iter().map(|path_sym| normalize_path(PathBuf::from(path_sym.as_str())));
|
2021-04-09 14:35:40 +00:00
|
|
|
files.extend(extra_tracked_files);
|
|
|
|
|
2022-08-20 11:40:18 +00:00
|
|
|
// We also need to track used PGO profile files
|
|
|
|
if let Some(ref profile_instr) = sess.opts.cg.profile_use {
|
|
|
|
files.push(normalize_path(profile_instr.as_path().to_path_buf()));
|
|
|
|
}
|
|
|
|
if let Some(ref profile_sample) = sess.opts.unstable_opts.profile_sample_use {
|
|
|
|
files.push(normalize_path(profile_sample.as_path().to_path_buf()));
|
|
|
|
}
|
|
|
|
|
2023-05-16 11:37:46 +00:00
|
|
|
// Debugger visualizer files
|
|
|
|
for debugger_visualizer in tcx.debugger_visualizers(LOCAL_CRATE) {
|
|
|
|
files.push(normalize_path(debugger_visualizer.path.clone().unwrap()));
|
|
|
|
}
|
|
|
|
|
2019-07-24 15:00:09 +00:00
|
|
|
if sess.binary_dep_depinfo() {
|
2022-07-06 12:44:47 +00:00
|
|
|
if let Some(ref backend) = sess.opts.unstable_opts.codegen_backend {
|
2022-02-13 18:24:15 +00:00
|
|
|
if backend.contains('.') {
|
|
|
|
// If the backend name contain a `.`, it is the path to an external dynamic
|
|
|
|
// library. If not, it is not a path.
|
|
|
|
files.push(backend.to_string());
|
|
|
|
}
|
2022-02-13 18:21:33 +00:00
|
|
|
}
|
|
|
|
|
2023-02-22 14:08:44 +00:00
|
|
|
for &cnum in tcx.crates(()) {
|
|
|
|
let source = tcx.used_crate_source(cnum);
|
2022-12-07 09:33:25 +00:00
|
|
|
if let Some((path, _)) = &source.dylib {
|
|
|
|
files.push(escape_dep_filename(&path.display().to_string()));
|
2019-07-24 15:00:09 +00:00
|
|
|
}
|
2022-12-07 09:33:25 +00:00
|
|
|
if let Some((path, _)) = &source.rlib {
|
|
|
|
files.push(escape_dep_filename(&path.display().to_string()));
|
|
|
|
}
|
|
|
|
if let Some((path, _)) = &source.rmeta {
|
|
|
|
files.push(escape_dep_filename(&path.display().to_string()));
|
|
|
|
}
|
|
|
|
}
|
2019-06-10 16:18:53 +00:00
|
|
|
}
|
|
|
|
|
2023-02-26 20:27:27 +00:00
|
|
|
let write_deps_to_file = |file: &mut dyn Write| -> io::Result<()> {
|
|
|
|
for path in out_filenames {
|
|
|
|
writeln!(file, "{}: {}\n", path.display(), files.join(" "))?;
|
|
|
|
}
|
2018-12-08 19:30:23 +00:00
|
|
|
|
2023-02-26 20:27:27 +00:00
|
|
|
// 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
|
|
|
|
2023-02-26 20:27:27 +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() {
|
2023-12-05 05:32:39 +00:00
|
|
|
// We will soon sort, so the initial order does not matter.
|
|
|
|
#[allow(rustc::potential_query_instability)]
|
2023-02-26 20:27:27 +00:00
|
|
|
let mut envs: Vec<_> = env_depinfo
|
|
|
|
.iter()
|
|
|
|
.map(|(k, v)| (escape_dep_env(*k), v.map(escape_dep_env)))
|
|
|
|
.collect();
|
|
|
|
envs.sort_unstable();
|
2020-05-03 17:47:51 +00:00
|
|
|
writeln!(file)?;
|
2023-02-26 20:27:27 +00:00
|
|
|
for (k, v) in envs {
|
|
|
|
write!(file, "# env-dep:{k}")?;
|
|
|
|
if let Some(v) = v {
|
|
|
|
write!(file, "={v}")?;
|
|
|
|
}
|
|
|
|
writeln!(file)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
};
|
|
|
|
|
|
|
|
match deps_output {
|
|
|
|
OutFileName::Stdout => {
|
|
|
|
let mut file = BufWriter::new(io::stdout());
|
|
|
|
write_deps_to_file(&mut file)?;
|
|
|
|
}
|
|
|
|
OutFileName::Real(ref path) => {
|
|
|
|
let mut file = BufWriter::new(fs::File::create(path)?);
|
|
|
|
write_deps_to_file(&mut file)?;
|
2020-05-03 17:47:51 +00:00
|
|
|
}
|
|
|
|
}
|
2023-01-09 15:15:26 +00:00
|
|
|
};
|
2018-12-08 19:30:23 +00:00
|
|
|
|
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 {
|
2023-12-17 19:21:26 +00:00
|
|
|
sess.dcx().emit_artifact_notification(deps_filename, "dep-info");
|
2019-07-18 08:02:22 +00:00
|
|
|
}
|
|
|
|
}
|
2022-08-22 13:58:26 +00:00
|
|
|
Err(error) => {
|
2023-12-18 11:21:37 +00:00
|
|
|
sess.dcx().emit_fatal(errors::ErrorWritingDependencies { path: deps_filename, error });
|
2022-08-20 16:33:02 +00:00
|
|
|
}
|
2018-12-08 19:30:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-16 14:07:42 +00:00
|
|
|
fn resolver_for_lowering<'tcx>(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
(): (),
|
|
|
|
) -> &'tcx Steal<(ty::ResolverAstLowering, Lrc<ast::Crate>)> {
|
|
|
|
let arenas = Resolver::arenas();
|
2023-03-06 10:56:23 +00:00
|
|
|
let _ = tcx.registered_tools(()); // Uses `crate_for_resolver`.
|
2023-03-14 12:53:04 +00:00
|
|
|
let (krate, pre_configured_attrs) = tcx.crate_for_resolver(()).steal();
|
|
|
|
let mut resolver = Resolver::new(tcx, &pre_configured_attrs, krate.spans.inner_span, &arenas);
|
|
|
|
let krate = configure_and_expand(krate, &pre_configured_attrs, &mut resolver);
|
2023-02-16 14:07:42 +00:00
|
|
|
|
|
|
|
// Make sure we don't mutate the cstore from here on.
|
2023-09-09 14:02:11 +00:00
|
|
|
tcx.untracked().cstore.freeze();
|
2023-02-16 14:07:42 +00:00
|
|
|
|
|
|
|
let ty::ResolverOutputs {
|
|
|
|
global_ctxt: untracked_resolutions,
|
|
|
|
ast_lowering: untracked_resolver_for_lowering,
|
|
|
|
} = resolver.into_outputs();
|
|
|
|
|
|
|
|
let feed = tcx.feed_unit_query();
|
|
|
|
feed.resolutions(tcx.arena.alloc(untracked_resolutions));
|
|
|
|
tcx.arena.alloc(Steal::new((untracked_resolver_for_lowering, Lrc::new(krate))))
|
|
|
|
}
|
|
|
|
|
2023-11-04 17:35:28 +00:00
|
|
|
pub(crate) fn write_dep_info(tcx: TyCtxt<'_>) {
|
2023-11-04 16:06:23 +00:00
|
|
|
// Make sure name resolution and macro expansion is run for
|
|
|
|
// the side-effect of providing a complete set of all
|
|
|
|
// accessed files and env vars.
|
|
|
|
let _ = tcx.resolver_for_lowering(());
|
|
|
|
|
2023-01-23 10:25:51 +00:00
|
|
|
let sess = tcx.sess;
|
2023-11-04 15:59:29 +00:00
|
|
|
let _timer = sess.timer("write_dep_info");
|
2023-01-23 10:25:51 +00:00
|
|
|
let crate_name = tcx.crate_name(LOCAL_CRATE);
|
2020-01-09 02:48:00 +00:00
|
|
|
|
2023-11-04 15:59:29 +00:00
|
|
|
let outputs = tcx.output_filenames(());
|
2019-12-22 22:42:04 +00:00
|
|
|
let output_paths =
|
2023-08-08 10:28:20 +00:00
|
|
|
generated_output_paths(tcx, &outputs, sess.io.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.
|
2023-11-21 19:07:32 +00:00
|
|
|
if let Some(input_path) = sess.io.input.opt_path() {
|
2018-12-08 19:30:23 +00:00
|
|
|
if sess.opts.will_create_output_file() {
|
|
|
|
if output_contains_path(&output_paths, input_path) {
|
2023-12-18 11:21:37 +00:00
|
|
|
sess.dcx().emit_fatal(errors::InputFileWouldBeOverWritten { path: input_path });
|
2018-12-08 19:30:23 +00:00
|
|
|
}
|
2023-11-21 19:07:32 +00:00
|
|
|
if let Some(dir_path) = output_conflicts_with_dir(&output_paths) {
|
2023-12-18 11:21:37 +00:00
|
|
|
sess.dcx().emit_fatal(errors::GeneratedFileConflictsWithDirectory {
|
2023-02-05 02:20:06 +00:00
|
|
|
input_path,
|
|
|
|
dir_path,
|
|
|
|
});
|
2018-12-08 19:30:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-07 09:24:00 +00:00
|
|
|
if let Some(ref dir) = sess.io.temps_dir {
|
2021-04-04 11:33:33 +00:00
|
|
|
if fs::create_dir_all(dir).is_err() {
|
2023-12-18 11:21:37 +00:00
|
|
|
sess.dcx().emit_fatal(errors::TempsDirError);
|
2021-04-04 11:33:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-22 14:08:44 +00:00
|
|
|
write_out_deps(tcx, &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 {
|
2022-12-07 09:24:00 +00:00
|
|
|
if let Some(ref dir) = sess.io.output_dir {
|
2018-12-08 19:30:23 +00:00
|
|
|
if fs::create_dir_all(dir).is_err() {
|
2023-12-18 11:21:37 +00:00
|
|
|
sess.dcx().emit_fatal(errors::OutDirError);
|
2018-12-08 19:30:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-22 16:26:20 +00:00
|
|
|
pub static DEFAULT_QUERY_PROVIDERS: LazyLock<Providers> = LazyLock::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;
|
2021-07-13 16:45:20 +00:00
|
|
|
providers.hir_crate = rustc_ast_lowering::lower_to_hir;
|
2023-02-16 14:07:42 +00:00
|
|
|
providers.resolver_for_lowering = resolver_for_lowering;
|
2023-03-06 10:56:53 +00:00
|
|
|
providers.early_lint_checks = early_lint_checks;
|
2018-12-08 19:30:23 +00:00
|
|
|
proc_macro_decls::provide(providers);
|
2023-09-22 16:26:20 +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);
|
2023-03-06 10:56:23 +00:00
|
|
|
rustc_resolve::provide(providers);
|
2022-09-26 11:00:29 +00:00
|
|
|
rustc_hir_analysis::provide(providers);
|
2022-10-20 15:51:37 +00:00
|
|
|
rustc_hir_typeck::provide(providers);
|
2018-12-08 19:30:23 +00:00
|
|
|
ty::provide(providers);
|
|
|
|
traits::provide(providers);
|
|
|
|
rustc_passes::provide(providers);
|
|
|
|
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);
|
2023-09-22 16:26:20 +00:00
|
|
|
*providers
|
2020-07-15 00:23:35 +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,
|
2023-08-08 10:28:20 +00:00
|
|
|
crate_types: Vec<CrateType>,
|
2023-08-08 12:08:24 +00:00
|
|
|
stable_crate_id: StableCrateId,
|
2020-02-06 12:41:37 +00:00
|
|
|
dep_graph: DepGraph,
|
2022-12-06 15:43:52 +00:00
|
|
|
untracked: Untracked,
|
2023-08-31 23:14:33 +00:00
|
|
|
gcx_cell: &'tcx OnceLock<GlobalCtxt<'tcx>>,
|
2019-11-27 12:24:19 +00:00
|
|
|
arena: &'tcx WorkerLocal<Arena<'tcx>>,
|
2021-07-13 16:45:20 +00:00
|
|
|
hir_arena: &'tcx WorkerLocal<rustc_hir::Arena<'tcx>>,
|
2023-02-07 05:59:50 +00:00
|
|
|
) -> &'tcx GlobalCtxt<'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();
|
|
|
|
|
2023-11-20 02:26:09 +00:00
|
|
|
let sess = &compiler.sess;
|
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
|
|
|
|
2023-11-20 02:26:09 +00:00
|
|
|
let codegen_backend = &compiler.codegen_backend;
|
2023-09-22 16:26:20 +00:00
|
|
|
let mut providers = *DEFAULT_QUERY_PROVIDERS;
|
|
|
|
codegen_backend.provide(&mut providers);
|
2019-11-11 15:09:03 +00:00
|
|
|
|
2019-11-27 12:24:19 +00:00
|
|
|
if let Some(callback) = compiler.override_queries {
|
2023-09-22 16:38:31 +00:00
|
|
|
callback(sess, &mut providers);
|
2019-11-26 21:56:05 +00:00
|
|
|
}
|
2018-12-08 19:30:23 +00:00
|
|
|
|
2023-05-14 19:53:05 +00:00
|
|
|
let incremental = dep_graph.is_fully_enabled();
|
|
|
|
|
2023-02-07 05:59:50 +00:00
|
|
|
sess.time("setup_global_ctxt", || {
|
|
|
|
gcx_cell.get_or_init(move || {
|
2020-01-09 02:48:00 +00:00
|
|
|
TyCtxt::create_global_ctxt(
|
|
|
|
sess,
|
2023-08-08 10:28:20 +00:00
|
|
|
crate_types,
|
2023-08-08 12:08:24 +00:00
|
|
|
stable_crate_id,
|
2020-01-09 02:48:00 +00:00
|
|
|
arena,
|
2021-07-13 16:45:20 +00:00
|
|
|
hir_arena,
|
2022-12-07 14:31:50 +00:00
|
|
|
untracked,
|
2020-02-08 04:18:34 +00:00
|
|
|
dep_graph,
|
2021-10-16 19:12:34 +00:00
|
|
|
rustc_query_impl::query_callbacks(arena),
|
2023-02-07 07:32:30 +00:00
|
|
|
rustc_query_impl::query_system(
|
2023-09-22 16:26:20 +00:00
|
|
|
providers.queries,
|
2023-09-22 16:38:31 +00:00
|
|
|
providers.extern_queries,
|
2023-02-07 07:32:30 +00:00
|
|
|
query_result_on_disk_cache,
|
2023-05-14 19:53:05 +00:00
|
|
|
incremental,
|
2023-02-07 07:32:30 +00:00
|
|
|
),
|
2023-09-22 16:26:20 +00:00
|
|
|
providers.hooks,
|
2020-01-09 02:48:00 +00:00
|
|
|
)
|
|
|
|
})
|
2023-02-07 05:59:50 +00:00
|
|
|
})
|
2018-12-08 19:30:23 +00:00
|
|
|
}
|
|
|
|
|
2023-06-21 01:26:49 +00:00
|
|
|
/// Runs the 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;
|
|
|
|
|
2020-01-07 20:34:08 +00:00
|
|
|
sess.time("misc_checking_1", || {
|
2019-12-22 22:42:04 +00:00
|
|
|
parallel!(
|
|
|
|
{
|
2023-07-14 17:38:45 +00:00
|
|
|
sess.time("looking_for_entry_point", || tcx.ensure().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
|
|
|
},
|
2022-01-19 15:24:49 +00:00
|
|
|
{
|
|
|
|
sess.time("unused_lib_feature_checking", || {
|
|
|
|
rustc_passes::stability::check_unused_or_stable_features(tcx)
|
|
|
|
});
|
|
|
|
},
|
2021-06-25 23:48:26 +00:00
|
|
|
{
|
2022-03-30 19:14:15 +00:00
|
|
|
// We force these queries to run,
|
2021-07-04 18:02:51 +00:00
|
|
|
// 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(());
|
2022-01-16 18:36:22 +00:00
|
|
|
tcx.ensure().stability_index(());
|
2019-12-22 22:42:04 +00:00
|
|
|
}
|
|
|
|
);
|
2018-12-08 19:30:23 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// passes are timed inside typeck
|
2022-09-26 11:00:29 +00:00
|
|
|
rustc_hir_analysis::check_crate(tcx)?;
|
2018-12-08 19:30:23 +00:00
|
|
|
|
2020-01-07 20:34:08 +00:00
|
|
|
sess.time("MIR_borrow_checking", || {
|
2023-10-05 14:57:14 +00:00
|
|
|
tcx.hir().par_body_owners(|def_id| {
|
|
|
|
// Run THIR unsafety check because it's responsible for stealing
|
|
|
|
// and deallocating THIR when enabled.
|
|
|
|
tcx.ensure().thir_check_unsafety(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() {
|
2022-07-06 12:44:47 +00:00
|
|
|
if !tcx.sess.opts.unstable_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
|
|
|
}
|
2022-05-18 02:51:52 +00:00
|
|
|
tcx.ensure().has_ffi_unwind_calls(def_id);
|
2020-05-03 04:16:55 +00:00
|
|
|
|
2023-04-09 09:42:57 +00:00
|
|
|
// If we need to codegen, ensure that we emit all errors from
|
|
|
|
// `mir_drops_elaborated_and_const_checked` now, to avoid discovering
|
|
|
|
// them later during codegen.
|
|
|
|
if tcx.sess.opts.output_types.should_codegen()
|
|
|
|
|| tcx.hir().body_const_context(def_id).is_some()
|
|
|
|
{
|
2022-05-08 13:53:19 +00:00
|
|
|
tcx.ensure().mir_drops_elaborated_and_const_checked(def_id);
|
2023-04-09 09:42:57 +00:00
|
|
|
tcx.ensure().unused_generic_params(ty::InstanceDef::Item(def_id.to_def_id()));
|
2020-05-03 04:16:55 +00:00
|
|
|
}
|
2018-12-08 19:30:23 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-01-28 12:56:04 +00:00
|
|
|
tcx.hir().par_body_owners(|def_id| {
|
2023-11-26 13:05:08 +00:00
|
|
|
if tcx.is_coroutine(def_id.to_def_id()) {
|
2023-10-19 21:46:28 +00:00
|
|
|
tcx.ensure().mir_coroutine_witnesses(def_id);
|
|
|
|
tcx.ensure().check_coroutine_obligations(def_id);
|
2023-01-28 12:56:04 +00:00
|
|
|
}
|
|
|
|
});
|
2022-10-01 13:57:22 +00:00
|
|
|
|
2020-01-07 20:34:08 +00:00
|
|
|
sess.time("layout_testing", || layout_test::test_layout(tcx));
|
2023-08-25 21:01:06 +00:00
|
|
|
sess.time("abi_testing", || abi_test::test_abi(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
|
2023-12-21 05:26:09 +00:00
|
|
|
if let Some(reported) = sess.dcx().has_errors() {
|
2022-01-23 00:49:12 +00:00
|
|
|
return Err(reported);
|
2018-12-08 19:30:23 +00:00
|
|
|
}
|
|
|
|
|
2020-01-07 20:34:08 +00:00
|
|
|
sess.time("misc_checking_3", || {
|
2019-12-22 22:42:04 +00:00
|
|
|
parallel!(
|
|
|
|
{
|
2022-09-22 13:19:53 +00:00
|
|
|
tcx.ensure().effective_visibilities(());
|
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("lint_checking", || {
|
2023-07-15 16:43:45 +00:00
|
|
|
rustc_lint::check_crate(tcx);
|
2019-12-22 22:42:04 +00:00
|
|
|
});
|
2023-07-15 08:38:50 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
tcx.ensure().clashing_extern_declarations(());
|
2019-12-22 22:42:04 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
);
|
2022-03-28 22:10:45 +00:00
|
|
|
|
|
|
|
// This check has to be run after all lints are done processing. We don't
|
|
|
|
// define a lint filter, as all lint checks should have finished at this point.
|
2023-07-14 17:38:45 +00:00
|
|
|
sess.time("check_lint_expectations", || tcx.ensure().check_expectations(None));
|
2023-10-23 09:41:30 +00:00
|
|
|
|
|
|
|
// This query is only invoked normally if a diagnostic is emitted that needs any
|
|
|
|
// diagnostic item. If the crate compiles without checking any diagnostic items,
|
|
|
|
// we will fail to emit overlap diagnostics. Thus we invoke it here unconditionally.
|
|
|
|
let _ = tcx.all_diagnostic_items(());
|
2018-12-08 19:30:23 +00:00
|
|
|
});
|
|
|
|
|
2023-05-17 12:28:04 +00:00
|
|
|
if sess.opts.unstable_opts.print_vtable_sizes {
|
|
|
|
let traits = tcx.traits(LOCAL_CRATE);
|
|
|
|
|
|
|
|
for &tr in traits {
|
|
|
|
if !tcx.check_is_object_safe(tr) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let name = ty::print::with_no_trimmed_paths!(tcx.def_path_str(tr));
|
|
|
|
|
|
|
|
let mut first_dsa = true;
|
|
|
|
|
|
|
|
// Number of vtable entries, if we didn't have upcasting
|
2023-06-13 12:07:12 +00:00
|
|
|
let mut entries_ignoring_upcasting = 0;
|
2023-05-17 12:28:04 +00:00
|
|
|
// Number of vtable entries needed solely for upcasting
|
2023-06-13 12:07:12 +00:00
|
|
|
let mut entries_for_upcasting = 0;
|
2023-05-17 12:28:04 +00:00
|
|
|
|
|
|
|
let trait_ref = ty::Binder::dummy(ty::TraitRef::identity(tcx, tr));
|
|
|
|
|
2023-10-30 04:33:05 +00:00
|
|
|
// A slightly edited version of the code in
|
|
|
|
// `rustc_trait_selection::traits::vtable::vtable_entries`, that works without self
|
|
|
|
// type and just counts number of entries.
|
2023-05-17 12:28:04 +00:00
|
|
|
//
|
2023-10-30 04:33:05 +00:00
|
|
|
// Note that this is technically wrong, for traits which have associated types in
|
|
|
|
// supertraits:
|
2023-05-17 12:28:04 +00:00
|
|
|
//
|
|
|
|
// trait A: AsRef<Self::T> + AsRef<()> { type T; }
|
|
|
|
//
|
2023-10-30 04:33:05 +00:00
|
|
|
// Without self type we can't normalize `Self::T`, so we can't know if `AsRef<Self::T>`
|
|
|
|
// and `AsRef<()>` are the same trait, thus we assume that those are different, and
|
|
|
|
// potentially over-estimate how many vtable entries there are.
|
2023-05-17 12:28:04 +00:00
|
|
|
//
|
|
|
|
// Similarly this is wrong for traits that have methods with possibly-impossible bounds.
|
|
|
|
// For example:
|
|
|
|
//
|
|
|
|
// trait B<T> { fn f(&self) where T: Copy; }
|
|
|
|
//
|
|
|
|
// Here `dyn B<u8>` will have 4 entries, while `dyn B<String>` will only have 3.
|
|
|
|
// However, since we don't know `T`, we can't know if `T: Copy` holds or not,
|
|
|
|
// thus we lean on the bigger side and say it has 4 entries.
|
|
|
|
traits::vtable::prepare_vtable_segments(tcx, trait_ref, |segment| {
|
|
|
|
match segment {
|
|
|
|
traits::vtable::VtblSegment::MetadataDSA => {
|
|
|
|
// If this is the first dsa, it would be included either way,
|
|
|
|
// otherwise it's needed for upcasting
|
|
|
|
if std::mem::take(&mut first_dsa) {
|
2023-06-13 12:07:12 +00:00
|
|
|
entries_ignoring_upcasting += 3;
|
2023-05-17 12:28:04 +00:00
|
|
|
} else {
|
2023-06-13 12:07:12 +00:00
|
|
|
entries_for_upcasting += 3;
|
2023-05-17 12:28:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
traits::vtable::VtblSegment::TraitOwnEntries { trait_ref, emit_vptr } => {
|
|
|
|
// Lookup the shape of vtable for the trait.
|
|
|
|
let own_existential_entries =
|
2023-06-13 11:46:40 +00:00
|
|
|
tcx.own_existential_vtable_entries(trait_ref.def_id());
|
2023-05-17 12:28:04 +00:00
|
|
|
|
2023-10-30 04:33:05 +00:00
|
|
|
// The original code here ignores the method if its predicates are
|
|
|
|
// impossible. We can't really do that as, for example, all not trivial
|
|
|
|
// bounds on generic parameters are impossible (since we don't know the
|
|
|
|
// parameters...), see the comment above.
|
2023-06-13 12:07:12 +00:00
|
|
|
entries_ignoring_upcasting += own_existential_entries.len();
|
2023-05-17 12:28:04 +00:00
|
|
|
|
|
|
|
if emit_vptr {
|
2023-06-13 12:07:12 +00:00
|
|
|
entries_for_upcasting += 1;
|
2023-05-17 12:28:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::ops::ControlFlow::Continue::<std::convert::Infallible>(())
|
|
|
|
});
|
|
|
|
|
|
|
|
sess.code_stats.record_vtable_size(
|
|
|
|
tr,
|
|
|
|
&name,
|
|
|
|
VTableSizeInfo {
|
|
|
|
trait_name: name.clone(),
|
2023-06-13 12:07:12 +00:00
|
|
|
entries: entries_ignoring_upcasting + entries_for_upcasting,
|
|
|
|
entries_ignoring_upcasting,
|
|
|
|
entries_for_upcasting,
|
|
|
|
upcasting_cost_percent: entries_for_upcasting as f64
|
|
|
|
/ entries_ignoring_upcasting as f64
|
|
|
|
* 100.,
|
2023-05-17 12:28:04 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-08 19:30:23 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
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
|
|
|
) -> 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
|
|
|
|
2022-12-03 12:28:01 +00:00
|
|
|
let (metadata, need_metadata_module) = rustc_metadata::fs::encode_and_write_metadata(tcx);
|
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
|
|
|
|
2023-09-19 11:23:35 +00:00
|
|
|
// Don't run this test assertions when not doing codegen. Compiletest tries to build
|
2020-12-17 09:05:39 +00:00
|
|
|
// 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_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) {
|
2022-12-03 12:28:01 +00:00
|
|
|
if let Err(error) = rustc_mir_transform::dump_mir::emit_mir(tcx) {
|
2023-12-21 05:26:09 +00:00
|
|
|
let dcx = tcx.dcx();
|
|
|
|
dcx.emit_err(errors::CantEmitMIR { error });
|
|
|
|
dcx.abort_if_errors();
|
2018-12-08 19:30:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|