Skip to content

Commit 3f93ffc

Browse files
committedNov 15, 2019
Remove SourceMapper trait
SourceMap is now in the root of all rustc-specific crates, syntax_pos, so there's no need for the trait object to decouple the dependencies between librustc_errors and libsyntax as was needed previously.
1 parent 942f0a6 commit 3f93ffc

File tree

6 files changed

+38
-84
lines changed

6 files changed

+38
-84
lines changed
 

‎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/librustc_errors/lib.rs

+5-28
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;
@@ -36,17 +38,6 @@ pub mod registry;
3638
mod styled_buffer;
3739
mod lock;
3840

39-
use syntax_pos::{
40-
BytePos,
41-
FileLinesResult,
42-
FileName,
43-
Loc,
44-
MultiSpan,
45-
SourceFile,
46-
Span,
47-
SpanSnippetError,
48-
};
49-
5041
pub type PResult<'a, T> = Result<T, DiagnosticBuilder<'a>>;
5142

5243
// `PResult` is used a lot. Make sure it doesn't unintentionally get bigger.
@@ -150,26 +141,12 @@ pub struct SubstitutionPart {
150141
pub snippet: String,
151142
}
152143

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-
167144
impl CodeSuggestion {
168145
/// Returns the assembled code suggestions, whether they should be shown with an underline
169146
/// and whether the substitution only differs in capitalization.
170147
pub fn splice_lines(
171148
&self,
172-
cm: &SourceMapperDyn,
149+
cm: &SourceMap,
173150
) -> Vec<(String, Vec<SubstitutionPart>, bool)> {
174151
use syntax_pos::{CharPos, Pos};
175152

@@ -376,7 +353,7 @@ impl Handler {
376353
color_config: ColorConfig,
377354
can_emit_warnings: bool,
378355
treat_err_as_bug: Option<usize>,
379-
cm: Option<Lrc<SourceMapperDyn>>,
356+
cm: Option<Lrc<SourceMap>>,
380357
) -> Self {
381358
Self::with_tty_emitter_and_flags(
382359
color_config,
@@ -391,7 +368,7 @@ impl Handler {
391368

392369
pub fn with_tty_emitter_and_flags(
393370
color_config: ColorConfig,
394-
cm: Option<Lrc<SourceMapperDyn>>,
371+
cm: Option<Lrc<SourceMap>>,
395372
flags: HandlerFlags,
396373
) -> Self {
397374
let emitter = Box::new(EmitterWriter::stderr(

‎src/libsyntax/json.rs

+3-3
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::{SubDiagnostic, CodeSuggestion, SourceMapper, SourceMapperDyn};
15+
use errors::{SubDiagnostic, CodeSuggestion};
1616
use errors::{DiagnosticId, Applicability};
1717
use errors::emitter::{Emitter, HumanReadableErrorType};
1818

@@ -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,
@@ -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

‎src/libsyntax_pos/source_map.rs

+8-34
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ use std::fs;
2222
use std::io;
2323
use log::debug;
2424

25-
use errors::SourceMapper;
26-
2725
#[cfg(test)]
2826
mod tests;
2927

@@ -956,28 +954,15 @@ impl SourceMap {
956954

957955
None
958956
}
959-
}
960-
961-
impl SourceMapper for SourceMap {
962-
fn lookup_char_pos(&self, pos: BytePos) -> Loc {
963-
self.lookup_char_pos(pos)
964-
}
965-
fn span_to_lines(&self, sp: Span) -> FileLinesResult {
966-
self.span_to_lines(sp)
967-
}
968-
fn span_to_string(&self, sp: Span) -> String {
969-
self.span_to_string(sp)
970-
}
971-
fn span_to_snippet(&self, sp: Span) -> Result<String, SpanSnippetError> {
972-
self.span_to_snippet(sp)
973-
}
974-
fn span_to_filename(&self, sp: Span) -> FileName {
975-
self.span_to_filename(sp)
976-
}
977-
fn merge_spans(&self, sp_lhs: Span, sp_rhs: Span) -> Option<Span> {
978-
self.merge_spans(sp_lhs, sp_rhs)
957+
pub fn ensure_source_file_source_present(&self, source_file: Lrc<SourceFile>) -> bool {
958+
source_file.add_external_src(
959+
|| match source_file.name {
960+
FileName::Real(ref name) => self.file_loader.read_file(name).ok(),
961+
_ => None,
962+
}
963+
)
979964
}
980-
fn call_span_if_macro(&self, sp: Span) -> Span {
965+
pub fn call_span_if_macro(&self, sp: Span) -> Span {
981966
if self.span_to_filename(sp.clone()).is_macros() {
982967
let v = sp.macro_backtrace();
983968
if let Some(use_site) = v.last() {
@@ -986,17 +971,6 @@ impl SourceMapper for SourceMap {
986971
}
987972
sp
988973
}
989-
fn ensure_source_file_source_present(&self, source_file: Lrc<SourceFile>) -> bool {
990-
source_file.add_external_src(
991-
|| match source_file.name {
992-
FileName::Real(ref name) => self.file_loader.read_file(name).ok(),
993-
_ => None,
994-
}
995-
)
996-
}
997-
fn doctest_offset_line(&self, file: &FileName, line: usize) -> usize {
998-
self.doctest_offset_line(file, line)
999-
}
1000974
}
1001975

1002976
#[derive(Clone)]

0 commit comments

Comments
 (0)
Please sign in to comment.