Skip to content

Commit ae0c8b5

Browse files
authored
Rollup merge of #66427 - Mark-Simulacrum:errors-json, r=Centril
Move the JSON error emitter to librustc_errors This is done both as a cleanup (it makes little sense for this emitter to be in libsyntax), but also as part of broader work to decouple Session from librustc itself. Along the way, this also moves SourceMap to syntax_pos, which is also nice for the above reasons, as well as allowing dropping the SourceMapper trait from code. This had the unfortunate side-effect of moving `FatalError` to rustc_data_structures (it's needed in syntax_pos, due to SourceMap, but putting it there feels somehow worse).
2 parents 4e6e1ec + c31a875 commit ae0c8b5

File tree

16 files changed

+99
-134
lines changed

16 files changed

+99
-134
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -4439,6 +4439,7 @@ version = "0.0.0"
44394439
dependencies = [
44404440
"arena",
44414441
"cfg-if",
4442+
"log",
44424443
"rustc_data_structures",
44434444
"rustc_index",
44444445
"rustc_macros",

src/librustc/session/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use errors::emitter::HumanReadableErrorType;
2222
use errors::annotate_snippet_emitter_writer::{AnnotateSnippetEmitterWriter};
2323
use syntax::edition::Edition;
2424
use syntax::feature_gate::{self, AttributeType};
25-
use syntax::json::JsonEmitter;
25+
use errors::json::JsonEmitter;
2626
use syntax::source_map;
2727
use syntax::sess::{ParseSess, ProcessCfgMod};
2828
use syntax::symbol::Symbol;

src/librustc_codegen_ssa/back/write.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ use rustc_data_structures::profiling::SelfProfilerRef;
2323
use rustc_fs_util::link_or_copy;
2424
use rustc_data_structures::svh::Svh;
2525
use rustc_data_structures::sync::Lrc;
26-
use rustc_errors::{Handler, Level, FatalError, DiagnosticId, SourceMapperDyn};
26+
use rustc_errors::{Handler, Level, FatalError, DiagnosticId};
27+
use syntax_pos::source_map::SourceMap;
2728
use rustc_errors::emitter::{Emitter};
2829
use rustc_target::spec::MergeFunctions;
2930
use syntax::attr;
@@ -1679,7 +1680,7 @@ impl Emitter for SharedEmitter {
16791680
}
16801681
drop(self.sender.send(SharedEmitterMessage::AbortIfErrors));
16811682
}
1682-
fn source_map(&self) -> Option<&Lrc<SourceMapperDyn>> {
1683+
fn source_map(&self) -> Option<&Lrc<SourceMap>> {
16831684
None
16841685
}
16851686
}

src/librustc_errors/annotate_snippet_emitter_writer.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
//! [annotate_snippets]: https://docs.rs/crate/annotate-snippets/
77
88
use syntax_pos::{SourceFile, MultiSpan, Loc};
9+
use syntax_pos::source_map::SourceMap;
910
use crate::{
1011
Level, CodeSuggestion, Diagnostic, Emitter,
11-
SourceMapperDyn, SubDiagnostic, DiagnosticId
12+
SubDiagnostic, DiagnosticId
1213
};
1314
use crate::emitter::FileWithAnnotatedLines;
1415
use rustc_data_structures::sync::Lrc;
@@ -20,7 +21,7 @@ use annotate_snippets::formatter::DisplayListFormatter;
2021

2122
/// Generates diagnostics using annotate-snippet
2223
pub struct AnnotateSnippetEmitterWriter {
23-
source_map: Option<Lrc<SourceMapperDyn>>,
24+
source_map: Option<Lrc<SourceMap>>,
2425
/// If true, hides the longer explanation text
2526
short_message: bool,
2627
/// If true, will normalize line numbers with `LL` to prevent noise in UI test diffs.
@@ -49,7 +50,7 @@ impl Emitter for AnnotateSnippetEmitterWriter {
4950
&suggestions);
5051
}
5152

52-
fn source_map(&self) -> Option<&Lrc<SourceMapperDyn>> {
53+
fn source_map(&self) -> Option<&Lrc<SourceMap>> {
5354
self.source_map.as_ref()
5455
}
5556

@@ -61,7 +62,7 @@ impl Emitter for AnnotateSnippetEmitterWriter {
6162
/// Collects all the data needed to generate the data structures needed for the
6263
/// `annotate-snippets` library.
6364
struct DiagnosticConverter<'a> {
64-
source_map: Option<Lrc<SourceMapperDyn>>,
65+
source_map: Option<Lrc<SourceMap>>,
6566
level: Level,
6667
message: String,
6768
code: Option<DiagnosticId>,
@@ -168,7 +169,7 @@ impl<'a> DiagnosticConverter<'a> {
168169

169170
impl AnnotateSnippetEmitterWriter {
170171
pub fn new(
171-
source_map: Option<Lrc<SourceMapperDyn>>,
172+
source_map: Option<Lrc<SourceMap>>,
172173
short_message: bool,
173174
external_macro_backtrace: bool,
174175
) -> Self {

src/librustc_errors/emitter.rs

+13-12
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@
1010
use Destination::*;
1111

1212
use syntax_pos::{SourceFile, Span, MultiSpan};
13+
use syntax_pos::source_map::SourceMap;
1314

1415
use crate::{
1516
Level, CodeSuggestion, Diagnostic, SubDiagnostic, pluralize,
16-
SuggestionStyle, SourceMapper, SourceMapperDyn, DiagnosticId,
17+
SuggestionStyle, DiagnosticId,
1718
};
1819
use crate::Level::Error;
1920
use crate::snippet::{Annotation, AnnotationType, Line, MultilineAnnotation, StyledString, Style};
@@ -49,7 +50,7 @@ impl HumanReadableErrorType {
4950
pub fn new_emitter(
5051
self,
5152
dst: Box<dyn Write + Send>,
52-
source_map: Option<Lrc<SourceMapperDyn>>,
53+
source_map: Option<Lrc<SourceMap>>,
5354
teach: bool,
5455
terminal_width: Option<usize>,
5556
external_macro_backtrace: bool,
@@ -192,7 +193,7 @@ pub trait Emitter {
192193
true
193194
}
194195

195-
fn source_map(&self) -> Option<&Lrc<SourceMapperDyn>>;
196+
fn source_map(&self) -> Option<&Lrc<SourceMap>>;
196197

197198
/// Formats the substitutions of the primary_span
198199
///
@@ -271,7 +272,7 @@ pub trait Emitter {
271272
// point directly at <*macros>. Since these are often difficult to read, this
272273
// will change the span to point at the use site.
273274
fn fix_multispans_in_std_macros(&self,
274-
source_map: &Option<Lrc<SourceMapperDyn>>,
275+
source_map: &Option<Lrc<SourceMap>>,
275276
span: &mut MultiSpan,
276277
children: &mut Vec<SubDiagnostic>,
277278
level: &Level,
@@ -311,7 +312,7 @@ pub trait Emitter {
311312
// <*macros>. Since these locations are often difficult to read, we move these Spans from
312313
// <*macros> to their corresponding use site.
313314
fn fix_multispan_in_std_macros(&self,
314-
source_map: &Option<Lrc<SourceMapperDyn>>,
315+
source_map: &Option<Lrc<SourceMap>>,
315316
span: &mut MultiSpan,
316317
always_backtrace: bool) -> bool {
317318
let sm = match source_map {
@@ -397,7 +398,7 @@ pub trait Emitter {
397398
}
398399

399400
impl Emitter for EmitterWriter {
400-
fn source_map(&self) -> Option<&Lrc<SourceMapperDyn>> {
401+
fn source_map(&self) -> Option<&Lrc<SourceMap>> {
401402
self.sm.as_ref()
402403
}
403404

@@ -428,7 +429,7 @@ impl Emitter for EmitterWriter {
428429
pub struct SilentEmitter;
429430

430431
impl Emitter for SilentEmitter {
431-
fn source_map(&self) -> Option<&Lrc<SourceMapperDyn>> { None }
432+
fn source_map(&self) -> Option<&Lrc<SourceMap>> { None }
432433
fn emit_diagnostic(&mut self, _: &Diagnostic) {}
433434
}
434435

@@ -476,7 +477,7 @@ impl ColorConfig {
476477
/// Handles the writing of `HumanReadableErrorType::Default` and `HumanReadableErrorType::Short`
477478
pub struct EmitterWriter {
478479
dst: Destination,
479-
sm: Option<Lrc<SourceMapperDyn>>,
480+
sm: Option<Lrc<SourceMap>>,
480481
short_message: bool,
481482
teach: bool,
482483
ui_testing: bool,
@@ -495,7 +496,7 @@ pub struct FileWithAnnotatedLines {
495496
impl EmitterWriter {
496497
pub fn stderr(
497498
color_config: ColorConfig,
498-
source_map: Option<Lrc<SourceMapperDyn>>,
499+
source_map: Option<Lrc<SourceMap>>,
499500
short_message: bool,
500501
teach: bool,
501502
terminal_width: Option<usize>,
@@ -515,7 +516,7 @@ impl EmitterWriter {
515516

516517
pub fn new(
517518
dst: Box<dyn Write + Send>,
518-
source_map: Option<Lrc<SourceMapperDyn>>,
519+
source_map: Option<Lrc<SourceMap>>,
519520
short_message: bool,
520521
teach: bool,
521522
colored: bool,
@@ -1685,7 +1686,7 @@ impl FileWithAnnotatedLines {
16851686
/// This helps us quickly iterate over the whole message (including secondary file spans)
16861687
pub fn collect_annotations(
16871688
msp: &MultiSpan,
1688-
source_map: &Option<Lrc<SourceMapperDyn>>
1689+
source_map: &Option<Lrc<SourceMap>>
16891690
) -> Vec<FileWithAnnotatedLines> {
16901691
fn add_annotation_to_file(file_vec: &mut Vec<FileWithAnnotatedLines>,
16911692
file: Lrc<SourceFile>,
@@ -2067,7 +2068,7 @@ impl<'a> Drop for WritableDst<'a> {
20672068
}
20682069

20692070
/// Whether the original and suggested code are visually similar enough to warrant extra wording.
2070-
pub fn is_case_difference(sm: &dyn SourceMapper, suggested: &str, sp: Span) -> bool {
2071+
pub fn is_case_difference(sm: &SourceMap, suggested: &str, sp: Span) -> bool {
20712072
// FIXME: this should probably be extended to also account for `FO0` → `FOO` and unicode.
20722073
let found = sm.span_to_snippet(sp).unwrap();
20732074
let ascii_confusables = &['c', 'f', 'i', 'k', 'o', 's', 'u', 'v', 'w', 'x', 'y', 'z'];

src/libsyntax/json.rs src/librustc_errors/json.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99
1010
// FIXME: spec the JSON output properly.
1111

12-
use crate::source_map::{SourceMap, FilePathMapping};
12+
use syntax_pos::source_map::{SourceMap, FilePathMapping};
1313

14-
use errors::registry::Registry;
15-
use errors::{SubDiagnostic, CodeSuggestion, SourceMapper, SourceMapperDyn};
16-
use errors::{DiagnosticId, Applicability};
17-
use errors::emitter::{Emitter, HumanReadableErrorType};
14+
use crate::registry::Registry;
15+
use crate::{SubDiagnostic, CodeSuggestion};
16+
use crate::{DiagnosticId, Applicability};
17+
use crate::emitter::{Emitter, HumanReadableErrorType};
1818

1919
use syntax_pos::{MacroBacktrace, Span, SpanLabel, MultiSpan};
20-
use rustc_data_structures::sync::{self, Lrc};
20+
use rustc_data_structures::sync::Lrc;
2121
use std::io::{self, Write};
2222
use std::path::Path;
2323
use std::vec;
@@ -31,7 +31,7 @@ mod tests;
3131
pub struct JsonEmitter {
3232
dst: Box<dyn Write + Send>,
3333
registry: Option<Registry>,
34-
sm: Lrc<dyn SourceMapper + sync::Send + sync::Sync>,
34+
sm: Lrc<SourceMap>,
3535
pretty: bool,
3636
ui_testing: bool,
3737
json_rendered: HumanReadableErrorType,
@@ -92,7 +92,7 @@ impl JsonEmitter {
9292
}
9393

9494
impl Emitter for JsonEmitter {
95-
fn emit_diagnostic(&mut self, diag: &errors::Diagnostic) {
95+
fn emit_diagnostic(&mut self, diag: &crate::Diagnostic) {
9696
let data = Diagnostic::from_errors_diagnostic(diag, self);
9797
let result = if self.pretty {
9898
writeln!(&mut self.dst, "{}", as_pretty_json(&data))
@@ -116,7 +116,7 @@ impl Emitter for JsonEmitter {
116116
}
117117
}
118118

119-
fn source_map(&self) -> Option<&Lrc<SourceMapperDyn>> {
119+
fn source_map(&self) -> Option<&Lrc<SourceMap>> {
120120
Some(&self.sm)
121121
}
122122

@@ -212,7 +212,7 @@ struct ArtifactNotification<'a> {
212212
}
213213

214214
impl Diagnostic {
215-
fn from_errors_diagnostic(diag: &errors::Diagnostic,
215+
fn from_errors_diagnostic(diag: &crate::Diagnostic,
216216
je: &JsonEmitter)
217217
-> Diagnostic {
218218
let sugg = diag.suggestions.iter().map(|sugg| {

src/libsyntax/json/tests.rs src/librustc_errors/json/tests.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
use super::*;
22

33
use crate::json::JsonEmitter;
4-
use crate::source_map::{FilePathMapping, SourceMap};
5-
use crate::with_default_globals;
4+
use syntax_pos::source_map::{FilePathMapping, SourceMap};
65

7-
use errors::emitter::{ColorConfig, HumanReadableErrorType};
8-
use errors::Handler;
6+
use crate::emitter::{ColorConfig, HumanReadableErrorType};
7+
use crate::Handler;
98
use rustc_serialize::json::decode;
109
use syntax_pos::{BytePos, Span};
1110

@@ -40,6 +39,13 @@ impl<T: Write> Write for Shared<T> {
4039
}
4140
}
4241

42+
fn with_default_globals(f: impl FnOnce()) {
43+
let globals = syntax_pos::Globals::new(syntax_pos::edition::DEFAULT_EDITION);
44+
syntax_pos::GLOBALS.set(&globals, || {
45+
syntax_pos::GLOBALS.set(&globals, f)
46+
})
47+
}
48+
4349
/// Test the span yields correct positions in JSON.
4450
fn test_positions(code: &str, span: (u32, u32), expected_output: SpanTestData) {
4551
let expected_output = TestData { spans: vec![expected_output] };

src/librustc_errors/lib.rs

+7-58
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ use registry::Registry;
1818
use rustc_data_structures::sync::{self, Lrc, Lock};
1919
use rustc_data_structures::fx::{FxHashSet, FxIndexMap};
2020
use rustc_data_structures::stable_hasher::StableHasher;
21+
use syntax_pos::source_map::SourceMap;
22+
use syntax_pos::{Loc, Span, MultiSpan};
2123

2224
use std::borrow::Cow;
2325
use std::cell::Cell;
@@ -35,17 +37,7 @@ mod snippet;
3537
pub mod registry;
3638
mod styled_buffer;
3739
mod lock;
38-
39-
use syntax_pos::{
40-
BytePos,
41-
FileLinesResult,
42-
FileName,
43-
Loc,
44-
MultiSpan,
45-
SourceFile,
46-
Span,
47-
SpanSnippetError,
48-
};
40+
pub mod json;
4941

5042
pub type PResult<'a, T> = Result<T, DiagnosticBuilder<'a>>;
5143

@@ -150,26 +142,12 @@ pub struct SubstitutionPart {
150142
pub snippet: String,
151143
}
152144

153-
pub type SourceMapperDyn = dyn SourceMapper + sync::Send + sync::Sync;
154-
155-
pub trait SourceMapper {
156-
fn lookup_char_pos(&self, pos: BytePos) -> Loc;
157-
fn span_to_lines(&self, sp: Span) -> FileLinesResult;
158-
fn span_to_string(&self, sp: Span) -> String;
159-
fn span_to_snippet(&self, sp: Span) -> Result<String, SpanSnippetError>;
160-
fn span_to_filename(&self, sp: Span) -> FileName;
161-
fn merge_spans(&self, sp_lhs: Span, sp_rhs: Span) -> Option<Span>;
162-
fn call_span_if_macro(&self, sp: Span) -> Span;
163-
fn ensure_source_file_source_present(&self, source_file: Lrc<SourceFile>) -> bool;
164-
fn doctest_offset_line(&self, file: &FileName, line: usize) -> usize;
165-
}
166-
167145
impl CodeSuggestion {
168146
/// Returns the assembled code suggestions, whether they should be shown with an underline
169147
/// and whether the substitution only differs in capitalization.
170148
pub fn splice_lines(
171149
&self,
172-
cm: &SourceMapperDyn,
150+
cm: &SourceMap,
173151
) -> Vec<(String, Vec<SubstitutionPart>, bool)> {
174152
use syntax_pos::{CharPos, Pos};
175153

@@ -259,36 +237,7 @@ impl CodeSuggestion {
259237
}
260238
}
261239

262-
/// Used as a return value to signify a fatal error occurred. (It is also
263-
/// used as the argument to panic at the moment, but that will eventually
264-
/// not be true.)
265-
#[derive(Copy, Clone, Debug)]
266-
#[must_use]
267-
pub struct FatalError;
268-
269-
pub struct FatalErrorMarker;
270-
271-
// Don't implement Send on FatalError. This makes it impossible to panic!(FatalError).
272-
// We don't want to invoke the panic handler and print a backtrace for fatal errors.
273-
impl !Send for FatalError {}
274-
275-
impl FatalError {
276-
pub fn raise(self) -> ! {
277-
panic::resume_unwind(Box::new(FatalErrorMarker))
278-
}
279-
}
280-
281-
impl fmt::Display for FatalError {
282-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
283-
write!(f, "parser fatal error")
284-
}
285-
}
286-
287-
impl error::Error for FatalError {
288-
fn description(&self) -> &str {
289-
"The parser has encountered a fatal error"
290-
}
291-
}
240+
pub use syntax_pos::fatal_error::{FatalError, FatalErrorMarker};
292241

293242
/// Signifies that the compiler died with an explicit call to `.bug`
294243
/// or `.span_bug` rather than a failed assertion, etc.
@@ -405,7 +354,7 @@ impl Handler {
405354
color_config: ColorConfig,
406355
can_emit_warnings: bool,
407356
treat_err_as_bug: Option<usize>,
408-
cm: Option<Lrc<SourceMapperDyn>>,
357+
cm: Option<Lrc<SourceMap>>,
409358
) -> Self {
410359
Self::with_tty_emitter_and_flags(
411360
color_config,
@@ -420,7 +369,7 @@ impl Handler {
420369

421370
pub fn with_tty_emitter_and_flags(
422371
color_config: ColorConfig,
423-
cm: Option<Lrc<SourceMapperDyn>>,
372+
cm: Option<Lrc<SourceMap>>,
424373
flags: HandlerFlags,
425374
) -> Self {
426375
let emitter = Box::new(EmitterWriter::stderr(

0 commit comments

Comments
 (0)