2016-10-19 17:19:00 -04:00
|
|
|
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2016-10-11 12:26:32 -04:00
|
|
|
use CodeSuggestion;
|
2017-11-03 16:17:33 +01:00
|
|
|
use SubstitutionPart;
|
2017-05-11 15:26:22 +02:00
|
|
|
use Substitution;
|
2018-04-24 15:42:27 -07:00
|
|
|
use SuggestionApproximate;
|
2016-10-11 12:26:32 -04:00
|
|
|
use Level;
|
|
|
|
use std::fmt;
|
|
|
|
use syntax_pos::{MultiSpan, Span};
|
2017-01-11 13:55:41 -08:00
|
|
|
use snippet::Style;
|
2016-10-11 12:26:32 -04:00
|
|
|
|
|
|
|
#[must_use]
|
2017-10-25 15:01:06 +02:00
|
|
|
#[derive(Clone, Debug, PartialEq, Hash, RustcEncodable, RustcDecodable)]
|
2016-10-11 12:26:32 -04:00
|
|
|
pub struct Diagnostic {
|
|
|
|
pub level: Level,
|
2017-01-11 13:55:41 -08:00
|
|
|
pub message: Vec<(String, Style)>,
|
2017-10-27 08:21:22 +02:00
|
|
|
pub code: Option<DiagnosticId>,
|
2016-10-11 12:26:32 -04:00
|
|
|
pub span: MultiSpan,
|
|
|
|
pub children: Vec<SubDiagnostic>,
|
2017-05-09 10:04:24 +02:00
|
|
|
pub suggestions: Vec<CodeSuggestion>,
|
2016-10-11 12:26:32 -04:00
|
|
|
}
|
|
|
|
|
2018-01-21 21:19:37 -08:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
|
2017-10-27 08:21:22 +02:00
|
|
|
pub enum DiagnosticId {
|
|
|
|
Error(String),
|
|
|
|
Lint(String),
|
|
|
|
}
|
|
|
|
|
2016-10-11 12:26:32 -04:00
|
|
|
/// For example a note attached to an error.
|
2017-10-25 15:01:06 +02:00
|
|
|
#[derive(Clone, Debug, PartialEq, Hash, RustcEncodable, RustcDecodable)]
|
2016-10-11 12:26:32 -04:00
|
|
|
pub struct SubDiagnostic {
|
|
|
|
pub level: Level,
|
2017-01-11 13:55:41 -08:00
|
|
|
pub message: Vec<(String, Style)>,
|
2016-10-11 12:26:32 -04:00
|
|
|
pub span: MultiSpan,
|
2017-11-16 16:36:49 +01:00
|
|
|
pub render_span: Option<MultiSpan>,
|
2016-10-11 12:26:32 -04:00
|
|
|
}
|
|
|
|
|
Highlight and simplify mismatched types
Shorten mismatched types errors by replacing subtypes that are not
different with `_`, and highlighting only the subtypes that are
different.
Given a file
```rust
struct X<T1, T2> {
x: T1,
y: T2,
}
fn foo() -> X<X<String, String>, String> {
X { x: X {x: "".to_string(), y: 2}, y: "".to_string()}
}
fn bar() -> Option<String> {
"".to_string()
}
```
provide the following output
```rust
error[E0308]: mismatched types
--> file.rs:6:5
|
6 | X { x: X {x: "".to_string(), y: 2}, y: "".to_string()}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct `std::string::String`, found {integer}
|
= note: expected type `X<X<_, std::string::String>, _>`
^^^^^^^^^^^^^^^^^^^ // < highlighted
found type `X<X<_, {integer}>, _>`
^^^^^^^^^ // < highlighted
error[E0308]: mismatched types
--> file.rs:6:5
|
10 | "".to_string()
| ^^^^^^^^^^^^^^ expected struct `std::option::Option`, found `std::string::String`
|
= note: expected type `Option<std::string::String>`
^^^^^^^ ^ // < highlighted
found type `std::string::String`
```
2017-02-17 14:31:59 -08:00
|
|
|
#[derive(PartialEq, Eq)]
|
|
|
|
pub struct DiagnosticStyledString(pub Vec<StringPart>);
|
|
|
|
|
|
|
|
impl DiagnosticStyledString {
|
|
|
|
pub fn new() -> DiagnosticStyledString {
|
|
|
|
DiagnosticStyledString(vec![])
|
|
|
|
}
|
|
|
|
pub fn push_normal<S: Into<String>>(&mut self, t: S) {
|
|
|
|
self.0.push(StringPart::Normal(t.into()));
|
|
|
|
}
|
|
|
|
pub fn push_highlighted<S: Into<String>>(&mut self, t: S) {
|
|
|
|
self.0.push(StringPart::Highlighted(t.into()));
|
|
|
|
}
|
|
|
|
pub fn normal<S: Into<String>>(t: S) -> DiagnosticStyledString {
|
|
|
|
DiagnosticStyledString(vec![StringPart::Normal(t.into())])
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn highlighted<S: Into<String>>(t: S) -> DiagnosticStyledString {
|
|
|
|
DiagnosticStyledString(vec![StringPart::Highlighted(t.into())])
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn content(&self) -> String {
|
|
|
|
self.0.iter().map(|x| x.content()).collect::<String>()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(PartialEq, Eq)]
|
|
|
|
pub enum StringPart {
|
|
|
|
Normal(String),
|
|
|
|
Highlighted(String),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl StringPart {
|
|
|
|
pub fn content(&self) -> String {
|
|
|
|
match self {
|
|
|
|
&StringPart::Normal(ref s) | & StringPart::Highlighted(ref s) => s.to_owned()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-11 12:26:32 -04:00
|
|
|
impl Diagnostic {
|
|
|
|
pub fn new(level: Level, message: &str) -> Self {
|
|
|
|
Diagnostic::new_with_code(level, None, message)
|
|
|
|
}
|
|
|
|
|
2017-10-27 08:21:22 +02:00
|
|
|
pub fn new_with_code(level: Level, code: Option<DiagnosticId>, message: &str) -> Self {
|
2016-10-11 12:26:32 -04:00
|
|
|
Diagnostic {
|
2017-08-06 22:54:09 -07:00
|
|
|
level,
|
2017-01-11 13:55:41 -08:00
|
|
|
message: vec![(message.to_owned(), Style::NoStyle)],
|
2017-08-06 22:54:09 -07:00
|
|
|
code,
|
2016-10-11 12:26:32 -04:00
|
|
|
span: MultiSpan::new(),
|
|
|
|
children: vec![],
|
2017-05-09 10:04:24 +02:00
|
|
|
suggestions: vec![],
|
2016-10-11 12:26:32 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Cancel the diagnostic (a structured diagnostic must either be emitted or
|
2017-08-15 21:45:21 +02:00
|
|
|
/// canceled or it will panic when dropped).
|
2016-10-11 12:26:32 -04:00
|
|
|
pub fn cancel(&mut self) {
|
|
|
|
self.level = Level::Cancelled;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn cancelled(&self) -> bool {
|
|
|
|
self.level == Level::Cancelled
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Add a span/label to be included in the resulting snippet.
|
|
|
|
/// This is pushed onto the `MultiSpan` that was created when the
|
|
|
|
/// diagnostic was first built. If you don't call this function at
|
|
|
|
/// all, and you just supplied a `Span` to create the diagnostic,
|
|
|
|
/// then the snippet will just include that `Span`, which is
|
|
|
|
/// called the primary span.
|
2017-05-04 14:17:23 +02:00
|
|
|
pub fn span_label<T: Into<String>>(&mut self, span: Span, label: T) -> &mut Self {
|
|
|
|
self.span.push_span_label(span, label.into());
|
2016-10-11 12:26:32 -04:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn note_expected_found(&mut self,
|
|
|
|
label: &fmt::Display,
|
Highlight and simplify mismatched types
Shorten mismatched types errors by replacing subtypes that are not
different with `_`, and highlighting only the subtypes that are
different.
Given a file
```rust
struct X<T1, T2> {
x: T1,
y: T2,
}
fn foo() -> X<X<String, String>, String> {
X { x: X {x: "".to_string(), y: 2}, y: "".to_string()}
}
fn bar() -> Option<String> {
"".to_string()
}
```
provide the following output
```rust
error[E0308]: mismatched types
--> file.rs:6:5
|
6 | X { x: X {x: "".to_string(), y: 2}, y: "".to_string()}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct `std::string::String`, found {integer}
|
= note: expected type `X<X<_, std::string::String>, _>`
^^^^^^^^^^^^^^^^^^^ // < highlighted
found type `X<X<_, {integer}>, _>`
^^^^^^^^^ // < highlighted
error[E0308]: mismatched types
--> file.rs:6:5
|
10 | "".to_string()
| ^^^^^^^^^^^^^^ expected struct `std::option::Option`, found `std::string::String`
|
= note: expected type `Option<std::string::String>`
^^^^^^^ ^ // < highlighted
found type `std::string::String`
```
2017-02-17 14:31:59 -08:00
|
|
|
expected: DiagnosticStyledString,
|
|
|
|
found: DiagnosticStyledString)
|
2016-10-11 12:26:32 -04:00
|
|
|
-> &mut Self
|
|
|
|
{
|
|
|
|
self.note_expected_found_extra(label, expected, found, &"", &"")
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn note_expected_found_extra(&mut self,
|
|
|
|
label: &fmt::Display,
|
Highlight and simplify mismatched types
Shorten mismatched types errors by replacing subtypes that are not
different with `_`, and highlighting only the subtypes that are
different.
Given a file
```rust
struct X<T1, T2> {
x: T1,
y: T2,
}
fn foo() -> X<X<String, String>, String> {
X { x: X {x: "".to_string(), y: 2}, y: "".to_string()}
}
fn bar() -> Option<String> {
"".to_string()
}
```
provide the following output
```rust
error[E0308]: mismatched types
--> file.rs:6:5
|
6 | X { x: X {x: "".to_string(), y: 2}, y: "".to_string()}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct `std::string::String`, found {integer}
|
= note: expected type `X<X<_, std::string::String>, _>`
^^^^^^^^^^^^^^^^^^^ // < highlighted
found type `X<X<_, {integer}>, _>`
^^^^^^^^^ // < highlighted
error[E0308]: mismatched types
--> file.rs:6:5
|
10 | "".to_string()
| ^^^^^^^^^^^^^^ expected struct `std::option::Option`, found `std::string::String`
|
= note: expected type `Option<std::string::String>`
^^^^^^^ ^ // < highlighted
found type `std::string::String`
```
2017-02-17 14:31:59 -08:00
|
|
|
expected: DiagnosticStyledString,
|
|
|
|
found: DiagnosticStyledString,
|
2016-10-11 12:26:32 -04:00
|
|
|
expected_extra: &fmt::Display,
|
|
|
|
found_extra: &fmt::Display)
|
|
|
|
-> &mut Self
|
|
|
|
{
|
Highlight and simplify mismatched types
Shorten mismatched types errors by replacing subtypes that are not
different with `_`, and highlighting only the subtypes that are
different.
Given a file
```rust
struct X<T1, T2> {
x: T1,
y: T2,
}
fn foo() -> X<X<String, String>, String> {
X { x: X {x: "".to_string(), y: 2}, y: "".to_string()}
}
fn bar() -> Option<String> {
"".to_string()
}
```
provide the following output
```rust
error[E0308]: mismatched types
--> file.rs:6:5
|
6 | X { x: X {x: "".to_string(), y: 2}, y: "".to_string()}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct `std::string::String`, found {integer}
|
= note: expected type `X<X<_, std::string::String>, _>`
^^^^^^^^^^^^^^^^^^^ // < highlighted
found type `X<X<_, {integer}>, _>`
^^^^^^^^^ // < highlighted
error[E0308]: mismatched types
--> file.rs:6:5
|
10 | "".to_string()
| ^^^^^^^^^^^^^^ expected struct `std::option::Option`, found `std::string::String`
|
= note: expected type `Option<std::string::String>`
^^^^^^^ ^ // < highlighted
found type `std::string::String`
```
2017-02-17 14:31:59 -08:00
|
|
|
let mut msg: Vec<_> = vec![(format!("expected {} `", label), Style::NoStyle)];
|
|
|
|
msg.extend(expected.0.iter()
|
|
|
|
.map(|x| match *x {
|
|
|
|
StringPart::Normal(ref s) => (s.to_owned(), Style::NoStyle),
|
|
|
|
StringPart::Highlighted(ref s) => (s.to_owned(), Style::Highlight),
|
|
|
|
}));
|
|
|
|
msg.push((format!("`{}\n", expected_extra), Style::NoStyle));
|
|
|
|
msg.push((format!(" found {} `", label), Style::NoStyle));
|
|
|
|
msg.extend(found.0.iter()
|
|
|
|
.map(|x| match *x {
|
|
|
|
StringPart::Normal(ref s) => (s.to_owned(), Style::NoStyle),
|
|
|
|
StringPart::Highlighted(ref s) => (s.to_owned(), Style::Highlight),
|
|
|
|
}));
|
|
|
|
msg.push((format!("`{}", found_extra), Style::NoStyle));
|
|
|
|
|
2016-10-11 12:26:32 -04:00
|
|
|
// For now, just attach these as notes
|
Highlight and simplify mismatched types
Shorten mismatched types errors by replacing subtypes that are not
different with `_`, and highlighting only the subtypes that are
different.
Given a file
```rust
struct X<T1, T2> {
x: T1,
y: T2,
}
fn foo() -> X<X<String, String>, String> {
X { x: X {x: "".to_string(), y: 2}, y: "".to_string()}
}
fn bar() -> Option<String> {
"".to_string()
}
```
provide the following output
```rust
error[E0308]: mismatched types
--> file.rs:6:5
|
6 | X { x: X {x: "".to_string(), y: 2}, y: "".to_string()}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct `std::string::String`, found {integer}
|
= note: expected type `X<X<_, std::string::String>, _>`
^^^^^^^^^^^^^^^^^^^ // < highlighted
found type `X<X<_, {integer}>, _>`
^^^^^^^^^ // < highlighted
error[E0308]: mismatched types
--> file.rs:6:5
|
10 | "".to_string()
| ^^^^^^^^^^^^^^ expected struct `std::option::Option`, found `std::string::String`
|
= note: expected type `Option<std::string::String>`
^^^^^^^ ^ // < highlighted
found type `std::string::String`
```
2017-02-17 14:31:59 -08:00
|
|
|
self.highlighted_note(msg);
|
2016-10-11 12:26:32 -04:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
Show trait method signature when impl differs
When the trait's span is available, it is already being used, add a
`note` for the cases where the span isn't available:
```
error[E0053]: method `fmt` has an incompatible type for trait
--> $DIR/trait_type.rs:17:4
|
17 | fn fmt(&self, x: &str) -> () { }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ types differ in mutability
|
= note: expected type `fn(&MyType, &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error>`
found type `fn(&MyType, &str)`
error[E0050]: method `fmt` has 1 parameter but the declaration in trait `std::fmt::Display::fmt` has 2
--> $DIR/trait_type.rs:21:11
|
21 | fn fmt(&self) -> () { }
| ^^^^^ expected 2 parameters, found 1
|
= note: `fmt` from trait: `fn(&Self, &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error>`
error[E0186]: method `fmt` has a `&self` declaration in the trait, but not in the impl
--> $DIR/trait_type.rs:25:4
|
25 | fn fmt() -> () { }
| ^^^^^^^^^^^^^^^^^^ expected `&self` in impl
|
= note: `fmt` from trait: `fn(&Self, &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error>`
error[E0046]: not all trait items implemented, missing: `fmt`
--> $DIR/trait_type.rs:28:1
|
28 | impl std::fmt::Display for MyType4 {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ missing `fmt` in implementation
|
= note: `fmt` from trait: `fn(&Self, &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error>`
```
2017-05-31 23:14:43 -07:00
|
|
|
pub fn note_trait_signature(&mut self, name: String, signature: String) -> &mut Self {
|
|
|
|
self.highlighted_note(vec![
|
|
|
|
(format!("`{}` from trait: `", name), Style::NoStyle),
|
|
|
|
(signature, Style::Highlight),
|
|
|
|
("`".to_string(), Style::NoStyle)]);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2016-10-11 12:26:32 -04:00
|
|
|
pub fn note(&mut self, msg: &str) -> &mut Self {
|
|
|
|
self.sub(Level::Note, msg, MultiSpan::new(), None);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2017-01-11 13:55:41 -08:00
|
|
|
pub fn highlighted_note(&mut self, msg: Vec<(String, Style)>) -> &mut Self {
|
|
|
|
self.sub_with_highlights(Level::Note, msg, MultiSpan::new(), None);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2016-10-11 12:26:32 -04:00
|
|
|
pub fn span_note<S: Into<MultiSpan>>(&mut self,
|
|
|
|
sp: S,
|
|
|
|
msg: &str)
|
|
|
|
-> &mut Self {
|
|
|
|
self.sub(Level::Note, msg, sp.into(), None);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn warn(&mut self, msg: &str) -> &mut Self {
|
|
|
|
self.sub(Level::Warning, msg, MultiSpan::new(), None);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn span_warn<S: Into<MultiSpan>>(&mut self,
|
|
|
|
sp: S,
|
|
|
|
msg: &str)
|
|
|
|
-> &mut Self {
|
|
|
|
self.sub(Level::Warning, msg, sp.into(), None);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn help(&mut self , msg: &str) -> &mut Self {
|
|
|
|
self.sub(Level::Help, msg, MultiSpan::new(), None);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn span_help<S: Into<MultiSpan>>(&mut self,
|
|
|
|
sp: S,
|
|
|
|
msg: &str)
|
|
|
|
-> &mut Self {
|
|
|
|
self.sub(Level::Help, msg, sp.into(), None);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2017-07-16 11:43:24 -07:00
|
|
|
/// Prints out a message with a suggested edit of the code. If the suggestion is presented
|
|
|
|
/// inline it will only show the text message and not the text.
|
|
|
|
///
|
2017-09-29 23:38:33 -07:00
|
|
|
/// See `CodeSuggestion` for more information.
|
2017-07-16 11:43:24 -07:00
|
|
|
pub fn span_suggestion_short(&mut self, sp: Span, msg: &str, suggestion: String) -> &mut Self {
|
|
|
|
self.suggestions.push(CodeSuggestion {
|
2017-11-03 16:17:33 +01:00
|
|
|
substitutions: vec![Substitution {
|
|
|
|
parts: vec![SubstitutionPart {
|
|
|
|
snippet: suggestion,
|
|
|
|
span: sp,
|
|
|
|
}],
|
2017-07-16 11:43:24 -07:00
|
|
|
}],
|
|
|
|
msg: msg.to_owned(),
|
|
|
|
show_code_when_inline: false,
|
2018-04-24 15:42:27 -07:00
|
|
|
approximate: SuggestionApproximate::Unspecified,
|
2017-07-16 11:43:24 -07:00
|
|
|
});
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2016-10-11 12:26:32 -04:00
|
|
|
/// Prints out a message with a suggested edit of the code.
|
|
|
|
///
|
2017-05-16 15:12:24 +02:00
|
|
|
/// In case of short messages and a simple suggestion,
|
|
|
|
/// rustc displays it as a label like
|
|
|
|
///
|
|
|
|
/// "try adding parentheses: `(tup.0).1`"
|
|
|
|
///
|
|
|
|
/// The message
|
2017-10-24 13:08:30 +02:00
|
|
|
///
|
2017-05-16 15:12:24 +02:00
|
|
|
/// * should not end in any punctuation (a `:` is added automatically)
|
|
|
|
/// * should not be a question
|
|
|
|
/// * should not contain any parts like "the following", "as shown"
|
|
|
|
/// * may look like "to do xyz, use" or "to do xyz, use abc"
|
|
|
|
/// * may contain a name of a function, variable or type, but not whole expressions
|
|
|
|
///
|
2017-09-29 23:38:33 -07:00
|
|
|
/// See `CodeSuggestion` for more information.
|
2017-03-24 17:31:41 +01:00
|
|
|
pub fn span_suggestion(&mut self, sp: Span, msg: &str, suggestion: String) -> &mut Self {
|
2017-05-09 10:04:24 +02:00
|
|
|
self.suggestions.push(CodeSuggestion {
|
2017-11-03 16:17:33 +01:00
|
|
|
substitutions: vec![Substitution {
|
|
|
|
parts: vec![SubstitutionPart {
|
|
|
|
snippet: suggestion,
|
|
|
|
span: sp,
|
|
|
|
}],
|
2017-05-11 15:26:22 +02:00
|
|
|
}],
|
2017-05-09 10:04:24 +02:00
|
|
|
msg: msg.to_owned(),
|
2017-07-16 11:43:24 -07:00
|
|
|
show_code_when_inline: true,
|
2018-04-24 15:42:27 -07:00
|
|
|
approximate: SuggestionApproximate::Unspecified,
|
2017-05-09 10:04:24 +02:00
|
|
|
});
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2017-10-24 13:08:30 +02:00
|
|
|
/// Prints out a message with multiple suggested edits of the code.
|
2017-05-09 10:04:24 +02:00
|
|
|
pub fn span_suggestions(&mut self, sp: Span, msg: &str, suggestions: Vec<String>) -> &mut Self {
|
|
|
|
self.suggestions.push(CodeSuggestion {
|
2017-11-03 16:17:33 +01:00
|
|
|
substitutions: suggestions.into_iter().map(|snippet| Substitution {
|
|
|
|
parts: vec![SubstitutionPart {
|
|
|
|
snippet,
|
|
|
|
span: sp,
|
|
|
|
}],
|
|
|
|
}).collect(),
|
2017-03-24 17:31:41 +01:00
|
|
|
msg: msg.to_owned(),
|
2017-07-16 11:43:24 -07:00
|
|
|
show_code_when_inline: true,
|
2018-04-24 15:42:27 -07:00
|
|
|
approximate: SuggestionApproximate::Unspecified,
|
2018-01-18 17:17:46 +05:30
|
|
|
});
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This is a suggestion that may contain mistakes or fillers and should
|
|
|
|
/// be read and understood by a human.
|
|
|
|
pub fn span_approximate_suggestion(&mut self, sp: Span, msg: &str,
|
2018-04-24 15:42:27 -07:00
|
|
|
suggestion: String,
|
|
|
|
approximate: SuggestionApproximate) -> &mut Self {
|
2018-01-18 17:17:46 +05:30
|
|
|
self.suggestions.push(CodeSuggestion {
|
|
|
|
substitutions: vec![Substitution {
|
|
|
|
parts: vec![SubstitutionPart {
|
|
|
|
snippet: suggestion,
|
|
|
|
span: sp,
|
|
|
|
}],
|
|
|
|
}],
|
|
|
|
msg: msg.to_owned(),
|
|
|
|
show_code_when_inline: true,
|
2018-04-24 15:42:27 -07:00
|
|
|
approximate,
|
2018-01-18 17:17:46 +05:30
|
|
|
});
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn span_approximate_suggestions(&mut self, sp: Span, msg: &str,
|
2018-04-24 15:42:27 -07:00
|
|
|
suggestions: Vec<String>,
|
|
|
|
approximate: SuggestionApproximate) -> &mut Self {
|
2018-01-18 17:17:46 +05:30
|
|
|
self.suggestions.push(CodeSuggestion {
|
|
|
|
substitutions: suggestions.into_iter().map(|snippet| Substitution {
|
|
|
|
parts: vec![SubstitutionPart {
|
|
|
|
snippet,
|
|
|
|
span: sp,
|
|
|
|
}],
|
|
|
|
}).collect(),
|
|
|
|
msg: msg.to_owned(),
|
|
|
|
show_code_when_inline: true,
|
2018-04-24 15:42:27 -07:00
|
|
|
approximate,
|
2017-03-24 17:31:41 +01:00
|
|
|
});
|
2016-10-11 12:26:32 -04:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_span<S: Into<MultiSpan>>(&mut self, sp: S) -> &mut Self {
|
|
|
|
self.span = sp.into();
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2017-10-27 08:21:22 +02:00
|
|
|
pub fn code(&mut self, s: DiagnosticId) -> &mut Self {
|
2016-10-11 12:26:32 -04:00
|
|
|
self.code = Some(s);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2018-01-21 21:19:37 -08:00
|
|
|
pub fn get_code(&self) -> Option<DiagnosticId> {
|
|
|
|
self.code.clone()
|
|
|
|
}
|
|
|
|
|
2017-01-11 13:55:41 -08:00
|
|
|
pub fn message(&self) -> String {
|
|
|
|
self.message.iter().map(|i| i.0.to_owned()).collect::<String>()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn styled_message(&self) -> &Vec<(String, Style)> {
|
2016-10-11 12:26:32 -04:00
|
|
|
&self.message
|
|
|
|
}
|
|
|
|
|
2016-10-11 14:02:06 -04:00
|
|
|
/// Used by a lint. Copies over all details *but* the "main
|
|
|
|
/// message".
|
|
|
|
pub fn copy_details_not_message(&mut self, from: &Diagnostic) {
|
|
|
|
self.span = from.span.clone();
|
|
|
|
self.code = from.code.clone();
|
|
|
|
self.children.extend(from.children.iter().cloned())
|
|
|
|
}
|
|
|
|
|
2016-10-11 12:26:32 -04:00
|
|
|
/// Convenience function for internal use, clients should use one of the
|
|
|
|
/// public methods above.
|
2017-08-28 02:56:43 -07:00
|
|
|
pub(crate) fn sub(&mut self,
|
2016-10-11 12:26:32 -04:00
|
|
|
level: Level,
|
|
|
|
message: &str,
|
|
|
|
span: MultiSpan,
|
2017-11-16 16:36:49 +01:00
|
|
|
render_span: Option<MultiSpan>) {
|
2016-10-11 12:26:32 -04:00
|
|
|
let sub = SubDiagnostic {
|
2017-08-06 22:54:09 -07:00
|
|
|
level,
|
2017-01-11 13:55:41 -08:00
|
|
|
message: vec![(message.to_owned(), Style::NoStyle)],
|
2017-08-06 22:54:09 -07:00
|
|
|
span,
|
|
|
|
render_span,
|
2016-10-11 12:26:32 -04:00
|
|
|
};
|
|
|
|
self.children.push(sub);
|
|
|
|
}
|
2017-01-11 13:55:41 -08:00
|
|
|
|
|
|
|
/// Convenience function for internal use, clients should use one of the
|
|
|
|
/// public methods above.
|
|
|
|
fn sub_with_highlights(&mut self,
|
|
|
|
level: Level,
|
|
|
|
message: Vec<(String, Style)>,
|
|
|
|
span: MultiSpan,
|
2017-11-16 16:36:49 +01:00
|
|
|
render_span: Option<MultiSpan>) {
|
2017-01-11 13:55:41 -08:00
|
|
|
let sub = SubDiagnostic {
|
2017-08-06 22:54:09 -07:00
|
|
|
level,
|
|
|
|
message,
|
|
|
|
span,
|
|
|
|
render_span,
|
2017-01-11 13:55:41 -08:00
|
|
|
};
|
|
|
|
self.children.push(sub);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SubDiagnostic {
|
|
|
|
pub fn message(&self) -> String {
|
|
|
|
self.message.iter().map(|i| i.0.to_owned()).collect::<String>()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn styled_message(&self) -> &Vec<(String, Style)> {
|
|
|
|
&self.message
|
|
|
|
}
|
2016-10-11 12:26:32 -04:00
|
|
|
}
|