From 684b9ea408afee07d254113d49c2426bfd2a1f8b Mon Sep 17 00:00:00 2001 From: Zalathar Date: Thu, 14 Dec 2023 13:47:26 +1100 Subject: coverage: Check that the function signature span precedes the body This will normally be true, but in cases where it's not true we're better off not making any assumptions about the signature. --- compiler/rustc_mir_transform/src/coverage/mod.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'compiler/rustc_mir_transform/src') diff --git a/compiler/rustc_mir_transform/src/coverage/mod.rs b/compiler/rustc_mir_transform/src/coverage/mod.rs index 5944282527f..65a0924f1c9 100644 --- a/compiler/rustc_mir_transform/src/coverage/mod.rs +++ b/compiler/rustc_mir_transform/src/coverage/mod.rs @@ -324,14 +324,16 @@ fn extract_hir_info<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> ExtractedHir let body_span = get_body_span(tcx, hir_body, def_id); // The actual signature span is only used if it has the same context and - // filename as the body. + // filename as the body, and precedes the body. let maybe_fn_sig_span = hir_node.fn_sig().map(|fn_sig| fn_sig.span); let fn_sig_span = maybe_fn_sig_span .filter(|&fn_sig_span| { let source_map = tcx.sess.source_map(); let file_idx = |span: Span| source_map.lookup_source_file_idx(span.lo()); - fn_sig_span.eq_ctxt(body_span) && file_idx(fn_sig_span) == file_idx(body_span) + fn_sig_span.eq_ctxt(body_span) + && fn_sig_span.hi() <= body_span.lo() + && file_idx(fn_sig_span) == file_idx(body_span) }) // If so, extend it to the start of the body span. .map(|fn_sig_span| fn_sig_span.with_hi(body_span.lo())) -- cgit 1.4.1-3-g733a5