@@ -12,7 +12,6 @@ use rustc_span::source_map::original_sp;
1212use rustc_span:: { BytePos , ExpnKind , MacroKind , Span , Symbol } ;
1313
1414use std:: cell:: OnceCell ;
15- use std:: cmp:: Ordering ;
1615
1716#[ derive( Debug , Copy , Clone ) ]
1817pub ( super ) enum CoverageStatement {
@@ -334,32 +333,20 @@ impl<'a, 'tcx> CoverageSpans<'a, 'tcx> {
334333 initial_spans. push ( CoverageSpan :: for_fn_sig ( self . fn_sig_span ) ) ;
335334
336335 initial_spans. sort_by ( |a, b| {
337- if a. span . lo ( ) == b. span . lo ( ) {
338- if a. span . hi ( ) == b. span . hi ( ) {
339- if a. is_in_same_bcb ( b) {
340- Some ( Ordering :: Equal )
341- } else {
342- // Sort equal spans by dominator relationship (so dominators always come
343- // before the dominated equal spans). When later comparing two spans in
344- // order, the first will either dominate the second, or they will have no
345- // dominator relationship.
346- self . basic_coverage_blocks . rank_partial_cmp ( a. bcb , b. bcb )
347- }
348- } else {
349- // Sort hi() in reverse order so shorter spans are attempted after longer spans.
350- // This guarantees that, if a `prev` span overlaps, and is not equal to, a
351- // `curr` span, the prev span either extends further left of the curr span, or
352- // they start at the same position and the prev span extends further right of
353- // the end of the curr span.
354- b. span . hi ( ) . partial_cmp ( & a. span . hi ( ) )
355- }
356- } else {
357- a. span . lo ( ) . partial_cmp ( & b. span . lo ( ) )
358- }
359- . unwrap ( )
360- // If two spans are otherwise identical, put closure spans first,
361- // as this seems to be what the refinement step expects.
362- . then_with ( || Ord :: cmp ( & a. is_closure , & b. is_closure ) . reverse ( ) )
336+ // First sort by span start.
337+ Ord :: cmp ( & a. span . lo ( ) , & b. span . lo ( ) )
338+ // If span starts are the same, sort by span end in reverse order.
339+ // This ensures that if spans A and B are adjacent in the list,
340+ // and they overlap but are not equal, then either:
341+ // - Span A extends further left, or
342+ // - Both have the same start and span A extends further right
343+ . then_with ( || Ord :: cmp ( & a. span . hi ( ) , & b. span . hi ( ) ) . reverse ( ) )
344+ // If both spans are equal, sort the BCBs in dominator order,
345+ // so that dominating BCBs come before other BCBs they dominate.
346+ . then_with ( || self . basic_coverage_blocks . cmp_in_dominator_order ( a. bcb , b. bcb ) )
347+ // If two spans are otherwise identical, put closure spans first,
348+ // as this seems to be what the refinement step expects.
349+ . then_with ( || Ord :: cmp ( & a. is_closure , & b. is_closure ) . reverse ( ) )
363350 } ) ;
364351
365352 initial_spans
0 commit comments