Skip to main content

rustc_incremental/persist/
clean.rs

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:
4//!
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
8//!   current node).
9//! - `#[rustc_clean(cfg="rev2")]` same as above, except that the
10//!   fingerprints must be the SAME (along with all other fingerprints).
11//!
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
17//!   `except`
18//!
19//! Errors are reported if we are in the suitable configuration but
20//! the required condition is not met.
21
22use rustc_data_structures::fx::FxHashSet;
23use rustc_data_structures::unord::UnordSet;
24use rustc_hir::attrs::{AttributeKind, RustcCleanAttribute};
25use rustc_hir::def_id::LocalDefId;
26use rustc_hir::{
27    Attribute, ImplItemKind, ItemKind as HirItem, Node as HirNode, TraitItemKind, find_attr,
28    intravisit,
29};
30use rustc_middle::dep_graph::{DepNode, dep_kind_from_label, label_strs};
31use rustc_middle::hir::nested_filter;
32use rustc_middle::ty::TyCtxt;
33use rustc_span::{Span, Symbol};
34use tracing::debug;
35
36use crate::errors;
37
38// Base and Extra labels to build up the labels
39
40/// For typedef, constants, and statics
41const BASE_CONST: &[&str] = &[label_strs::type_of];
42
43/// DepNodes for functions + methods
44const BASE_FN: &[&str] = &[
45    // Callers will depend on the signature of these items, so we better test
46    label_strs::fn_sig,
47    label_strs::generics_of,
48    label_strs::predicates_of,
49    label_strs::type_of,
50    // And a big part of compilation (that we eventually want to cache) is type inference
51    // information:
52    label_strs::typeck_root,
53];
54
55/// DepNodes for Hir, which is pretty much everything
56const BASE_HIR: &[&str] = &[
57    // opt_hir_owner_nodes should be computed for all nodes
58    label_strs::opt_hir_owner_nodes,
59];
60
61/// `impl` implementation of struct/trait
62const BASE_IMPL: &[&str] =
63    &[label_strs::associated_item_def_ids, label_strs::generics_of, label_strs::impl_trait_header];
64
65/// DepNodes for exported mir bodies, which is relevant in "executable"
66/// code, i.e., functions+methods
67const BASE_MIR: &[&str] = &[label_strs::optimized_mir, label_strs::promoted_mir];
68
69/// Struct, Enum and Union DepNodes
70///
71/// Note that changing the type of a field does not change the type of the struct or enum, but
72/// adding/removing fields or changing a fields name or visibility does.
73const BASE_STRUCT: &[&str] =
74    &[label_strs::generics_of, label_strs::predicates_of, label_strs::type_of];
75
76/// Trait definition `DepNode`s.
77/// Extra `DepNode`s for functions and methods.
78const EXTRA_ASSOCIATED: &[&str] = &[label_strs::associated_item];
79
80const EXTRA_TRAIT: &[&str] = &[];
81
82// Fully Built Labels
83
84const LABELS_CONST: &[&[&str]] = &[BASE_HIR, BASE_CONST];
85
86/// Constant/Typedef in an impl
87const LABELS_CONST_IN_IMPL: &[&[&str]] = &[BASE_HIR, BASE_CONST, EXTRA_ASSOCIATED];
88
89/// Trait-Const/Typedef DepNodes
90const LABELS_CONST_IN_TRAIT: &[&[&str]] = &[BASE_HIR, BASE_CONST, EXTRA_ASSOCIATED, EXTRA_TRAIT];
91
92/// Function `DepNode`s.
93const LABELS_FN: &[&[&str]] = &[BASE_HIR, BASE_MIR, BASE_FN];
94
95/// Method `DepNode`s.
96const LABELS_FN_IN_IMPL: &[&[&str]] = &[BASE_HIR, BASE_MIR, BASE_FN, EXTRA_ASSOCIATED];
97
98/// Trait method `DepNode`s.
99const LABELS_FN_IN_TRAIT: &[&[&str]] =
100    &[BASE_HIR, BASE_MIR, BASE_FN, EXTRA_ASSOCIATED, EXTRA_TRAIT];
101
102/// For generic cases like inline-assembly, modules, etc.
103const LABELS_HIR_ONLY: &[&[&str]] = &[BASE_HIR];
104
105/// Impl `DepNode`s.
106const LABELS_TRAIT: &[&[&str]] = &[
107    BASE_HIR,
108    &[label_strs::associated_item_def_ids, label_strs::predicates_of, label_strs::generics_of],
109];
110
111/// Impl `DepNode`s.
112const LABELS_IMPL: &[&[&str]] = &[BASE_HIR, BASE_IMPL];
113
114/// Abstract data type (struct, enum, union) `DepNode`s.
115const LABELS_ADT: &[&[&str]] = &[BASE_HIR, BASE_STRUCT];
116
117// FIXME: Struct/Enum/Unions Fields (there is currently no way to attach these)
118//
119// Fields are kind of separate from their containers, as they can change independently from
120// them. We should at least check
121//
122//     type_of for these.
123
124type Labels = UnordSet<String>;
125
126/// Represents the requested configuration by rustc_clean
127struct Assertion {
128    clean: Labels,
129    dirty: Labels,
130    loaded_from_disk: Labels,
131}
132
133pub(crate) fn check_clean_annotations(tcx: TyCtxt<'_>) {
134    if !tcx.sess.opts.unstable_opts.query_dep_graph {
135        return;
136    }
137
138    // can't add `#[rustc_clean]` etc without opting into this feature
139    if !tcx.features().rustc_attrs() {
140        return;
141    }
142
143    tcx.dep_graph.with_ignore(|| {
144        let mut clean_visitor = CleanVisitor { tcx, checked_attrs: Default::default() };
145
146        let crate_items = tcx.hir_crate_items(());
147
148        for id in crate_items.free_items() {
149            clean_visitor.check_item(id.owner_id.def_id);
150        }
151
152        for id in crate_items.trait_items() {
153            clean_visitor.check_item(id.owner_id.def_id);
154        }
155
156        for id in crate_items.impl_items() {
157            clean_visitor.check_item(id.owner_id.def_id);
158        }
159
160        for id in crate_items.foreign_items() {
161            clean_visitor.check_item(id.owner_id.def_id);
162        }
163
164        let mut all_attrs = FindAllAttrs { tcx, found_attrs: ::alloc::vec::Vec::new()vec![] };
165        tcx.hir_walk_attributes(&mut all_attrs);
166
167        // Note that we cannot use the existing "unused attribute"-infrastructure
168        // here, since that is running before codegen. This is also the reason why
169        // all codegen-specific attributes are `AssumedUsed` in rustc_ast::feature_gate.
170        all_attrs.report_unchecked_attrs(clean_visitor.checked_attrs);
171    })
172}
173
174struct CleanVisitor<'tcx> {
175    tcx: TyCtxt<'tcx>,
176    checked_attrs: FxHashSet<Span>,
177}
178
179impl<'tcx> CleanVisitor<'tcx> {
180    /// Convert the attribute to an [`Assertion`] if the relevant cfg is active
181    fn assertion_maybe(
182        &mut self,
183        item_id: LocalDefId,
184        attr: &RustcCleanAttribute,
185    ) -> Option<Assertion> {
186        self.tcx.sess.config.contains(&(attr.cfg, None)).then(|| self.assertion_auto(item_id, attr))
187    }
188
189    /// Gets the "auto" assertion on pre-validated attr, along with the `except` labels.
190    fn assertion_auto(&mut self, item_id: LocalDefId, attr: &RustcCleanAttribute) -> Assertion {
191        let (name, mut auto) = self.auto_labels(item_id, attr.span);
192        let except = self.except(attr);
193        let loaded_from_disk = self.loaded_from_disk(attr);
194        for e in except.items().into_sorted_stable_ord() {
195            if !auto.remove(e) {
196                self.tcx.dcx().emit_fatal(errors::AssertionAuto { span: attr.span, name, e });
197            }
198        }
199        Assertion { clean: auto, dirty: except, loaded_from_disk }
200    }
201
202    /// `loaded_from_disk=` attribute value
203    fn loaded_from_disk(&self, attr: &RustcCleanAttribute) -> Labels {
204        attr.loaded_from_disk
205            .as_ref()
206            .map(|queries| self.resolve_labels(&queries.entries, queries.span))
207            .unwrap_or_default()
208    }
209
210    /// `except=` attribute value
211    fn except(&self, attr: &RustcCleanAttribute) -> Labels {
212        attr.except
213            .as_ref()
214            .map(|queries| self.resolve_labels(&queries.entries, queries.span))
215            .unwrap_or_default()
216    }
217
218    /// Return all DepNode labels that should be asserted for this item.
219    /// index=0 is the "name" used for error messages
220    fn auto_labels(&mut self, item_id: LocalDefId, span: Span) -> (&'static str, Labels) {
221        let node = self.tcx.hir_node_by_def_id(item_id);
222        let (name, labels) = match node {
223            HirNode::Item(item) => {
224                match item.kind {
225                    // note: these are in the same order as hir::Item_;
226                    // FIXME(michaelwoerister): do commented out ones
227
228                    // // An `extern crate` item, with optional original crate name,
229                    // HirItem::ExternCrate(..),  // intentionally no assertions
230
231                    // // `use foo::bar::*;` or `use foo::bar::baz as quux;`
232                    // HirItem::Use(..),  // intentionally no assertions
233
234                    // A `static` item
235                    HirItem::Static(..) => ("ItemStatic", LABELS_CONST),
236
237                    // A `const` item
238                    HirItem::Const(..) => ("ItemConst", LABELS_CONST),
239
240                    // A function declaration
241                    HirItem::Fn { .. } => ("ItemFn", LABELS_FN),
242
243                    // // A module
244                    HirItem::Mod(..) => ("ItemMod", LABELS_HIR_ONLY),
245
246                    // // An external module
247                    HirItem::ForeignMod { .. } => ("ItemForeignMod", LABELS_HIR_ONLY),
248
249                    // Module-level inline assembly (from global_asm!)
250                    HirItem::GlobalAsm { .. } => ("ItemGlobalAsm", LABELS_HIR_ONLY),
251
252                    // A type alias, e.g., `type Foo = Bar<u8>`
253                    HirItem::TyAlias(..) => ("ItemTy", LABELS_HIR_ONLY),
254
255                    // An enum definition, e.g., `enum Foo<A, B> {C<A>, D<B>}`
256                    HirItem::Enum(..) => ("ItemEnum", LABELS_ADT),
257
258                    // A struct definition, e.g., `struct Foo<A> {x: A}`
259                    HirItem::Struct(..) => ("ItemStruct", LABELS_ADT),
260
261                    // A union definition, e.g., `union Foo<A, B> {x: A, y: B}`
262                    HirItem::Union(..) => ("ItemUnion", LABELS_ADT),
263
264                    // Represents a Trait Declaration
265                    HirItem::Trait { .. } => ("ItemTrait", LABELS_TRAIT),
266
267                    // An implementation, eg `impl<A> Trait for Foo { .. }`
268                    HirItem::Impl { .. } => ("ItemKind::Impl", LABELS_IMPL),
269
270                    _ => self.tcx.dcx().emit_fatal(errors::UndefinedCleanDirtyItem {
271                        span,
272                        kind: ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0:?}", item.kind))
    })format!("{:?}", item.kind),
273                    }),
274                }
275            }
276            HirNode::TraitItem(item) => match item.kind {
277                TraitItemKind::Fn(..) => ("Node::TraitItem", LABELS_FN_IN_TRAIT),
278                TraitItemKind::Const(..) => ("NodeTraitConst", LABELS_CONST_IN_TRAIT),
279                TraitItemKind::Type(..) => ("NodeTraitType", LABELS_CONST_IN_TRAIT),
280            },
281            HirNode::ImplItem(item) => match item.kind {
282                ImplItemKind::Fn(..) => ("Node::ImplItem", LABELS_FN_IN_IMPL),
283                ImplItemKind::Const(..) => ("NodeImplConst", LABELS_CONST_IN_IMPL),
284                ImplItemKind::Type(..) => ("NodeImplType", LABELS_CONST_IN_IMPL),
285            },
286            _ => self
287                .tcx
288                .dcx()
289                .emit_fatal(errors::UndefinedCleanDirty { span, kind: ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0:?}", node))
    })format!("{node:?}") }),
