coverage: Use original HIR info for synthetic by-move coroutine bodies

This commit is contained in:
qaijuang
2026-05-26 10:01:36 -04:00
parent 78a6b58a42
commit cf3249c0cc
3 changed files with 23 additions and 10 deletions
@@ -1,7 +1,7 @@
use rustc_hir as hir;
use rustc_hir::intravisit::{Visitor, walk_expr};
use rustc_middle::hir::nested_filter;
use rustc_middle::ty::TyCtxt;
use rustc_middle::ty::{self, TyCtxt};
use rustc_span::Span;
use rustc_span::def_id::LocalDefId;
@@ -24,9 +24,16 @@ pub(crate) fn extract_hir_info<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> E
// FIXME(#79625): Consider improving MIR to provide the information needed, to avoid going back
// to HIR for it.
// HACK: For synthetic MIR bodies (async closures), use the def id of the HIR body.
// Synthetic by-move coroutine bodies don't have useful HIR of their own.
// Use the original coroutine body instead. These synthetic bodies are
// created with a coroutine type, so we can inspect that type as-is.
if tcx.is_synthetic_mir(def_id) {
return extract_hir_info(tcx, tcx.local_parent(def_id));
let effective_def_id =
match *tcx.type_of(def_id).instantiate_identity().skip_normalization().kind() {
ty::Coroutine(coroutine_def_id, _) => coroutine_def_id.expect_local(),
_ => tcx.local_parent(def_id),
};
return extract_hir_info(tcx, effective_def_id);
}
let hir_node = tcx.hir_node_by_def_id(def_id);
+9 -3
View File
@@ -39,13 +39,19 @@ Number of file 0 mappings: 10
Highest counter ID seen: c1
Function name: async_closure2::main::{closure#0}
Raw bytes (14): 0x[01, 01, 00, 02, 01, 11, 22, 00, 23, 01, 03, 05, 00, 06]
Raw bytes (44): 0x[01, 01, 00, 08, 01, 11, 22, 00, 23, 01, 01, 09, 00, 0e, 01, 00, 0f, 00, 18, 01, 00, 1c, 00, 2c, 01, 01, 09, 00, 0e, 01, 00, 0f, 00, 18, 01, 00, 1c, 00, 2c, 01, 01, 05, 00, 06]
Number of files: 1
- file 0 => $DIR/async_closure2.rs
Number of expressions: 0
Number of file 0 mappings: 2
Number of file 0 mappings: 8
- Code(Counter(0)) at (prev + 17, 34) to (start + 0, 35)
- Code(Counter(0)) at (prev + 3, 5) to (start + 0, 6)
- Code(Counter(0)) at (prev + 1, 9) to (start + 0, 14)
- Code(Counter(0)) at (prev + 0, 15) to (start + 0, 24)
- Code(Counter(0)) at (prev + 0, 28) to (start + 0, 44)
- Code(Counter(0)) at (prev + 1, 9) to (start + 0, 14)
- Code(Counter(0)) at (prev + 0, 15) to (start + 0, 24)
- Code(Counter(0)) at (prev + 0, 28) to (start + 0, 44)
- Code(Counter(0)) at (prev + 1, 5) to (start + 0, 6)
Highest counter ID seen: c0
Function name: async_closure2::main::{closure#0}::{closure#0}::<_> (unused)
+4 -4
View File
@@ -15,16 +15,16 @@
LL| |
LL| 1|pub fn main() {
LL| 1| let async_closure = async || {
LL| 0| STEPS.fetch_add(1, Ordering::SeqCst);
LL| 0| STEPS.fetch_add(1, Ordering::SeqCst);
LL| 1| STEPS.fetch_add(1, Ordering::SeqCst);
LL| 1| STEPS.fetch_add(1, Ordering::SeqCst);
LL| 1| };
------------------
| Unexecuted instantiation: async_closure2::main::{closure#0}::{closure#0}::<_>
------------------
| async_closure2::main::{closure#0}:
| LL| 1| let async_closure = async || {
| LL| | STEPS.fetch_add(1, Ordering::SeqCst);
| LL| | STEPS.fetch_add(1, Ordering::SeqCst);
| LL| 1| STEPS.fetch_add(1, Ordering::SeqCst);
| LL| 1| STEPS.fetch_add(1, Ordering::SeqCst);
| LL| 1| };
------------------
LL| 1| executor::block_on(call_once(async_closure));