Skip to content

Commit cdd8055

Browse files
Replace DiagnosticBuilder with Diagnostic when emitting error
1 parent 5670d04 commit cdd8055

File tree

11 files changed

+60
-26
lines changed

11 files changed

+60
-26
lines changed

src/librustc/session/mod.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,6 +1040,7 @@ fn default_emitter(
10401040
source_map: &Lrc<source_map::SourceMap>,
10411041
emitter_dest: Option<Box<dyn Write + Send>>,
10421042
) -> Box<dyn Emitter + sync::Send> {
1043+
let external_macro_backtrace = sopts.debugging_opts.external_macro_backtrace;
10431044
match (sopts.error_format, emitter_dest) {
10441045
(config::ErrorOutputType::HumanReadable(kind), dst) => {
10451046
let (short, color_config) = kind.unzip();
@@ -1048,6 +1049,7 @@ fn default_emitter(
10481049
let emitter = AnnotateSnippetEmitterWriter::new(
10491050
Some(source_map.clone()),
10501051
short,
1052+
external_macro_backtrace,
10511053
);
10521054
Box::new(emitter.ui_testing(sopts.debugging_opts.ui_testing))
10531055
} else {
@@ -1058,6 +1060,7 @@ fn default_emitter(
10581060
short,
10591061
sopts.debugging_opts.teach,
10601062
sopts.debugging_opts.terminal_width,
1063+
external_macro_backtrace,
10611064
),
10621065
Some(dst) => EmitterWriter::new(
10631066
dst,
@@ -1066,6 +1069,7 @@ fn default_emitter(
10661069
false, // no teach messages when writing to a buffer
10671070
false, // no colors when writing to a buffer
10681071
None, // no terminal width
1072+
external_macro_backtrace,
10691073
),
10701074
};
10711075
Box::new(emitter.ui_testing(sopts.debugging_opts.ui_testing))
@@ -1077,6 +1081,7 @@ fn default_emitter(
10771081
source_map.clone(),
10781082
pretty,
10791083
json_rendered,
1084+
external_macro_backtrace,
10801085
).ui_testing(sopts.debugging_opts.ui_testing),
10811086
),
10821087
(config::ErrorOutputType::Json { pretty, json_rendered }, Some(dst)) => Box::new(
@@ -1086,6 +1091,7 @@ fn default_emitter(
10861091
source_map.clone(),
10871092
pretty,
10881093
json_rendered,
1094+
external_macro_backtrace,
10891095
).ui_testing(sopts.debugging_opts.ui_testing),
10901096
),
10911097
}
@@ -1382,10 +1388,10 @@ pub fn early_error(output: config::ErrorOutputType, msg: &str) -> ! {
13821388
let emitter: Box<dyn Emitter + sync::Send> = match output {
13831389
config::ErrorOutputType::HumanReadable(kind) => {
13841390
let (short, color_config) = kind.unzip();
1385-
Box::new(EmitterWriter::stderr(color_config, None, short, false, None))
1391+
Box::new(EmitterWriter::stderr(color_config, None, short, false, None, false))
13861392
}
13871393
config::ErrorOutputType::Json { pretty, json_rendered } =>
1388-
Box::new(JsonEmitter::basic(pretty, json_rendered)),
1394+
Box::new(JsonEmitter::basic(pretty, json_rendered, false)),
13891395
};
13901396
let handler = errors::Handler::with_emitter(true, None, emitter);
13911397
handler.emit(&MultiSpan::new(), msg, errors::Level::Fatal);
@@ -1396,10 +1402,10 @@ pub fn early_warn(output: config::ErrorOutputType, msg: &str) {
13961402
let emitter: Box<dyn Emitter + sync::Send> = match output {
13971403
config::ErrorOutputType::HumanReadable(kind) => {
13981404
let (short, color_config) = kind.unzip();
1399-
Box::new(EmitterWriter::stderr(color_config, None, short, false, None))
1405+
Box::new(EmitterWriter::stderr(color_config, None, short, false, None, false))
14001406
}
14011407
config::ErrorOutputType::Json { pretty, json_rendered } =>
1402-
Box::new(JsonEmitter::basic(pretty, json_rendered)),
1408+
Box::new(JsonEmitter::basic(pretty, json_rendered, false)),
14031409
};
14041410
let handler = errors::Handler::with_emitter(true, None, emitter);
14051411
handler.emit(&MultiSpan::new(), msg, errors::Level::Warning);

src/librustc_codegen_ssa/back/write.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use rustc::util::common::{time_depth, set_time_depth, print_time_passes_entry};
2222
use rustc::util::profiling::SelfProfiler;
2323
use rustc_fs_util::link_or_copy;
2424
use rustc_data_structures::svh::Svh;
25-
use rustc_errors::{Handler, Level, DiagnosticBuilder, FatalError, DiagnosticId};
25+
use rustc_errors::{Handler, Level, FatalError, DiagnosticId};
2626
use rustc_errors::emitter::{Emitter};
2727
use rustc_target::spec::MergeFunctions;
2828
use syntax::attr;
@@ -1725,7 +1725,7 @@ impl SharedEmitter {
17251725
}
17261726

17271727
impl Emitter for SharedEmitter {
1728-
fn emit_diagnostic(&mut self, db: &DiagnosticBuilder<'_>) {
1728+
fn emit_diagnostic(&mut self, db: &rustc_errors::Diagnostic) {
17291729
drop(self.sender.send(SharedEmitterMessage::Diagnostic(Diagnostic {
17301730
msg: db.message(),
17311731
code: db.code.clone(),

src/librustc_driver/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1196,6 +1196,7 @@ pub fn report_ice(info: &panic::PanicInfo<'_>, bug_report_url: &str) {
11961196
false,
11971197
false,
11981198
None,
1199+
false,
11991200
));
12001201
let handler = errors::Handler::with_emitter(true, None, emitter);
12011202

src/librustc_errors/annotate_snippet_emitter_writer.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
88
use syntax_pos::{SourceFile, MultiSpan, Loc};
99
use crate::{
10-
Level, CodeSuggestion, DiagnosticBuilder, Emitter,
10+
Level, CodeSuggestion, Diagnostic, Emitter,
1111
SourceMapperDyn, SubDiagnostic, DiagnosticId
1212
};
1313
use crate::emitter::FileWithAnnotatedLines;
@@ -25,19 +25,21 @@ pub struct AnnotateSnippetEmitterWriter {
2525
short_message: bool,
2626
/// If true, will normalize line numbers with `LL` to prevent noise in UI test diffs.
2727
ui_testing: bool,
28+
29+
external_macro_backtrace: bool,
2830
}
2931

3032
impl Emitter for AnnotateSnippetEmitterWriter {
3133
/// The entry point for the diagnostics generation
32-
fn emit_diagnostic(&mut self, db: &DiagnosticBuilder<'_>) {
34+
fn emit_diagnostic(&mut self, db: &Diagnostic) {
3335
let mut children = db.children.clone();
3436
let (mut primary_span, suggestions) = self.primary_span_formatted(&db);
3537

3638
self.fix_multispans_in_std_macros(&self.source_map,
3739
&mut primary_span,
3840
&mut children,
3941
&db.level,
40-
db.handler().flags.external_macro_backtrace);
42+
self.external_macro_backtrace);
4143

4244
self.emit_messages_default(&db.level,
4345
db.message(),
@@ -163,12 +165,14 @@ impl<'a> DiagnosticConverter<'a> {
163165
impl AnnotateSnippetEmitterWriter {
164166
pub fn new(
165167
source_map: Option<Lrc<SourceMapperDyn>>,
166-
short_message: bool
168+
short_message: bool,
169+
external_macro_backtrace: bool,
167170
) -> Self {
168171
Self {
169172
source_map,
170173
short_message,
171174
ui_testing: false,
175+
external_macro_backtrace,
172176
}
173177
}
174178

src/librustc_errors/emitter.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use Destination::*;
1212
use syntax_pos::{SourceFile, Span, MultiSpan};
1313

1414
use crate::{
15-
Level, CodeSuggestion, DiagnosticBuilder, SubDiagnostic,
15+
Level, CodeSuggestion, Diagnostic, SubDiagnostic,
1616
SuggestionStyle, SourceMapperDyn, DiagnosticId,
1717
};
1818
use crate::Level::Error;
@@ -52,10 +52,12 @@ impl HumanReadableErrorType {
5252
source_map: Option<Lrc<SourceMapperDyn>>,
5353
teach: bool,
5454
terminal_width: Option<usize>,
55+
external_macro_backtrace: bool,
5556
) -> EmitterWriter {
5657
let (short, color_config) = self.unzip();
5758
let color = color_config.suggests_using_colors();
58-
EmitterWriter::new(dst, source_map, short, teach, color, terminal_width)
59+
EmitterWriter::new(dst, source_map, short, teach, color, terminal_width,
60+
external_macro_backtrace)
5961
}
6062
}
6163

@@ -180,7 +182,7 @@ const ANONYMIZED_LINE_NUM: &str = "LL";
180182
/// Emitter trait for emitting errors.
181183
pub trait Emitter {
182184
/// Emit a structured diagnostic.
183-
fn emit_diagnostic(&mut self, db: &DiagnosticBuilder<'_>);
185+
fn emit_diagnostic(&mut self, db: &Diagnostic);
184186

185187
/// Emit a notification that an artifact has been output.
186188
/// This is currently only supported for the JSON format,
@@ -204,7 +206,7 @@ pub trait Emitter {
204206
/// we return the original `primary_span` and the original suggestions.
205207
fn primary_span_formatted<'a>(
206208
&mut self,
207-
db: &'a DiagnosticBuilder<'_>
209+
db: &'a Diagnostic
208210
) -> (MultiSpan, &'a [CodeSuggestion]) {
209211
let mut primary_span = db.span.clone();
210212
if let Some((sugg, rest)) = db.suggestions.split_first() {
@@ -377,15 +379,15 @@ pub trait Emitter {
377379
}
378380

379381
impl Emitter for EmitterWriter {
380-
fn emit_diagnostic(&mut self, db: &DiagnosticBuilder<'_>) {
382+
fn emit_diagnostic(&mut self, db: &Diagnostic) {
381383
let mut children = db.children.clone();
382384
let (mut primary_span, suggestions) = self.primary_span_formatted(&db);
383385

384386
self.fix_multispans_in_std_macros(&self.sm,
385387
&mut primary_span,
386388
&mut children,
387389
&db.level,
388-
db.handler().flags.external_macro_backtrace);
390+
self.external_macro_backtrace);
389391

390392
self.emit_messages_default(&db.level,
391393
&db.styled_message(),
@@ -449,6 +451,8 @@ pub struct EmitterWriter {
449451
teach: bool,
450452
ui_testing: bool,
451453
terminal_width: Option<usize>,
454+
455+
external_macro_backtrace: bool,
452456
}
453457

454458
#[derive(Debug)]
@@ -465,6 +469,7 @@ impl EmitterWriter {
465469
short_message: bool,
466470
teach: bool,
467471
terminal_width: Option<usize>,
472+
external_macro_backtrace: bool,
468473
) -> EmitterWriter {
469474
let dst = Destination::from_stderr(color_config);
470475
EmitterWriter {
@@ -474,6 +479,7 @@ impl EmitterWriter {
474479
teach,
475480
ui_testing: false,
476481
terminal_width,
482+
external_macro_backtrace,
477483
}
478484
}
479485

@@ -484,6 +490,7 @@ impl EmitterWriter {
484490
teach: bool,
485491
colored: bool,
486492
terminal_width: Option<usize>,
493+
external_macro_backtrace: bool,
487494
) -> EmitterWriter {
488495
EmitterWriter {
489496
dst: Raw(dst, colored),
@@ -492,6 +499,7 @@ impl EmitterWriter {
492499
teach,
493500
ui_testing: false,
494501
terminal_width,
502+
external_macro_backtrace,
495503
}
496504
}
497505

src/librustc_errors/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,8 @@ impl Handler {
383383
cm: Option<Lrc<SourceMapperDyn>>,
384384
flags: HandlerFlags)
385385
-> Handler {
386-
let emitter = Box::new(EmitterWriter::stderr(color_config, cm, false, false, None));
386+
let emitter = Box::new(EmitterWriter::stderr(
387+
color_config, cm, false, false, None, flags.external_macro_backtrace));
387388
Handler::with_emitter_and_flags(emitter, flags)
388389
}
389390

src/librustdoc/core.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ pub fn new_handler(error_format: ErrorOutputType,
193193
short,
194194
sessopts.debugging_opts.teach,
195195
sessopts.debugging_opts.terminal_width,
196+
false,
196197
).ui_testing(ui_testing)
197198
)
198199
},
@@ -205,6 +206,7 @@ pub fn new_handler(error_format: ErrorOutputType,
205206
source_map,
206207
pretty,
207208
json_rendered,
209+
false,
208210
).ui_testing(ui_testing)
209211
)
210212
},

src/librustdoc/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ pub fn make_test(s: &str,
401401
// Any errors in parsing should also appear when the doctest is compiled for real, so just
402402
// send all the errors that libsyntax emits directly into a `Sink` instead of stderr.
403403
let cm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
404-
let emitter = EmitterWriter::new(box io::sink(), None, false, false, false, None);
404+
let emitter = EmitterWriter::new(box io::sink(), None, false, false, false, None, false);
405405
// FIXME(misdreavus): pass `-Z treat-err-as-bug` to the doctest parser
406406
let handler = Handler::with_emitter(false, None, box emitter);
407407
let sess = ParseSess::with_span_handler(handler, cm);

src/libsyntax/json.rs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use crate::source_map::{SourceMap, FilePathMapping};
1313

1414
use errors::registry::Registry;
15-
use errors::{DiagnosticBuilder, SubDiagnostic, CodeSuggestion, SourceMapper};
15+
use errors::{SubDiagnostic, CodeSuggestion, SourceMapper};
1616
use errors::{DiagnosticId, Applicability};
1717
use errors::emitter::{Emitter, HumanReadableErrorType};
1818

@@ -32,6 +32,7 @@ pub struct JsonEmitter {
3232
pretty: bool,
3333
ui_testing: bool,
3434
json_rendered: HumanReadableErrorType,
35+
external_macro_backtrace: bool,
3536
}
3637

3738
impl JsonEmitter {
@@ -40,6 +41,7 @@ impl JsonEmitter {
4041
source_map: Lrc<SourceMap>,
4142
pretty: bool,
4243
json_rendered: HumanReadableErrorType,
44+
external_macro_backtrace: bool,
4345
) -> JsonEmitter {
4446
JsonEmitter {
4547
dst: Box::new(io::stderr()),
@@ -48,13 +50,18 @@ impl JsonEmitter {
4850
pretty,
4951
ui_testing: false,
5052
json_rendered,
53+
external_macro_backtrace,
5154
}
5255
}
5356

54-
pub fn basic(pretty: bool, json_rendered: HumanReadableErrorType) -> JsonEmitter {
57+
pub fn basic(
58+
pretty: bool,
59+
json_rendered: HumanReadableErrorType,
60+
external_macro_backtrace: bool,
61+
) -> JsonEmitter {
5562
let file_path_mapping = FilePathMapping::empty();
5663
JsonEmitter::stderr(None, Lrc::new(SourceMap::new(file_path_mapping)),
57-
pretty, json_rendered)
64+
pretty, json_rendered, external_macro_backtrace)
5865
}
5966

6067
pub fn new(
@@ -63,6 +70,7 @@ impl JsonEmitter {
6370
source_map: Lrc<SourceMap>,
6471
pretty: bool,
6572
json_rendered: HumanReadableErrorType,
73+
external_macro_backtrace: bool,
6674
) -> JsonEmitter {
6775
JsonEmitter {
6876
dst,
@@ -71,6 +79,7 @@ impl JsonEmitter {
7179
pretty,
7280
ui_testing: false,
7381
json_rendered,
82+
external_macro_backtrace,
7483
}
7584
}
7685

@@ -80,8 +89,8 @@ impl JsonEmitter {
8089
}
8190

8291
impl Emitter for JsonEmitter {
83-
fn emit_diagnostic(&mut self, db: &DiagnosticBuilder<'_>) {
84-
let data = Diagnostic::from_diagnostic_builder(db, self);
92+
fn emit_diagnostic(&mut self, db: &errors::Diagnostic) {
93+
let data = Diagnostic::from_errors_diagnostic(db, self);
8594
let result = if self.pretty {
8695
writeln!(&mut self.dst, "{}", as_pretty_json(&data))
8796
} else {
@@ -189,7 +198,7 @@ struct ArtifactNotification<'a> {
189198
}
190199

191200
impl Diagnostic {
192-
fn from_diagnostic_builder(db: &DiagnosticBuilder<'_>,
201+
fn from_errors_diagnostic(db: &errors::Diagnostic,
193202
je: &JsonEmitter)
194203
-> Diagnostic {
195204
let sugg = db.suggestions.iter().map(|sugg| {
@@ -219,8 +228,9 @@ impl Diagnostic {
219228
}
220229
let buf = BufWriter::default();
221230
let output = buf.clone();
222-
je.json_rendered.new_emitter(Box::new(buf), Some(je.sm.clone()), false, None)
223-
.ui_testing(je.ui_testing).emit_diagnostic(db);
231+
je.json_rendered.new_emitter(
232+
Box::new(buf), Some(je.sm.clone()), false, None, je.external_macro_backtrace
233+
).ui_testing(je.ui_testing).emit_diagnostic(db);
224234
let output = Arc::try_unwrap(output.0).unwrap().into_inner().unwrap();
225235
let output = String::from_utf8(output).unwrap();
226236

src/libsyntax/parse/lexer/tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ fn mk_sess(sm: Lrc<SourceMap>) -> ParseSess {
1818
false,
1919
false,
2020
None,
21+
false,
2122
);
2223
ParseSess::with_span_handler(Handler::with_emitter(true, None, Box::new(emitter)), sm)
2324
}

src/libsyntax/tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ fn test_harness(file_text: &str, span_labels: Vec<SpanLabel>, expected_output: &
147147
false,
148148
false,
149149
None,
150+
false,
150151
);
151152
let handler = Handler::with_emitter(true, None, Box::new(emitter));
152153
handler.span_err(msp, "foo");

0 commit comments

Comments
 (0)