290        };
291        let labels =
292            Labels::from_iter(labels.iter().flat_map(|s| s.iter().map(|l| (*l).to_string())));
293        (name, labels)
294    }
295
296    fn resolve_labels(&self, values: &[Symbol], span: Span) -> Labels {
297        let mut out = Labels::default();
298        for label in values {
299            let label_str = label.as_str();
300            if DepNode::has_label_string(label_str) {
301                if out.contains(label_str) {
302                    self.tcx
303                        .dcx()
304                        .emit_fatal(errors::RepeatedDepNodeLabel { span, label: label_str });
305                }
306                out.insert(label_str.to_string());
307            } else {
308                self.tcx
309                    .dcx()
310                    .emit_fatal(errors::UnrecognizedDepNodeLabel { span, label: label_str });
311            }
312        }
313        out
314    }
315
316    fn dep_node_str(&self, dep_node: &DepNode) -> String {
317        if let Some(def_id) = dep_node.extract_def_id(self.tcx) {
318            ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0:?}({1})", dep_node.kind,
                self.tcx.def_path_str(def_id)))
    })format!("{:?}({})", dep_node.kind, self.tcx.def_path_str(def_id))
319        } else {
320            ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0:?}({1:?})", dep_node.kind,
                dep_node.key_fingerprint))
    })format!("{:?}({:?})", dep_node.kind, dep_node.key_fingerprint)
