1 //! This pass is only used for UNIT TESTS related to incremental
2 //! compilation. It tests whether a particular `.o` file will be re-used
3 //! from a previous compilation or whether it must be regenerated.
5 //! The user adds annotations to the crate of the following form:
8 //! # #![feature(rustc_attrs)]
9 //! #![rustc_partition_reused(module="spike", cfg="rpass2")]
10 //! #![rustc_partition_codegened(module="spike-x", cfg="rpass2")]
13 //! The first indicates (in the cfg `rpass2`) that `spike.o` will be
14 //! reused, the second that `spike-x.o` will be recreated. If these
15 //! annotations are inaccurate, errors are reported.
17 //! The reason that we use `cfg=...` and not `#[cfg_attr]` is so that
18 //! the HIR doesn't change as a result of the annotations, which might
19 //! perturb the reuse results.
21 //! `#![rustc_expected_cgu_reuse(module="spike", cfg="rpass2", kind="post-lto")]
22 //! allows for doing a more fine-grained check to see if pre- or post-lto data
26 use rustc_data_structures::fx::FxHashSet;
27 use rustc_hir::def_id::LOCAL_CRATE;
28 use rustc_middle::mir::mono::CodegenUnitNameBuilder;
29 use rustc_middle::ty::TyCtxt;
30 use rustc_session::cgu_reuse_tracker::*;
31 use rustc_span::symbol::{sym, Symbol};
33 #[allow(missing_docs)]
34 pub fn assert_module_sources(tcx: TyCtxt<'_>) {
35 tcx.dep_graph.with_ignore(|| {
36 if tcx.sess.opts.incremental.is_none() {
41 tcx.collect_and_partition_mono_items(()).1.iter().map(|cgu| cgu.name()).collect();
43 let ams = AssertModuleSource { tcx, available_cgus };
45 for attr in tcx.hir().attrs(rustc_hir::CRATE_HIR_ID) {
51 struct AssertModuleSource<'tcx> {
53 available_cgus: FxHashSet<Symbol>,
56 impl<'tcx> AssertModuleSource<'tcx> {
57 fn check_attr(&self, attr: &ast::Attribute) {
58 let (expected_reuse, comp_kind) = if attr.has_name(sym::rustc_partition_reused) {
59 (CguReuse::PreLto, ComparisonKind::AtLeast)
60 } else if attr.has_name(sym::rustc_partition_codegened) {
61 (CguReuse::No, ComparisonKind::Exact)
62 } else if attr.has_name(sym::rustc_expected_cgu_reuse) {
63 match self.field(attr, sym::kind) {
64 sym::no => (CguReuse::No, ComparisonKind::Exact),
65 sym::pre_dash_lto => (CguReuse::PreLto, ComparisonKind::Exact),
66 sym::post_dash_lto => (CguReuse::PostLto, ComparisonKind::Exact),
67 sym::any => (CguReuse::PreLto, ComparisonKind::AtLeast),
69 self.tcx.sess.span_fatal(
71 &format!("unknown cgu-reuse-kind `{}` specified", other),
79 if !self.tcx.sess.opts.unstable_opts.query_dep_graph {
80 self.tcx.sess.span_fatal(
82 "found CGU-reuse attribute but `-Zquery-dep-graph` was not specified",
86 if !self.check_config(attr) {
87 debug!("check_attr: config does not match, ignoring attr");
91 let user_path = self.field(attr, sym::module).to_string();
92 let crate_name = self.tcx.crate_name(LOCAL_CRATE).to_string();
94 if !user_path.starts_with(&crate_name) {
96 "Found malformed codegen unit name `{}`. \
97 Codegen units names must always start with the name of the \
98 crate (`{}` in this case).",
101 self.tcx.sess.span_fatal(attr.span, &msg);
104 // Split of the "special suffix" if there is one.
105 let (user_path, cgu_special_suffix) = if let Some(index) = user_path.rfind('.') {
106 (&user_path[..index], Some(&user_path[index + 1..]))
108 (&user_path[..], None)
111 let mut iter = user_path.split('-');
113 // Remove the crate name
114 assert_eq!(iter.next().unwrap(), crate_name);
116 let cgu_path_components = iter.collect::<Vec<_>>();
118 let cgu_name_builder = &mut CodegenUnitNameBuilder::new(self.tcx);
120 cgu_name_builder.build_cgu_name(LOCAL_CRATE, cgu_path_components, cgu_special_suffix);
122 debug!("mapping '{}' to cgu name '{}'", self.field(attr, sym::module), cgu_name);
124 if !self.available_cgus.contains(&cgu_name) {
125 let mut cgu_names: Vec<&str> =
126 self.available_cgus.iter().map(|cgu| cgu.as_str()).collect();
128 self.tcx.sess.span_err(
131 "no module named `{}` (mangled: {}). Available modules: {}",
139 self.tcx.sess.cgu_reuse_tracker.set_expectation(
148 fn field(&self, attr: &ast::Attribute, name: Symbol) -> Symbol {
149 for item in attr.meta_item_list().unwrap_or_else(Vec::new) {
150 if item.has_name(name) {
151 if let Some(value) = item.value_str() {
154 self.tcx.sess.span_fatal(
156 &format!("associated value expected for `{}`", name),
162 self.tcx.sess.span_fatal(attr.span, &format!("no field `{}`", name));
165 /// Scan for a `cfg="foo"` attribute and check whether we have a
166 /// cfg flag called `foo`.
167 fn check_config(&self, attr: &ast::Attribute) -> bool {
168 let config = &self.tcx.sess.parse_sess.config;
169 let value = self.field(attr, sym::cfg);
170 debug!("check_config(config={:?}, value={:?})", config, value);
171 if config.iter().any(|&(name, _)| name == value) {
172 debug!("check_config: matched");
175 debug!("check_config: no match found");