@@ -24,7 +24,7 @@ use rustc_lint_defs::pluralize;
24
24
25
25
use rustc_data_structures:: fx:: { FxHashMap , FxIndexMap } ;
26
26
use rustc_data_structures:: sync:: Lrc ;
27
- use rustc_error_messages:: FluentArgs ;
27
+ use rustc_error_messages:: { FluentArgs , SpanLabel } ;
28
28
use rustc_span:: hygiene:: { ExpnKind , MacroKind } ;
29
29
use std:: borrow:: Cow ;
30
30
use std:: cmp:: { max, min, Reverse } ;
@@ -773,6 +773,7 @@ impl EmitterWriter {
773
773
draw_col_separator_no_space ( buffer, line_offset, width_offset - 2 ) ;
774
774
}
775
775
776
+ #[ instrument( level = "trace" , skip( self ) , ret) ]
776
777
fn render_source_line (
777
778
& self ,
778
779
buffer : & mut StyledBuffer ,
@@ -804,6 +805,7 @@ impl EmitterWriter {
804
805
Some ( s) => normalize_whitespace ( & s) ,
805
806
None => return Vec :: new ( ) ,
806
807
} ;
808
+ trace ! ( ?source_string) ;
807
809
808
810
let line_offset = buffer. num_lines ( ) ;
809
811
@@ -1323,6 +1325,7 @@ impl EmitterWriter {
1323
1325
}
1324
1326
}
1325
1327
1328
+ #[ instrument( level = "trace" , skip( self , args) , ret) ]
1326
1329
fn emit_message_default (
1327
1330
& mut self ,
1328
1331
msp : & MultiSpan ,
@@ -1384,22 +1387,15 @@ impl EmitterWriter {
1384
1387
}
1385
1388
}
1386
1389
let mut annotated_files = FileWithAnnotatedLines :: collect_annotations ( self , args, msp) ;
1390
+ trace ! ( "{annotated_files:#?}" ) ;
1387
1391
1388
1392
// Make sure our primary file comes first
1389
- let ( primary_lo, sm) = if let ( Some ( sm) , Some ( ref primary_span) ) =
1390
- ( self . sm . as_ref ( ) , msp. primary_span ( ) . as_ref ( ) )
1391
- {
1392
- if !primary_span. is_dummy ( ) {
1393
- ( sm. lookup_char_pos ( primary_span. lo ( ) ) , sm)
1394
- } else {
1395
- emit_to_destination ( & buffer. render ( ) , level, & mut self . dst , self . short_message ) ?;
1396
- return Ok ( ( ) ) ;
1397
- }
1398
- } else {
1393
+ let primary_span = msp. primary_span ( ) . unwrap_or_default ( ) ;
1394
+ let ( Some ( sm) , false ) = ( self . sm . as_ref ( ) , primary_span. is_dummy ( ) ) else {
1399
1395
// If we don't have span information, emit and exit
1400
- emit_to_destination ( & buffer. render ( ) , level, & mut self . dst , self . short_message ) ?;
1401
- return Ok ( ( ) ) ;
1396
+ return emit_to_destination ( & buffer. render ( ) , level, & mut self . dst , self . short_message ) ;
1402
1397
} ;
1398
+ let primary_lo = sm. lookup_char_pos ( primary_span. lo ( ) ) ;
1403
1399
if let Ok ( pos) =
1404
1400
annotated_files. binary_search_by ( |x| x. file . name . cmp ( & primary_lo. file . name ) )
1405
1401
{
@@ -1410,6 +1406,54 @@ impl EmitterWriter {
1410
1406
for annotated_file in annotated_files {
1411
1407
// we can't annotate anything if the source is unavailable.
1412
1408
if !sm. ensure_source_file_source_present ( annotated_file. file . clone ( ) ) {
1409
+ if !self . short_message {
1410
+ // We'll just print an unannotated message.
1411
+ for ( annotation_id, line) in annotated_file. lines . into_iter ( ) . enumerate ( ) {
1412
+ let mut annotations = line. annotations . clone ( ) ;
1413
+ annotations. sort_by_key ( |a| Reverse ( a. start_col ) ) ;
1414
+ let mut line_idx = buffer. num_lines ( ) ;
1415
+ buffer. append (
1416
+ line_idx,
1417
+ & format ! (
1418
+ "{}:{}:{}" ,
1419
+ sm. filename_for_diagnostics( & annotated_file. file. name) ,
1420
+ sm. doctest_offset_line( & annotated_file. file. name, line. line_index) ,
1421
+ annotations[ 0 ] . start_col + 1 ,
1422
+ ) ,
1423
+ Style :: LineAndColumn ,
1424
+ ) ;
1425
+ if annotation_id == 0 {
1426
+ buffer. prepend ( line_idx, "--> " , Style :: LineNumber ) ;
1427
+ for _ in 0 ..max_line_num_len {
1428
+ buffer. prepend ( line_idx, " " , Style :: NoStyle ) ;
1429
+ }
1430
+ line_idx += 1 ;
1431
+ } ;
1432
+ for ( i, annotation) in annotations. into_iter ( ) . enumerate ( ) {
1433
+ if let Some ( label) = & annotation. label {
1434
+ let style = if annotation. is_primary {
1435
+ Style :: LabelPrimary
1436
+ } else {
1437
+ Style :: LabelSecondary
1438
+ } ;
1439
+ if annotation_id == 0 {
1440
+ buffer. prepend ( line_idx, " |" , Style :: LineNumber ) ;
1441
+ for _ in 0 ..max_line_num_len {
1442
+ buffer. prepend ( line_idx, " " , Style :: NoStyle ) ;
1443
+ }
1444
+ line_idx += 1 ;
1445
+ buffer. append ( line_idx + i, " = note: " , style) ;
1446
+ for _ in 0 ..max_line_num_len {
1447
+ buffer. prepend ( line_idx, " " , Style :: NoStyle ) ;
1448
+ }
1449
+ } else {
1450
+ buffer. append ( line_idx + i, ": " , style) ;
1451
+ }
1452
+ buffer. append ( line_idx + i, label, style) ;
1453
+ }
1454
+ }
1455
+ }
1456
+ }
1413
1457
continue ;
1414
1458
}
1415
1459
@@ -1656,6 +1700,7 @@ impl EmitterWriter {
1656
1700
multilines. extend ( & to_add) ;
1657
1701
}
1658
1702
}
1703
+ trace ! ( "buffer: {:#?}" , buffer. render( ) ) ;
1659
1704
}
1660
1705
1661
1706
if let Some ( tracked) = emitted_at {
@@ -1979,6 +2024,7 @@ impl EmitterWriter {
1979
2024
Ok ( ( ) )
1980
2025
}
1981
2026
2027
+ #[ instrument( level = "trace" , skip( self , args, code, children, suggestions) ) ]
1982
2028
fn emit_messages_default (
1983
2029
& mut self ,
1984
2030
level : & Level ,
@@ -2209,46 +2255,28 @@ impl FileWithAnnotatedLines {
2209
2255
let mut multiline_annotations = vec ! [ ] ;
2210
2256
2211
2257
if let Some ( ref sm) = emitter. source_map ( ) {
2212
- for span_label in msp. span_labels ( ) {
2213
- let fixup_lo_hi = |span : Span | {
2214
- let lo = sm. lookup_char_pos ( span. lo ( ) ) ;
2215
- let mut hi = sm. lookup_char_pos ( span. hi ( ) ) ;
2216
-
2217
- // Watch out for "empty spans". If we get a span like 6..6, we
2218
- // want to just display a `^` at 6, so convert that to
2219
- // 6..7. This is degenerate input, but it's best to degrade
2220
- // gracefully -- and the parser likes to supply a span like
2221
- // that for EOF, in particular.
2222
-
2223
- if lo. col_display == hi. col_display && lo. line == hi. line {
2224
- hi. col_display += 1 ;
2225
- }
2226
- ( lo, hi)
2258
+ for SpanLabel { span, is_primary, label } in msp. span_labels ( ) {
2259
+ // If we don't have a useful span, pick the primary span if that exists.
2260
+ // Worst case we'll just print an error at the top of the main file.
2261
+ let span = match ( span. is_dummy ( ) , msp. primary_span ( ) ) {
2262
+ ( _, None ) | ( false , _) => span,
2263
+ ( true , Some ( span) ) => span,
2227
2264
} ;
2228
2265
2229
- if span_label. span . is_dummy ( ) {
2230
- if let Some ( span) = msp. primary_span ( ) {
2231
- // if we don't know where to render the annotation, emit it as a note
2232
- // on the primary span.
2233
-
2234
- let ( lo, hi) = fixup_lo_hi ( span) ;
2235
-
2236
- let ann = Annotation {
2237
- start_col : lo. col_display ,
2238
- end_col : hi. col_display ,
2239
- is_primary : span_label. is_primary ,
2240
- label : span_label
2241
- . label
2242
- . as_ref ( )
2243
- . map ( |m| emitter. translate_message ( m, args) . to_string ( ) ) ,
2244
- annotation_type : AnnotationType :: Singleline ,
2245
- } ;
2246
- add_annotation_to_file ( & mut output, lo. file , lo. line , ann) ;
2247
- }
2248
- continue ;
2266
+ let lo = sm. lookup_char_pos ( span. lo ( ) ) ;
2267
+ let mut hi = sm. lookup_char_pos ( span. hi ( ) ) ;
2268
+
2269
+ // Watch out for "empty spans". If we get a span like 6..6, we
2270
+ // want to just display a `^` at 6, so convert that to
2271
+ // 6..7. This is degenerate input, but it's best to degrade
2272
+ // gracefully -- and the parser likes to supply a span like
2273
+ // that for EOF, in particular.
2274
+
2275
+ if lo. col_display == hi. col_display && lo. line == hi. line {
2276
+ hi. col_display += 1 ;
2249
2277
}
2250
2278
2251
- let ( lo , hi ) = fixup_lo_hi ( span_label . span ) ;
2279
+ let label = label . as_ref ( ) . map ( |m| emitter . translate_message ( m , args ) . to_string ( ) ) ;
2252
2280
2253
2281
if lo. line != hi. line {
2254
2282
let ml = MultilineAnnotation {
@@ -2257,23 +2285,17 @@ impl FileWithAnnotatedLines {
2257
2285
line_end : hi. line ,
2258
2286
start_col : lo. col_display ,
2259
2287
end_col : hi. col_display ,
2260
- is_primary : span_label. is_primary ,
2261
- label : span_label
2262
- . label
2263
- . as_ref ( )
2264
- . map ( |m| emitter. translate_message ( m, args) . to_string ( ) ) ,
2288
+ is_primary,
2289
+ label,
2265
2290
overlaps_exactly : false ,
2266
2291
} ;
2267
2292
multiline_annotations. push ( ( lo. file , ml) ) ;
2268
2293
} else {
2269
2294
let ann = Annotation {
2270
2295
start_col : lo. col_display ,
2271
2296
end_col : hi. col_display ,
2272
- is_primary : span_label. is_primary ,
2273
- label : span_label
2274
- . label
2275
- . as_ref ( )
2276
- . map ( |m| emitter. translate_message ( m, args) . to_string ( ) ) ,
2297
+ is_primary,
2298
+ label,
2277
2299
annotation_type : AnnotationType :: Singleline ,
2278
2300
} ;
2279
2301
add_annotation_to_file ( & mut output, lo. file , lo. line , ann) ;
0 commit comments