diff --git a/src/librustc_codegen_ssa/back/write.rs b/src/librustc_codegen_ssa/back/write.rs index ed901fa064a4..f35a31d59fe6 100644 --- a/src/librustc_codegen_ssa/back/write.rs +++ b/src/librustc_codegen_ssa/back/write.rs @@ -23,7 +23,8 @@ use rustc_data_structures::profiling::SelfProfilerRef; use rustc_fs_util::link_or_copy; use rustc_data_structures::svh::Svh; use rustc_data_structures::sync::Lrc; -use rustc_errors::{Handler, Level, FatalError, DiagnosticId, SourceMapperDyn}; +use rustc_errors::{Handler, Level, FatalError, DiagnosticId}; +use syntax_pos::source_map::SourceMap; use rustc_errors::emitter::{Emitter}; use rustc_target::spec::MergeFunctions; use syntax::attr; @@ -1679,7 +1680,7 @@ impl Emitter for SharedEmitter { } drop(self.sender.send(SharedEmitterMessage::AbortIfErrors)); } - fn source_map(&self) -> Option<&Lrc> { + fn source_map(&self) -> Option<&Lrc> { None } } diff --git a/src/librustc_errors/annotate_snippet_emitter_writer.rs b/src/librustc_errors/annotate_snippet_emitter_writer.rs index 491bc2aa6a2e..4c5d0178b2c6 100644 --- a/src/librustc_errors/annotate_snippet_emitter_writer.rs +++ b/src/librustc_errors/annotate_snippet_emitter_writer.rs @@ -6,9 +6,10 @@ //! [annotate_snippets]: https://docs.rs/crate/annotate-snippets/ use syntax_pos::{SourceFile, MultiSpan, Loc}; +use syntax_pos::source_map::SourceMap; use crate::{ Level, CodeSuggestion, Diagnostic, Emitter, - SourceMapperDyn, SubDiagnostic, DiagnosticId + SubDiagnostic, DiagnosticId }; use crate::emitter::FileWithAnnotatedLines; use rustc_data_structures::sync::Lrc; @@ -20,7 +21,7 @@ use annotate_snippets::formatter::DisplayListFormatter; /// Generates diagnostics using annotate-snippet pub struct AnnotateSnippetEmitterWriter { - source_map: Option>, + source_map: Option>, /// If true, hides the longer explanation text short_message: bool, /// If true, will normalize line numbers with `LL` to prevent noise in UI test diffs. @@ -49,7 +50,7 @@ impl Emitter for AnnotateSnippetEmitterWriter { &suggestions); } - fn source_map(&self) -> Option<&Lrc> { + fn source_map(&self) -> Option<&Lrc> { self.source_map.as_ref() } @@ -61,7 +62,7 @@ impl Emitter for AnnotateSnippetEmitterWriter { /// Collects all the data needed to generate the data structures needed for the /// `annotate-snippets` library. struct DiagnosticConverter<'a> { - source_map: Option>, + source_map: Option>, level: Level, message: String, code: Option, @@ -168,7 +169,7 @@ impl<'a> DiagnosticConverter<'a> { impl AnnotateSnippetEmitterWriter { pub fn new( - source_map: Option>, + source_map: Option>, short_message: bool, external_macro_backtrace: bool, ) -> Self { diff --git a/src/librustc_errors/emitter.rs b/src/librustc_errors/emitter.rs index 291920f17f66..ea779982ba96 100644 --- a/src/librustc_errors/emitter.rs +++ b/src/librustc_errors/emitter.rs @@ -10,10 +10,11 @@ use Destination::*; use syntax_pos::{SourceFile, Span, MultiSpan}; +use syntax_pos::source_map::SourceMap; use crate::{ Level, CodeSuggestion, Diagnostic, SubDiagnostic, pluralize, - SuggestionStyle, SourceMapper, SourceMapperDyn, DiagnosticId, + SuggestionStyle, DiagnosticId, }; use crate::Level::Error; use crate::snippet::{Annotation, AnnotationType, Line, MultilineAnnotation, StyledString, Style}; @@ -49,7 +50,7 @@ impl HumanReadableErrorType { pub fn new_emitter( self, dst: Box, - source_map: Option>, + source_map: Option>, teach: bool, terminal_width: Option, external_macro_backtrace: bool, @@ -192,7 +193,7 @@ pub trait Emitter { true } - fn source_map(&self) -> Option<&Lrc>; + fn source_map(&self) -> Option<&Lrc>; /// Formats the substitutions of the primary_span /// @@ -271,7 +272,7 @@ pub trait Emitter { // point directly at <*macros>. Since these are often difficult to read, this // will change the span to point at the use site. fn fix_multispans_in_std_macros(&self, - source_map: &Option>, + source_map: &Option>, span: &mut MultiSpan, children: &mut Vec, level: &Level, @@ -311,7 +312,7 @@ pub trait Emitter { // <*macros>. Since these locations are often difficult to read, we move these Spans from // <*macros> to their corresponding use site. fn fix_multispan_in_std_macros(&self, - source_map: &Option>, + source_map: &Option>, span: &mut MultiSpan, always_backtrace: bool) -> bool { let sm = match source_map { @@ -397,7 +398,7 @@ pub trait Emitter { } impl Emitter for EmitterWriter { - fn source_map(&self) -> Option<&Lrc> { + fn source_map(&self) -> Option<&Lrc> { self.sm.as_ref() } @@ -428,7 +429,7 @@ impl Emitter for EmitterWriter { pub struct SilentEmitter; impl Emitter for SilentEmitter { - fn source_map(&self) -> Option<&Lrc> { None } + fn source_map(&self) -> Option<&Lrc> { None } fn emit_diagnostic(&mut self, _: &Diagnostic) {} } @@ -476,7 +477,7 @@ impl ColorConfig { /// Handles the writing of `HumanReadableErrorType::Default` and `HumanReadableErrorType::Short` pub struct EmitterWriter { dst: Destination, - sm: Option>, + sm: Option>, short_message: bool, teach: bool, ui_testing: bool, @@ -495,7 +496,7 @@ pub struct FileWithAnnotatedLines { impl EmitterWriter { pub fn stderr( color_config: ColorConfig, - source_map: Option>, + source_map: Option>, short_message: bool, teach: bool, terminal_width: Option, @@ -515,7 +516,7 @@ impl EmitterWriter { pub fn new( dst: Box, - source_map: Option>, + source_map: Option>, short_message: bool, teach: bool, colored: bool, @@ -1685,7 +1686,7 @@ impl FileWithAnnotatedLines { /// This helps us quickly iterate over the whole message (including secondary file spans) pub fn collect_annotations( msp: &MultiSpan, - source_map: &Option> + source_map: &Option> ) -> Vec { fn add_annotation_to_file(file_vec: &mut Vec, file: Lrc, @@ -2067,7 +2068,7 @@ impl<'a> Drop for WritableDst<'a> { } /// Whether the original and suggested code are visually similar enough to warrant extra wording. -pub fn is_case_difference(sm: &dyn SourceMapper, suggested: &str, sp: Span) -> bool { +pub fn is_case_difference(sm: &SourceMap, suggested: &str, sp: Span) -> bool { // FIXME: this should probably be extended to also account for `FO0` → `FOO` and unicode. let found = sm.span_to_snippet(sp).unwrap(); let ascii_confusables = &['c', 'f', 'i', 'k', 'o', 's', 'u', 'v', 'w', 'x', 'y', 'z']; diff --git a/src/librustc_errors/lib.rs b/src/librustc_errors/lib.rs index ee35d23af34f..1a6ac328a47d 100644 --- a/src/librustc_errors/lib.rs +++ b/src/librustc_errors/lib.rs @@ -18,6 +18,8 @@ use registry::Registry; use rustc_data_structures::sync::{self, Lrc, Lock}; use rustc_data_structures::fx::{FxHashSet, FxIndexMap}; use rustc_data_structures::stable_hasher::StableHasher; +use syntax_pos::source_map::SourceMap; +use syntax_pos::{Loc, Span, MultiSpan}; use std::borrow::Cow; use std::cell::Cell; @@ -36,17 +38,6 @@ pub mod registry; mod styled_buffer; mod lock; -use syntax_pos::{ - BytePos, - FileLinesResult, - FileName, - Loc, - MultiSpan, - SourceFile, - Span, - SpanSnippetError, -}; - pub type PResult<'a, T> = Result>; // `PResult` is used a lot. Make sure it doesn't unintentionally get bigger. @@ -150,26 +141,12 @@ pub struct SubstitutionPart { pub snippet: String, } -pub type SourceMapperDyn = dyn SourceMapper + sync::Send + sync::Sync; - -pub trait SourceMapper { - fn lookup_char_pos(&self, pos: BytePos) -> Loc; - fn span_to_lines(&self, sp: Span) -> FileLinesResult; - fn span_to_string(&self, sp: Span) -> String; - fn span_to_snippet(&self, sp: Span) -> Result; - fn span_to_filename(&self, sp: Span) -> FileName; - fn merge_spans(&self, sp_lhs: Span, sp_rhs: Span) -> Option; - fn call_span_if_macro(&self, sp: Span) -> Span; - fn ensure_source_file_source_present(&self, source_file: Lrc) -> bool; - fn doctest_offset_line(&self, file: &FileName, line: usize) -> usize; -} - impl CodeSuggestion { /// Returns the assembled code suggestions, whether they should be shown with an underline /// and whether the substitution only differs in capitalization. pub fn splice_lines( &self, - cm: &SourceMapperDyn, + cm: &SourceMap, ) -> Vec<(String, Vec, bool)> { use syntax_pos::{CharPos, Pos}; @@ -376,7 +353,7 @@ impl Handler { color_config: ColorConfig, can_emit_warnings: bool, treat_err_as_bug: Option, - cm: Option>, + cm: Option>, ) -> Self { Self::with_tty_emitter_and_flags( color_config, @@ -391,7 +368,7 @@ impl Handler { pub fn with_tty_emitter_and_flags( color_config: ColorConfig, - cm: Option>, + cm: Option>, flags: HandlerFlags, ) -> Self { let emitter = Box::new(EmitterWriter::stderr( diff --git a/src/libsyntax/json.rs b/src/libsyntax/json.rs index 0b157938375e..6096a930acfc 100644 --- a/src/libsyntax/json.rs +++ b/src/libsyntax/json.rs @@ -12,7 +12,7 @@ use crate::source_map::{SourceMap, FilePathMapping}; use errors::registry::Registry; -use errors::{SubDiagnostic, CodeSuggestion, SourceMapper, SourceMapperDyn}; +use errors::{SubDiagnostic, CodeSuggestion}; use errors::{DiagnosticId, Applicability}; use errors::emitter::{Emitter, HumanReadableErrorType}; @@ -31,7 +31,7 @@ mod tests; pub struct JsonEmitter { dst: Box, registry: Option, - sm: Lrc, + sm: Lrc, pretty: bool, ui_testing: bool, json_rendered: HumanReadableErrorType, @@ -116,7 +116,7 @@ impl Emitter for JsonEmitter { } } - fn source_map(&self) -> Option<&Lrc> { + fn source_map(&self) -> Option<&Lrc> { Some(&self.sm) } diff --git a/src/libsyntax_pos/source_map.rs b/src/libsyntax_pos/source_map.rs index 035e61f9dea7..77d9807225ec 100644 --- a/src/libsyntax_pos/source_map.rs +++ b/src/libsyntax_pos/source_map.rs @@ -22,8 +22,6 @@ use std::fs; use std::io; use log::debug; -use errors::SourceMapper; - #[cfg(test)] mod tests; @@ -956,28 +954,15 @@ impl SourceMap { None } -} - -impl SourceMapper for SourceMap { - fn lookup_char_pos(&self, pos: BytePos) -> Loc { - self.lookup_char_pos(pos) - } - fn span_to_lines(&self, sp: Span) -> FileLinesResult { - self.span_to_lines(sp) - } - fn span_to_string(&self, sp: Span) -> String { - self.span_to_string(sp) - } - fn span_to_snippet(&self, sp: Span) -> Result { - self.span_to_snippet(sp) - } - fn span_to_filename(&self, sp: Span) -> FileName { - self.span_to_filename(sp) - } - fn merge_spans(&self, sp_lhs: Span, sp_rhs: Span) -> Option { - self.merge_spans(sp_lhs, sp_rhs) + pub fn ensure_source_file_source_present(&self, source_file: Lrc) -> bool { + source_file.add_external_src( + || match source_file.name { + FileName::Real(ref name) => self.file_loader.read_file(name).ok(), + _ => None, + } + ) } - fn call_span_if_macro(&self, sp: Span) -> Span { + pub fn call_span_if_macro(&self, sp: Span) -> Span { if self.span_to_filename(sp.clone()).is_macros() { let v = sp.macro_backtrace(); if let Some(use_site) = v.last() { @@ -986,17 +971,6 @@ impl SourceMapper for SourceMap { } sp } - fn ensure_source_file_source_present(&self, source_file: Lrc) -> bool { - source_file.add_external_src( - || match source_file.name { - FileName::Real(ref name) => self.file_loader.read_file(name).ok(), - _ => None, - } - ) - } - fn doctest_offset_line(&self, file: &FileName, line: usize) -> usize { - self.doctest_offset_line(file, line) - } } #[derive(Clone)]