Skip to content

Commit c5e283b

Browse files
committed
Auto merge of #148188 - Muscraft:annotate-snippets-default-on-nightly, r=estebank
feat: Use annotate-snippets by default on nightly This PR switches the default renderer to use `annotate-snippets` on nightly, but does not affect stable. This is part of the ongoing effort to use `annotate-snippets` to render all diagnostics. [MCP](rust-lang/compiler-team#937) Note: This contains the test change from #148004, without the change to the default emitter. #59346 rust-lang/rust-project-goals#123 r? `@davidtwco`
2 parents 642c19b + 9243928 commit c5e283b

25 files changed

+247
-176
lines changed

Cargo.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ dependencies = [
8080

8181
[[package]]
8282
name = "annotate-snippets"
83-
version = "0.12.7"
83+
version = "0.12.8"
8484
source = "registry+https://github.com/rust-lang/crates.io-index"
85-
checksum = "47224528f74de27d1d06aad6a5dda4f865b6ebe2e56c538943d746a7270cb67e"
85+
checksum = "025c7edcdffa4ccc5c0905f472a0ae3759378cfbef88ef518a3575e19ae3aebd"
8686
dependencies = [
8787
"anstyle",
8888
"unicode-width 0.2.2",
@@ -3766,7 +3766,7 @@ dependencies = [
37663766
name = "rustc_errors"
37673767
version = "0.0.0"
37683768
dependencies = [
3769-
"annotate-snippets 0.12.7",
3769+
"annotate-snippets 0.12.8",
37703770
"anstream",
37713771
"anstyle",
37723772
"derive_setters",

compiler/rustc_errors/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ edition = "2024"
55

66
[dependencies]
77
# tidy-alphabetical-start
8-
annotate-snippets = "0.12.7"
8+
annotate-snippets = "0.12.8"
99
anstream = "0.6.20"
1010
anstyle = "1.0.13"
1111
derive_setters = "0.1.6"

compiler/rustc_errors/src/annotate_snippet_emitter_writer.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -329,20 +329,22 @@ impl AnnotateSnippetEmitter {
329329
let substitutions = suggestion
330330
.substitutions
331331
.into_iter()
332-
.filter_map(|mut subst| {
332+
.filter(|subst| {
333333
// Suggestions coming from macros can have malformed spans. This is a heavy
334334
// handed approach to avoid ICEs by ignoring the suggestion outright.
335335
let invalid =
336336
subst.parts.iter().any(|item| sm.is_valid_span(item.span).is_err());
337337
if invalid {
338338
debug!("suggestion contains an invalid span: {:?}", subst);
339339
}
340-
340+
!invalid
341+
})
342+
.filter_map(|mut subst| {
341343
// Assumption: all spans are in the same file, and all spans
342344
// are disjoint. Sort in ascending order.
343345
subst.parts.sort_by_key(|part| part.span.lo());
344346
// Verify the assumption that all spans are disjoint
345-
assert_eq!(
347+
debug_assert_eq!(
346348
subst.parts.array_windows().find(|[a, b]| a.span.overlaps(b.span)),
347349
None,
348350
"all spans must be disjoint",
@@ -355,13 +357,11 @@ impl AnnotateSnippetEmitter {
355357

356358
let item_span = subst.parts.first()?;
357359
let file = sm.lookup_source_file(item_span.span.lo());
358-
if !invalid
359-
&& should_show_source_code(
360-
&self.ignored_directories_in_source_blocks,
361-
sm,
362-
&file,
363-
)
364-
{
360+
if should_show_source_code(
361+
&self.ignored_directories_in_source_blocks,
362+
sm,
363+
&file,
364+
) {
365365
Some(subst)
366366
} else {
367367
None

compiler/rustc_errors/src/emitter.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,16 @@ const DEFAULT_COLUMN_WIDTH: usize = 140;
4747
/// Describes the way the content of the `rendered` field of the json output is generated
4848
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
4949
pub enum HumanReadableErrorType {
50-
Default,
51-
Unicode,
52-
AnnotateSnippet,
53-
Short,
50+
Default { short: bool },
51+
AnnotateSnippet { short: bool, unicode: bool },
5452
}
5553

5654
impl HumanReadableErrorType {
5755
pub fn short(&self) -> bool {
58-
*self == HumanReadableErrorType::Short
56+
match self {
57+
HumanReadableErrorType::Default { short }
58+
| HumanReadableErrorType::AnnotateSnippet { short, .. } => *short,
59+
}
5960
}
6061
}
6162

compiler/rustc_errors/src/json.rs

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use rustc_span::hygiene::ExpnData;
2525
use rustc_span::source_map::{FilePathMapping, SourceMap};
2626
use serde::Serialize;
2727

28+
use crate::annotate_snippet_emitter_writer::AnnotateSnippetEmitter;
2829
use crate::diagnostic::IsLint;
2930
use crate::emitter::{
3031
ColorConfig, Destination, Emitter, HumanEmitter, HumanReadableErrorType, OutputTheme,
@@ -370,29 +371,46 @@ impl Diagnostic {
370371
.insert(0, Diagnostic::from_sub_diagnostic(&diag.emitted_at_sub_diag(), &args, je));
371372
}
372373
let buf = BufWriter(Arc::new(Mutex::new(Vec::new())));
373-
let short = je.json_rendered.short();
374374
let dst: Destination = AutoStream::new(
375375
Box::new(buf.clone()),
376376
match je.color_config.to_color_choice() {
377377
ColorChoice::Auto => ColorChoice::Always,
378378
choice => choice,
379379
},
380380
);
381-
HumanEmitter::new(dst, je.translator.clone())
382-
.short_message(short)
383-
.sm(je.sm.clone())
384-
.diagnostic_width(je.diagnostic_width)
385-
.macro_backtrace(je.macro_backtrace)
386-
.track_diagnostics(je.track_diagnostics)
387-
.terminal_url(je.terminal_url)
388-
.ui_testing(je.ui_testing)
389-
.ignored_directories_in_source_blocks(je.ignored_directories_in_source_blocks.clone())
390-
.theme(if let HumanReadableErrorType::Unicode = je.json_rendered {
391-
OutputTheme::Unicode
392-
} else {
393-
OutputTheme::Ascii
394-
})
395-
.emit_diagnostic(diag, registry);
381+
match je.json_rendered {
382+
HumanReadableErrorType::AnnotateSnippet { short, unicode } => {
383+
AnnotateSnippetEmitter::new(dst, je.translator.clone())
384+
.short_message(short)
385+
.sm(je.sm.clone())
386+
.diagnostic_width(je.diagnostic_width)
387+
.macro_backtrace(je.macro_backtrace)
388+
.track_diagnostics(je.track_diagnostics)
389+
.terminal_url(je.terminal_url)
390+
.ui_testing(je.ui_testing)
391+
.ignored_directories_in_source_blocks(
392+
je.ignored_directories_in_source_blocks.clone(),
393+
)
394+
.theme(if unicode { OutputTheme::Unicode } else { OutputTheme::Ascii })
395+
.emit_diagnostic(diag, registry)
396+
}
397+
HumanReadableErrorType::Default { short } => {
398+
HumanEmitter::new(dst, je.translator.clone())
399+
.short_message(short)
400+
.sm(je.sm.clone())
401+
.diagnostic_width(je.diagnostic_width)
402+
.macro_backtrace(je.macro_backtrace)
403+
.track_diagnostics(je.track_diagnostics)
404+
.terminal_url(je.terminal_url)
405+
.ui_testing(je.ui_testing)
406+
.ignored_directories_in_source_blocks(
407+
je.ignored_directories_in_source_blocks.clone(),
408+
)
409+
.theme(OutputTheme::Ascii)
410+
.emit_diagnostic(diag, registry)
411+
}
412+
}
413+
396414
let buf = Arc::try_unwrap(buf.0).unwrap().into_inner().unwrap();
397415
let buf = String::from_utf8(buf).unwrap();
398416

compiler/rustc_errors/src/json/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ fn test_positions(code: &str, span: (u32, u32), expected_output: SpanTestData) {
5050
Some(sm),
5151
translator,
5252
true, // pretty
53-
HumanReadableErrorType::Short,
53+
HumanReadableErrorType::Default { short: true },
5454
ColorConfig::Never,
5555
);
5656

compiler/rustc_interface/src/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ fn test_search_paths_tracking_hash_different_order() {
321321
let early_dcx = EarlyDiagCtxt::new(JSON);
322322
const JSON: ErrorOutputType = ErrorOutputType::Json {
323323
pretty: false,
324-
json_rendered: HumanReadableErrorType::Default,
324+
json_rendered: HumanReadableErrorType::Default { short: false },
325325
color_config: ColorConfig::Never,
326326
};
327327

compiler/rustc_parse/src/parser/tests.rs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#![allow(rustc::symbol_intern_string_literal)]
2-
32
use std::assert_matches::assert_matches;
43
use std::io::prelude::*;
54
use std::iter::Peekable;
@@ -12,6 +11,7 @@ use rustc_ast::token::{self, Delimiter, Token};
1211
use rustc_ast::tokenstream::{DelimSpacing, DelimSpan, Spacing, TokenStream, TokenTree};
1312
use rustc_ast::{self as ast, PatKind, visit};
1413
use rustc_ast_pretty::pprust::item_to_string;
14+
use rustc_errors::annotate_snippet_emitter_writer::AnnotateSnippetEmitter;
1515
use rustc_errors::emitter::{HumanEmitter, OutputTheme};
1616
use rustc_errors::translation::Translator;
1717
use rustc_errors::{AutoStream, DiagCtxt, MultiSpan, PResult};
@@ -43,12 +43,22 @@ fn create_test_handler(theme: OutputTheme) -> (DiagCtxt, Arc<SourceMap>, Arc<Mut
4343
let output = Arc::new(Mutex::new(Vec::new()));
4444
let source_map = Arc::new(SourceMap::new(FilePathMapping::empty()));
4545
let translator = Translator::with_fallback_bundle(vec![crate::DEFAULT_LOCALE_RESOURCE], false);
46-
let mut emitter =
47-
HumanEmitter::new(AutoStream::never(Box::new(Shared { data: output.clone() })), translator)
48-
.sm(Some(source_map.clone()))
49-
.diagnostic_width(Some(140));
50-
emitter = emitter.theme(theme);
51-
let dcx = DiagCtxt::new(Box::new(emitter));
46+
let shared: Box<dyn Write + Send> = Box::new(Shared { data: output.clone() });
47+
let auto_stream = AutoStream::never(shared);
48+
let dcx = DiagCtxt::new(match theme {
49+
OutputTheme::Ascii => Box::new(
50+
HumanEmitter::new(auto_stream, translator)
51+
.sm(Some(source_map.clone()))
52+
.diagnostic_width(Some(140))
53+
.theme(theme),
54+
),
55+
OutputTheme::Unicode => Box::new(
56+
AnnotateSnippetEmitter::new(auto_stream, translator)
57+
.sm(Some(source_map.clone()))
58+
.diagnostic_width(Some(140))
59+
.theme(theme),
60+
),
61+
});
5262
(dcx, source_map, output)
5363
}
5464

0 commit comments

Comments
 (0)