Skip to main content

rustc_mir_transform/coverage/
hir_info.rs

1use rustc_hir as hir;
2use rustc_hir::intravisit::{Visitor, walk_expr};
3use rustc_middle::hir::nested_filter;
4use rustc_middle::ty::{self, TyCtxt};
5use rustc_span::Span;
6use rustc_span::def_id::LocalDefId;
7
8/// Function information extracted from HIR by the coverage instrumentor.
9#[derive(Debug)]
10pub(crate) struct ExtractedHirInfo {
11    pub(crate) function_source_hash: u64,
12    pub(crate) is_async_fn: bool,
13    /// The span of the function's signature, if available.
14    /// Must have the same context and filename as the body span.
15    pub(crate) fn_sig_span: Option<Span>,
16    pub(crate) body_span: Span,
17    /// "Holes" are regions within the function body (or its expansions) that
18    /// should not be included in coverage spans for this function
19    /// (e.g. closures and nested items).
20    pub(crate) hole_spans: Vec<Span>,
21}
22
23pub(crate) fn extract_hir_info<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> ExtractedHirInfo {
24    // FIXME(#79625): Consider improving MIR to provide the information needed, to avoid going back
25    // to HIR for it.
26
27    // Synthetic by-move coroutine bodies don't have useful HIR of their own.
28    // Use the original coroutine body instead. These synthetic bodies are
29    // created with a coroutine type, so we can inspect that type as-is.
30    if tcx.is_synthetic_mir(def_id) {
31        let effective_def_id =
32            match *tcx.type_of(def_id).instantiate_identity().skip_normalization().kind() {
33                ty::Coroutine(coroutine_def_id, _) => coroutine_def_id.expect_local(),
34                _ => tcx.local_parent(def_id),
35            };
36        return extract_hir_info(tcx, effective_def_id);
37    }
38
39    let hir_node = tcx.hir_node_by_def_id(def_id);
40    let fn_body_id = hir_node.body_id().expect("HIR node is a function with body");
41    let hir_body = tcx.hir_body(fn_body_id);
42
43    let maybe_fn_sig = hir_node.fn_sig();
44    let is_async_fn = maybe_fn_sig.is_some_and(|fn_sig| fn_sig.header.is_async());
45
46    let mut body_span = hir_body.value.span;
47
48    use hir::{Closure, Expr, ExprKind, Node};
49    // Unexpand a closure's body span back to the context of its declaration.
50    // This helps with closure bodies that consist of just a single bang-macro,
51    // and also with closure bodies produced by async desugaring.
52    if let Node::Expr(&Expr { kind: ExprKind::Closure(&Closure { fn_decl_span, .. }), .. }) =
53        hir_node
54    {
55        body_span = body_span.find_ancestor_in_same_ctxt(fn_decl_span).unwrap_or(body_span);
56    }
57
58    // The actual signature span is only used if it has the same context and
59    // filename as the body, and precedes the body.
60    let fn_sig_span = maybe_fn_sig.map(|fn_sig| fn_sig.span).filter(|&fn_sig_span| {
61        let source_map = tcx.sess.source_map();
62        let file_idx = |span: Span| source_map.lookup_source_file_idx(span.lo());
63
64        fn_sig_span.eq_ctxt(body_span)
65            && fn_sig_span.hi() <= body_span.lo()
66            && file_idx(fn_sig_span) == file_idx(body_span)
67    });
68
69    let function_source_hash = hash_mir_source(tcx, hir_body);
70
71    let hole_spans = extract_hole_spans_from_hir(tcx, hir_body);
72
73    ExtractedHirInfo { function_source_hash, is_async_fn, fn_sig_span, body_span, hole_spans }
74}
75
76fn hash_mir_source<'tcx>(tcx: TyCtxt<'tcx>, hir_body: &'tcx hir::Body<'tcx>) -> u64 {
77    let owner = hir_body.id().hir_id.owner;
78    tcx.hir_owner_nodes(owner)
79        .opt_hash_including_bodies
80        .expect("hash should be present when coverage instrumentation is enabled")
81        .to_smaller_hash()
82        .as_u64()
83}
84
85fn extract_hole_spans_from_hir<'tcx>(tcx: TyCtxt<'tcx>, hir_body: &hir::Body<'tcx>) -> Vec<Span> {
86    struct HolesVisitor<'tcx> {
87        tcx: TyCtxt<'tcx>,
88        hole_spans: Vec<Span>,
89    }
90
91    impl<'tcx> Visitor<'tcx> for HolesVisitor<'tcx> {
92        /// We have special handling for nested items, but we still want to
93        /// traverse into nested bodies of things that are not considered items,
94        /// such as "anon consts" (e.g. array lengths).
95        type NestedFilter = nested_filter::OnlyBodies;
96
97        fn maybe_tcx(&mut self) -> TyCtxt<'tcx> {
98            self.tcx
99        }
100
101        /// We override `visit_nested_item` instead of `visit_item` because we
102        /// only need the item's span, not the item itself.
103        fn visit_nested_item(&mut self, id: hir::ItemId) -> Self::Result {
104            let span = self.tcx.def_span(id.owner_id.def_id);
105            self.visit_hole_span(span);
106            // Having visited this item, we don't care about its children,
107            // so don't call `walk_item`.
108        }
109
110        // We override `visit_expr` instead of the more specific expression
111        // visitors, so that we have direct access to the expression span.
112        fn visit_expr(&mut self, expr: &'tcx hir::Expr<'tcx>) {
113            match expr.kind {
114                hir::ExprKind::Closure(_) | hir::ExprKind::ConstBlock(_) => {
115                    self.visit_hole_span(expr.span);
116                    // Having visited this expression, we don't care about its
117                    // children, so don't call `walk_expr`.
118                }
119
120                // For other expressions, recursively visit as normal.
121                _ => walk_expr(self, expr),
122            }
123        }
124    }
125    impl HolesVisitor<'_> {
126        fn visit_hole_span(&mut self, hole_span: Span) {
127            self.hole_spans.push(hole_span);
128        }
129    }
130
131    let mut visitor = HolesVisitor { tcx, hole_spans: vec![] };
132
133    visitor.visit_body(hir_body);
134    visitor.hole_spans
135}