Skip to content

Commit 644ce5e

Browse files
committed
Address PR reviews
1 parent f859f25 commit 644ce5e

File tree

4 files changed

+59
-40
lines changed

4 files changed

+59
-40
lines changed

src/librustc_errors/diagnostic.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// except according to those terms.
1010

1111
use CodeSuggestion;
12+
use Substitution;
1213
use Level;
1314
use RenderSpan;
1415
use std::fmt;
@@ -205,15 +206,21 @@ impl Diagnostic {
205206
/// See `diagnostic::CodeSuggestion` for more information.
206207
pub fn span_suggestion(&mut self, sp: Span, msg: &str, suggestion: String) -> &mut Self {
207208
self.suggestions.push(CodeSuggestion {
208-
substitutes: vec![(sp, vec![suggestion])],
209+
substitution_parts: vec![Substitution {
210+
span: sp,
211+
substitutions: vec![suggestion],
212+
}],
209213
msg: msg.to_owned(),
210214
});
211215
self
212216
}
213217

214218
pub fn span_suggestions(&mut self, sp: Span, msg: &str, suggestions: Vec<String>) -> &mut Self {
215219
self.suggestions.push(CodeSuggestion {
216-
substitutes: vec![(sp, suggestions)],
220+
substitution_parts: vec![Substitution {
221+
span: sp,
222+
substitutions: suggestions,
223+
}],
217224
msg: msg.to_owned(),
218225
});
219226
self

src/librustc_errors/emitter.rs

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -35,38 +35,32 @@ impl Emitter for EmitterWriter {
3535
let mut primary_span = db.span.clone();
3636
let mut children = db.children.clone();
3737

38-
if db.suggestions.len() == 1 {
39-
let sugg = &db.suggestions[0];
40-
// don't display multispans as labels
41-
if sugg.substitutes.len() == 1 &&
38+
if let Some((sugg, rest)) = db.suggestions.split_first() {
39+
if rest.is_empty() &&
40+
// don't display multipart suggestions as labels
41+
sugg.substitution_parts.len() == 1 &&
4242
// don't display multi-suggestions as labels
43-
sugg.substitutes[0].1.len() == 1 &&
43+
sugg.substitutions() == 1 &&
4444
// don't display long messages as labels
4545
sugg.msg.split_whitespace().count() < 10 &&
4646
// don't display multiline suggestions as labels
47-
sugg.substitutes[0].1[0].find('\n').is_none() {
48-
let msg = format!("help: {} `{}`", sugg.msg, sugg.substitutes[0].1[0]);
49-
primary_span.push_span_label(sugg.substitutes[0].0, msg);
47+
sugg.substitution_parts[0].substitutions[0].find('\n').is_none() {
48+
let substitution = &sugg.substitution_parts[0].substitutions[0];
49+
let msg = format!("help: {} `{}`", sugg.msg, substitution);
50+
primary_span.push_span_label(sugg.substitution_spans().next().unwrap(), msg);
5051
} else {
51-
children.push(SubDiagnostic {
52-
level: Level::Help,
53-
message: Vec::new(),
54-
span: MultiSpan::new(),
55-
render_span: Some(Suggestion(sugg.clone())),
56-
});
57-
}
58-
} else {
59-
// if there are multiple suggestions, print them all in full
60-
// to be consistent. We could try to figure out if we can
61-
// make one (or the first one) inline, but that would give
62-
// undue importance to a semi-random suggestion
63-
for sugg in &db.suggestions {
64-
children.push(SubDiagnostic {
65-
level: Level::Help,
66-
message: Vec::new(),
67-
span: MultiSpan::new(),
68-
render_span: Some(Suggestion(sugg.clone())),
69-
});
52+
// if there are multiple suggestions, print them all in full
53+
// to be consistent. We could try to figure out if we can
54+
// make one (or the first one) inline, but that would give
55+
// undue importance to a semi-random suggestion
56+
for sugg in &db.suggestions {
57+
children.push(SubDiagnostic {
58+
level: Level::Help,
59+
message: Vec::new(),
60+
span: MultiSpan::new(),
61+
render_span: Some(Suggestion(sugg.clone())),
62+
});
63+
}
7064
}
7165
}
7266

@@ -1073,7 +1067,7 @@ impl EmitterWriter {
10731067
-> io::Result<()> {
10741068
use std::borrow::Borrow;
10751069

1076-
let primary_span = suggestion.substitutes[0].0;
1070+
let primary_span = suggestion.substitution_spans().next().unwrap();
10771071
if let Some(ref cm) = self.cm {
10781072
let mut buffer = StyledBuffer::new();
10791073

src/librustc_errors/lib.rs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#![feature(staged_api)]
2424
#![feature(range_contains)]
2525
#![feature(libc)]
26+
#![feature(conservative_impl_trait)]
2627

2728
extern crate term;
2829
extern crate libc;
@@ -83,10 +84,17 @@ pub struct CodeSuggestion {
8384
/// ```
8485
/// vec![(0..7, vec!["a.b", "x.y"])]
8586
/// ```
86-
pub substitutes: Vec<(Span, Vec<String>)>,
87+
pub substitution_parts: Vec<Substitution>,
8788
pub msg: String,
8889
}
8990

91+
#[derive(Clone, Debug, PartialEq, RustcEncodable, RustcDecodable)]
92+
/// See the docs on `CodeSuggestion::substitutions`
93+
pub struct Substitution {
94+
pub span: Span,
95+
pub substitutions: Vec<String>,
96+
}
97+
9098
pub trait CodeMapper {
9199
fn lookup_char_pos(&self, pos: BytePos) -> Loc;
92100
fn span_to_lines(&self, sp: Span) -> FileLinesResult;
@@ -96,6 +104,16 @@ pub trait CodeMapper {
96104
}
97105

98106
impl CodeSuggestion {
107+
/// Returns the number of substitutions
108+
fn substitutions(&self) -> usize {
109+
self.substitution_parts[0].substitutions.len()
110+
}
111+
112+
/// Returns the number of substitutions
113+
pub fn substitution_spans<'a>(&'a self) -> impl Iterator<Item = Span> + 'a {
114+
self.substitution_parts.iter().map(|sub| sub.span)
115+
}
116+
99117
/// Returns the assembled code suggestions.
100118
pub fn splice_lines(&self, cm: &CodeMapper) -> Vec<String> {
101119
use syntax_pos::{CharPos, Loc, Pos};
@@ -119,13 +137,13 @@ impl CodeSuggestion {
119137
}
120138
}
121139

122-
if self.substitutes.is_empty() {
140+
if self.substitution_parts.is_empty() {
123141
return vec![String::new()];
124142
}
125143

126-
let mut primary_spans: Vec<_> = self.substitutes
144+
let mut primary_spans: Vec<_> = self.substitution_parts
127145
.iter()
128-
.map(|&(sp, ref sub)| (sp, sub))
146+
.map(|sub| (sub.span, &sub.substitutions))
129147
.collect();
130148

131149
// Assumption: all spans are in the same file, and all spans
@@ -157,7 +175,7 @@ impl CodeSuggestion {
157175
prev_hi.col = CharPos::from_usize(0);
158176

159177
let mut prev_line = fm.get_line(lines.lines[0].line_index);
160-
let mut bufs = vec![String::new(); self.substitutes[0].1.len()];
178+
let mut bufs = vec![String::new(); self.substitutions()];
161179

162180
for (sp, substitutes) in primary_spans {
163181
let cur_lo = cm.lookup_char_pos(sp.lo);

src/libsyntax/json.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -279,12 +279,12 @@ impl DiagnosticSpan {
279279

280280
fn from_suggestion(suggestion: &CodeSuggestion, je: &JsonEmitter)
281281
-> Vec<DiagnosticSpan> {
282-
suggestion.substitutes
282+
suggestion.substitution_parts
283283
.iter()
284-
.flat_map(|&(span, ref suggestion)| {
285-
suggestion.iter().map(move |suggestion| {
284+
.flat_map(|substitution| {
285+
substitution.substitutions.iter().map(move |suggestion| {
286286
let span_label = SpanLabel {
287-
span,
287+
span: substitution.span,
288288
is_primary: true,
289289
label: None,
290290
};
@@ -301,7 +301,7 @@ impl DiagnosticSpan {
301301
RenderSpan::FullSpan(ref msp) =>
302302
DiagnosticSpan::from_multispan(msp, je),
303303
// regular diagnostics don't produce this anymore
304-
// will be removed in a later commit
304+
// FIXME(oli_obk): remove it entirely
305305
RenderSpan::Suggestion(_) => unreachable!(),
306306
}
307307
}

0 commit comments

Comments
 (0)