diff options
| author | David Wood <david.wood@huawei.com> | 2022-03-24 02:03:04 +0000 |
|---|---|---|
| committer | David Wood <david.wood@huawei.com> | 2022-04-05 07:01:00 +0100 |
| commit | c45f29595df6f6a178b7998bc33c76099f3c12b7 (patch) | |
| tree | 0f54f00c31b0d2ec11408f843e6b9ec726ce8a11 /compiler/rustc_errors/src | |
| parent | 8c684563a59900d96a4fcadd41e5e92074c13df1 (diff) | |
| download | rust-c45f29595df6f6a178b7998bc33c76099f3c12b7.tar.gz rust-c45f29595df6f6a178b7998bc33c76099f3c12b7.zip | |
span: move `MultiSpan`
`MultiSpan` contains labels, which are more complicated with the introduction of diagnostic translation and will use types from `rustc_errors` - however, `rustc_errors` depends on `rustc_span` so `rustc_span` cannot use types like `DiagnosticMessage` without dependency cycles. Introduce a new `rustc_error_messages` crate that can contain `DiagnosticMessage` and `MultiSpan`. Signed-off-by: David Wood <david.wood@huawei.com>
Diffstat (limited to 'compiler/rustc_errors/src')
| -rw-r--r-- | compiler/rustc_errors/src/annotate_snippet_emitter_writer.rs | 4 | ||||
| -rw-r--r-- | compiler/rustc_errors/src/diagnostic.rs | 42 | ||||
| -rw-r--r-- | compiler/rustc_errors/src/diagnostic_builder.rs | 4 | ||||
| -rw-r--r-- | compiler/rustc_errors/src/emitter.rs | 10 | ||||
| -rw-r--r-- | compiler/rustc_errors/src/json.rs | 12 | ||||
| -rw-r--r-- | compiler/rustc_errors/src/lib.rs | 8 |
6 files changed, 28 insertions, 52 deletions
diff --git a/compiler/rustc_errors/src/annotate_snippet_emitter_writer.rs b/compiler/rustc_errors/src/annotate_snippet_emitter_writer.rs index 76c8396cf91..330c3d218fc 100644 --- a/compiler/rustc_errors/src/annotate_snippet_emitter_writer.rs +++ b/compiler/rustc_errors/src/annotate_snippet_emitter_writer.rs @@ -7,12 +7,12 @@ use crate::emitter::FileWithAnnotatedLines; use crate::snippet::Line; -use crate::{CodeSuggestion, Diagnostic, DiagnosticId, Emitter, Level, SubDiagnostic}; +use crate::{CodeSuggestion, Diagnostic, DiagnosticId, Emitter, Level, MultiSpan, SubDiagnostic}; use annotate_snippets::display_list::{DisplayList, FormatOptions}; use annotate_snippets::snippet::*; use rustc_data_structures::sync::Lrc; use rustc_span::source_map::SourceMap; -use rustc_span::{MultiSpan, SourceFile}; +use rustc_span::SourceFile; /// Generates diagnostics using annotate-snippet pub struct AnnotateSnippetEmitterWriter { diff --git a/compiler/rustc_errors/src/diagnostic.rs b/compiler/rustc_errors/src/diagnostic.rs index d31593a132b..f2c0e2701b3 100644 --- a/compiler/rustc_errors/src/diagnostic.rs +++ b/compiler/rustc_errors/src/diagnostic.rs @@ -1,15 +1,13 @@ use crate::snippet::Style; -use crate::CodeSuggestion; -use crate::Level; -use crate::Substitution; -use crate::SubstitutionPart; -use crate::SuggestionStyle; -use crate::ToolMetadata; +use crate::{ + CodeSuggestion, DiagnosticMessage, Level, MultiSpan, Substitution, SubstitutionPart, + SuggestionStyle, ToolMetadata, +}; use rustc_data_structures::stable_map::FxHashMap; use rustc_lint_defs::{Applicability, LintExpectationId}; use rustc_serialize::json::Json; use rustc_span::edition::LATEST_STABLE_EDITION; -use rustc_span::{MultiSpan, Span, DUMMY_SP}; +use rustc_span::{Span, DUMMY_SP}; use std::fmt; use std::hash::{Hash, Hasher}; @@ -18,34 +16,6 @@ use std::hash::{Hash, Hasher}; #[derive(Clone, Debug, PartialEq, Eq, Hash, Encodable, Decodable)] pub struct SuggestionsDisabled; -/// Abstraction over a message in a diagnostic to support both translatable and non-translatable -/// diagnostic messages. -#[derive(Clone, Debug, PartialEq, Eq, Hash, Encodable, Decodable)] -pub enum DiagnosticMessage { - /// Non-translatable diagnostic message. - Str(String), - /// Identifier for a Fluent message corresponding to the diagnostic message. - FluentIdentifier(String), -} - -impl DiagnosticMessage { - /// Convert `DiagnosticMessage` to a `&str`. - pub fn as_str(&self) -> &str { - match self { - DiagnosticMessage::Str(msg) => msg, - DiagnosticMessage::FluentIdentifier(..) => unimplemented!(), - } - } - - /// Convert `DiagnosticMessage` to an owned `String`. - pub fn to_string(self) -> String { - match self { - DiagnosticMessage::Str(msg) => msg, - DiagnosticMessage::FluentIdentifier(..) => unimplemented!(), - } - } -} - #[must_use] #[derive(Clone, Debug, Encodable, Decodable)] pub struct Diagnostic { @@ -262,7 +232,7 @@ impl Diagnostic { self.set_span(after); for span_label in before.span_labels() { if let Some(label) = span_label.label { - self.span_label(after, label); + self.span.push_span_message(after, label); } } self diff --git a/compiler/rustc_errors/src/diagnostic_builder.rs b/compiler/rustc_errors/src/diagnostic_builder.rs index 853243ef3f0..4a7e252edb2 100644 --- a/compiler/rustc_errors/src/diagnostic_builder.rs +++ b/compiler/rustc_errors/src/diagnostic_builder.rs @@ -1,8 +1,8 @@ use crate::{Diagnostic, DiagnosticId, DiagnosticStyledString, ErrorGuaranteed}; -use crate::{Handler, Level, StashKey}; +use crate::{Handler, Level, MultiSpan, StashKey}; use rustc_lint_defs::Applicability; -use rustc_span::{MultiSpan, Span}; +use rustc_span::Span; use std::fmt::{self, Debug}; use std::marker::PhantomData; use std::ops::{Deref, DerefMut}; diff --git a/compiler/rustc_errors/src/emitter.rs b/compiler/rustc_errors/src/emitter.rs index 1f26b002f6a..881938ec85f 100644 --- a/compiler/rustc_errors/src/emitter.rs +++ b/compiler/rustc_errors/src/emitter.rs @@ -10,13 +10,13 @@ use Destination::*; use rustc_span::source_map::SourceMap; -use rustc_span::{MultiSpan, SourceFile, Span}; +use rustc_span::{SourceFile, Span}; use crate::snippet::{Annotation, AnnotationType, Line, MultilineAnnotation, Style, StyledString}; use crate::styled_buffer::StyledBuffer; use crate::{ - CodeSuggestion, Diagnostic, DiagnosticId, DiagnosticMessage, Handler, Level, SubDiagnostic, - SubstitutionHighlight, SuggestionStyle, + CodeSuggestion, Diagnostic, DiagnosticId, DiagnosticMessage, Handler, Level, MultiSpan, + SubDiagnostic, SubstitutionHighlight, SuggestionStyle, }; use rustc_lint_defs::pluralize; @@ -2003,7 +2003,7 @@ impl FileWithAnnotatedLines { start_col: lo.col_display, end_col: hi.col_display, is_primary: span_label.is_primary, - label: span_label.label, + label: span_label.label.map(|m| m.to_string()), overlaps_exactly: false, }; multiline_annotations.push((lo.file, ml)); @@ -2012,7 +2012,7 @@ impl FileWithAnnotatedLines { start_col: lo.col_display, end_col: hi.col_display, is_primary: span_label.is_primary, - label: span_label.label, + label: span_label.label.map(|m| m.to_string()), annotation_type: AnnotationType::Singleline, }; add_annotation_to_file(&mut output, lo.file, lo.line, ann); diff --git a/compiler/rustc_errors/src/json.rs b/compiler/rustc_errors/src/json.rs index 90f6df2d571..b93ab70e1f3 100644 --- a/compiler/rustc_errors/src/json.rs +++ b/compiler/rustc_errors/src/json.rs @@ -15,12 +15,12 @@ use crate::emitter::{Emitter, HumanReadableErrorType}; use crate::registry::Registry; use crate::DiagnosticId; use crate::ToolMetadata; -use crate::{CodeSuggestion, SubDiagnostic}; +use crate::{CodeSuggestion, MultiSpan, SpanLabel, SubDiagnostic}; use rustc_lint_defs::Applicability; use rustc_data_structures::sync::Lrc; use rustc_span::hygiene::ExpnData; -use rustc_span::{MultiSpan, Span, SpanLabel}; +use rustc_span::Span; use std::io::{self, Write}; use std::path::Path; use std::sync::{Arc, Mutex}; @@ -423,7 +423,13 @@ impl DiagnosticSpan { suggestion: Option<(&String, Applicability)>, je: &JsonEmitter, ) -> DiagnosticSpan { - Self::from_span_etc(span.span, span.is_primary, span.label, suggestion, je) + Self::from_span_etc( + span.span, + span.is_primary, + span.label.map(|m| m.to_string()), + suggestion, + je, + ) } fn from_span_etc( diff --git a/compiler/rustc_errors/src/lib.rs b/compiler/rustc_errors/src/lib.rs index 0f55ef7a9ec..5e770f7d5ae 100644 --- a/compiler/rustc_errors/src/lib.rs +++ b/compiler/rustc_errors/src/lib.rs @@ -31,11 +31,12 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap}; use rustc_data_structures::stable_hasher::StableHasher; use rustc_data_structures::sync::{self, Lock, Lrc}; use rustc_data_structures::AtomicRef; +pub use rustc_error_messages::{DiagnosticMessage, MultiSpan, SpanLabel}; pub use rustc_lint_defs::{pluralize, Applicability}; use rustc_serialize::json::Json; use rustc_serialize::{Decodable, Decoder, Encodable, Encoder}; use rustc_span::source_map::SourceMap; -use rustc_span::{Loc, MultiSpan, Span}; +use rustc_span::{Loc, Span}; use std::borrow::Cow; use std::hash::{Hash, Hasher}; @@ -55,6 +56,7 @@ mod lock; pub mod registry; mod snippet; mod styled_buffer; + pub use snippet::Style; pub type PResult<'a, T> = Result<T, DiagnosticBuilder<'a, ErrorGuaranteed>>; @@ -400,9 +402,7 @@ impl fmt::Display for ExplicitBug { impl error::Error for ExplicitBug {} -pub use diagnostic::{ - Diagnostic, DiagnosticId, DiagnosticMessage, DiagnosticStyledString, SubDiagnostic, -}; +pub use diagnostic::{Diagnostic, DiagnosticId, DiagnosticStyledString, SubDiagnostic}; pub use diagnostic_builder::{DiagnosticBuilder, EmissionGuarantee}; use std::backtrace::Backtrace; |
