Skip to content

Commit

Permalink
Make the difference between lint codes and error codes explicit
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed Nov 2, 2017
1 parent 88fb4c4 commit 6ae440e
Show file tree
Hide file tree
Showing 13 changed files with 143 additions and 69 deletions.
4 changes: 2 additions & 2 deletions src/librustc/lint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub use self::LintSource::*;

use std::rc::Rc;

use errors::DiagnosticBuilder;
use errors::{DiagnosticBuilder, DiagnosticId};
use hir::def_id::{CrateNum, LOCAL_CRATE};
use hir::intravisit::{self, FnKind};
use hir;
Expand Down Expand Up @@ -463,7 +463,7 @@ pub fn struct_lint_level<'a>(sess: &'a Session,
}
}

err.code(name);
err.code(DiagnosticId::Lint(name));

// Check for future incompatibility lints and issue a stronger warning.
let lints = sess.lint_store.borrow();
Expand Down
25 changes: 17 additions & 8 deletions src/librustc/session/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use util::nodemap::{FxHashMap, FxHashSet};
use util::common::{duration_to_secs_str, ErrorReported};

use syntax::ast::NodeId;
use errors::{self, DiagnosticBuilder};
use errors::{self, DiagnosticBuilder, DiagnosticId};
use errors::emitter::{Emitter, EmitterWriter};
use syntax::json::JsonEmitter;
use syntax::feature_gate;
Expand Down Expand Up @@ -187,7 +187,7 @@ impl Session {
pub fn struct_span_warn_with_code<'a, S: Into<MultiSpan>>(&'a self,
sp: S,
msg: &str,
code: &str)
code: DiagnosticId)
-> DiagnosticBuilder<'a> {
self.diagnostic().struct_span_warn_with_code(sp, msg, code)
}
Expand All @@ -203,15 +203,19 @@ impl Session {
pub fn struct_span_err_with_code<'a, S: Into<MultiSpan>>(&'a self,
sp: S,
msg: &str,
code: &str)
code: DiagnosticId)
-> DiagnosticBuilder<'a> {
self.diagnostic().struct_span_err_with_code(sp, msg, code)
}
// FIXME: This method should be removed (every error should have an associated error code).
pub fn struct_err<'a>(&'a self, msg: &str) -> DiagnosticBuilder<'a> {
self.diagnostic().struct_err(msg)
}
pub fn struct_err_with_code<'a>(&'a self, msg: &str, code: &str) -> DiagnosticBuilder<'a> {
pub fn struct_err_with_code<'a>(
&'a self,
msg: &str,
code: DiagnosticId,
) -> DiagnosticBuilder<'a> {
self.diagnostic().struct_err_with_code(msg, code)
}
pub fn struct_span_fatal<'a, S: Into<MultiSpan>>(&'a self,
Expand All @@ -223,7 +227,7 @@ impl Session {
pub fn struct_span_fatal_with_code<'a, S: Into<MultiSpan>>(&'a self,
sp: S,
msg: &str,
code: &str)
code: DiagnosticId)
-> DiagnosticBuilder<'a> {
self.diagnostic().struct_span_fatal_with_code(sp, msg, code)
}
Expand All @@ -234,7 +238,12 @@ impl Session {
pub fn span_fatal<S: Into<MultiSpan>>(&self, sp: S, msg: &str) -> ! {
panic!(self.diagnostic().span_fatal(sp, msg))
}
pub fn span_fatal_with_code<S: Into<MultiSpan>>(&self, sp: S, msg: &str, code: &str) -> ! {
pub fn span_fatal_with_code<S: Into<MultiSpan>>(
&self,
sp: S,
msg: &str,
code: DiagnosticId,
) -> ! {
panic!(self.diagnostic().span_fatal_with_code(sp, msg, code))
}
pub fn fatal(&self, msg: &str) -> ! {
Expand All @@ -250,7 +259,7 @@ impl Session {
pub fn span_err<S: Into<MultiSpan>>(&self, sp: S, msg: &str) {
self.diagnostic().span_err(sp, msg)
}
pub fn span_err_with_code<S: Into<MultiSpan>>(&self, sp: S, msg: &str, code: &str) {
pub fn span_err_with_code<S: Into<MultiSpan>>(&self, sp: S, msg: &str, code: DiagnosticId) {
self.diagnostic().span_err_with_code(sp, &msg, code)
}
pub fn err(&self, msg: &str) {
Expand Down Expand Up @@ -283,7 +292,7 @@ impl Session {
pub fn span_warn<S: Into<MultiSpan>>(&self, sp: S, msg: &str) {
self.diagnostic().span_warn(sp, msg)
}
pub fn span_warn_with_code<S: Into<MultiSpan>>(&self, sp: S, msg: &str, code: &str) {
pub fn span_warn_with_code<S: Into<MultiSpan>>(&self, sp: S, msg: &str, code: DiagnosticId) {
self.diagnostic().span_warn_with_code(sp, msg, code)
}
pub fn warn(&self, msg: &str) {
Expand Down
13 changes: 9 additions & 4 deletions src/librustc_borrowck/borrowck/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ use std::rc::Rc;
use std::hash::{Hash, Hasher};
use syntax::ast;
use syntax_pos::{MultiSpan, Span};
use errors::DiagnosticBuilder;
use errors::{DiagnosticBuilder, DiagnosticId};

use rustc::hir;
use rustc::hir::intravisit::{self, Visitor};
Expand Down Expand Up @@ -256,7 +256,7 @@ impl<'b, 'tcx: 'b> BorrowckErrors for BorrowckCtxt<'b, 'tcx> {
fn struct_span_err_with_code<'a, S: Into<MultiSpan>>(&'a self,
sp: S,
msg: &str,
code: &str)
code: DiagnosticId)
-> DiagnosticBuilder<'a>
{
self.tcx.sess.struct_span_err_with_code(sp, msg, code)
Expand Down Expand Up @@ -755,12 +755,17 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
pub fn struct_span_err_with_code<S: Into<MultiSpan>>(&self,
s: S,
msg: &str,
code: &str)
code: DiagnosticId)
-> DiagnosticBuilder<'a> {
self.tcx.sess.struct_span_err_with_code(s, msg, code)
}

pub fn span_err_with_code<S: Into<MultiSpan>>(&self, s: S, msg: &str, code: &str) {
pub fn span_err_with_code<S: Into<MultiSpan>>(
&self,
s: S,
msg: &str,
code: DiagnosticId,
) {
self.tcx.sess.span_err_with_code(s, msg, code);
}

Expand Down
12 changes: 9 additions & 3 deletions src/librustc_errors/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,18 @@ use snippet::Style;
pub struct Diagnostic {
pub level: Level,
pub message: Vec<(String, Style)>,
pub code: Option<String>,
pub code: Option<DiagnosticId>,
pub span: MultiSpan,
pub children: Vec<SubDiagnostic>,
pub suggestions: Vec<CodeSuggestion>,
}

#[derive(Clone, Debug, PartialEq, Hash, RustcEncodable, RustcDecodable)]
pub enum DiagnosticId {
Error(String),
Lint(String),
}

/// For example a note attached to an error.
#[derive(Clone, Debug, PartialEq, Hash, RustcEncodable, RustcDecodable)]
pub struct SubDiagnostic {
Expand Down Expand Up @@ -81,7 +87,7 @@ impl Diagnostic {
Diagnostic::new_with_code(level, None, message)
}

pub fn new_with_code(level: Level, code: Option<String>, message: &str) -> Self {
pub fn new_with_code(level: Level, code: Option<DiagnosticId>, message: &str) -> Self {
Diagnostic {
level,
message: vec![(message.to_owned(), Style::NoStyle)],
Expand Down Expand Up @@ -267,7 +273,7 @@ impl Diagnostic {
self
}

pub fn code(&mut self, s: String) -> &mut Self {
pub fn code(&mut self, s: DiagnosticId) -> &mut Self {
self.code = Some(s);
self
}
Expand Down
5 changes: 3 additions & 2 deletions src/librustc_errors/diagnostic_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
// except according to those terms.

use Diagnostic;
use DiagnosticId;
use DiagnosticStyledString;

use Level;
Expand Down Expand Up @@ -192,7 +193,7 @@ impl<'a> DiagnosticBuilder<'a> {
suggestions: Vec<String>)
-> &mut Self);
forward!(pub fn set_span<S: Into<MultiSpan>>(&mut self, sp: S) -> &mut Self);
forward!(pub fn code(&mut self, s: String) -> &mut Self);
forward!(pub fn code(&mut self, s: DiagnosticId) -> &mut Self);

/// Convenience function for internal use, clients should use one of the
/// struct_* methods on Handler.
Expand All @@ -204,7 +205,7 @@ impl<'a> DiagnosticBuilder<'a> {
/// struct_* methods on Handler.
pub fn new_with_code(handler: &'a Handler,
level: Level,
code: Option<String>,
code: Option<DiagnosticId>,
message: &str)
-> DiagnosticBuilder<'a> {
let diagnostic = Diagnostic::new_with_code(level, code, message);
Expand Down
19 changes: 8 additions & 11 deletions src/librustc_errors/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use self::Destination::*;

use syntax_pos::{DUMMY_SP, FileMap, Span, MultiSpan, CharPos};

use {Level, CodeSuggestion, DiagnosticBuilder, SubDiagnostic, CodeMapper};
use {Level, CodeSuggestion, DiagnosticBuilder, SubDiagnostic, CodeMapper, DiagnosticId};
use RenderSpan::*;
use snippet::{Annotation, AnnotationType, Line, MultilineAnnotation, StyledString, Style};
use styled_buffer::StyledBuffer;
Expand Down Expand Up @@ -886,7 +886,7 @@ impl EmitterWriter {
fn emit_message_default(&mut self,
msp: &MultiSpan,
msg: &Vec<(String, Style)>,
code: &Option<String>,
code: &Option<DiagnosticId>,
level: &Level,
max_line_num_len: usize,
is_secondary: bool)
Expand All @@ -905,14 +905,11 @@ impl EmitterWriter {
self.msg_to_buffer(&mut buffer, msg, max_line_num_len, "note", None);
} else {
buffer.append(0, &level.to_string(), Style::Level(level.clone()));
match code {
// only render error codes, not lint codes
&Some(ref code) if code.starts_with("E") && code.len() == 5 => {
buffer.append(0, "[", Style::Level(level.clone()));
buffer.append(0, &code, Style::Level(level.clone()));
buffer.append(0, "]", Style::Level(level.clone()));
}
_ => {}
// only render error codes, not lint codes
if let Some(DiagnosticId::Error(ref code)) = *code {
buffer.append(0, "[", Style::Level(level.clone()));
buffer.append(0, &code, Style::Level(level.clone()));
buffer.append(0, "]", Style::Level(level.clone()));
}
buffer.append(0, ": ", Style::HeaderMsg);
for &(ref text, _) in msg.iter() {
Expand Down Expand Up @@ -1175,7 +1172,7 @@ impl EmitterWriter {
fn emit_messages_default(&mut self,
level: &Level,
message: &Vec<(String, Style)>,
code: &Option<String>,
code: &Option<DiagnosticId>,
span: &MultiSpan,
children: &Vec<SubDiagnostic>) {
let max_line_num = self.get_max_line_num(span, children);
Expand Down
32 changes: 18 additions & 14 deletions src/librustc_errors/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ impl error::Error for ExplicitBug {
}
}

pub use diagnostic::{Diagnostic, SubDiagnostic, DiagnosticStyledString};
pub use diagnostic::{Diagnostic, SubDiagnostic, DiagnosticStyledString, DiagnosticId};
pub use diagnostic_builder::DiagnosticBuilder;

/// A handler deals with errors; certain errors
Expand Down Expand Up @@ -337,11 +337,11 @@ impl Handler {
pub fn struct_span_warn_with_code<'a, S: Into<MultiSpan>>(&'a self,
sp: S,
msg: &str,
code: &str)
code: DiagnosticId)
-> DiagnosticBuilder<'a> {
let mut result = DiagnosticBuilder::new(self, Level::Warning, msg);
result.set_span(sp);
result.code(code.to_owned());
result.code(code);
if !self.can_emit_warnings {
result.cancel();
}
Expand All @@ -365,20 +365,24 @@ impl Handler {
pub fn struct_span_err_with_code<'a, S: Into<MultiSpan>>(&'a self,
sp: S,
msg: &str,
code: &str)
code: DiagnosticId)
-> DiagnosticBuilder<'a> {
let mut result = DiagnosticBuilder::new(self, Level::Error, msg);
result.set_span(sp);
result.code(code.to_owned());
result.code(code);
result
}
// FIXME: This method should be removed (every error should have an associated error code).
pub fn struct_err<'a>(&'a self, msg: &str) -> DiagnosticBuilder<'a> {
DiagnosticBuilder::new(self, Level::Error, msg)
}
pub fn struct_err_with_code<'a>(&'a self, msg: &str, code: &str) -> DiagnosticBuilder<'a> {
pub fn struct_err_with_code<'a>(
&'a self,
msg: &str,
code: DiagnosticId,
) -> DiagnosticBuilder<'a> {
let mut result = DiagnosticBuilder::new(self, Level::Error, msg);
result.code(code.to_owned());
result.code(code);
result
}
pub fn struct_span_fatal<'a, S: Into<MultiSpan>>(&'a self,
Expand All @@ -392,11 +396,11 @@ impl Handler {
pub fn struct_span_fatal_with_code<'a, S: Into<MultiSpan>>(&'a self,
sp: S,
msg: &str,
code: &str)
code: DiagnosticId)
-> DiagnosticBuilder<'a> {
let mut result = DiagnosticBuilder::new(self, Level::Fatal, msg);
result.set_span(sp);
result.code(code.to_owned());
result.code(code);
result
}
pub fn struct_fatal<'a>(&'a self, msg: &str) -> DiagnosticBuilder<'a> {
Expand All @@ -420,7 +424,7 @@ impl Handler {
pub fn span_fatal_with_code<S: Into<MultiSpan>>(&self,
sp: S,
msg: &str,
code: &str)
code: DiagnosticId)
-> FatalError {
self.emit_with_code(&sp.into(), msg, code, Fatal);
FatalError
Expand All @@ -436,13 +440,13 @@ impl Handler {
result.set_span(sp);
result
}
pub fn span_err_with_code<S: Into<MultiSpan>>(&self, sp: S, msg: &str, code: &str) {
pub fn span_err_with_code<S: Into<MultiSpan>>(&self, sp: S, msg: &str, code: DiagnosticId) {
self.emit_with_code(&sp.into(), msg, code, Error);
}
pub fn span_warn<S: Into<MultiSpan>>(&self, sp: S, msg: &str) {
self.emit(&sp.into(), msg, Warning);
}
pub fn span_warn_with_code<S: Into<MultiSpan>>(&self, sp: S, msg: &str, code: &str) {
pub fn span_warn_with_code<S: Into<MultiSpan>>(&self, sp: S, msg: &str, code: DiagnosticId) {
self.emit_with_code(&sp.into(), msg, code, Warning);
}
pub fn span_bug<S: Into<MultiSpan>>(&self, sp: S, msg: &str) -> ! {
Expand Down Expand Up @@ -546,11 +550,11 @@ impl Handler {
self.abort_if_errors();
}
}
pub fn emit_with_code(&self, msp: &MultiSpan, msg: &str, code: &str, lvl: Level) {
pub fn emit_with_code(&self, msp: &MultiSpan, msg: &str, code: DiagnosticId, lvl: Level) {
if lvl == Warning && !self.can_emit_warnings {
return;
}
let mut db = DiagnosticBuilder::new_with_code(self, lvl, Some(code.to_owned()), msg);
let mut db = DiagnosticBuilder::new_with_code(self, lvl, Some(code), msg);
db.set_span(msp.clone());
db.emit();
if !self.continue_after_error.get() {
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_mir/util/borrowck_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
// except according to those terms.

use rustc::ty::{self, TyCtxt};
use rustc_errors::DiagnosticBuilder;
use rustc_errors::{DiagnosticBuilder, DiagnosticId};
use syntax_pos::{MultiSpan, Span};

use std::fmt;
Expand Down Expand Up @@ -41,7 +41,7 @@ pub trait BorrowckErrors {
fn struct_span_err_with_code<'a, S: Into<MultiSpan>>(&'a self,
sp: S,
msg: &str,
code: &str)
code: DiagnosticId)
-> DiagnosticBuilder<'a>;

fn struct_span_err<'a, S: Into<MultiSpan>>(&'a self,
Expand Down Expand Up @@ -445,7 +445,7 @@ impl<'b, 'gcx, 'tcx> BorrowckErrors for TyCtxt<'b, 'gcx, 'tcx> {
fn struct_span_err_with_code<'a, S: Into<MultiSpan>>(&'a self,
sp: S,
msg: &str,
code: &str)
code: DiagnosticId)
-> DiagnosticBuilder<'a>
{
self.sess.struct_span_err_with_code(sp, msg, code)
Expand Down
Loading

0 comments on commit 6ae440e

Please sign in to comment.