Skip to content

Commit 0e920fd

Browse files
committed
Minimize single span suggestions into a note
1 parent 0777c75 commit 0e920fd

File tree

6 files changed

+47
-28
lines changed

6 files changed

+47
-28
lines changed

src/librustc_errors/diagnostic.rs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use CodeSuggestion;
1212
use Level;
1313
use RenderSpan;
14-
use RenderSpan::Suggestion;
1514
use std::fmt;
1615
use syntax_pos::{MultiSpan, Span};
1716
use snippet::Style;
@@ -24,6 +23,7 @@ pub struct Diagnostic {
2423
pub code: Option<String>,
2524
pub span: MultiSpan,
2625
pub children: Vec<SubDiagnostic>,
26+
pub suggestion: Option<CodeSuggestion>,
2727
}
2828

2929
/// For example a note attached to an error.
@@ -87,6 +87,7 @@ impl Diagnostic {
8787
code: code,
8888
span: MultiSpan::new(),
8989
children: vec![],
90+
suggestion: None,
9091
}
9192
}
9293

@@ -202,19 +203,14 @@ impl Diagnostic {
202203

203204
/// Prints out a message with a suggested edit of the code.
204205
///
205-
/// See `diagnostic::RenderSpan::Suggestion` for more information.
206-
pub fn span_suggestion<S: Into<MultiSpan>>(&mut self,
207-
sp: S,
208-
msg: &str,
209-
suggestion: String)
210-
-> &mut Self {
211-
self.sub(Level::Help,
212-
msg,
213-
MultiSpan::new(),
214-
Some(Suggestion(CodeSuggestion {
215-
msp: sp.into(),
216-
substitutes: vec![suggestion],
217-
})));
206+
/// See `diagnostic::CodeSuggestion` for more information.
207+
pub fn span_suggestion(&mut self, sp: Span, msg: &str, suggestion: String) -> &mut Self {
208+
assert!(self.suggestion.is_none());
209+
self.suggestion = Some(CodeSuggestion {
210+
msp: sp.into(),
211+
substitutes: vec![suggestion],
212+
msg: msg.to_owned(),
213+
});
218214
self
219215
}
220216

src/librustc_errors/diagnostic_builder.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,11 @@ impl<'a> DiagnosticBuilder<'a> {
141141
sp: S,
142142
msg: &str)
143143
-> &mut Self);
144-
forward!(pub fn span_suggestion<S: Into<MultiSpan>>(&mut self,
145-
sp: S,
146-
msg: &str,
147-
suggestion: String)
148-
-> &mut Self);
144+
forward!(pub fn span_suggestion(&mut self,
145+
sp: Span,
146+
msg: &str,
147+
suggestion: String)
148+
-> &mut Self);
149149
forward!(pub fn set_span<S: Into<MultiSpan>>(&mut self, sp: S) -> &mut Self);
150150
forward!(pub fn code(&mut self, s: String) -> &mut Self);
151151

src/librustc_errors/emitter.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,22 @@ impl Emitter for EmitterWriter {
3434
fn emit(&mut self, db: &DiagnosticBuilder) {
3535
let mut primary_span = db.span.clone();
3636
let mut children = db.children.clone();
37+
38+
if let Some(sugg) = db.suggestion.clone() {
39+
assert_eq!(sugg.msp.primary_spans().len(), sugg.substitutes.len());
40+
if sugg.substitutes.len() == 1 {
41+
let msg = format!("{} `{}`", sugg.msg, sugg.substitutes[0]);
42+
primary_span.push_span_label(sugg.msp.primary_spans()[0], msg);
43+
} else {
44+
children.push(SubDiagnostic {
45+
level: Level::Help,
46+
message: Vec::new(),
47+
span: MultiSpan::new(),
48+
render_span: Some(Suggestion(sugg)),
49+
});
50+
}
51+
}
52+
3753
self.fix_multispans_in_std_macros(&mut primary_span, &mut children);
3854
self.emit_messages_default(&db.level,
3955
&db.styled_message(),
@@ -756,7 +772,7 @@ impl EmitterWriter {
756772
/// displayed, keeping the provided highlighting.
757773
fn msg_to_buffer(&self,
758774
buffer: &mut StyledBuffer,
759-
msg: &Vec<(String, Style)>,
775+
msg: &[(String, Style)],
760776
padding: usize,
761777
label: &str,
762778
override_style: Option<Style>) {
@@ -1022,7 +1038,6 @@ impl EmitterWriter {
10221038
fn emit_suggestion_default(&mut self,
10231039
suggestion: &CodeSuggestion,
10241040
level: &Level,
1025-
msg: &Vec<(String, Style)>,
10261041
max_line_num_len: usize)
10271042
-> io::Result<()> {
10281043
use std::borrow::Borrow;
@@ -1034,7 +1049,7 @@ impl EmitterWriter {
10341049
buffer.append(0, &level.to_string(), Style::Level(level.clone()));
10351050
buffer.append(0, ": ", Style::HeaderMsg);
10361051
self.msg_to_buffer(&mut buffer,
1037-
msg,
1052+
&[(suggestion.msg.to_owned(), Style::NoStyle)],
10381053
max_line_num_len,
10391054
"suggestion",
10401055
Some(Style::HeaderMsg));
@@ -1099,7 +1114,6 @@ impl EmitterWriter {
10991114
Some(Suggestion(ref cs)) => {
11001115
match self.emit_suggestion_default(cs,
11011116
&child.level,
1102-
&child.styled_message(),
11031117
max_line_num_len) {
11041118
Err(e) => panic!("failed to emit error: {}", e),
11051119
_ => ()

src/librustc_errors/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ pub enum RenderSpan {
6767
pub struct CodeSuggestion {
6868
pub msp: MultiSpan,
6969
pub substitutes: Vec<String>,
70+
pub msg: String,
7071
}
7172

7273
pub trait CodeMapper {

src/librustc_typeck/check/mod.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3804,9 +3804,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
38043804
let snip = tcx.sess.codemap().span_to_snippet(base.span);
38053805
if let Ok(snip) = snip {
38063806
err.span_suggestion(expr.span,
3807-
"to access tuple elements, \
3808-
use tuple indexing syntax \
3809-
as shown",
3807+
"to access tuple elements, use",
38103808
format!("{}.{}", snip, i));
38113809
needs_note = false;
38123810
}

src/libsyntax/json.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@
2222
use codemap::CodeMap;
2323
use syntax_pos::{self, MacroBacktrace, Span, SpanLabel, MultiSpan};
2424
use errors::registry::Registry;
25-
use errors::{DiagnosticBuilder, SubDiagnostic, RenderSpan, CodeSuggestion, CodeMapper};
25+
use errors::{Level, DiagnosticBuilder, SubDiagnostic, RenderSpan, CodeSuggestion, CodeMapper};
2626
use errors::emitter::Emitter;
27+
use errors::snippet::Style;
2728

2829
use std::rc::Rc;
2930
use std::io::{self, Write};
@@ -152,12 +153,21 @@ impl Diagnostic {
152153
fn from_diagnostic_builder(db: &DiagnosticBuilder,
153154
je: &JsonEmitter)
154155
-> Diagnostic {
156+
let sugg = db.suggestion.as_ref().map(|sugg| {
157+
SubDiagnostic {
158+
level: Level::Help,
159+
message: vec![(sugg.msg.clone(), Style::NoStyle)],
160+
span: MultiSpan::new(),
161+
render_span: Some(RenderSpan::Suggestion(sugg.clone())),
162+
}
163+
});
164+
let sugg = sugg.as_ref();
155165
Diagnostic {
156166
message: db.message(),
157167
code: DiagnosticCode::map_opt_string(db.code.clone(), je),
158168
level: db.level.to_str(),
159169
spans: DiagnosticSpan::from_multispan(&db.span, je),
160-
children: db.children.iter().map(|c| {
170+
children: db.children.iter().chain(sugg).map(|c| {
161171
Diagnostic::from_sub_diagnostic(c, je)
162172
}).collect(),
163173
rendered: None,

0 commit comments

Comments
 (0)