]> git.lizzy.rs Git - rust.git/blob - src/librustc_incremental/assert_module_sources.rs
Auto merge of #68057 - Aaron1011:fix/marker-trait-selection, r=matthewjasper
[rust.git] / src / librustc_incremental / assert_module_sources.rs
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.
4 //!
5 //! The user adds annotations to the crate of the following form:
6 //!
7 //! ```
8 //! #![rustc_partition_reused(module="spike", cfg="rpass2")]
9 //! #![rustc_partition_codegened(module="spike-x", cfg="rpass2")]
10 //! ```
11 //!
12 //! The first indicates (in the cfg `rpass2`) that `spike.o` will be
13 //! reused, the second that `spike-x.o` will be recreated. If these
14 //! annotations are inaccurate, errors are reported.
15 //!
16 //! The reason that we use `cfg=...` and not `#[cfg_attr]` is so that
17 //! the HIR doesn't change as a result of the annotations, which might
18 //! perturb the reuse results.
19 //!
20 //! `#![rustc_expected_cgu_reuse(module="spike", cfg="rpass2", kind="post-lto")]
21 //! allows for doing a more fine-grained check to see if pre- or post-lto data
22 //! was re-used.
23
24 use rustc::mir::mono::CodegenUnitNameBuilder;
25 use rustc::ty::TyCtxt;
26 use rustc_hir::def_id::LOCAL_CRATE;
27 use rustc_session::cgu_reuse_tracker::*;
28 use rustc_span::symbol::{sym, Symbol};
29 use std::collections::BTreeSet;
30 use syntax::ast;
31
32 pub fn assert_module_sources(tcx: TyCtxt<'_>) {
33     tcx.dep_graph.with_ignore(|| {
34         if tcx.sess.opts.incremental.is_none() {
35             return;
36         }
37
38         let available_cgus = tcx
39             .collect_and_partition_mono_items(LOCAL_CRATE)
40             .1
41             .iter()
42             .map(|cgu| cgu.name())
43             .collect::<BTreeSet<Symbol>>();
44
45         let ams = AssertModuleSource { tcx, available_cgus };
46
47         for attr in tcx.hir().krate().attrs {
48             ams.check_attr(attr);
49         }
50     })
51 }
52
53 struct AssertModuleSource<'tcx> {
54     tcx: TyCtxt<'tcx>,
55     available_cgus: BTreeSet<Symbol>,
56 }
57
58 impl AssertModuleSource<'tcx> {
59     fn check_attr(&self, attr: &ast::Attribute) {
60         let (expected_reuse, comp_kind) = if attr.check_name(sym::rustc_partition_reused) {
61             (CguReuse::PreLto, ComparisonKind::AtLeast)
62         } else if attr.check_name(sym::rustc_partition_codegened) {
63             (CguReuse::No, ComparisonKind::Exact)
64         } else if attr.check_name(sym::rustc_expected_cgu_reuse) {
65             match &*self.field(attr, sym::kind).as_str() {
66                 "no" => (CguReuse::No, ComparisonKind::Exact),
67                 "pre-lto" => (CguReuse::PreLto, ComparisonKind::Exact),
68                 "post-lto" => (CguReuse::PostLto, ComparisonKind::Exact),
69                 "any" => (CguReuse::PreLto, ComparisonKind::AtLeast),
70                 other => {
71                     self.tcx.sess.span_fatal(
72                         attr.span,
73                         &format!("unknown cgu-reuse-kind `{}` specified", other),
74                     );
75                 }
76             }
77         } else {
78             return;
79         };
80
81         if !self.tcx.sess.opts.debugging_opts.query_dep_graph {
82             self.tcx.sess.span_fatal(
83                 attr.span,
84                 &format!(
85                     "found CGU-reuse attribute but `-Zquery-dep-graph` \
86                           was not specified"
87                 ),
88             );
89         }
90
91         if !self.check_config(attr) {
92             debug!("check_attr: config does not match, ignoring attr");
93             return;
94         }
95
96         let user_path = self.field(attr, sym::module).to_string();
97         let crate_name = self.tcx.crate_name(LOCAL_CRATE).to_string();
98
99         if !user_path.starts_with(&crate_name) {
100             let msg = format!(
101                 "Found malformed codegen unit name `{}`. \
102                 Codegen units names must always start with the name of the \
103                 crate (`{}` in this case).",
104                 user_path, crate_name
105             );
106             self.tcx.sess.span_fatal(attr.span, &msg);
107         }
108
109         // Split of the "special suffix" if there is one.
110         let (user_path, cgu_special_suffix) = if let Some(index) = user_path.rfind(".") {
111             (&user_path[..index], Some(&user_path[index + 1..]))
112         } else {
113             (&user_path[..], None)
114         };
115
116         let mut cgu_path_components = user_path.split('-').collect::<Vec<_>>();
117
118         // Remove the crate name
119         assert_eq!(cgu_path_components.remove(0), crate_name);
120
121         let cgu_name_builder = &mut CodegenUnitNameBuilder::new(self.tcx);
122         let cgu_name =
123             cgu_name_builder.build_cgu_name(LOCAL_CRATE, cgu_path_components, cgu_special_suffix);
124
125         debug!("mapping '{}' to cgu name '{}'", self.field(attr, sym::module), cgu_name);
126
127         if !self.available_cgus.contains(&cgu_name) {
128             self.tcx.sess.span_err(
129                 attr.span,
130                 &format!(
131                     "no module named `{}` (mangled: {}). \
132                           Available modules: {}",
133                     user_path,
134                     cgu_name,
135                     self.available_cgus
136                         .iter()
137                         .map(|cgu| cgu.to_string())
138                         .collect::<Vec<_>>()
139                         .join(", ")
140                 ),
141             );
142         }
143
144         self.tcx.sess.cgu_reuse_tracker.set_expectation(
145             &cgu_name.as_str(),
146             &user_path,
147             attr.span,
148             expected_reuse,
149             comp_kind,
150         );
151     }
152
153     fn field(&self, attr: &ast::Attribute, name: Symbol) -> ast::Name {
154         for item in attr.meta_item_list().unwrap_or_else(Vec::new) {
155             if item.check_name(name) {
156                 if let Some(value) = item.value_str() {
157                     return value;
158                 } else {
159                     self.tcx.sess.span_fatal(
160                         item.span(),
161                         &format!("associated value expected for `{}`", name),
162                     );
163                 }
164             }
165         }
166
167         self.tcx.sess.span_fatal(attr.span, &format!("no field `{}`", name));
168     }
169
170     /// Scan for a `cfg="foo"` attribute and check whether we have a
171     /// cfg flag called `foo`.
172     fn check_config(&self, attr: &ast::Attribute) -> bool {
173         let config = &self.tcx.sess.parse_sess.config;
174         let value = self.field(attr, sym::cfg);
175         debug!("check_config(config={:?}, value={:?})", config, value);
176         if config.iter().any(|&(name, _)| name == value) {
177             debug!("check_config: matched");
178             return true;
179         }
180         debug!("check_config: no match found");
181         return false;
182     }
183 }