1 //! Debugging code to test fingerprints computed for query results. For each node marked with
2 //! `#[rustc_clean]` we will compare the fingerprint from the current and from the previous
3 //! compilation session as appropriate:
5 //! - `#[rustc_clean(cfg="rev2", except="typeck")]` if we are
6 //! in `#[cfg(rev2)]`, then the fingerprints associated with
7 //! `DepNode::typeck(X)` must be DIFFERENT (`X` is the `DefId` of the
9 //! - `#[rustc_clean(cfg="rev2")]` same as above, except that the
10 //! fingerprints must be the SAME (along with all other fingerprints).
12 //! - `#[rustc_clean(cfg="rev2", loaded_from_disk='typeck")]` asserts that
13 //! the query result for `DepNode::typeck(X)` was actually
14 //! loaded from disk (not just marked green). This can be useful
15 //! to ensure that a test is actually exercising the deserialization
16 //! logic for a particular query result. This can be combined with
19 //! Errors are reported if we are in the suitable configuration but
20 //! the required condition is not met.
22 use rustc_ast::{self as ast, Attribute, NestedMetaItem};
23 use rustc_data_structures::fx::FxHashSet;
24 use rustc_hir::def_id::LocalDefId;
25 use rustc_hir::intravisit;
26 use rustc_hir::Node as HirNode;
27 use rustc_hir::{ImplItemKind, ItemKind as HirItem, TraitItemKind};
28 use rustc_middle::dep_graph::{label_strs, DepNode, DepNodeExt};
29 use rustc_middle::hir::nested_filter;
30 use rustc_middle::ty::TyCtxt;
31 use rustc_span::symbol::{sym, Symbol};
33 use std::iter::FromIterator;
36 const LOADED_FROM_DISK: Symbol = sym::loaded_from_disk;
37 const EXCEPT: Symbol = sym::except;
38 const CFG: Symbol = sym::cfg;
40 // Base and Extra labels to build up the labels
42 /// For typedef, constants, and statics
43 const BASE_CONST: &[&str] = &[label_strs::type_of];
45 /// DepNodes for functions + methods
46 const BASE_FN: &[&str] = &[
47 // Callers will depend on the signature of these items, so we better test
49 label_strs::generics_of,
50 label_strs::predicates_of,
52 // And a big part of compilation (that we eventually want to cache) is type inference
57 /// DepNodes for Hir, which is pretty much everything
58 const BASE_HIR: &[&str] = &[
59 // hir_owner and hir_owner_nodes should be computed for all nodes
60 label_strs::hir_owner,
61 label_strs::hir_owner_nodes,
64 /// `impl` implementation of struct/trait
65 const BASE_IMPL: &[&str] =
66 &[label_strs::associated_item_def_ids, label_strs::generics_of, label_strs::impl_trait_ref];
68 /// DepNodes for mir_built/Optimized, which is relevant in "executable"
69 /// code, i.e., functions+methods
70 const BASE_MIR: &[&str] = &[label_strs::optimized_mir, label_strs::promoted_mir];
72 /// Struct, Enum and Union DepNodes
74 /// Note that changing the type of a field does not change the type of the struct or enum, but
75 /// adding/removing fields or changing a fields name or visibility does.
76 const BASE_STRUCT: &[&str] =
77 &[label_strs::generics_of, label_strs::predicates_of, label_strs::type_of];
79 /// Trait definition `DepNode`s.
80 /// Extra `DepNode`s for functions and methods.
81 const EXTRA_ASSOCIATED: &[&str] = &[label_strs::associated_item];
83 const EXTRA_TRAIT: &[&str] = &[];
87 const LABELS_CONST: &[&[&str]] = &[BASE_HIR, BASE_CONST];
89 /// Constant/Typedef in an impl
90 const LABELS_CONST_IN_IMPL: &[&[&str]] = &[BASE_HIR, BASE_CONST, EXTRA_ASSOCIATED];
92 /// Trait-Const/Typedef DepNodes
93 const LABELS_CONST_IN_TRAIT: &[&[&str]] = &[BASE_HIR, BASE_CONST, EXTRA_ASSOCIATED, EXTRA_TRAIT];
95 /// Function `DepNode`s.
96 const LABELS_FN: &[&[&str]] = &[BASE_HIR, BASE_MIR, BASE_FN];
98 /// Method `DepNode`s.
99 const LABELS_FN_IN_IMPL: &[&[&str]] = &[BASE_HIR, BASE_MIR, BASE_FN, EXTRA_ASSOCIATED];
101 /// Trait method `DepNode`s.
102 const LABELS_FN_IN_TRAIT: &[&[&str]] =
103 &[BASE_HIR, BASE_MIR, BASE_FN, EXTRA_ASSOCIATED, EXTRA_TRAIT];
105 /// For generic cases like inline-assembly, modules, etc.
106 const LABELS_HIR_ONLY: &[&[&str]] = &[BASE_HIR];
109 const LABELS_TRAIT: &[&[&str]] = &[
111 &[label_strs::associated_item_def_ids, label_strs::predicates_of, label_strs::generics_of],
115 const LABELS_IMPL: &[&[&str]] = &[BASE_HIR, BASE_IMPL];
117 /// Abstract data type (struct, enum, union) `DepNode`s.
118 const LABELS_ADT: &[&[&str]] = &[BASE_HIR, BASE_STRUCT];
120 // FIXME: Struct/Enum/Unions Fields (there is currently no way to attach these)
122 // Fields are kind of separate from their containers, as they can change independently from
123 // them. We should at least check
125 // type_of for these.
127 type Labels = FxHashSet<String>;
129 /// Represents the requested configuration by rustc_clean/dirty
133 loaded_from_disk: Labels,
136 pub fn check_dirty_clean_annotations(tcx: TyCtxt<'_>) {
137 if !tcx.sess.opts.unstable_opts.query_dep_graph {
141 // can't add `#[rustc_clean]` etc without opting in to this feature
142 if !tcx.features().rustc_attrs {
146 tcx.dep_graph.with_ignore(|| {
147 let mut dirty_clean_visitor = DirtyCleanVisitor { tcx, checked_attrs: Default::default() };
149 let crate_items = tcx.hir_crate_items(());
151 for id in crate_items.items() {
152 dirty_clean_visitor.check_item(id.def_id);
155 for id in crate_items.trait_items() {
156 dirty_clean_visitor.check_item(id.def_id);
159 for id in crate_items.impl_items() {
160 dirty_clean_visitor.check_item(id.def_id);
163 for id in crate_items.foreign_items() {
164 dirty_clean_visitor.check_item(id.def_id);
167 let mut all_attrs = FindAllAttrs { tcx, found_attrs: vec![] };
168 tcx.hir().walk_attributes(&mut all_attrs);
170 // Note that we cannot use the existing "unused attribute"-infrastructure
171 // here, since that is running before codegen. This is also the reason why
172 // all codegen-specific attributes are `AssumedUsed` in rustc_ast::feature_gate.
173 all_attrs.report_unchecked_attrs(dirty_clean_visitor.checked_attrs);
177 pub struct DirtyCleanVisitor<'tcx> {
179 checked_attrs: FxHashSet<ast::AttrId>,
182 impl<'tcx> DirtyCleanVisitor<'tcx> {
183 /// Possibly "deserialize" the attribute into a clean/dirty assertion
184 fn assertion_maybe(&mut self, item_id: LocalDefId, attr: &Attribute) -> Option<Assertion> {
185 assert!(attr.has_name(sym::rustc_clean));
186 if !check_config(self.tcx, attr) {
187 // skip: not the correct `cfg=`
190 let assertion = self.assertion_auto(item_id, attr);
194 /// Gets the "auto" assertion on pre-validated attr, along with the `except` labels.
195 fn assertion_auto(&mut self, item_id: LocalDefId, attr: &Attribute) -> Assertion {
196 let (name, mut auto) = self.auto_labels(item_id, attr);
197 let except = self.except(attr);
198 let loaded_from_disk = self.loaded_from_disk(attr);
199 for e in except.iter() {
202 "`except` specified DepNodes that can not be affected for \"{}\": \"{}\"",
205 self.tcx.sess.span_fatal(attr.span, &msg);
208 Assertion { clean: auto, dirty: except, loaded_from_disk }
211 /// `loaded_from_disk=` attribute value
212 fn loaded_from_disk(&self, attr: &Attribute) -> Labels {
213 for item in attr.meta_item_list().unwrap_or_else(Vec::new) {
214 if item.has_name(LOADED_FROM_DISK) {
215 let value = expect_associated_value(self.tcx, &item);
216 return self.resolve_labels(&item, value);
219 // If `loaded_from_disk=` is not specified, don't assert anything
223 /// `except=` attribute value
224 fn except(&self, attr: &Attribute) -> Labels {
225 for item in attr.meta_item_list().unwrap_or_else(Vec::new) {
226 if item.has_name(EXCEPT) {
227 let value = expect_associated_value(self.tcx, &item);
228 return self.resolve_labels(&item, value);
231 // if no `label` or `except` is given, only the node's group are asserted
235 /// Return all DepNode labels that should be asserted for this item.
236 /// index=0 is the "name" used for error messages
237 fn auto_labels(&mut self, item_id: LocalDefId, attr: &Attribute) -> (&'static str, Labels) {
238 let node = self.tcx.hir().get_by_def_id(item_id);
239 let (name, labels) = match node {
240 HirNode::Item(item) => {
242 // note: these are in the same order as hir::Item_;
243 // FIXME(michaelwoerister): do commented out ones
245 // // An `extern crate` item, with optional original crate name,
246 // HirItem::ExternCrate(..), // intentionally no assertions
248 // // `use foo::bar::*;` or `use foo::bar::baz as quux;`
249 // HirItem::Use(..), // intentionally no assertions
252 HirItem::Static(..) => ("ItemStatic", LABELS_CONST),
255 HirItem::Const(..) => ("ItemConst", LABELS_CONST),
257 // A function declaration
258 HirItem::Fn(..) => ("ItemFn", LABELS_FN),
261 HirItem::Mod(..) => ("ItemMod", LABELS_HIR_ONLY),
263 // // An external module
264 HirItem::ForeignMod { .. } => ("ItemForeignMod", LABELS_HIR_ONLY),
266 // Module-level inline assembly (from global_asm!)
267 HirItem::GlobalAsm(..) => ("ItemGlobalAsm", LABELS_HIR_ONLY),
269 // A type alias, e.g., `type Foo = Bar<u8>`
270 HirItem::TyAlias(..) => ("ItemTy", LABELS_HIR_ONLY),
272 // An enum definition, e.g., `enum Foo<A, B> {C<A>, D<B>}`
273 HirItem::Enum(..) => ("ItemEnum", LABELS_ADT),
275 // A struct definition, e.g., `struct Foo<A> {x: A}`
276 HirItem::Struct(..) => ("ItemStruct", LABELS_ADT),
278 // A union definition, e.g., `union Foo<A, B> {x: A, y: B}`
279 HirItem::Union(..) => ("ItemUnion", LABELS_ADT),
281 // Represents a Trait Declaration
282 HirItem::Trait(..) => ("ItemTrait", LABELS_TRAIT),
284 // An implementation, eg `impl<A> Trait for Foo { .. }`
285 HirItem::Impl { .. } => ("ItemKind::Impl", LABELS_IMPL),
287 _ => self.tcx.sess.span_fatal(
290 "clean/dirty auto-assertions not yet defined \
291 for Node::Item.node={:?}",
297 HirNode::TraitItem(item) => match item.kind {
298 TraitItemKind::Fn(..) => ("Node::TraitItem", LABELS_FN_IN_TRAIT),
299 TraitItemKind::Const(..) => ("NodeTraitConst", LABELS_CONST_IN_TRAIT),
300 TraitItemKind::Type(..) => ("NodeTraitType", LABELS_CONST_IN_TRAIT),
302 HirNode::ImplItem(item) => match item.kind {
303 ImplItemKind::Fn(..) => ("Node::ImplItem", LABELS_FN_IN_IMPL),
304 ImplItemKind::Const(..) => ("NodeImplConst", LABELS_CONST_IN_IMPL),
305 ImplItemKind::TyAlias(..) => ("NodeImplType", LABELS_CONST_IN_IMPL),
307 _ => self.tcx.sess.span_fatal(
309 &format!("clean/dirty auto-assertions not yet defined for {:?}", node),
313 Labels::from_iter(labels.iter().flat_map(|s| s.iter().map(|l| (*l).to_string())));
317 fn resolve_labels(&self, item: &NestedMetaItem, value: Symbol) -> Labels {
318 let mut out = Labels::default();
319 for label in value.as_str().split(',') {
320 let label = label.trim();
321 if DepNode::has_label_string(label) {
322 if out.contains(label) {
323 self.tcx.sess.span_fatal(
325 &format!("dep-node label `{}` is repeated", label),
328 out.insert(label.to_string());
332 .span_fatal(item.span(), &format!("dep-node label `{}` not recognized", label));
338 fn dep_node_str(&self, dep_node: &DepNode) -> String {
339 if let Some(def_id) = dep_node.extract_def_id(self.tcx) {
340 format!("{:?}({})", dep_node.kind, self.tcx.def_path_str(def_id))
342 format!("{:?}({:?})", dep_node.kind, dep_node.hash)
346 fn assert_dirty(&self, item_span: Span, dep_node: DepNode) {
347 debug!("assert_dirty({:?})", dep_node);
349 if self.tcx.dep_graph.is_green(&dep_node) {
350 let dep_node_str = self.dep_node_str(&dep_node);
353 .span_err(item_span, &format!("`{}` should be dirty but is not", dep_node_str));
357 fn assert_clean(&self, item_span: Span, dep_node: DepNode) {
358 debug!("assert_clean({:?})", dep_node);
360 if self.tcx.dep_graph.is_red(&dep_node) {
361 let dep_node_str = self.dep_node_str(&dep_node);
364 .span_err(item_span, &format!("`{}` should be clean but is not", dep_node_str));
368 fn assert_loaded_from_disk(&self, item_span: Span, dep_node: DepNode) {
369 debug!("assert_loaded_from_disk({:?})", dep_node);
371 if !self.tcx.dep_graph.debug_was_loaded_from_disk(dep_node) {
372 let dep_node_str = self.dep_node_str(&dep_node);
373 self.tcx.sess.span_err(
375 &format!("`{}` should have been loaded from disk but it was not", dep_node_str),
380 fn check_item(&mut self, item_id: LocalDefId) {
381 let item_span = self.tcx.def_span(item_id.to_def_id());
382 let def_path_hash = self.tcx.def_path_hash(item_id.to_def_id());
383 for attr in self.tcx.get_attrs(item_id.to_def_id(), sym::rustc_clean) {
384 let Some(assertion) = self.assertion_maybe(item_id, attr) else {
387 self.checked_attrs.insert(attr.id);
388 for label in assertion.clean {
389 let dep_node = DepNode::from_label_string(self.tcx, &label, def_path_hash).unwrap();
390 self.assert_clean(item_span, dep_node);
392 for label in assertion.dirty {
393 let dep_node = DepNode::from_label_string(self.tcx, &label, def_path_hash).unwrap();
394 self.assert_dirty(item_span, dep_node);
396 for label in assertion.loaded_from_disk {
397 let dep_node = DepNode::from_label_string(self.tcx, &label, def_path_hash).unwrap();
398 self.assert_loaded_from_disk(item_span, dep_node);
404 /// Given a `#[rustc_clean]` attribute, scan for a `cfg="foo"` attribute and check whether we have
405 /// a cfg flag called `foo`.
406 fn check_config(tcx: TyCtxt<'_>, attr: &Attribute) -> bool {
407 debug!("check_config(attr={:?})", attr);
408 let config = &tcx.sess.parse_sess.config;
409 debug!("check_config: config={:?}", config);
411 for item in attr.meta_item_list().unwrap_or_else(Vec::new) {
412 if item.has_name(CFG) {
413 let value = expect_associated_value(tcx, &item);
414 debug!("check_config: searching for cfg {:?}", value);
415 cfg = Some(config.contains(&(value, None)));
416 } else if !(item.has_name(EXCEPT) || item.has_name(LOADED_FROM_DISK)) {
417 tcx.sess.span_err(attr.span, &format!("unknown item `{}`", item.name_or_empty()));
422 None => tcx.sess.span_fatal(attr.span, "no cfg attribute"),
427 fn expect_associated_value(tcx: TyCtxt<'_>, item: &NestedMetaItem) -> Symbol {
428 if let Some(value) = item.value_str() {
431 let msg = if let Some(ident) = item.ident() {
432 format!("associated value expected for `{}`", ident)
434 "expected an associated value".to_string()
437 tcx.sess.span_fatal(item.span(), &msg);
441 // A visitor that collects all #[rustc_clean] attributes from
442 // the HIR. It is used to verify that we really ran checks for all annotated
444 pub struct FindAllAttrs<'tcx> {
446 found_attrs: Vec<&'tcx Attribute>,
449 impl<'tcx> FindAllAttrs<'tcx> {
450 fn is_active_attr(&mut self, attr: &Attribute) -> bool {
451 if attr.has_name(sym::rustc_clean) && check_config(self.tcx, attr) {
458 fn report_unchecked_attrs(&self, mut checked_attrs: FxHashSet<ast::AttrId>) {
459 for attr in &self.found_attrs {
460 if !checked_attrs.contains(&attr.id) {
461 self.tcx.sess.span_err(attr.span, "found unchecked `#[rustc_clean]` attribute");
462 checked_attrs.insert(attr.id);
468 impl<'tcx> intravisit::Visitor<'tcx> for FindAllAttrs<'tcx> {
469 type NestedFilter = nested_filter::All;
471 fn nested_visit_map(&mut self) -> Self::Map {
475 fn visit_attribute(&mut self, attr: &'tcx Attribute) {
476 if self.is_active_attr(attr) {
477 self.found_attrs.push(attr);