Skip to content

Commit bea90ee

Browse files
committed
feat: Always use annotate-snippets for Unicode output
1 parent b4c78d2 commit bea90ee

File tree

9 files changed

+54
-78
lines changed

9 files changed

+54
-78
lines changed

compiler/rustc_errors/src/emitter.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ const DEFAULT_COLUMN_WIDTH: usize = 140;
4848
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
4949
pub enum HumanReadableErrorType {
5050
Default,
51-
Unicode,
52-
AnnotateSnippet,
51+
AnnotateSnippet { unicode: bool },
5352
Short,
5453
}
5554

compiler/rustc_errors/src/json.rs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ impl Diagnostic {
379379
choice => choice,
380380
},
381381
);
382-
if let HumanReadableErrorType::AnnotateSnippet = je.json_rendered {
382+
if let HumanReadableErrorType::AnnotateSnippet { unicode } = je.json_rendered {
383383
AnnotateSnippetEmitter::new(dst, je.translator.clone())
384384
.short_message(short)
385385
.sm(je.sm.clone())
@@ -391,12 +391,8 @@ impl Diagnostic {
391391
.ignored_directories_in_source_blocks(
392392
je.ignored_directories_in_source_blocks.clone(),
393393
)
394-
.theme(if let HumanReadableErrorType::Unicode = je.json_rendered {
395-
OutputTheme::Unicode
396-
} else {
397-
OutputTheme::Ascii
398-
})
399-
.emit_diagnostic(diag, registry);
394+
.theme(if unicode { OutputTheme::Unicode } else { OutputTheme::Ascii })
395+
.emit_diagnostic(diag, registry)
400396
} else {
401397
HumanEmitter::new(dst, je.translator.clone())
402398
.short_message(short)
@@ -409,12 +405,8 @@ impl Diagnostic {
409405
.ignored_directories_in_source_blocks(
410406
je.ignored_directories_in_source_blocks.clone(),
411407
)
412-
.theme(if let HumanReadableErrorType::Unicode = je.json_rendered {
413-
OutputTheme::Unicode
414-
} else {
415-
OutputTheme::Ascii
416-
})
417-
.emit_diagnostic(diag, registry);
408+
.theme(OutputTheme::Ascii)
409+
.emit_diagnostic(diag, registry)
418410
}
419411

420412
let buf = Arc::try_unwrap(buf.0).unwrap().into_inner().unwrap();

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

compiler/rustc_session/src/config.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2071,7 +2071,7 @@ pub fn parse_json(early_dcx: &EarlyDiagCtxt, matches: &getopts::Matches) -> Json
20712071
match sub_option {
20722072
"diagnostic-short" => json_rendered = HumanReadableErrorType::Short,
20732073
"diagnostic-unicode" => {
2074-
json_rendered = HumanReadableErrorType::Unicode;
2074+
json_rendered = HumanReadableErrorType::AnnotateSnippet { unicode: true };
20752075
}
20762076
"diagnostic-rendered-ansi" => json_color = ColorConfig::Always,
20772077
"artifacts" => json_artifact_notifications = true,
@@ -2110,7 +2110,7 @@ pub fn parse_error_format(
21102110
match matches.opt_str("error-format").as_deref() {
21112111
None | Some("human") => ErrorOutputType::HumanReadable { color_config, .. },
21122112
Some("human-annotate-rs") => ErrorOutputType::HumanReadable {
2113-
kind: HumanReadableErrorType::AnnotateSnippet,
2113+
kind: HumanReadableErrorType::AnnotateSnippet { unicode: false },
21142114
color_config,
21152115
},
21162116
Some("json") => {
@@ -2123,7 +2123,7 @@ pub fn parse_error_format(
21232123
ErrorOutputType::HumanReadable { kind: HumanReadableErrorType::Short, color_config }
21242124
}
21252125
Some("human-unicode") => ErrorOutputType::HumanReadable {
2126-
kind: HumanReadableErrorType::Unicode,
2126+
kind: HumanReadableErrorType::AnnotateSnippet { unicode: true },
21272127
color_config,
21282128
},
21292129
Some(arg) => {
@@ -2191,8 +2191,8 @@ fn check_error_format_stability(
21912191
let format = match format {
21922192
ErrorOutputType::Json { pretty: true, .. } => "pretty-json",
21932193
ErrorOutputType::HumanReadable { kind, .. } => match kind {
2194-
HumanReadableErrorType::AnnotateSnippet => "human-annotate-rs",
2195-
HumanReadableErrorType::Unicode => "human-unicode",
2194+
HumanReadableErrorType::AnnotateSnippet { unicode: false } => "human-annotate-rs",
2195+
HumanReadableErrorType::AnnotateSnippet { unicode: true } => "human-unicode",
21962196
_ => return,
21972197
},
21982198
_ => return,

compiler/rustc_session/src/session.rs

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -952,8 +952,7 @@ fn default_emitter(
952952
match sopts.error_format {
953953
config::ErrorOutputType::HumanReadable { kind, color_config } => {
954954
let short = kind.short();
955-
956-
if let HumanReadableErrorType::AnnotateSnippet = kind {
955+
if let HumanReadableErrorType::AnnotateSnippet { unicode } = kind {
957956
let emitter =
958957
AnnotateSnippetEmitter::new(stderr_destination(color_config), translator)
959958
.sm(source_map)
@@ -962,11 +961,7 @@ fn default_emitter(
962961
.macro_backtrace(macro_backtrace)
963962
.track_diagnostics(track_diagnostics)
964963
.terminal_url(terminal_url)
965-
.theme(if let HumanReadableErrorType::Unicode = kind {
966-
OutputTheme::Unicode
967-
} else {
968-
OutputTheme::Ascii
969-
})
964+
.theme(if unicode { OutputTheme::Unicode } else { OutputTheme::Ascii })
970965
.ignored_directories_in_source_blocks(
971966
sopts
972967
.unstable_opts
@@ -982,11 +977,7 @@ fn default_emitter(
982977
.macro_backtrace(macro_backtrace)
983978
.track_diagnostics(track_diagnostics)
984979
.terminal_url(terminal_url)
985-
.theme(if let HumanReadableErrorType::Unicode = kind {
986-
OutputTheme::Unicode
987-
} else {
988-
OutputTheme::Ascii
989-
})
980+
.theme(OutputTheme::Ascii)
990981
.ignored_directories_in_source_blocks(
991982
sopts.unstable_opts.ignore_directory_in_diagnostics_source_blocks.clone(),
992983
);
@@ -1501,24 +1492,16 @@ fn mk_emitter(output: ErrorOutputType) -> Box<DynEmitter> {
15011492
let emitter: Box<DynEmitter> = match output {
15021493
config::ErrorOutputType::HumanReadable { kind, color_config } => {
15031494
let short = kind.short();
1504-
if let HumanReadableErrorType::AnnotateSnippet = kind {
1495+
if let HumanReadableErrorType::AnnotateSnippet { unicode } = kind {
15051496
Box::new(
15061497
AnnotateSnippetEmitter::new(stderr_destination(color_config), translator)
1507-
.theme(if let HumanReadableErrorType::Unicode = kind {
1508-
OutputTheme::Unicode
1509-
} else {
1510-
OutputTheme::Ascii
1511-
})
1498+
.theme(if unicode { OutputTheme::Unicode } else { OutputTheme::Ascii })
15121499
.short_message(short),
15131500
)
15141501
} else {
15151502
Box::new(
15161503
HumanEmitter::new(stderr_destination(color_config), translator)
1517-
.theme(if let HumanReadableErrorType::Unicode = kind {
1518-
OutputTheme::Unicode
1519-
} else {
1520-
OutputTheme::Ascii
1521-
})
1504+
.theme(OutputTheme::Ascii)
15221505
.short_message(short),
15231506
)
15241507
}

src/librustdoc/core.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -155,18 +155,14 @@ pub(crate) fn new_dcx(
155155
let emitter: Box<DynEmitter> = match error_format {
156156
ErrorOutputType::HumanReadable { kind, color_config } => {
157157
let short = kind.short();
158-
if let HumanReadableErrorType::AnnotateSnippet = kind {
158+
if let HumanReadableErrorType::AnnotateSnippet { unicode } = kind {
159159
Box::new(
160160
AnnotateSnippetEmitter::new(stderr_destination(color_config), translator)
161161
.sm(source_map.map(|sm| sm as _))
162162
.short_message(short)
163163
.diagnostic_width(diagnostic_width)
164164
.track_diagnostics(unstable_opts.track_diagnostics)
165-
.theme(if let HumanReadableErrorType::Unicode = kind {
166-
OutputTheme::Unicode
167-
} else {
168-
OutputTheme::Ascii
169-
})
165+
.theme(if unicode { OutputTheme::Unicode } else { OutputTheme::Ascii })
170166
.ui_testing(unstable_opts.ui_testing),
171167
)
172168
} else {
@@ -176,11 +172,7 @@ pub(crate) fn new_dcx(
176172
.short_message(short)
177173
.diagnostic_width(diagnostic_width)
178174
.track_diagnostics(unstable_opts.track_diagnostics)
179-
.theme(if let HumanReadableErrorType::Unicode = kind {
180-
OutputTheme::Unicode
181-
} else {
182-
OutputTheme::Ascii
183-
})
175+
.theme(OutputTheme::Ascii)
184176
.ui_testing(unstable_opts.ui_testing),
185177
)
186178
}

src/librustdoc/doctest.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ fn run_test(
599599
]);
600600
if let ErrorOutputType::HumanReadable { kind, color_config } = rustdoc_options.error_format {
601601
let short = kind.short();
602-
let unicode = kind == HumanReadableErrorType::Unicode;
602+
let unicode = kind == HumanReadableErrorType::AnnotateSnippet { unicode: true };
603603

604604
if short {
605605
compiler_args.extend_from_slice(&["--error-format".to_owned(), "short".to_owned()]);

tests/ui/diagnostic-width/non-1-width-unicode-multiline-label.unicode.stderr

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
error[E0369]: cannot add `&str` to `&str`
22
╭▸ $DIR/non-1-width-unicode-multiline-label.rs:8:237
33
4-
LL │ …👧👦👨👩👧👦👨👩👧👦👨👩👧👦👨👩👧👦👨👩👧👦"; let _a = unicode_is_fun + " really fun!";
5-
┬───────────── ┯ ────────────── &str
6-
│ │
7-
│ `+` cannot be used to concatenate two `&str` strings
8-
&str
4+
LL │ … 👧👦👨👩👧👦👨👩👧👦"; let _a = unicode_is_fun + " really fun!";
5+
│ ┬───────────── ┯ ────────────── &str
6+
│ │ │
7+
│ │ `+` cannot be used to concatenate two `&str` strings
8+
│ &str
99
1010
╰ note: string concatenation requires an owned `String` on the left
1111
help: create an owned `String` from a string reference
@@ -16,11 +16,11 @@ LL │ let _ = "👨👩👧👦👨👩👧👦👨👩👧👦👨👩👧
1616
error[E0369]: cannot add `&str` to `&str`
1717
╭▸ $DIR/non-1-width-unicode-multiline-label.rs:10:384
1818
19-
LL │ …👨👩👧👦👨👩👧👦👨👩👧👦"; let _a = unicode_is_fun + " really fun!";
20-
┬───────────── ┯ ────────────── &str
21-
│ │
22-
│ `+` cannot be used to concatenate two `&str` strings
23-
&str
19+
LL │ … 👧👦👨👩👧👦👨👩👧👦"; let _a = unicode_is_fun + " really fun!";
20+
│ ┬───────────── ┯ ────────────── &str
21+
│ │ │
22+
│ │ `+` cannot be used to concatenate two `&str` strings
23+
│ &str
2424
2525
╰ note: string concatenation requires an owned `String` on the left
2626
help: create an owned `String` from a string reference
@@ -31,11 +31,11 @@ LL │ let _ = "👨👩👧👦👨👩👧👦👨👩👧👦👨👩👧
3131
error[E0369]: cannot add `&str` to `&str`
3232
╭▸ $DIR/non-1-width-unicode-multiline-label.rs:12:260
3333
34-
LL │ …࿅࿆࿇࿈࿉࿊࿋࿌࿍࿎࿏࿐࿑࿒࿓࿔࿕࿖࿗࿘࿙࿚"; let _a = unicode_is_fun + " really fun!";
35-
│ ┬───────────── ┯ ────────────── &str
36-
│ │ │
37-
│ │ `+` cannot be used to concatenate two `&str` strings
38-
│ &str
34+
LL │ …࿅࿆࿇࿈࿉࿊࿋࿌࿍࿎࿏࿐࿑࿒࿓࿔࿕࿖࿗࿘࿙࿚"; let _a = unicode_is_fun + " really fun!";
35+
┬───────────── ┯ ────────────── &str
36+
│ │
37+
│ `+` cannot be used to concatenate two `&str` strings
38+
&str
3939
4040
╰ note: string concatenation requires an owned `String` on the left
4141
help: create an owned `String` from a string reference

tests/ui/error-emitter/unicode-output.svg

Lines changed: 1 addition & 1 deletion
Loading

0 commit comments

Comments
 (0)