]>
git.proxmox.com Git - rustc.git/blob - src/librustc_errors/diagnostic.rs
1 // Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
14 use RenderSpan
::Suggestion
;
16 use syntax_pos
::{MultiSpan, Span}
;
20 #[derive(Clone, Debug, PartialEq, RustcEncodable, RustcDecodable)]
21 pub struct Diagnostic
{
23 pub message
: Vec
<(String
, Style
)>,
24 pub code
: Option
<String
>,
26 pub children
: Vec
<SubDiagnostic
>,
29 /// For example a note attached to an error.
30 #[derive(Clone, Debug, PartialEq, RustcEncodable, RustcDecodable)]
31 pub struct SubDiagnostic
{
33 pub message
: Vec
<(String
, Style
)>,
35 pub render_span
: Option
<RenderSpan
>,
39 pub fn new(level
: Level
, message
: &str) -> Self {
40 Diagnostic
::new_with_code(level
, None
, message
)
43 pub fn new_with_code(level
: Level
, code
: Option
<String
>, message
: &str) -> Self {
46 message
: vec
![(message
.to_owned(), Style
::NoStyle
)],
48 span
: MultiSpan
::new(),
53 /// Cancel the diagnostic (a structured diagnostic must either be emitted or
54 /// cancelled or it will panic when dropped).
55 /// BEWARE: if this DiagnosticBuilder is an error, then creating it will
56 /// bump the error count on the Handler and cancelling it won't undo that.
57 /// If you want to decrement the error count you should use `Handler::cancel`.
58 pub fn cancel(&mut self) {
59 self.level
= Level
::Cancelled
;
62 pub fn cancelled(&self) -> bool
{
63 self.level
== Level
::Cancelled
66 pub fn is_fatal(&self) -> bool
{
67 self.level
== Level
::Fatal
70 /// Add a span/label to be included in the resulting snippet.
71 /// This is pushed onto the `MultiSpan` that was created when the
72 /// diagnostic was first built. If you don't call this function at
73 /// all, and you just supplied a `Span` to create the diagnostic,
74 /// then the snippet will just include that `Span`, which is
75 /// called the primary span.
76 pub fn span_label(&mut self, span
: Span
, label
: &fmt
::Display
)
78 self.span
.push_span_label(span
, format
!("{}", label
));
82 pub fn note_expected_found(&mut self,
84 expected
: &fmt
::Display
,
88 self.note_expected_found_extra(label
, expected
, found
, &"", &"")
91 pub fn note_expected_found_extra(&mut self,
93 expected
: &fmt
::Display
,
95 expected_extra
: &fmt
::Display
,
96 found_extra
: &fmt
::Display
)
99 // For now, just attach these as notes
100 self.highlighted_note(vec
![
101 (format
!("expected {} `", label
), Style
::NoStyle
),
102 (format
!("{}", expected
), Style
::Highlight
),
103 (format
!("`{}\n", expected_extra
), Style
::NoStyle
),
104 (format
!(" found {} `", label
), Style
::NoStyle
),
105 (format
!("{}", found
), Style
::Highlight
),
106 (format
!("`{}", found_extra
), Style
::NoStyle
),
111 pub fn note(&mut self, msg
: &str) -> &mut Self {
112 self.sub(Level
::Note
, msg
, MultiSpan
::new(), None
);
116 pub fn highlighted_note(&mut self, msg
: Vec
<(String
, Style
)>) -> &mut Self {
117 self.sub_with_highlights(Level
::Note
, msg
, MultiSpan
::new(), None
);
121 pub fn span_note
<S
: Into
<MultiSpan
>>(&mut self,
125 self.sub(Level
::Note
, msg
, sp
.into(), None
);
129 pub fn warn(&mut self, msg
: &str) -> &mut Self {
130 self.sub(Level
::Warning
, msg
, MultiSpan
::new(), None
);
134 pub fn span_warn
<S
: Into
<MultiSpan
>>(&mut self,
138 self.sub(Level
::Warning
, msg
, sp
.into(), None
);
142 pub fn help(&mut self , msg
: &str) -> &mut Self {
143 self.sub(Level
::Help
, msg
, MultiSpan
::new(), None
);
147 pub fn span_help
<S
: Into
<MultiSpan
>>(&mut self,
151 self.sub(Level
::Help
, msg
, sp
.into(), None
);
155 /// Prints out a message with a suggested edit of the code.
157 /// See `diagnostic::RenderSpan::Suggestion` for more information.
158 pub fn span_suggestion
<S
: Into
<MultiSpan
>>(&mut self,
163 self.sub(Level
::Help
,
166 Some(Suggestion(CodeSuggestion
{
168 substitutes
: vec
![suggestion
],
173 pub fn set_span
<S
: Into
<MultiSpan
>>(&mut self, sp
: S
) -> &mut Self {
174 self.span
= sp
.into();
178 pub fn code(&mut self, s
: String
) -> &mut Self {
183 pub fn message(&self) -> String
{
184 self.message
.iter().map(|i
| i
.0.to_owned()).collect
::<String
>()
187 pub fn styled_message(&self) -> &Vec
<(String
, Style
)> {
191 pub fn level(&self) -> Level
{
195 /// Used by a lint. Copies over all details *but* the "main
197 pub fn copy_details_not_message(&mut self, from
: &Diagnostic
) {
198 self.span
= from
.span
.clone();
199 self.code
= from
.code
.clone();
200 self.children
.extend(from
.children
.iter().cloned())
203 /// Convenience function for internal use, clients should use one of the
204 /// public methods above.
209 render_span
: Option
<RenderSpan
>) {
210 let sub
= SubDiagnostic
{
212 message
: vec
![(message
.to_owned(), Style
::NoStyle
)],
214 render_span
: render_span
,
216 self.children
.push(sub
);
219 /// Convenience function for internal use, clients should use one of the
220 /// public methods above.
221 fn sub_with_highlights(&mut self,
223 message
: Vec
<(String
, Style
)>,
225 render_span
: Option
<RenderSpan
>) {
226 let sub
= SubDiagnostic
{
230 render_span
: render_span
,
232 self.children
.push(sub
);
237 pub fn message(&self) -> String
{
238 self.message
.iter().map(|i
| i
.0.to_owned()).collect
::<String
>()
241 pub fn styled_message(&self) -> &Vec
<(String
, Style
)> {