@@ -346,9 +346,20 @@ impl EmitterWriter {
346346 // and "annotations lines", where the highlight lines have the `^`.
347347
348348 // Sort the annotations by (start, end col)
349+ // The labels are reversed, sort and then reversed again.
350+ // Consider a list of annotations (A1, A2, C1, C2, B1, B2) where
351+ // the letter signifies the span. Here we are only sorting by the
352+ // span and hence, the order of the elements with the same span will
353+ // not change. On reversing the ordering (|a, b| but b.cmp(a)), you get
354+ // (C1, C2, B1, B2, A1, A2). All the elements with the same span are
355+ // still ordered first to last, but all the elements with different
356+ // spans are ordered by their spans in last to first order. Last to
357+ // first order is important, because the jiggly lines and | are on
358+ // the left, so the rightmost span needs to be rendered first,
359+ // otherwise the lines would end up needing to go over a message.
360+
349361 let mut annotations = line. annotations . clone ( ) ;
350- annotations. sort ( ) ;
351- annotations. reverse ( ) ;
362+ annotations. sort_by ( |a, b| b. start_col . cmp ( & a. start_col ) ) ;
352363
353364 // First, figure out where each label will be positioned.
354365 //
0 commit comments