2016-07-21 16:50:15 +00:00
|
|
|
//! This pass is only used for UNIT TESTS related to incremental
|
|
|
|
//! compilation. It tests whether a particular `.o` file will be re-used
|
|
|
|
//! from a previous compilation or whether it must be regenerated.
|
|
|
|
//!
|
|
|
|
//! The user adds annotations to the crate of the following form:
|
|
|
|
//!
|
|
|
|
//! ```
|
2022-04-15 22:04:34 +00:00
|
|
|
//! # #![feature(rustc_attrs)]
|
2016-07-21 16:50:15 +00:00
|
|
|
//! #![rustc_partition_reused(module="spike", cfg="rpass2")]
|
2018-05-08 13:10:16 +00:00
|
|
|
//! #![rustc_partition_codegened(module="spike-x", cfg="rpass2")]
|
2016-07-21 16:50:15 +00:00
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! The first indicates (in the cfg `rpass2`) that `spike.o` will be
|
|
|
|
//! reused, the second that `spike-x.o` will be recreated. If these
|
|
|
|
//! annotations are inaccurate, errors are reported.
|
|
|
|
//!
|
|
|
|
//! The reason that we use `cfg=...` and not `#[cfg_attr]` is so that
|
|
|
|
//! the HIR doesn't change as a result of the annotations, which might
|
|
|
|
//! perturb the reuse results.
|
2018-09-18 14:33:24 +00:00
|
|
|
//!
|
2023-03-03 03:10:46 +00:00
|
|
|
//! `#![rustc_expected_cgu_reuse(module="spike", cfg="rpass2", kind="post-lto")]`
|
2018-09-18 14:33:24 +00:00
|
|
|
//! allows for doing a more fine-grained check to see if pre- or post-lto data
|
|
|
|
//! was re-used.
|
2016-07-21 16:50:15 +00:00
|
|
|
|
2022-09-26 14:42:12 +00:00
|
|
|
use crate::errors;
|
2020-04-27 17:56:11 +00:00
|
|
|
use rustc_ast as ast;
|
2023-05-07 23:52:19 +00:00
|
|
|
use rustc_data_structures::unord::UnordSet;
|
2020-01-05 01:37:57 +00:00
|
|
|
use rustc_hir::def_id::LOCAL_CRATE;
|
2020-03-29 15:19:48 +00:00
|
|
|
use rustc_middle::mir::mono::CodegenUnitNameBuilder;
|
|
|
|
use rustc_middle::ty::TyCtxt;
|
2019-11-12 13:22:16 +00:00
|
|
|
use rustc_session::cgu_reuse_tracker::*;
|
2020-01-01 18:30:57 +00:00
|
|
|
use rustc_span::symbol::{sym, Symbol};
|
2022-11-23 00:55:16 +00:00
|
|
|
use thin_vec::ThinVec;
|
2016-07-21 16:50:15 +00:00
|
|
|
|
2021-10-29 17:14:17 +00:00
|
|
|
#[allow(missing_docs)]
|
2019-06-21 18:27:44 +00:00
|
|
|
pub fn assert_module_sources(tcx: TyCtxt<'_>) {
|
2017-12-28 05:05:45 +00:00
|
|
|
tcx.dep_graph.with_ignore(|| {
|
|
|
|
if tcx.sess.opts.incremental.is_none() {
|
|
|
|
return;
|
|
|
|
}
|
2016-07-21 16:50:15 +00:00
|
|
|
|
2022-04-05 12:52:53 +00:00
|
|
|
let available_cgus =
|
|
|
|
tcx.collect_and_partition_mono_items(()).1.iter().map(|cgu| cgu.name()).collect();
|
2018-09-18 14:33:24 +00:00
|
|
|
|
|
|
|
let ams = AssertModuleSource { tcx, available_cgus };
|
|
|
|
|
2020-11-26 22:38:53 +00:00
|
|
|
for attr in tcx.hir().attrs(rustc_hir::CRATE_HIR_ID) {
|
2017-12-28 05:05:45 +00:00
|
|
|
ams.check_attr(attr);
|
|
|
|
}
|
|
|
|
})
|
2016-07-21 16:50:15 +00:00
|
|
|
}
|
|
|
|
|
2019-06-11 19:03:44 +00:00
|
|
|
struct AssertModuleSource<'tcx> {
|
2019-06-13 21:48:52 +00:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2023-05-07 23:52:19 +00:00
|
|
|
available_cgus: UnordSet<Symbol>,
|
2016-07-21 16:50:15 +00:00
|
|
|
}
|
|
|
|
|
2021-12-14 04:49:21 +00:00
|
|
|
impl<'tcx> AssertModuleSource<'tcx> {
|
2016-07-21 16:50:15 +00:00
|
|
|
fn check_attr(&self, attr: &ast::Attribute) {
|
2021-07-29 17:00:41 +00:00
|
|
|
let (expected_reuse, comp_kind) = if attr.has_name(sym::rustc_partition_reused) {
|
|
|
|
(CguReuse::PreLto, ComparisonKind::AtLeast)
|
|
|
|
} else if attr.has_name(sym::rustc_partition_codegened) {
|
|
|
|
(CguReuse::No, ComparisonKind::Exact)
|
|
|
|
} else if attr.has_name(sym::rustc_expected_cgu_reuse) {
|
|
|
|
match self.field(attr, sym::kind) {
|
|
|
|
sym::no => (CguReuse::No, ComparisonKind::Exact),
|
|
|
|
sym::pre_dash_lto => (CguReuse::PreLto, ComparisonKind::Exact),
|
|
|
|
sym::post_dash_lto => (CguReuse::PostLto, ComparisonKind::Exact),
|
|
|
|
sym::any => (CguReuse::PreLto, ComparisonKind::AtLeast),
|
|
|
|
other => {
|
2022-09-26 14:42:12 +00:00
|
|
|
self.tcx
|
|
|
|
.sess
|
|
|
|
.emit_fatal(errors::UnknownReuseKind { span: attr.span, kind: other });
|
2018-09-18 14:33:24 +00:00
|
|
|
}
|
2021-07-29 17:00:41 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
};
|
2016-07-21 16:50:15 +00:00
|
|
|
|
2022-07-06 12:44:47 +00:00
|
|
|
if !self.tcx.sess.opts.unstable_opts.query_dep_graph {
|
2022-09-26 14:42:12 +00:00
|
|
|
self.tcx.sess.emit_fatal(errors::MissingQueryDepGraph { span: attr.span });
|
2018-09-18 14:33:24 +00:00
|
|
|
}
|
|
|
|
|
2016-07-21 16:50:15 +00:00
|
|
|
if !self.check_config(attr) {
|
|
|
|
debug!("check_attr: config does not match, ignoring attr");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-10-21 21:31:37 +00:00
|
|
|
let user_path = self.field(attr, sym::module).to_string();
|
|
|
|
let crate_name = self.tcx.crate_name(LOCAL_CRATE).to_string();
|
2018-07-10 17:16:22 +00:00
|
|
|
|
|
|
|
if !user_path.starts_with(&crate_name) {
|
2022-09-26 14:42:12 +00:00
|
|
|
self.tcx.sess.emit_fatal(errors::MalformedCguName {
|
|
|
|
span: attr.span,
|
|
|
|
user_path,
|
|
|
|
crate_name,
|
|
|
|
});
|
2018-07-10 17:16:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Split of the "special suffix" if there is one.
|
2020-02-26 12:03:46 +00:00
|
|
|
let (user_path, cgu_special_suffix) = if let Some(index) = user_path.rfind('.') {
|
2018-07-10 17:16:22 +00:00
|
|
|
(&user_path[..index], Some(&user_path[index + 1..]))
|
|
|
|
} else {
|
|
|
|
(&user_path[..], None)
|
|
|
|
};
|
|
|
|
|
2020-10-28 16:05:49 +00:00
|
|
|
let mut iter = user_path.split('-');
|
2018-07-10 17:16:22 +00:00
|
|
|
|
|
|
|
// Remove the crate name
|
2020-10-28 16:05:49 +00:00
|
|
|
assert_eq!(iter.next().unwrap(), crate_name);
|
|
|
|
|
|
|
|
let cgu_path_components = iter.collect::<Vec<_>>();
|
2018-07-10 17:16:22 +00:00
|
|
|
|
2018-08-14 15:55:22 +00:00
|
|
|
let cgu_name_builder = &mut CodegenUnitNameBuilder::new(self.tcx);
|
|
|
|
let cgu_name =
|
|
|
|
cgu_name_builder.build_cgu_name(LOCAL_CRATE, cgu_path_components, cgu_special_suffix);
|
2018-07-10 17:16:22 +00:00
|
|
|
|
2019-10-21 21:22:23 +00:00
|
|
|
debug!("mapping '{}' to cgu name '{}'", self.field(attr, sym::module), cgu_name);
|
2016-07-21 16:50:15 +00:00
|
|
|
|
2022-04-05 12:52:53 +00:00
|
|
|
if !self.available_cgus.contains(&cgu_name) {
|
2023-06-05 04:16:20 +00:00
|
|
|
let cgu_names: Vec<&str> =
|
2023-06-08 04:38:50 +00:00
|
|
|
self.available_cgus.items().map(|cgu| cgu.as_str()).into_sorted_stable_ord();
|
2022-09-26 14:42:12 +00:00
|
|
|
self.tcx.sess.emit_err(errors::NoModuleNamed {
|
|
|
|
span: attr.span,
|
|
|
|
user_path,
|
|
|
|
cgu_name,
|
|
|
|
cgu_names: cgu_names.join(", "),
|
|
|
|
});
|
2016-07-21 16:50:15 +00:00
|
|
|
}
|
2018-09-18 14:33:24 +00:00
|
|
|
|
|
|
|
self.tcx.sess.cgu_reuse_tracker.set_expectation(
|
2020-07-08 10:03:37 +00:00
|
|
|
cgu_name,
|
2018-09-18 14:33:24 +00:00
|
|
|
&user_path,
|
|
|
|
attr.span,
|
|
|
|
expected_reuse,
|
|
|
|
comp_kind,
|
|
|
|
);
|
2016-07-21 16:50:15 +00:00
|
|
|
}
|
|
|
|
|
2020-04-19 11:00:18 +00:00
|
|
|
fn field(&self, attr: &ast::Attribute, name: Symbol) -> Symbol {
|
2022-11-23 00:55:16 +00:00
|
|
|
for item in attr.meta_item_list().unwrap_or_else(ThinVec::new) {
|
2020-08-02 10:17:20 +00:00
|
|
|
if item.has_name(name) {
|
2016-07-21 16:50:15 +00:00
|
|
|
if let Some(value) = item.value_str() {
|
2016-11-16 10:52:37 +00:00
|
|
|
return value;
|
2016-07-21 16:50:15 +00:00
|
|
|
} else {
|
2022-09-26 14:42:12 +00:00
|
|
|
self.tcx.sess.emit_fatal(errors::FieldAssociatedValueExpected {
|
|
|
|
span: item.span(),
|
|
|
|
name,
|
|
|
|
});
|
2016-07-21 16:50:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-26 14:42:12 +00:00
|
|
|
self.tcx.sess.emit_fatal(errors::NoField { span: attr.span, name });
|
2016-07-21 16:50:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Scan for a `cfg="foo"` attribute and check whether we have a
|
|
|
|
/// cfg flag called `foo`.
|
|
|
|
fn check_config(&self, attr: &ast::Attribute) -> bool {
|
2016-10-27 06:36:56 +00:00
|
|
|
let config = &self.tcx.sess.parse_sess.config;
|
2019-10-21 21:22:23 +00:00
|
|
|
let value = self.field(attr, sym::cfg);
|
2016-07-21 16:50:15 +00:00
|
|
|
debug!("check_config(config={:?}, value={:?})", config, value);
|
2016-11-15 08:54:27 +00:00
|
|
|
if config.iter().any(|&(name, _)| name == value) {
|
2016-07-21 16:50:15 +00:00
|
|
|
debug!("check_config: matched");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
debug!("check_config: no match found");
|
2020-03-20 14:03:11 +00:00
|
|
|
false
|
2016-07-21 16:50:15 +00:00
|
|
|
}
|
|
|
|
}
|