Skip to content

Commit 560a2f4

Browse files
committed
Auto merge of #45752 - estebank:highlight-primary, r=nikomatsakis
Highlight code on diagnostics when underlined Highlight the label's span with the respective color: <img width="692" alt="" src="https://user-images.githubusercontent.com/1606434/32411026-a1842482-c18d-11e7-9933-6510eefbad19.png"> Fix #42112.
2 parents b8f2674 + 08287c1 commit 560a2f4

File tree

9 files changed

+59
-17
lines changed

9 files changed

+59
-17
lines changed

src/librustc/session/mod.rs

+11-8
Original file line numberDiff line numberDiff line change
@@ -904,10 +904,13 @@ pub fn build_session_with_codemap(sopts: config::Options,
904904

905905
let emitter: Box<Emitter> = match (sopts.error_format, emitter_dest) {
906906
(config::ErrorOutputType::HumanReadable(color_config), None) => {
907-
Box::new(EmitterWriter::stderr(color_config, Some(codemap.clone()), false))
907+
Box::new(EmitterWriter::stderr(color_config,
908+
Some(codemap.clone()),
909+
false,
910+
sopts.debugging_opts.teach))
908911
}
909912
(config::ErrorOutputType::HumanReadable(_), Some(dst)) => {
910-
Box::new(EmitterWriter::new(dst, Some(codemap.clone()), false))
913+
Box::new(EmitterWriter::new(dst, Some(codemap.clone()), false, false))
911914
}
912915
(config::ErrorOutputType::Json(pretty), None) => {
913916
Box::new(JsonEmitter::stderr(Some(registry), codemap.clone(), pretty))
@@ -916,10 +919,10 @@ pub fn build_session_with_codemap(sopts: config::Options,
916919
Box::new(JsonEmitter::new(dst, Some(registry), codemap.clone(), pretty))
917920
}
918921
(config::ErrorOutputType::Short(color_config), None) => {
919-
Box::new(EmitterWriter::stderr(color_config, Some(codemap.clone()), true))
922+
Box::new(EmitterWriter::stderr(color_config, Some(codemap.clone()), true, false))
920923
}
921924
(config::ErrorOutputType::Short(_), Some(dst)) => {
922-
Box::new(EmitterWriter::new(dst, Some(codemap.clone()), true))
925+
Box::new(EmitterWriter::new(dst, Some(codemap.clone()), true, false))
923926
}
924927
};
925928

@@ -1095,11 +1098,11 @@ pub enum IncrCompSession {
10951098
pub fn early_error(output: config::ErrorOutputType, msg: &str) -> ! {
10961099
let emitter: Box<Emitter> = match output {
10971100
config::ErrorOutputType::HumanReadable(color_config) => {
1098-
Box::new(EmitterWriter::stderr(color_config, None, false))
1101+
Box::new(EmitterWriter::stderr(color_config, None, false, false))
10991102
}
11001103
config::ErrorOutputType::Json(pretty) => Box::new(JsonEmitter::basic(pretty)),
11011104
config::ErrorOutputType::Short(color_config) => {
1102-
Box::new(EmitterWriter::stderr(color_config, None, true))
1105+
Box::new(EmitterWriter::stderr(color_config, None, true, false))
11031106
}
11041107
};
11051108
let handler = errors::Handler::with_emitter(true, false, emitter);
@@ -1110,11 +1113,11 @@ pub fn early_error(output: config::ErrorOutputType, msg: &str) -> ! {
11101113
pub fn early_warn(output: config::ErrorOutputType, msg: &str) {
11111114
let emitter: Box<Emitter> = match output {
11121115
config::ErrorOutputType::HumanReadable(color_config) => {
1113-
Box::new(EmitterWriter::stderr(color_config, None, false))
1116+
Box::new(EmitterWriter::stderr(color_config, None, false, false))
11141117
}
11151118
config::ErrorOutputType::Json(pretty) => Box::new(JsonEmitter::basic(pretty)),
11161119
config::ErrorOutputType::Short(color_config) => {
1117-
Box::new(EmitterWriter::stderr(color_config, None, true))
1120+
Box::new(EmitterWriter::stderr(color_config, None, true, false))
11181121
}
11191122
};
11201123
let handler = errors::Handler::with_emitter(true, false, emitter);

src/librustc_driver/lib.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,8 @@ pub fn run<F>(run_compiler: F) -> isize
167167
let emitter =
168168
errors::emitter::EmitterWriter::stderr(errors::ColorConfig::Auto,
169169
None,
170-
true);
170+
true,
171+
false);
171172
let handler = errors::Handler::with_emitter(true, false, Box::new(emitter));
172173
handler.emit(&MultiSpan::new(),
173174
"aborting due to previous error(s)",
@@ -1434,6 +1435,7 @@ pub fn monitor<F: FnOnce() + Send + 'static>(f: F) {
14341435
let emitter =
14351436
Box::new(errors::emitter::EmitterWriter::stderr(errors::ColorConfig::Auto,
14361437
None,
1438+
false,
14371439
false));
14381440
let handler = errors::Handler::with_emitter(true, false, emitter);
14391441

src/librustc_errors/emitter.rs

+19-6
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ pub struct EmitterWriter {
106106
dst: Destination,
107107
cm: Option<Rc<CodeMapper>>,
108108
short_message: bool,
109+
teach: bool,
109110
}
110111

111112
struct FileWithAnnotatedLines {
@@ -117,32 +118,37 @@ struct FileWithAnnotatedLines {
117118
impl EmitterWriter {
118119
pub fn stderr(color_config: ColorConfig,
119120
code_map: Option<Rc<CodeMapper>>,
120-
short_message: bool)
121+
short_message: bool,
122+
teach: bool)
121123
-> EmitterWriter {
122124
if color_config.use_color() {
123125
let dst = Destination::from_stderr();
124126
EmitterWriter {
125127
dst,
126128
cm: code_map,
127-
short_message: short_message,
129+
short_message,
130+
teach,
128131
}
129132
} else {
130133
EmitterWriter {
131134
dst: Raw(Box::new(io::stderr())),
132135
cm: code_map,
133-
short_message: short_message,
136+
short_message,
137+
teach,
134138
}
135139
}
136140
}
137141

138142
pub fn new(dst: Box<Write + Send>,
139143
code_map: Option<Rc<CodeMapper>>,
140-
short_message: bool)
144+
short_message: bool,
145+
teach: bool)
141146
-> EmitterWriter {
142147
EmitterWriter {
143148
dst: Raw(dst),
144149
cm: code_map,
145-
short_message: short_message,
150+
short_message,
151+
teach,
146152
}
147153
}
148154

@@ -551,7 +557,14 @@ impl EmitterWriter {
551557
code_offset + annotation.start_col,
552558
style);
553559
}
554-
_ => (),
560+
_ if self.teach => {
561+
buffer.set_style_range(line_offset,
562+
code_offset + annotation.start_col,
563+
code_offset + annotation.end_col,
564+
style,
565+
annotation.is_primary);
566+
}
567+
_ => {}
555568
}
556569
}
557570

src/librustc_errors/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ impl Handler {
297297
cm: Option<Rc<CodeMapper>>,
298298
flags: HandlerFlags)
299299
-> Handler {
300-
let emitter = Box::new(EmitterWriter::stderr(color_config, cm, false));
300+
let emitter = Box::new(EmitterWriter::stderr(color_config, cm, false, false));
301301
Handler::with_emitter_and_flags(emitter, flags)
302302
}
303303

src/librustc_errors/styled_buffer.rs

+21
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,25 @@ impl StyledBuffer {
144144
pub fn num_lines(&self) -> usize {
145145
self.text.len()
146146
}
147+
148+
pub fn set_style_range(&mut self,
149+
line: usize,
150+
col_start: usize,
151+
col_end: usize,
152+
style: Style,
153+
overwrite: bool) {
154+
for col in col_start..col_end {
155+
self.set_style(line, col, style, overwrite);
156+
}
157+
}
158+
159+
pub fn set_style(&mut self, line: usize, col: usize, style: Style, overwrite: bool) {
160+
if let Some(ref mut line) = self.styles.get_mut(line) {
161+
if let Some(s) = line.get_mut(col) {
162+
if *s == Style::NoStyle || *s == Style::Quotation || overwrite {
163+
*s = style;
164+
}
165+
}
166+
}
167+
}
147168
}

src/librustdoc/test.rs

+1
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ fn run_test(test: &str, cratename: &str, filename: &FileName, line: usize,
238238
));
239239
let emitter = errors::emitter::EmitterWriter::new(box Sink(data.clone()),
240240
Some(codemap.clone()),
241+
false,
241242
false);
242243
let old = io::set_panic(Some(box Sink(data.clone())));
243244
let _bomb = Bomb(data.clone(), old.unwrap_or(box io::stdout()));

src/libsyntax/json.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ impl Diagnostic {
188188
}
189189
let buf = BufWriter::default();
190190
let output = buf.clone();
191-
EmitterWriter::new(Box::new(buf), Some(je.cm.clone()), false).emit(db);
191+
EmitterWriter::new(Box::new(buf), Some(je.cm.clone()), false, false).emit(db);
192192
let output = Arc::try_unwrap(output.0).unwrap().into_inner().unwrap();
193193
let output = String::from_utf8(output).unwrap();
194194

src/libsyntax/parse/lexer/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1745,6 +1745,7 @@ mod tests {
17451745
fn mk_sess(cm: Rc<CodeMap>) -> ParseSess {
17461746
let emitter = errors::emitter::EmitterWriter::new(Box::new(io::sink()),
17471747
Some(cm.clone()),
1748+
false,
17481749
false);
17491750
ParseSess {
17501751
span_diagnostic: errors::Handler::with_emitter(true, false, Box::new(emitter)),

src/libsyntax/test_snippet.rs

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ fn test_harness(file_text: &str, span_labels: Vec<SpanLabel>, expected_output: &
6262

6363
let emitter = EmitterWriter::new(Box::new(Shared { data: output.clone() }),
6464
Some(code_map.clone()),
65+
false,
6566
false);
6667
let handler = Handler::with_emitter(true, false, Box::new(emitter));
6768
handler.span_err(msp, "foo");

0 commit comments

Comments
 (0)