|
| 1 | +use rustc_macros::{Decodable, Encodable}; |
| 2 | +use rustc_span::Span; |
| 3 | + |
| 4 | +/// Abstraction over a message in a diagnostic to support both translatable and non-translatable |
| 5 | +/// diagnostic messages. |
| 6 | +#[derive(Clone, Debug, PartialEq, Eq, Hash, Encodable, Decodable)] |
| 7 | +pub enum DiagnosticMessage { |
| 8 | + /// Non-translatable diagnostic message. |
| 9 | + Str(String), |
| 10 | + /// Identifier for a Fluent message corresponding to the diagnostic message. |
| 11 | + FluentIdentifier(String), |
| 12 | +} |
| 13 | + |
| 14 | +impl DiagnosticMessage { |
| 15 | + /// Convert `DiagnosticMessage` to a `&str`. |
| 16 | + pub fn as_str(&self) -> &str { |
| 17 | + match self { |
| 18 | + DiagnosticMessage::Str(msg) => msg, |
| 19 | + DiagnosticMessage::FluentIdentifier(..) => unimplemented!(), |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + /// Convert `DiagnosticMessage` to an owned `String`. |
| 24 | + pub fn to_string(self) -> String { |
| 25 | + match self { |
| 26 | + DiagnosticMessage::Str(msg) => msg, |
| 27 | + DiagnosticMessage::FluentIdentifier(..) => unimplemented!(), |
| 28 | + } |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +/// A span together with some additional data. |
| 33 | +#[derive(Clone, Debug)] |
| 34 | +pub struct SpanLabel { |
| 35 | + /// The span we are going to include in the final snippet. |
| 36 | + pub span: Span, |
| 37 | + |
| 38 | + /// Is this a primary span? This is the "locus" of the message, |
| 39 | + /// and is indicated with a `^^^^` underline, versus `----`. |
| 40 | + pub is_primary: bool, |
| 41 | + |
| 42 | + /// What label should we attach to this span (if any)? |
| 43 | + pub label: Option<DiagnosticMessage>, |
| 44 | +} |
| 45 | + |
| 46 | +/// A collection of `Span`s. |
| 47 | +/// |
| 48 | +/// Spans have two orthogonal attributes: |
| 49 | +/// |
| 50 | +/// - They can be *primary spans*. In this case they are the locus of |
| 51 | +/// the error, and would be rendered with `^^^`. |
| 52 | +/// - They can have a *label*. In this case, the label is written next |
| 53 | +/// to the mark in the snippet when we render. |
| 54 | +#[derive(Clone, Debug, Hash, PartialEq, Eq, Encodable, Decodable)] |
| 55 | +pub struct MultiSpan { |
| 56 | + primary_spans: Vec<Span>, |
| 57 | + span_labels: Vec<(Span, DiagnosticMessage)>, |
| 58 | +} |
| 59 | + |
| 60 | +impl MultiSpan { |
| 61 | + #[inline] |
| 62 | + pub fn new() -> MultiSpan { |
| 63 | + MultiSpan { primary_spans: vec![], span_labels: vec![] } |
| 64 | + } |
| 65 | + |
| 66 | + pub fn from_span(primary_span: Span) -> MultiSpan { |
| 67 | + MultiSpan { primary_spans: vec![primary_span], span_labels: vec![] } |
| 68 | + } |
| 69 | + |
| 70 | + pub fn from_spans(mut vec: Vec<Span>) -> MultiSpan { |
| 71 | + vec.sort(); |
| 72 | + MultiSpan { primary_spans: vec, span_labels: vec![] } |
| 73 | + } |
| 74 | + |
| 75 | + pub fn push_span_label(&mut self, span: Span, label: String) { |
| 76 | + self.span_labels.push((span, DiagnosticMessage::Str(label))); |
| 77 | + } |
| 78 | + |
| 79 | + pub fn push_span_message(&mut self, span: Span, message: DiagnosticMessage) { |
| 80 | + self.span_labels.push((span, message)); |
| 81 | + } |
| 82 | + |
| 83 | + /// Selects the first primary span (if any). |
| 84 | + pub fn primary_span(&self) -> Option<Span> { |
| 85 | + self.primary_spans.first().cloned() |
| 86 | + } |
| 87 | + |
| 88 | + /// Returns all primary spans. |
| 89 | + pub fn primary_spans(&self) -> &[Span] { |
| 90 | + &self.primary_spans |
| 91 | + } |
| 92 | + |
| 93 | + /// Returns `true` if any of the primary spans are displayable. |
| 94 | + pub fn has_primary_spans(&self) -> bool { |
| 95 | + self.primary_spans.iter().any(|sp| !sp.is_dummy()) |
| 96 | + } |
| 97 | + |
| 98 | + /// Returns `true` if this contains only a dummy primary span with any hygienic context. |
| 99 | + pub fn is_dummy(&self) -> bool { |
| 100 | + let mut is_dummy = true; |
| 101 | + for span in &self.primary_spans { |
| 102 | + if !span.is_dummy() { |
| 103 | + is_dummy = false; |
| 104 | + } |
| 105 | + } |
| 106 | + is_dummy |
| 107 | + } |
| 108 | + |
| 109 | + /// Replaces all occurrences of one Span with another. Used to move `Span`s in areas that don't |
| 110 | + /// display well (like std macros). Returns whether replacements occurred. |
| 111 | + pub fn replace(&mut self, before: Span, after: Span) -> bool { |
| 112 | + let mut replacements_occurred = false; |
| 113 | + for primary_span in &mut self.primary_spans { |
| 114 | + if *primary_span == before { |
| 115 | + *primary_span = after; |
| 116 | + replacements_occurred = true; |
| 117 | + } |
| 118 | + } |
| 119 | + for span_label in &mut self.span_labels { |
| 120 | + if span_label.0 == before { |
| 121 | + span_label.0 = after; |
| 122 | + replacements_occurred = true; |
| 123 | + } |
| 124 | + } |
| 125 | + replacements_occurred |
| 126 | + } |
| 127 | + |
| 128 | + /// Returns the strings to highlight. We always ensure that there |
| 129 | + /// is an entry for each of the primary spans -- for each primary |
| 130 | + /// span `P`, if there is at least one label with span `P`, we return |
| 131 | + /// those labels (marked as primary). But otherwise we return |
| 132 | + /// `SpanLabel` instances with empty labels. |
| 133 | + pub fn span_labels(&self) -> Vec<SpanLabel> { |
| 134 | + let is_primary = |span| self.primary_spans.contains(&span); |
| 135 | + |
| 136 | + let mut span_labels = self |
| 137 | + .span_labels |
| 138 | + .iter() |
| 139 | + .map(|&(span, ref label)| SpanLabel { |
| 140 | + span, |
| 141 | + is_primary: is_primary(span), |
| 142 | + label: Some(label.clone()), |
| 143 | + }) |
| 144 | + .collect::<Vec<_>>(); |
| 145 | + |
| 146 | + for &span in &self.primary_spans { |
| 147 | + if !span_labels.iter().any(|sl| sl.span == span) { |
| 148 | + span_labels.push(SpanLabel { span, is_primary: true, label: None }); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + span_labels |
| 153 | + } |
| 154 | + |
| 155 | + /// Returns `true` if any of the span labels is displayable. |
| 156 | + pub fn has_span_labels(&self) -> bool { |
| 157 | + self.span_labels.iter().any(|(sp, _)| !sp.is_dummy()) |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +impl From<Span> for MultiSpan { |
| 162 | + fn from(span: Span) -> MultiSpan { |
| 163 | + MultiSpan::from_span(span) |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +impl From<Vec<Span>> for MultiSpan { |
| 168 | + fn from(spans: Vec<Span>) -> MultiSpan { |
| 169 | + MultiSpan::from_spans(spans) |
| 170 | + } |
| 171 | +} |
0 commit comments