From 88fb4c4fdad1b97c3499a26e24dcbc60c6853a80 Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Tue, 24 Oct 2017 08:37:41 +0200 Subject: [PATCH 1/2] Report lint names in json diagnostics --- src/librustc/lint/mod.rs | 2 ++ src/librustc_errors/emitter.rs | 3 ++- src/test/ui/lint/unused_parens_json_suggestion.stderr | 2 +- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/librustc/lint/mod.rs b/src/librustc/lint/mod.rs index 52dcbfdedef5d..9317b6d3d5b95 100644 --- a/src/librustc/lint/mod.rs +++ b/src/librustc/lint/mod.rs @@ -463,6 +463,8 @@ pub fn struct_lint_level<'a>(sess: &'a Session, } } + err.code(name); + // Check for future incompatibility lints and issue a stronger warning. let lints = sess.lint_store.borrow(); if let Some(future_incompatible) = lints.future_incompatible(LintId::of(lint)) { diff --git a/src/librustc_errors/emitter.rs b/src/librustc_errors/emitter.rs index 5db5a9a1133d8..356b3dadb7b55 100644 --- a/src/librustc_errors/emitter.rs +++ b/src/librustc_errors/emitter.rs @@ -906,7 +906,8 @@ impl EmitterWriter { } else { buffer.append(0, &level.to_string(), Style::Level(level.clone())); match code { - &Some(ref 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())); diff --git a/src/test/ui/lint/unused_parens_json_suggestion.stderr b/src/test/ui/lint/unused_parens_json_suggestion.stderr index ae5b53da17503..02bb76722e6fa 100644 --- a/src/test/ui/lint/unused_parens_json_suggestion.stderr +++ b/src/test/ui/lint/unused_parens_json_suggestion.stderr @@ -1 +1 @@ -{"message":"unnecessary parentheses around assigned value","code":null,"level":"warning","spans":[{"file_name":"$DIR/unused_parens_json_suggestion.rs","byte_start":1001,"byte_end":1014,"line_start":24,"line_end":24,"column_start":14,"column_end":27,"is_primary":true,"text":[{"text":" let _a = (1 / (2 + 3));","highlight_start":14,"highlight_end":27}],"label":null,"suggested_replacement":null,"expansion":null}],"children":[{"message":"lint level defined here","code":null,"level":"note","spans":[{"file_name":"$DIR/unused_parens_json_suggestion.rs","byte_start":847,"byte_end":860,"line_start":19,"line_end":19,"column_start":9,"column_end":22,"is_primary":true,"text":[{"text":"#![warn(unused_parens)]","highlight_start":9,"highlight_end":22}],"label":null,"suggested_replacement":null,"expansion":null}],"children":[],"rendered":null},{"message":"remove these parentheses","code":null,"level":"help","spans":[{"file_name":"$DIR/unused_parens_json_suggestion.rs","byte_start":1001,"byte_end":1014,"line_start":24,"line_end":24,"column_start":14,"column_end":27,"is_primary":true,"text":[{"text":" let _a = (1 / (2 + 3));","highlight_start":14,"highlight_end":27}],"label":null,"suggested_replacement":"1 / (2 + 3)","expansion":null}],"children":[],"rendered":null}],"rendered":null} +{"message":"unnecessary parentheses around assigned value","code":{"code":"unused_parens","explanation":null},"level":"warning","spans":[{"file_name":"$DIR/unused_parens_json_suggestion.rs","byte_start":1001,"byte_end":1014,"line_start":24,"line_end":24,"column_start":14,"column_end":27,"is_primary":true,"text":[{"text":" let _a = (1 / (2 + 3));","highlight_start":14,"highlight_end":27}],"label":null,"suggested_replacement":null,"expansion":null}],"children":[{"message":"lint level defined here","code":null,"level":"note","spans":[{"file_name":"$DIR/unused_parens_json_suggestion.rs","byte_start":847,"byte_end":860,"line_start":19,"line_end":19,"column_start":9,"column_end":22,"is_primary":true,"text":[{"text":"#![warn(unused_parens)]","highlight_start":9,"highlight_end":22}],"label":null,"suggested_replacement":null,"expansion":null}],"children":[],"rendered":null},{"message":"remove these parentheses","code":null,"level":"help","spans":[{"file_name":"$DIR/unused_parens_json_suggestion.rs","byte_start":1001,"byte_end":1014,"line_start":24,"line_end":24,"column_start":14,"column_end":27,"is_primary":true,"text":[{"text":" let _a = (1 / (2 + 3));","highlight_start":14,"highlight_end":27}],"label":null,"suggested_replacement":"1 / (2 + 3)","expansion":null}],"children":[],"rendered":null}],"rendered":null} From 6ae440e04806ef2a9d21df4ee6f9e0006db5b05f Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Fri, 27 Oct 2017 08:21:22 +0200 Subject: [PATCH 2/2] Make the difference between lint codes and error codes explicit --- src/librustc/lint/mod.rs | 4 +- src/librustc/session/mod.rs | 25 ++++++--- src/librustc_borrowck/borrowck/mod.rs | 13 +++-- src/librustc_errors/diagnostic.rs | 12 +++-- src/librustc_errors/diagnostic_builder.rs | 5 +- src/librustc_errors/emitter.rs | 19 +++---- src/librustc_errors/lib.rs | 32 ++++++----- src/librustc_mir/util/borrowck_errors.rs | 6 +-- src/librustc_resolve/lib.rs | 13 +++-- src/librustc_trans/back/write.rs | 6 +-- src/librustc_typeck/check/mod.rs | 4 +- src/libsyntax/diagnostics/macros.rs | 65 +++++++++++++++++++---- src/libsyntax/json.rs | 8 ++- 13 files changed, 143 insertions(+), 69 deletions(-) diff --git a/src/librustc/lint/mod.rs b/src/librustc/lint/mod.rs index 9317b6d3d5b95..bca4dad220fcd 100644 --- a/src/librustc/lint/mod.rs +++ b/src/librustc/lint/mod.rs @@ -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; @@ -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(); diff --git a/src/librustc/session/mod.rs b/src/librustc/session/mod.rs index be35cc8e4a1c0..a15a9a84580b3 100644 --- a/src/librustc/session/mod.rs +++ b/src/librustc/session/mod.rs @@ -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; @@ -187,7 +187,7 @@ impl Session { pub fn struct_span_warn_with_code<'a, S: Into>(&'a self, sp: S, msg: &str, - code: &str) + code: DiagnosticId) -> DiagnosticBuilder<'a> { self.diagnostic().struct_span_warn_with_code(sp, msg, code) } @@ -203,7 +203,7 @@ impl Session { pub fn struct_span_err_with_code<'a, S: Into>(&'a self, sp: S, msg: &str, - code: &str) + code: DiagnosticId) -> DiagnosticBuilder<'a> { self.diagnostic().struct_span_err_with_code(sp, msg, code) } @@ -211,7 +211,11 @@ impl Session { 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>(&'a self, @@ -223,7 +227,7 @@ impl Session { pub fn struct_span_fatal_with_code<'a, S: Into>(&'a self, sp: S, msg: &str, - code: &str) + code: DiagnosticId) -> DiagnosticBuilder<'a> { self.diagnostic().struct_span_fatal_with_code(sp, msg, code) } @@ -234,7 +238,12 @@ impl Session { pub fn span_fatal>(&self, sp: S, msg: &str) -> ! { panic!(self.diagnostic().span_fatal(sp, msg)) } - pub fn span_fatal_with_code>(&self, sp: S, msg: &str, code: &str) -> ! { + pub fn span_fatal_with_code>( + &self, + sp: S, + msg: &str, + code: DiagnosticId, + ) -> ! { panic!(self.diagnostic().span_fatal_with_code(sp, msg, code)) } pub fn fatal(&self, msg: &str) -> ! { @@ -250,7 +259,7 @@ impl Session { pub fn span_err>(&self, sp: S, msg: &str) { self.diagnostic().span_err(sp, msg) } - pub fn span_err_with_code>(&self, sp: S, msg: &str, code: &str) { + pub fn span_err_with_code>(&self, sp: S, msg: &str, code: DiagnosticId) { self.diagnostic().span_err_with_code(sp, &msg, code) } pub fn err(&self, msg: &str) { @@ -283,7 +292,7 @@ impl Session { pub fn span_warn>(&self, sp: S, msg: &str) { self.diagnostic().span_warn(sp, msg) } - pub fn span_warn_with_code>(&self, sp: S, msg: &str, code: &str) { + pub fn span_warn_with_code>(&self, sp: S, msg: &str, code: DiagnosticId) { self.diagnostic().span_warn_with_code(sp, msg, code) } pub fn warn(&self, msg: &str) { diff --git a/src/librustc_borrowck/borrowck/mod.rs b/src/librustc_borrowck/borrowck/mod.rs index add128cc2cf6a..6be07878487b9 100644 --- a/src/librustc_borrowck/borrowck/mod.rs +++ b/src/librustc_borrowck/borrowck/mod.rs @@ -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}; @@ -256,7 +256,7 @@ impl<'b, 'tcx: 'b> BorrowckErrors for BorrowckCtxt<'b, 'tcx> { fn struct_span_err_with_code<'a, S: Into>(&'a self, sp: S, msg: &str, - code: &str) + code: DiagnosticId) -> DiagnosticBuilder<'a> { self.tcx.sess.struct_span_err_with_code(sp, msg, code) @@ -755,12 +755,17 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> { pub fn struct_span_err_with_code>(&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>(&self, s: S, msg: &str, code: &str) { + pub fn span_err_with_code>( + &self, + s: S, + msg: &str, + code: DiagnosticId, + ) { self.tcx.sess.span_err_with_code(s, msg, code); } diff --git a/src/librustc_errors/diagnostic.rs b/src/librustc_errors/diagnostic.rs index 5e0e624082e6f..2d70de89355ee 100644 --- a/src/librustc_errors/diagnostic.rs +++ b/src/librustc_errors/diagnostic.rs @@ -21,12 +21,18 @@ use snippet::Style; pub struct Diagnostic { pub level: Level, pub message: Vec<(String, Style)>, - pub code: Option, + pub code: Option, pub span: MultiSpan, pub children: Vec, pub suggestions: Vec, } +#[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 { @@ -81,7 +87,7 @@ impl Diagnostic { Diagnostic::new_with_code(level, None, message) } - pub fn new_with_code(level: Level, code: Option, message: &str) -> Self { + pub fn new_with_code(level: Level, code: Option, message: &str) -> Self { Diagnostic { level, message: vec![(message.to_owned(), Style::NoStyle)], @@ -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 } diff --git a/src/librustc_errors/diagnostic_builder.rs b/src/librustc_errors/diagnostic_builder.rs index 2cd433bfe3aee..40b5810454b9a 100644 --- a/src/librustc_errors/diagnostic_builder.rs +++ b/src/librustc_errors/diagnostic_builder.rs @@ -9,6 +9,7 @@ // except according to those terms. use Diagnostic; +use DiagnosticId; use DiagnosticStyledString; use Level; @@ -192,7 +193,7 @@ impl<'a> DiagnosticBuilder<'a> { suggestions: Vec) -> &mut Self); forward!(pub fn set_span>(&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. @@ -204,7 +205,7 @@ impl<'a> DiagnosticBuilder<'a> { /// struct_* methods on Handler. pub fn new_with_code(handler: &'a Handler, level: Level, - code: Option, + code: Option, message: &str) -> DiagnosticBuilder<'a> { let diagnostic = Diagnostic::new_with_code(level, code, message); diff --git a/src/librustc_errors/emitter.rs b/src/librustc_errors/emitter.rs index 356b3dadb7b55..011be74ee7c45 100644 --- a/src/librustc_errors/emitter.rs +++ b/src/librustc_errors/emitter.rs @@ -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; @@ -886,7 +886,7 @@ impl EmitterWriter { fn emit_message_default(&mut self, msp: &MultiSpan, msg: &Vec<(String, Style)>, - code: &Option, + code: &Option, level: &Level, max_line_num_len: usize, is_secondary: bool) @@ -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() { @@ -1175,7 +1172,7 @@ impl EmitterWriter { fn emit_messages_default(&mut self, level: &Level, message: &Vec<(String, Style)>, - code: &Option, + code: &Option, span: &MultiSpan, children: &Vec) { let max_line_num = self.get_max_line_num(span, children); diff --git a/src/librustc_errors/lib.rs b/src/librustc_errors/lib.rs index ba7268a4bc393..b30ee7016ab1c 100644 --- a/src/librustc_errors/lib.rs +++ b/src/librustc_errors/lib.rs @@ -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 @@ -337,11 +337,11 @@ impl Handler { pub fn struct_span_warn_with_code<'a, S: Into>(&'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(); } @@ -365,20 +365,24 @@ impl Handler { pub fn struct_span_err_with_code<'a, S: Into>(&'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>(&'a self, @@ -392,11 +396,11 @@ impl Handler { pub fn struct_span_fatal_with_code<'a, S: Into>(&'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> { @@ -420,7 +424,7 @@ impl Handler { pub fn span_fatal_with_code>(&self, sp: S, msg: &str, - code: &str) + code: DiagnosticId) -> FatalError { self.emit_with_code(&sp.into(), msg, code, Fatal); FatalError @@ -436,13 +440,13 @@ impl Handler { result.set_span(sp); result } - pub fn span_err_with_code>(&self, sp: S, msg: &str, code: &str) { + pub fn span_err_with_code>(&self, sp: S, msg: &str, code: DiagnosticId) { self.emit_with_code(&sp.into(), msg, code, Error); } pub fn span_warn>(&self, sp: S, msg: &str) { self.emit(&sp.into(), msg, Warning); } - pub fn span_warn_with_code>(&self, sp: S, msg: &str, code: &str) { + pub fn span_warn_with_code>(&self, sp: S, msg: &str, code: DiagnosticId) { self.emit_with_code(&sp.into(), msg, code, Warning); } pub fn span_bug>(&self, sp: S, msg: &str) -> ! { @@ -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() { diff --git a/src/librustc_mir/util/borrowck_errors.rs b/src/librustc_mir/util/borrowck_errors.rs index a4a7699abda59..2f29b79eeb6cc 100644 --- a/src/librustc_mir/util/borrowck_errors.rs +++ b/src/librustc_mir/util/borrowck_errors.rs @@ -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; @@ -41,7 +41,7 @@ pub trait BorrowckErrors { fn struct_span_err_with_code<'a, S: Into>(&'a self, sp: S, msg: &str, - code: &str) + code: DiagnosticId) -> DiagnosticBuilder<'a>; fn struct_span_err<'a, S: Into>(&'a self, @@ -445,7 +445,7 @@ impl<'b, 'gcx, 'tcx> BorrowckErrors for TyCtxt<'b, 'gcx, 'tcx> { fn struct_span_err_with_code<'a, S: Into>(&'a self, sp: S, msg: &str, - code: &str) + code: DiagnosticId) -> DiagnosticBuilder<'a> { self.sess.struct_span_err_with_code(sp, msg, code) diff --git a/src/librustc_resolve/lib.rs b/src/librustc_resolve/lib.rs index 3b27890013a22..c7ead3c6bb59a 100644 --- a/src/librustc_resolve/lib.rs +++ b/src/librustc_resolve/lib.rs @@ -59,7 +59,7 @@ use syntax::ast::{QSelf, TraitItemKind, TraitRef, Ty, TyKind}; use syntax::feature_gate::{feature_err, emit_feature_err, GateIssue}; use syntax_pos::{Span, DUMMY_SP, MultiSpan}; -use errors::DiagnosticBuilder; +use errors::{DiagnosticBuilder, DiagnosticId}; use std::cell::{Cell, RefCell}; use std::cmp; @@ -223,7 +223,11 @@ fn resolve_struct_error<'sess, 'a>(resolver: &'sess Resolver, let target_sp = binding_error.target.iter().map(|x| *x).collect::>(); let msp = MultiSpan::from_spans(target_sp.clone()); let msg = format!("variable `{}` is not bound in all patterns", binding_error.name); - let mut err = resolver.session.struct_span_err_with_code(msp, &msg, "E0408"); + let mut err = resolver.session.struct_span_err_with_code( + msp, + &msg, + DiagnosticId::Error("E0408".into()), + ); for sp in target_sp { err.span_label(sp, format!("pattern doesn't bind `{}`", binding_error.name)); } @@ -2490,18 +2494,19 @@ impl<'a> Resolver<'a> { (format!("cannot find {} `{}` in {}{}", expected, item_str, mod_prefix, mod_str), format!("not found in {}", mod_str), item_span) }; + let code = DiagnosticId::Error(code.into()); let mut err = this.session.struct_span_err_with_code(base_span, &base_msg, code); // Emit special messages for unresolved `Self` and `self`. if is_self_type(path, ns) { __diagnostic_used!(E0411); - err.code("E0411".into()); + err.code(DiagnosticId::Error("E0411".into())); err.span_label(span, "`Self` is only available in traits and impls"); return (err, Vec::new()); } if is_self_value(path, ns) { __diagnostic_used!(E0424); - err.code("E0424".into()); + err.code(DiagnosticId::Error("E0424".into())); err.span_label(span, format!("`self` value is only available in \ methods with `self` parameter")); return (err, Vec::new()); diff --git a/src/librustc_trans/back/write.rs b/src/librustc_trans/back/write.rs index 5550ab9fa55e6..10475a2b1b1a0 100644 --- a/src/librustc_trans/back/write.rs +++ b/src/librustc_trans/back/write.rs @@ -32,7 +32,7 @@ use rustc::hir::def_id::{CrateNum, LOCAL_CRATE}; use rustc::ty::TyCtxt; use rustc::util::common::{time, time_depth, set_time_depth, path2cstr, print_time_passes_entry}; use rustc::util::fs::{link_or_copy, rename_or_copy_remove}; -use errors::{self, Handler, Level, DiagnosticBuilder, FatalError}; +use errors::{self, Handler, Level, DiagnosticBuilder, FatalError, DiagnosticId}; use errors::emitter::{Emitter}; use syntax::attr; use syntax::ext::hygiene::Mark; @@ -1262,7 +1262,7 @@ enum Message { struct Diagnostic { msg: String, - code: Option, + code: Option, lvl: Level, } @@ -2015,7 +2015,7 @@ impl SharedEmitterMain { Some(ref code) => { handler.emit_with_code(&MultiSpan::new(), &diag.msg, - &code, + code.clone(), diag.lvl); } None => { diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index 26f7a7a378477..d942c1176aa69 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -100,7 +100,7 @@ use rustc::ty::adjustment::{Adjust, Adjustment, AutoBorrow}; use rustc::ty::fold::{BottomUpFolder, TypeFoldable}; use rustc::ty::maps::Providers; use rustc::ty::util::{Representability, IntTypeExt}; -use errors::DiagnosticBuilder; +use errors::{DiagnosticBuilder, DiagnosticId}; use require_c_abi_if_variadic; use session::{CompileIncomplete, Session}; use TypeAndSubsts; @@ -2467,7 +2467,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { if expected_count == 1 {""} else {"s"}, arg_count, if arg_count == 1 {" was"} else {"s were"}), - error_code); + DiagnosticId::Error(error_code.to_owned())); if let Some(def_s) = def_span { err.span_label(def_s, "defined here"); diff --git a/src/libsyntax/diagnostics/macros.rs b/src/libsyntax/diagnostics/macros.rs index e8ecf58072a69..c01836b619411 100644 --- a/src/libsyntax/diagnostics/macros.rs +++ b/src/libsyntax/diagnostics/macros.rs @@ -18,7 +18,11 @@ macro_rules! register_diagnostic { macro_rules! span_fatal { ($session:expr, $span:expr, $code:ident, $($message:tt)*) => ({ __diagnostic_used!($code); - $session.span_fatal_with_code($span, &format!($($message)*), stringify!($code)) + $session.span_fatal_with_code( + $span, + &format!($($message)*), + $crate::errors::DiagnosticId::Error(stringify!($code).to_owned()), + ) }) } @@ -26,7 +30,11 @@ macro_rules! span_fatal { macro_rules! span_err { ($session:expr, $span:expr, $code:ident, $($message:tt)*) => ({ __diagnostic_used!($code); - $session.span_err_with_code($span, &format!($($message)*), stringify!($code)) + $session.span_err_with_code( + $span, + &format!($($message)*), + $crate::errors::DiagnosticId::Error(stringify!($code).to_owned()), + ) }) } @@ -34,7 +42,11 @@ macro_rules! span_err { macro_rules! span_warn { ($session:expr, $span:expr, $code:ident, $($message:tt)*) => ({ __diagnostic_used!($code); - $session.span_warn_with_code($span, &format!($($message)*), stringify!($code)) + $session.span_warn_with_code( + $span, + &format!($($message)*), + $crate::errors::DiagnosticId::Error(stringify!($code).to_owned()), + ) }) } @@ -42,7 +54,10 @@ macro_rules! span_warn { macro_rules! struct_err { ($session:expr, $code:ident, $($message:tt)*) => ({ __diagnostic_used!($code); - $session.struct_err_with_code(&format!($($message)*), stringify!($code)) + $session.struct_err_with_code( + &format!($($message)*), + $crate::errors::DiagnosticId::Error(stringify!($code).to_owned()), + ) }) } @@ -51,9 +66,17 @@ macro_rules! span_err_or_warn { ($is_warning:expr, $session:expr, $span:expr, $code:ident, $($message:tt)*) => ({ __diagnostic_used!($code); if $is_warning { - $session.span_warn_with_code($span, &format!($($message)*), stringify!($code)) + $session.span_warn_with_code( + $span, + &format!($($message)*), + $crate::errors::DiagnosticId::Error(stringify!($code).to_owned()), + ) } else { - $session.span_err_with_code($span, &format!($($message)*), stringify!($code)) + $session.span_err_with_code( + $span, + &format!($($message)*), + $crate::errors::DiagnosticId::Error(stringify!($code).to_owned()), + ) } }) } @@ -62,7 +85,11 @@ macro_rules! span_err_or_warn { macro_rules! struct_span_fatal { ($session:expr, $span:expr, $code:ident, $($message:tt)*) => ({ __diagnostic_used!($code); - $session.struct_span_fatal_with_code($span, &format!($($message)*), stringify!($code)) + $session.struct_span_fatal_with_code( + $span, + &format!($($message)*), + $crate::errors::DiagnosticId::Error(stringify!($code).to_owned()), + ) }) } @@ -70,7 +97,11 @@ macro_rules! struct_span_fatal { macro_rules! struct_span_err { ($session:expr, $span:expr, $code:ident, $($message:tt)*) => ({ __diagnostic_used!($code); - $session.struct_span_err_with_code($span, &format!($($message)*), stringify!($code)) + $session.struct_span_err_with_code( + $span, + &format!($($message)*), + $crate::errors::DiagnosticId::Error(stringify!($code).to_owned()), + ) }) } @@ -89,7 +120,11 @@ macro_rules! type_error_struct { macro_rules! struct_span_warn { ($session:expr, $span:expr, $code:ident, $($message:tt)*) => ({ __diagnostic_used!($code); - $session.struct_span_warn_with_code($span, &format!($($message)*), stringify!($code)) + $session.struct_span_warn_with_code( + $span, + &format!($($message)*), + $crate::errors::DiagnosticId::Error(stringify!($code).to_owned()), + ) }) } @@ -98,9 +133,17 @@ macro_rules! struct_span_err_or_warn { ($is_warning:expr, $session:expr, $span:expr, $code:ident, $($message:tt)*) => ({ __diagnostic_used!($code); if $is_warning { - $session.struct_span_warn_with_code($span, &format!($($message)*), stringify!($code)) + $session.struct_span_warn_with_code( + $span, + &format!($($message)*), + $crate::errors::DiagnosticId::Error(stringify!($code).to_owned()), + ) } else { - $session.struct_span_err_with_code($span, &format!($($message)*), stringify!($code)) + $session.struct_span_err_with_code( + $span, + &format!($($message)*), + $crate::errors::DiagnosticId::Error(stringify!($code).to_owned()), + ) } }) } diff --git a/src/libsyntax/json.rs b/src/libsyntax/json.rs index b8151819bffed..31fe0c234e8d3 100644 --- a/src/libsyntax/json.rs +++ b/src/libsyntax/json.rs @@ -23,6 +23,7 @@ use codemap::{CodeMap, FilePathMapping}; use syntax_pos::{self, MacroBacktrace, Span, SpanLabel, MultiSpan}; use errors::registry::Registry; use errors::{DiagnosticBuilder, SubDiagnostic, RenderSpan, CodeSuggestion, CodeMapper}; +use errors::DiagnosticId; use errors::emitter::Emitter; use std::rc::Rc; @@ -340,9 +341,12 @@ impl DiagnosticSpanLine { } impl DiagnosticCode { - fn map_opt_string(s: Option, je: &JsonEmitter) -> Option { + fn map_opt_string(s: Option, je: &JsonEmitter) -> Option { s.map(|s| { - + let s = match s { + DiagnosticId::Error(s) => s, + DiagnosticId::Lint(s) => s, + }; let explanation = je.registry .as_ref() .and_then(|registry| registry.find_description(&s));