Skip to content

Highlight code on diagnostics when underlined #45752

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions src/librustc/session/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -904,10 +904,13 @@ pub fn build_session_with_codemap(sopts: config::Options,

let emitter: Box<Emitter> = match (sopts.error_format, emitter_dest) {
(config::ErrorOutputType::HumanReadable(color_config), None) => {
Box::new(EmitterWriter::stderr(color_config, Some(codemap.clone()), false))
Box::new(EmitterWriter::stderr(color_config,
Some(codemap.clone()),
false,
sopts.debugging_opts.teach))
}
(config::ErrorOutputType::HumanReadable(_), Some(dst)) => {
Box::new(EmitterWriter::new(dst, Some(codemap.clone()), false))
Box::new(EmitterWriter::new(dst, Some(codemap.clone()), false, false))
}
(config::ErrorOutputType::Json(pretty), None) => {
Box::new(JsonEmitter::stderr(Some(registry), codemap.clone(), pretty))
Expand All @@ -916,10 +919,10 @@ pub fn build_session_with_codemap(sopts: config::Options,
Box::new(JsonEmitter::new(dst, Some(registry), codemap.clone(), pretty))
}
(config::ErrorOutputType::Short(color_config), None) => {
Box::new(EmitterWriter::stderr(color_config, Some(codemap.clone()), true))
Box::new(EmitterWriter::stderr(color_config, Some(codemap.clone()), true, false))
}
(config::ErrorOutputType::Short(_), Some(dst)) => {
Box::new(EmitterWriter::new(dst, Some(codemap.clone()), true))
Box::new(EmitterWriter::new(dst, Some(codemap.clone()), true, false))
}
};

Expand Down Expand Up @@ -1095,11 +1098,11 @@ pub enum IncrCompSession {
pub fn early_error(output: config::ErrorOutputType, msg: &str) -> ! {
let emitter: Box<Emitter> = match output {
config::ErrorOutputType::HumanReadable(color_config) => {
Box::new(EmitterWriter::stderr(color_config, None, false))
Box::new(EmitterWriter::stderr(color_config, None, false, false))
}
config::ErrorOutputType::Json(pretty) => Box::new(JsonEmitter::basic(pretty)),
config::ErrorOutputType::Short(color_config) => {
Box::new(EmitterWriter::stderr(color_config, None, true))
Box::new(EmitterWriter::stderr(color_config, None, true, false))
}
};
let handler = errors::Handler::with_emitter(true, false, emitter);
Expand All @@ -1110,11 +1113,11 @@ pub fn early_error(output: config::ErrorOutputType, msg: &str) -> ! {
pub fn early_warn(output: config::ErrorOutputType, msg: &str) {
let emitter: Box<Emitter> = match output {
config::ErrorOutputType::HumanReadable(color_config) => {
Box::new(EmitterWriter::stderr(color_config, None, false))
Box::new(EmitterWriter::stderr(color_config, None, false, false))
}
config::ErrorOutputType::Json(pretty) => Box::new(JsonEmitter::basic(pretty)),
config::ErrorOutputType::Short(color_config) => {
Box::new(EmitterWriter::stderr(color_config, None, true))
Box::new(EmitterWriter::stderr(color_config, None, true, false))
}
};
let handler = errors::Handler::with_emitter(true, false, emitter);
Expand Down
4 changes: 3 additions & 1 deletion src/librustc_driver/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ pub fn run<F>(run_compiler: F) -> isize
let emitter =
errors::emitter::EmitterWriter::stderr(errors::ColorConfig::Auto,
None,
true);
true,
false);
let handler = errors::Handler::with_emitter(true, false, Box::new(emitter));
handler.emit(&MultiSpan::new(),
"aborting due to previous error(s)",
Expand Down Expand Up @@ -1241,6 +1242,7 @@ pub fn monitor<F: FnOnce() + Send + 'static>(f: F) {
let emitter =
Box::new(errors::emitter::EmitterWriter::stderr(errors::ColorConfig::Auto,
None,
false,
false));
let handler = errors::Handler::with_emitter(true, false, emitter);

Expand Down
25 changes: 19 additions & 6 deletions src/librustc_errors/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ pub struct EmitterWriter {
dst: Destination,
cm: Option<Rc<CodeMapper>>,
short_message: bool,
teach: bool,
}

struct FileWithAnnotatedLines {
Expand All @@ -117,32 +118,37 @@ struct FileWithAnnotatedLines {
impl EmitterWriter {
pub fn stderr(color_config: ColorConfig,
code_map: Option<Rc<CodeMapper>>,
short_message: bool)
short_message: bool,
teach: bool)
-> EmitterWriter {
if color_config.use_color() {
let dst = Destination::from_stderr();
EmitterWriter {
dst,
cm: code_map,
short_message: short_message,
short_message,
teach,
}
} else {
EmitterWriter {
dst: Raw(Box::new(io::stderr())),
cm: code_map,
short_message: short_message,
short_message,
teach,
}
}
}

pub fn new(dst: Box<Write + Send>,
code_map: Option<Rc<CodeMapper>>,
short_message: bool)
short_message: bool,
teach: bool)
-> EmitterWriter {
EmitterWriter {
dst: Raw(dst),
cm: code_map,
short_message: short_message,
short_message,
teach,
}
}

Expand Down Expand Up @@ -551,7 +557,14 @@ impl EmitterWriter {
code_offset + annotation.start_col,
style);
}
_ => (),
_ if self.teach => {
buffer.set_style_range(line_offset,
code_offset + annotation.start_col,
code_offset + annotation.end_col,
style,
annotation.is_primary);
}
_ => {}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/librustc_errors/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ impl Handler {
cm: Option<Rc<CodeMapper>>,
flags: HandlerFlags)
-> Handler {
let emitter = Box::new(EmitterWriter::stderr(color_config, cm, false));
let emitter = Box::new(EmitterWriter::stderr(color_config, cm, false, false));
Handler::with_emitter_and_flags(emitter, flags)
}

Expand Down
21 changes: 21 additions & 0 deletions src/librustc_errors/styled_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,25 @@ impl StyledBuffer {
pub fn num_lines(&self) -> usize {
self.text.len()
}

pub fn set_style_range(&mut self,
line: usize,
col_start: usize,
col_end: usize,
style: Style,
overwrite: bool) {
for col in col_start..col_end {
self.set_style(line, col, style, overwrite);
}
}

pub fn set_style(&mut self, line: usize, col: usize, style: Style, overwrite: bool) {
if let Some(ref mut line) = self.styles.get_mut(line) {
if let Some(s) = line.get_mut(col) {
if *s == Style::NoStyle || *s == Style::Quotation || overwrite {
*s = style;
}
}
}
}
}
1 change: 1 addition & 0 deletions src/librustdoc/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ fn run_test(test: &str, cratename: &str, filename: &FileName, line: usize,
));
let emitter = errors::emitter::EmitterWriter::new(box Sink(data.clone()),
Some(codemap.clone()),
false,
false);
let old = io::set_panic(Some(box Sink(data.clone())));
let _bomb = Bomb(data.clone(), old.unwrap_or(box io::stdout()));
Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ impl Diagnostic {
}
let buf = BufWriter::default();
let output = buf.clone();
EmitterWriter::new(Box::new(buf), Some(je.cm.clone()), false).emit(db);
EmitterWriter::new(Box::new(buf), Some(je.cm.clone()), false, false).emit(db);
let output = Arc::try_unwrap(output.0).unwrap().into_inner().unwrap();
let output = String::from_utf8(output).unwrap();

Expand Down
1 change: 1 addition & 0 deletions src/libsyntax/parse/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1745,6 +1745,7 @@ mod tests {
fn mk_sess(cm: Rc<CodeMap>) -> ParseSess {
let emitter = errors::emitter::EmitterWriter::new(Box::new(io::sink()),
Some(cm.clone()),
false,
false);
ParseSess {
span_diagnostic: errors::Handler::with_emitter(true, false, Box::new(emitter)),
Expand Down
1 change: 1 addition & 0 deletions src/libsyntax/test_snippet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ fn test_harness(file_text: &str, span_labels: Vec<SpanLabel>, expected_output: &

let emitter = EmitterWriter::new(Box::new(Shared { data: output.clone() }),
Some(code_map.clone()),
false,
false);
let handler = Handler::with_emitter(true, false, Box::new(emitter));
handler.span_err(msp, "foo");
Expand Down