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>
This commit is contained in:
David Wood 2022-03-24 02:03:04 +00:00
parent 8c684563a5
commit c45f29595d
66 changed files with 354 additions and 293 deletions

View file

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

View file

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

View file

@ -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};

View file

@ -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);

View file

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

View file

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