Skip to content

Commit b9c44eb

Browse files
committed
Use enum for approximate suggestions
1 parent f5203d1 commit b9c44eb

File tree

4 files changed

+29
-15
lines changed

4 files changed

+29
-15
lines changed

src/librustc_errors/diagnostic.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use CodeSuggestion;
1212
use SubstitutionPart;
1313
use Substitution;
14+
use SuggestionApproximate;
1415
use Level;
1516
use std::fmt;
1617
use syntax_pos::{MultiSpan, Span};
@@ -222,7 +223,7 @@ impl Diagnostic {
222223
}],
223224
msg: msg.to_owned(),
224225
show_code_when_inline: false,
225-
approximate: false,
226+
approximate: SuggestionApproximate::Unspecified,
226227
});
227228
self
228229
}
@@ -253,7 +254,7 @@ impl Diagnostic {
253254
}],
254255
msg: msg.to_owned(),
255256
show_code_when_inline: true,
256-
approximate: false,
257+
approximate: SuggestionApproximate::Unspecified,
257258
});
258259
self
259260
}
@@ -269,15 +270,16 @@ impl Diagnostic {
269270
}).collect(),
270271
msg: msg.to_owned(),
271272
show_code_when_inline: true,
272-
approximate: false,
273+
approximate: SuggestionApproximate::Unspecified,
273274
});
274275
self
275276
}
276277

277278
/// This is a suggestion that may contain mistakes or fillers and should
278279
/// be read and understood by a human.
279280
pub fn span_approximate_suggestion(&mut self, sp: Span, msg: &str,
280-
suggestion: String) -> &mut Self {
281+
suggestion: String,
282+
approximate: SuggestionApproximate) -> &mut Self {
281283
self.suggestions.push(CodeSuggestion {
282284
substitutions: vec![Substitution {
283285
parts: vec![SubstitutionPart {
@@ -287,13 +289,14 @@ impl Diagnostic {
287289
}],
288290
msg: msg.to_owned(),
289291
show_code_when_inline: true,
290-
approximate: true,
292+
approximate,
291293
});
292294
self
293295
}
294296

295297
pub fn span_approximate_suggestions(&mut self, sp: Span, msg: &str,
296-
suggestions: Vec<String>) -> &mut Self {
298+
suggestions: Vec<String>,
299+
approximate: SuggestionApproximate) -> &mut Self {
297300
self.suggestions.push(CodeSuggestion {
298301
substitutions: suggestions.into_iter().map(|snippet| Substitution {
299302
parts: vec![SubstitutionPart {
@@ -303,7 +306,7 @@ impl Diagnostic {
303306
}).collect(),
304307
msg: msg.to_owned(),
305308
show_code_when_inline: true,
306-
approximate: true,
309+
approximate,
307310
});
308311
self
309312
}

src/librustc_errors/diagnostic_builder.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Diagnostic;
1212
use DiagnosticId;
1313
use DiagnosticStyledString;
14+
use SuggestionApproximate;
1415

1516
use Level;
1617
use Handler;
@@ -190,12 +191,14 @@ impl<'a> DiagnosticBuilder<'a> {
190191
forward!(pub fn span_approximate_suggestion(&mut self,
191192
sp: Span,
192193
msg: &str,
193-
suggestion: String)
194+
suggestion: String,
195+
approximate: SuggestionApproximate)
194196
-> &mut Self);
195197
forward!(pub fn span_approximate_suggestions(&mut self,
196198
sp: Span,
197199
msg: &str,
198-
suggestions: Vec<String>)
200+
suggestions: Vec<String>,
201+
approximate: SuggestionApproximate)
199202
-> &mut Self);
200203
forward!(pub fn set_span<S: Into<MultiSpan>>(&mut self, sp: S) -> &mut Self);
201204
forward!(pub fn code(&mut self, s: DiagnosticId) -> &mut Self);

src/librustc_errors/lib.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ mod lock;
5656

5757
use syntax_pos::{BytePos, Loc, FileLinesResult, FileMap, FileName, MultiSpan, Span, NO_EXPANSION};
5858

59+
#[derive(Copy, Clone, Debug, PartialEq, Hash, RustcEncodable, RustcDecodable)]
60+
pub enum SuggestionApproximate {
61+
MachineApplicable,
62+
HasPlaceholders,
63+
MaybeIncorrect,
64+
Unspecified
65+
}
66+
5967
#[derive(Clone, Debug, PartialEq, Hash, RustcEncodable, RustcDecodable)]
6068
pub struct CodeSuggestion {
6169
/// Each substitute can have multiple variants due to multiple
@@ -87,7 +95,7 @@ pub struct CodeSuggestion {
8795
/// Sometimes we may show suggestions with placeholders,
8896
/// which are useful for users but not useful for
8997
/// tools like rustfix
90-
pub approximate: bool,
98+
pub approximate: SuggestionApproximate,
9199
}
92100

93101
#[derive(Clone, Debug, PartialEq, Hash, RustcEncodable, RustcDecodable)]

src/libsyntax/json.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use codemap::{CodeMap, FilePathMapping};
2323
use syntax_pos::{self, MacroBacktrace, Span, SpanLabel, MultiSpan};
2424
use errors::registry::Registry;
2525
use errors::{DiagnosticBuilder, SubDiagnostic, CodeSuggestion, CodeMapper};
26-
use errors::DiagnosticId;
26+
use errors::{DiagnosticId, SuggestionApproximate};
2727
use errors::emitter::{Emitter, EmitterWriter};
2828

2929
use rustc_data_structures::sync::{self, Lrc};
@@ -138,7 +138,7 @@ struct DiagnosticSpan {
138138
suggested_replacement: Option<String>,
139139
/// If the suggestion is approximate
140140
#[rustc_serialize_exclude_null]
141-
suggestion_approximate: Option<bool>,
141+
suggestion_approximate: Option<SuggestionApproximate>,
142142
/// Macro invocations that created the code at this span, if any.
143143
expansion: Option<Box<DiagnosticSpanMacroExpansion>>,
144144
}
@@ -239,7 +239,7 @@ impl Diagnostic {
239239

240240
impl DiagnosticSpan {
241241
fn from_span_label(span: SpanLabel,
242-
suggestion: Option<(&String, bool)>,
242+
suggestion: Option<(&String, SuggestionApproximate)>,
243243
je: &JsonEmitter)
244244
-> DiagnosticSpan {
245245
Self::from_span_etc(span.span,
@@ -252,7 +252,7 @@ impl DiagnosticSpan {
252252
fn from_span_etc(span: Span,
253253
is_primary: bool,
254254
label: Option<String>,
255-
suggestion: Option<(&String, bool)>,
255+
suggestion: Option<(&String, SuggestionApproximate)>,
256256
je: &JsonEmitter)
257257
-> DiagnosticSpan {
258258
// obtain the full backtrace from the `macro_backtrace`
@@ -272,7 +272,7 @@ impl DiagnosticSpan {
272272
fn from_span_full(span: Span,
273273
is_primary: bool,
274274
label: Option<String>,
275-
suggestion: Option<(&String, bool)>,
275+
suggestion: Option<(&String, SuggestionApproximate)>,
276276
mut backtrace: vec::IntoIter<MacroBacktrace>,
277277
je: &JsonEmitter)
278278
-> DiagnosticSpan {

0 commit comments

Comments
 (0)