11use rustc_data_structures:: fx:: { FxIndexMap , FxIndexSet , IndexEntry } ;
2+ use rustc_middle:: mir;
23use rustc_middle:: mir:: coverage:: BasicCoverageBlock ;
34use rustc_span:: { ExpnId , ExpnKind , Span } ;
45
6+ use crate :: coverage:: from_mir;
7+ use crate :: coverage:: graph:: CoverageGraph ;
8+ use crate :: coverage:: hir_info:: ExtractedHirInfo ;
9+
510#[ derive( Clone , Copy , Debug ) ]
611pub ( crate ) struct SpanWithBcb {
712 pub ( crate ) span : Span ,
@@ -70,6 +75,10 @@ pub(crate) struct ExpnNode {
7075 pub ( crate ) spans : Vec < SpanWithBcb > ,
7176 /// Expansions whose call-site is in this expansion.
7277 pub ( crate ) child_expn_ids : FxIndexSet < ExpnId > ,
78+
79+ /// Hole spans belonging to this expansion, to be carved out from the
80+ /// code spans during span refinement.
81+ pub ( crate ) hole_spans : Vec < Span > ,
7382}
7483
7584impl ExpnNode {
@@ -88,17 +97,27 @@ impl ExpnNode {
8897
8998 spans : vec ! [ ] ,
9099 child_expn_ids : FxIndexSet :: default ( ) ,
100+
101+ hole_spans : vec ! [ ] ,
91102 }
92103 }
93104}
94105
95- /// Given a collection of span/BCB pairs from potentially-different syntax contexts,
106+ /// Extracts raw span/BCB pairs from potentially-different syntax contexts, and
96107/// arranges them into an "expansion tree" based on their expansion call-sites.
97- pub ( crate ) fn build_expn_tree ( spans : impl IntoIterator < Item = SpanWithBcb > ) -> ExpnTree {
108+ pub ( crate ) fn build_expn_tree (
109+ mir_body : & mir:: Body < ' _ > ,
110+ hir_info : & ExtractedHirInfo ,
111+ graph : & CoverageGraph ,
112+ ) -> ExpnTree {
113+ let raw_spans = from_mir:: extract_raw_spans_from_mir ( mir_body, graph) ;
114+
98115 let mut nodes = FxIndexMap :: default ( ) ;
99116 let new_node = |& expn_id: & ExpnId | ExpnNode :: new ( expn_id) ;
100117
101- for span_with_bcb in spans {
118+ for from_mir:: RawSpanFromMir { raw_span, bcb } in raw_spans {
119+ let span_with_bcb = SpanWithBcb { span : raw_span, bcb } ;
120+
102121 // Create a node for this span's enclosing expansion, and add the span to it.
103122 let expn_id = span_with_bcb. span . ctxt ( ) . outer_expn ( ) ;
104123 let node = nodes. entry ( expn_id) . or_insert_with_key ( new_node) ;
@@ -123,5 +142,13 @@ pub(crate) fn build_expn_tree(spans: impl IntoIterator<Item = SpanWithBcb>) -> E
123142 }
124143 }
125144
145+ // Associate each hole span (extracted from HIR) with its corresponding
146+ // expansion tree node.
147+ for & hole_span in & hir_info. hole_spans {
148+ let expn_id = hole_span. ctxt ( ) . outer_expn ( ) ;
149+ let Some ( node) = nodes. get_mut ( & expn_id) else { continue } ;
150+ node. hole_spans . push ( hole_span) ;
151+ }
152+
126153 ExpnTree { nodes }
127154}
0 commit comments