@@ -1882,9 +1882,8 @@ impl EmitterWriter {
1882
1882
& mut buffer,
1883
1883
& mut row_num,
1884
1884
& Vec :: new ( ) ,
1885
- p,
1885
+ p + line_start ,
1886
1886
l,
1887
- line_start,
1888
1887
show_code_change,
1889
1888
max_line_num_len,
1890
1889
& file_lines,
@@ -1907,9 +1906,8 @@ impl EmitterWriter {
1907
1906
& mut buffer,
1908
1907
& mut row_num,
1909
1908
& Vec :: new ( ) ,
1910
- p,
1909
+ p + line_start ,
1911
1910
l,
1912
- line_start,
1913
1911
show_code_change,
1914
1912
max_line_num_len,
1915
1913
& file_lines,
@@ -1925,9 +1923,8 @@ impl EmitterWriter {
1925
1923
& mut buffer,
1926
1924
& mut row_num,
1927
1925
& Vec :: new ( ) ,
1928
- p,
1926
+ p + line_start ,
1929
1927
l,
1930
- line_start,
1931
1928
show_code_change,
1932
1929
max_line_num_len,
1933
1930
& file_lines,
@@ -1941,9 +1938,8 @@ impl EmitterWriter {
1941
1938
& mut buffer,
1942
1939
& mut row_num,
1943
1940
highlight_parts,
1944
- line_pos,
1941
+ line_pos + line_start ,
1945
1942
line,
1946
- line_start,
1947
1943
show_code_change,
1948
1944
max_line_num_len,
1949
1945
& file_lines,
@@ -2167,49 +2163,44 @@ impl EmitterWriter {
2167
2163
buffer : & mut StyledBuffer ,
2168
2164
row_num : & mut usize ,
2169
2165
highlight_parts : & Vec < SubstitutionHighlight > ,
2170
- line_pos : usize ,
2171
- line : & str ,
2172
- line_start : usize ,
2166
+ line_num : usize ,
2167
+ line_to_add : & str ,
2173
2168
show_code_change : DisplaySuggestion ,
2174
2169
max_line_num_len : usize ,
2175
2170
file_lines : & FileLines ,
2176
2171
is_multiline : bool ,
2177
2172
) {
2178
2173
if let DisplaySuggestion :: Diff = show_code_change {
2179
- // Add the line number for both addition and removal to drive the point home.
2180
- //
2181
- // N - fn foo<A: T>(bar: A) {
2182
- // N + fn foo(bar: impl T) {
2183
- let number_of_lines = file_lines. lines . len ( ) ;
2184
- for ( index, line_to_remove) in
2185
- file_lines. lines . iter ( ) . take ( number_of_lines - 1 ) . enumerate ( )
2186
- {
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 ( ) {
2187
2178
buffer. puts (
2188
2179
* row_num - 1 ,
2189
2180
0 ,
2190
- & self . maybe_anonymized ( line_start + line_pos + index) ,
2181
+ & self . maybe_anonymized ( line_num + index) ,
2191
2182
Style :: LineNumber ,
2192
2183
) ;
2193
2184
buffer. puts ( * row_num - 1 , max_line_num_len + 1 , "- " , Style :: Removal ) ;
2194
- buffer. puts (
2195
- * row_num - 1 ,
2196
- max_line_num_len + 3 ,
2197
- & normalize_whitespace (
2198
- & file_lines. file . get_line ( line_to_remove. line_index ) . unwrap ( ) ,
2199
- ) ,
2200
- Style :: NoStyle ,
2185
+ let line = normalize_whitespace (
2186
+ & file_lines. file . get_line ( line_to_remove. line_index ) . unwrap ( ) ,
2201
2187
) ;
2188
+ buffer. puts ( * row_num - 1 , max_line_num_len + 3 , & line, Style :: NoStyle ) ;
2202
2189
* row_num += 1 ;
2203
2190
}
2204
- let last_line = & file_lines
2205
- . file
2206
- . get_line ( file_lines. lines [ number_of_lines - 1 ] . line_index )
2207
- . unwrap ( ) ;
2208
- if last_line != line {
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 {
2209
2200
buffer. puts (
2210
2201
* row_num - 1 ,
2211
2202
0 ,
2212
- & self . maybe_anonymized ( line_start + line_pos + number_of_lines - 1 ) ,
2203
+ & self . maybe_anonymized ( line_num + file_lines . lines . len ( ) - 1 ) ,
2213
2204
Style :: LineNumber ,
2214
2205
) ;
2215
2206
buffer. puts ( * row_num - 1 , max_line_num_len + 1 , "- " , Style :: Removal ) ;
@@ -2219,27 +2210,16 @@ impl EmitterWriter {
2219
2210
& normalize_whitespace ( last_line) ,
2220
2211
Style :: NoStyle ,
2221
2212
) ;
2222
- buffer. puts (
2223
- * row_num,
2224
- 0 ,
2225
- & self . maybe_anonymized ( line_start + line_pos) ,
2226
- Style :: LineNumber ,
2227
- ) ;
2213
+ buffer. puts ( * row_num, 0 , & self . maybe_anonymized ( line_num) , Style :: LineNumber ) ;
2228
2214
buffer. puts ( * row_num, max_line_num_len + 1 , "+ " , Style :: Addition ) ;
2229
- // print the suggestion
2230
- buffer. append ( * row_num, & normalize_whitespace ( line) , Style :: NoStyle ) ;
2215
+ buffer. append ( * row_num, & normalize_whitespace ( line_to_add) , Style :: NoStyle ) ;
2231
2216
} else {
2232
2217
* row_num -= 2 ;
2233
2218
}
2234
2219
} else if is_multiline {
2235
- buffer. puts (
2236
- * row_num,
2237
- 0 ,
2238
- & self . maybe_anonymized ( line_start + line_pos) ,
2239
- Style :: LineNumber ,
2240
- ) ;
2220
+ buffer. puts ( * row_num, 0 , & self . maybe_anonymized ( line_num) , Style :: LineNumber ) ;
2241
2221
match & highlight_parts[ ..] {
2242
- [ SubstitutionHighlight { start : 0 , end } ] if * end == line . len ( ) => {
2222
+ [ SubstitutionHighlight { start : 0 , end } ] if * end == line_to_add . len ( ) => {
2243
2223
buffer. puts ( * row_num, max_line_num_len + 1 , "+ " , Style :: Addition ) ;
2244
2224
}
2245
2225
[ ] => {
@@ -2249,24 +2229,17 @@ impl EmitterWriter {
2249
2229
buffer. puts ( * row_num, max_line_num_len + 1 , "~ " , Style :: Addition ) ;
2250
2230
}
2251
2231
}
2252
- // print the suggestion
2253
- buffer. append ( * row_num, & normalize_whitespace ( line) , Style :: NoStyle ) ;
2232
+ buffer. append ( * row_num, & normalize_whitespace ( line_to_add) , Style :: NoStyle ) ;
2254
2233
} else {
2255
- buffer. puts (
2256
- * row_num,
2257
- 0 ,
2258
- & self . maybe_anonymized ( line_start + line_pos) ,
2259
- Style :: LineNumber ,
2260
- ) ;
2234
+ buffer. puts ( * row_num, 0 , & self . maybe_anonymized ( line_num) , Style :: LineNumber ) ;
2261
2235
draw_col_separator ( buffer, * row_num, max_line_num_len + 1 ) ;
2262
- // print the suggestion
2263
- buffer. append ( * row_num, & normalize_whitespace ( line) , Style :: NoStyle ) ;
2236
+ buffer. append ( * row_num, & normalize_whitespace ( line_to_add) , Style :: NoStyle ) ;
2264
2237
}
2265
2238
2266
2239
// Colorize addition/replacements with green.
2267
2240
for & SubstitutionHighlight { start, end } in highlight_parts {
2268
2241
// Account for tabs when highlighting (#87972).
2269
- let tabs: usize = line
2242
+ let tabs: usize = line_to_add
2270
2243
. chars ( )
2271
2244
. take ( start)
2272
2245
. map ( |ch| match ch {
0 commit comments