@@ -1882,9 +1882,8 @@ impl EmitterWriter {
18821882 & mut buffer,
18831883 & mut row_num,
18841884 & Vec :: new ( ) ,
1885- p,
1885+ p + line_start ,
18861886 l,
1887- line_start,
18881887 show_code_change,
18891888 max_line_num_len,
18901889 & file_lines,
@@ -1907,9 +1906,8 @@ impl EmitterWriter {
19071906 & mut buffer,
19081907 & mut row_num,
19091908 & Vec :: new ( ) ,
1910- p,
1909+ p + line_start ,
19111910 l,
1912- line_start,
19131911 show_code_change,
19141912 max_line_num_len,
19151913 & file_lines,
@@ -1925,9 +1923,8 @@ impl EmitterWriter {
19251923 & mut buffer,
19261924 & mut row_num,
19271925 & Vec :: new ( ) ,
1928- p,
1926+ p + line_start ,
19291927 l,
1930- line_start,
19311928 show_code_change,
19321929 max_line_num_len,
19331930 & file_lines,
@@ -1941,9 +1938,8 @@ impl EmitterWriter {
19411938 & mut buffer,
19421939 & mut row_num,
19431940 highlight_parts,
1944- line_pos,
1941+ line_pos + line_start ,
19451942 line,
1946- line_start,
19471943 show_code_change,
19481944 max_line_num_len,
19491945 & file_lines,
@@ -2167,40 +2163,63 @@ impl EmitterWriter {
21672163 buffer : & mut StyledBuffer ,
21682164 row_num : & mut usize ,
21692165 highlight_parts : & Vec < SubstitutionHighlight > ,
2170- line_pos : usize ,
2171- line : & str ,
2172- line_start : usize ,
2166+ line_num : usize ,
2167+ line_to_add : & str ,
21732168 show_code_change : DisplaySuggestion ,
21742169 max_line_num_len : usize ,
21752170 file_lines : & FileLines ,
21762171 is_multiline : bool ,
21772172 ) {
2178- // Print the span column to avoid confusion
2179- buffer. puts ( * row_num, 0 , & self . maybe_anonymized ( line_start + line_pos) , Style :: LineNumber ) ;
21802173 if let DisplaySuggestion :: Diff = show_code_change {
2181- // Add the line number for both addition and removal to drive the point home.
2182- //
2183- // N - fn foo<A: T>(bar: A) {
2184- // N + fn foo(bar: impl T) {
2185- buffer. puts (
2186- * row_num - 1 ,
2187- 0 ,
2188- & self . maybe_anonymized ( line_start + line_pos) ,
2189- Style :: LineNumber ,
2190- ) ;
2191- buffer. puts ( * row_num - 1 , max_line_num_len + 1 , "- " , Style :: Removal ) ;
2192- buffer. puts (
2193- * row_num - 1 ,
2194- max_line_num_len + 3 ,
2195- & normalize_whitespace (
2196- & file_lines. file . get_line ( file_lines. lines [ line_pos] . line_index ) . unwrap ( ) ,
2197- ) ,
2198- Style :: NoStyle ,
2199- ) ;
2200- buffer. puts ( * row_num, max_line_num_len + 1 , "+ " , Style :: Addition ) ;
2174+ // We need to print more than one line if the span we need to remove is multiline.
2175+ // For more info: https://github.com/rust-lang/rust/issues/92741
2176+ let lines_to_remove = file_lines. lines . iter ( ) . take ( file_lines. lines . len ( ) - 1 ) ;
2177+ for ( index, line_to_remove) in lines_to_remove. enumerate ( ) {
2178+ buffer. puts (
2179+ * row_num - 1 ,
2180+ 0 ,
2181+ & self . maybe_anonymized ( line_num + index) ,
2182+ Style :: LineNumber ,
2183+ ) ;
2184+ buffer. puts ( * row_num - 1 , max_line_num_len + 1 , "- " , Style :: Removal ) ;
2185+ let line = normalize_whitespace (
2186+ & file_lines. file . get_line ( line_to_remove. line_index ) . unwrap ( ) ,
2187+ ) ;
2188+ buffer. puts ( * row_num - 1 , max_line_num_len + 3 , & line, Style :: NoStyle ) ;
2189+ * row_num += 1 ;
2190+ }
2191+ // If the last line is exactly equal to the line we need to add, we can skip both of them.
2192+ // This allows us to avoid output like the following:
2193+ // 2 - &
2194+ // 2 + if true { true } else { false }
2195+ // 3 - if true { true } else { false }
2196+ // If those lines aren't equal, we print their diff
2197+ let last_line_index = file_lines. lines [ file_lines. lines . len ( ) - 1 ] . line_index ;
2198+ let last_line = & file_lines. file . get_line ( last_line_index) . unwrap ( ) ;
2199+ if last_line != line_to_add {
2200+ buffer. puts (
2201+ * row_num - 1 ,
2202+ 0 ,
2203+ & self . maybe_anonymized ( line_num + file_lines. lines . len ( ) - 1 ) ,
2204+ Style :: LineNumber ,
2205+ ) ;
2206+ buffer. puts ( * row_num - 1 , max_line_num_len + 1 , "- " , Style :: Removal ) ;
2207+ buffer. puts (
2208+ * row_num - 1 ,
2209+ max_line_num_len + 3 ,
2210+ & normalize_whitespace ( last_line) ,
2211+ Style :: NoStyle ,
2212+ ) ;
2213+ buffer. puts ( * row_num, 0 , & self . maybe_anonymized ( line_num) , Style :: LineNumber ) ;
2214+ buffer. puts ( * row_num, max_line_num_len + 1 , "+ " , Style :: Addition ) ;
2215+ buffer. append ( * row_num, & normalize_whitespace ( line_to_add) , Style :: NoStyle ) ;
2216+ } else {
2217+ * row_num -= 2 ;
2218+ }
22012219 } else if is_multiline {
2220+ buffer. puts ( * row_num, 0 , & self . maybe_anonymized ( line_num) , Style :: LineNumber ) ;
22022221 match & highlight_parts[ ..] {
2203- [ SubstitutionHighlight { start : 0 , end } ] if * end == line . len ( ) => {
2222+ [ SubstitutionHighlight { start : 0 , end } ] if * end == line_to_add . len ( ) => {
22042223 buffer. puts ( * row_num, max_line_num_len + 1 , "+ " , Style :: Addition ) ;
22052224 }
22062225 [ ] => {
@@ -2210,17 +2229,17 @@ impl EmitterWriter {
22102229 buffer. puts ( * row_num, max_line_num_len + 1 , "~ " , Style :: Addition ) ;
22112230 }
22122231 }
2232+ buffer. append ( * row_num, & normalize_whitespace ( line_to_add) , Style :: NoStyle ) ;
22132233 } else {
2234+ buffer. puts ( * row_num, 0 , & self . maybe_anonymized ( line_num) , Style :: LineNumber ) ;
22142235 draw_col_separator ( buffer, * row_num, max_line_num_len + 1 ) ;
2236+ buffer. append ( * row_num, & normalize_whitespace ( line_to_add) , Style :: NoStyle ) ;
22152237 }
22162238
2217- // print the suggestion
2218- buffer. append ( * row_num, & normalize_whitespace ( line) , Style :: NoStyle ) ;
2219-
22202239 // Colorize addition/replacements with green.
22212240 for & SubstitutionHighlight { start, end } in highlight_parts {
22222241 // Account for tabs when highlighting (#87972).
2223- let tabs: usize = line
2242+ let tabs: usize = line_to_add
22242243 . chars ( )
22252244 . take ( start)
22262245 . map ( |ch| match ch {
0 commit comments