@@ -24,7 +24,7 @@ use rustc_lint_defs::pluralize;
2424
2525use rustc_data_structures:: fx:: { FxHashMap , FxIndexMap } ;
2626use rustc_data_structures:: sync:: Lrc ;
27- use rustc_error_messages:: FluentArgs ;
27+ use rustc_error_messages:: { FluentArgs , SpanLabel } ;
2828use rustc_span:: hygiene:: { ExpnKind , MacroKind } ;
2929use std:: borrow:: Cow ;
3030use std:: cmp:: { max, min, Reverse } ;
@@ -773,6 +773,7 @@ impl EmitterWriter {
773773 draw_col_separator_no_space ( buffer, line_offset, width_offset - 2 ) ;
774774 }
775775
776+ #[ instrument( level = "trace" , skip( self ) , ret) ]
776777 fn render_source_line (
777778 & self ,
778779 buffer : & mut StyledBuffer ,
@@ -804,6 +805,7 @@ impl EmitterWriter {
804805 Some ( s) => normalize_whitespace ( & s) ,
805806 None => return Vec :: new ( ) ,
806807 } ;
808+ trace ! ( ?source_string) ;
807809
808810 let line_offset = buffer. num_lines ( ) ;
809811
@@ -1323,6 +1325,7 @@ impl EmitterWriter {
13231325 }
13241326 }
13251327
1328+ #[ instrument( level = "trace" , skip( self , args) , ret) ]
13261329 fn emit_message_default (
13271330 & mut self ,
13281331 msp : & MultiSpan ,
@@ -1384,22 +1387,15 @@ impl EmitterWriter {
13841387 }
13851388 }
13861389 let mut annotated_files = FileWithAnnotatedLines :: collect_annotations ( self , args, msp) ;
1390+ trace ! ( "{annotated_files:#?}" ) ;
13871391
13881392 // 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 {
13991395 // 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 ) ;
14021397 } ;
1398+ let primary_lo = sm. lookup_char_pos ( primary_span. lo ( ) ) ;
14031399 if let Ok ( pos) =
14041400 annotated_files. binary_search_by ( |x| x. file . name . cmp ( & primary_lo. file . name ) )
14051401 {
@@ -1410,6 +1406,54 @@ impl EmitterWriter {
14101406 for annotated_file in annotated_files {
14111407 // we can't annotate anything if the source is unavailable.
14121408 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+ }
14131457 continue ;
14141458 }
14151459
@@ -1656,6 +1700,7 @@ impl EmitterWriter {
16561700 multilines. extend ( & to_add) ;
16571701 }
16581702 }
1703+ trace ! ( "buffer: {:#?}" , buffer. render( ) ) ;
16591704 }
16601705
16611706 if let Some ( tracked) = emitted_at {
@@ -1979,6 +2024,7 @@ impl EmitterWriter {
19792024 Ok ( ( ) )
19802025 }
19812026
2027+ #[ instrument( level = "trace" , skip( self , args, code, children, suggestions) ) ]
19822028 fn emit_messages_default (
19832029 & mut self ,
19842030 level : & Level ,
@@ -2209,46 +2255,28 @@ impl FileWithAnnotatedLines {
22092255 let mut multiline_annotations = vec ! [ ] ;
22102256
22112257 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,
22272264 } ;
22282265
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 ;
22492277 }
22502278
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 ( ) ) ;
22522280
22532281 if lo. line != hi. line {
22542282 let ml = MultilineAnnotation {
@@ -2257,23 +2285,17 @@ impl FileWithAnnotatedLines {
22572285 line_end : hi. line ,
22582286 start_col : lo. col_display ,
22592287 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,
22652290 overlaps_exactly : false ,
22662291 } ;
22672292 multiline_annotations. push ( ( lo. file , ml) ) ;
22682293 } else {
22692294 let ann = Annotation {
22702295 start_col : lo. col_display ,
22712296 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,
22772299 annotation_type : AnnotationType :: Singleline ,
22782300 } ;
22792301 add_annotation_to_file ( & mut output, lo. file , lo. line , ann) ;
0 commit comments