rls_span/
compiler.rs

1///! These are the structures emitted by the compiler as part of JSON errors.
2///! The original source can be found at
3///! https://github.com/rust-lang/rust/blob/master/src/librustc_errors/json.rs
4use std::path::PathBuf;
5
6#[cfg(feature = "derive")]
7use serde::Deserialize;
8
9use crate::{Column, OneIndexed, Row, Span};
10
11#[cfg_attr(feature = "derive", derive(Deserialize))]
12#[cfg_attr(feature = "serialize-rustc", derive(RustcDecodable))]
13#[derive(Debug, Clone)]
14pub struct DiagnosticSpan {
15    pub file_name: String,
16    pub byte_start: u32,
17    pub byte_end: u32,
18    /// 1-based.
19    pub line_start: usize,
20    pub line_end: usize,
21    /// 1-based, character offset.
22    pub column_start: usize,
23    pub column_end: usize,
24    /// Is this a "primary" span -- meaning the point, or one of the points,
25    /// where the error occurred?
26    pub is_primary: bool,
27    /// Source text from the start of line_start to the end of line_end.
28    pub text: Vec<DiagnosticSpanLine>,
29    /// Label that should be placed at this location (if any)
30    pub label: Option<String>,
31    /// If we are suggesting a replacement, this will contain text
32    /// that should be sliced in atop this span. You may prefer to
33    /// load the fully rendered version from the parent `Diagnostic`,
34    /// however.
35    pub suggested_replacement: Option<String>,
36    /// Macro invocations that created the code at this span, if any.
37    pub expansion: Option<Box<DiagnosticSpanMacroExpansion>>,
38}
39
40impl DiagnosticSpan {
41    pub fn rls_span(&self) -> Span<OneIndexed> {
42        Span::new(
43            Row::new(self.line_start as u32),
44            Row::new(self.line_end as u32),
45            Column::new(self.column_start as u32),
46            Column::new(self.column_end as u32),
47            PathBuf::from(&self.file_name),
48        )
49    }
50}
51
52#[cfg_attr(feature = "derive", derive(Deserialize))]
53#[cfg_attr(feature = "serialize-rustc", derive(RustcDecodable))]
54#[derive(Debug, Clone)]
55pub struct DiagnosticSpanLine {
56    pub text: String,
57
58    /// 1-based, character offset in self.text.
59    pub highlight_start: usize,
60
61    pub highlight_end: usize,
62}
63
64#[cfg_attr(feature = "derive", derive(Deserialize))]
65#[cfg_attr(feature = "serialize-rustc", derive(RustcDecodable))]
66#[derive(Debug, Clone)]
67pub struct DiagnosticSpanMacroExpansion {
68    /// span where macro was applied to generate this code; note that
69    /// this may itself derive from a macro (if
70    /// `span.expansion.is_some()`)
71    pub span: DiagnosticSpan,
72
73    /// name of macro that was applied (e.g., "foo!" or "#[derive(Eq)]")
74    pub macro_decl_name: String,
75
76    /// span where macro was defined (if known)
77    pub def_site_span: Option<DiagnosticSpan>,
78}