321        }
322    }
323
324    fn assert_dirty(&self, item_span: Span, dep_node: DepNode) {
325        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_incremental/src/persist/clean.rs:325",
                        "rustc_incremental::persist::clean",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_incremental/src/persist/clean.rs"),
                        ::tracing_core::__macro_support::Option::Some(325u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_incremental::persist::clean"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("assert_dirty({0:?})",
                                                    dep_node) as &dyn Value))])
            });
    } else { ; }
};debug!("assert_dirty({:?})", dep_node);
326
327        if self.tcx.dep_graph.is_green(&dep_node) {
328            let dep_node_str = self.dep_node_str(&dep_node);
329            self.tcx
330                .dcx()
331                .emit_err(errors::NotDirty { span: item_span, dep_node_str: &dep_node_str });
332        }
333    }
334
335    fn assert_clean(&self, item_span: Span, dep_node: DepNode) {
336        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_incremental/src/persist/clean.rs:336",
                        "rustc_incremental::persist::clean",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_incremental/src/persist/clean.rs"),
                        ::tracing_core::__macro_support::Option::Some(336u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_incremental::persist::clean"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("assert_clean({0:?})",
                                                    dep_node) as &dyn Value))])
            });
    } else { ; }
};debug!("assert_clean({:?})", dep_node);
337
338        if self.tcx.dep_graph.is_red(&dep_node) {
339            let dep_node_str = self.dep_node_str(&dep_node);
340            self.tcx
341                .dcx()
342                .emit_err(errors::NotClean { span: item_span, dep_node_str: &dep_node_str });
343        }
344    }
345
346    fn check_item(&mut self, item_id: LocalDefId) {
347        let item_span = self.tcx.def_span(item_id.to_def_id());
348        let def_path_hash = self.tcx.def_path_hash(item_id.to_def_id());
349
350        let Some(clean_attrs) = {
    {
        'done:
            {
            for i in
                ::rustc_hir::attrs::HasAttrs::get_attrs(item_id, &self.tcx) {
                #[allow(unused_imports)]
                use rustc_hir::attrs::AttributeKind::*;
                let i: &rustc_hir::Attribute = i;
                match i {
                    rustc_hir::Attribute::Parsed(RustcClean(attr)) => {
                        break 'done Some(attr);
                    }
                    rustc_hir::Attribute::Unparsed(..) =>
                        {}
                        #[deny(unreachable_patterns)]
                        _ => {}
                }
            }
            None
        }
    }
}find_attr!(self.tcx, item_id, RustcClean(attr) => attr) else {
351            return;
352        };
353
354        for attr in clean_attrs {
355            let Some(assertion) = self.assertion_maybe(item_id, attr) else {
356                continue;
357            };
358            self.checked_attrs.insert(attr.span);
359            for label in assertion.clean.items().into_sorted_stable_ord() {
360                let dep_node = DepNode::from_label_string(self.tcx, label, def_path_hash).unwrap();
361                self.assert_clean(item_span, dep_node);
362            }
363            for label in assertion.dirty.items().into_sorted_stable_ord() {
364                let dep_node = DepNode::from_label_string(self.tcx, label, def_path_hash).unwrap();
365                self.assert_dirty(item_span, dep_node);
366            }
367            for label in assertion.loaded_from_disk.items().into_sorted_stable_ord() {
368                match DepNode::from_label_string(self.tcx, label, def_path_hash) {
369                    Ok(dep_node) => {
370                        if !self.tcx.dep_graph.debug_was_loaded_from_disk(dep_node) {
371                            let dep_node_str = self.dep_node_str(&dep_node);
372                            self.tcx.dcx().emit_err(errors::NotLoaded {
373                                span: item_span,
374                                dep_node_str: &dep_node_str,
375                            });
376                        }
377                    }
378                    // Opaque/unit hash, we only know the dep kind
379                    Err(()) => {
380                        let dep_kind = dep_kind_from_label(label);
381                        if !self.tcx.dep_graph.debug_dep_kind_was_loaded_from_disk(dep_kind) {
382                            self.tcx.dcx().emit_err(errors::NotLoaded {
383                                span: item_span,
384                                dep_node_str: &label,
385                            });
386                        }
387                    }
388                }
389            }
390        }
391    }
392}
393
394/// A visitor that collects all `#[rustc_clean]` attributes from
395/// the HIR. It is used to verify that we really ran checks for all annotated
396/// nodes.
397struct FindAllAttrs<'tcx> {
398    tcx: TyCtxt<'tcx>,
399    found_attrs: Vec<&'tcx RustcCleanAttribute>,
400}
401
402impl<'tcx> FindAllAttrs<'tcx> {
403    fn is_active_attr(&self, attr: &RustcCleanAttribute) -> bool {
404        self.tcx.sess.config.contains(&(attr.cfg, None))
405    }
406
407    fn report_unchecked_attrs(&self, mut checked_attrs: FxHashSet<Span>) {
408        for attr in &self.found_attrs {
409            if !checked_attrs.contains(&attr.span) {
410                self.tcx.dcx().emit_err(errors::UncheckedClean { span: attr.span });
411                checked_attrs.insert(attr.span);
412            }
413        }
414    }
415}
416
417impl<'tcx> intravisit::Visitor<'tcx> for FindAllAttrs<'tcx> {
418    type NestedFilter = nested_filter::All;
419
420    fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt {
421        self.tcx
422    }
423
424    fn visit_attribute(&mut self, attr: &'tcx Attribute) {
425        if let Attribute::Parsed(AttributeKind::RustcClean(attrs)) = attr {
426            for attr in attrs {
427                if self.is_active_attr(attr) {
428                    self.found_attrs.push(attr);
429                }
430            }
431        }
432    }
433}