Skip to content

Commit 6ae440e

Browse files
committed
Make the difference between lint codes and error codes explicit
1 parent 88fb4c4 commit 6ae440e

File tree

13 files changed

+143
-69
lines changed

13 files changed

+143
-69
lines changed

Diff for: src/librustc/lint/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub use self::LintSource::*;
3333

3434
use std::rc::Rc;
3535

36-
use errors::DiagnosticBuilder;
36+
use errors::{DiagnosticBuilder, DiagnosticId};
3737
use hir::def_id::{CrateNum, LOCAL_CRATE};
3838
use hir::intravisit::{self, FnKind};
3939
use hir;
@@ -463,7 +463,7 @@ pub fn struct_lint_level<'a>(sess: &'a Session,
463463
}
464464
}
465465

466-
err.code(name);
466+
err.code(DiagnosticId::Lint(name));
467467

468468
// Check for future incompatibility lints and issue a stronger warning.
469469
let lints = sess.lint_store.borrow();

Diff for: src/librustc/session/mod.rs

+17-8
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use util::nodemap::{FxHashMap, FxHashSet};
2424
use util::common::{duration_to_secs_str, ErrorReported};
2525

2626
use syntax::ast::NodeId;
27-
use errors::{self, DiagnosticBuilder};
27+
use errors::{self, DiagnosticBuilder, DiagnosticId};
2828
use errors::emitter::{Emitter, EmitterWriter};
2929
use syntax::json::JsonEmitter;
3030
use syntax::feature_gate;
@@ -187,7 +187,7 @@ impl Session {
187187
pub fn struct_span_warn_with_code<'a, S: Into<MultiSpan>>(&'a self,
188188
sp: S,
189189
msg: &str,
190-
code: &str)
190+
code: DiagnosticId)
191191
-> DiagnosticBuilder<'a> {
192192
self.diagnostic().struct_span_warn_with_code(sp, msg, code)
193193
}
@@ -203,15 +203,19 @@ impl Session {
203203
pub fn struct_span_err_with_code<'a, S: Into<MultiSpan>>(&'a self,
204204
sp: S,
205205
msg: &str,
206-
code: &str)
206+
code: DiagnosticId)
207207
-> DiagnosticBuilder<'a> {
208208
self.diagnostic().struct_span_err_with_code(sp, msg, code)
209209
}
210210
// FIXME: This method should be removed (every error should have an associated error code).
211211
pub fn struct_err<'a>(&'a self, msg: &str) -> DiagnosticBuilder<'a> {
212212
self.diagnostic().struct_err(msg)
213213
}
214-
pub fn struct_err_with_code<'a>(&'a self, msg: &str, code: &str) -> DiagnosticBuilder<'a> {
214+
pub fn struct_err_with_code<'a>(
215+
&'a self,
216+
msg: &str,
217+
code: DiagnosticId,
218+
) -> DiagnosticBuilder<'a> {
215219
self.diagnostic().struct_err_with_code(msg, code)
216220
}
217221
pub fn struct_span_fatal<'a, S: Into<MultiSpan>>(&'a self,
@@ -223,7 +227,7 @@ impl Session {
223227
pub fn struct_span_fatal_with_code<'a, S: Into<MultiSpan>>(&'a self,
224228
sp: S,
225229
msg: &str,
226-
code: &str)
230+
code: DiagnosticId)
227231
-> DiagnosticBuilder<'a> {
228232
self.diagnostic().struct_span_fatal_with_code(sp, msg, code)
229233
}
@@ -234,7 +238,12 @@ impl Session {
234238
pub fn span_fatal<S: Into<MultiSpan>>(&self, sp: S, msg: &str) -> ! {
235239
panic!(self.diagnostic().span_fatal(sp, msg))
236240
}
237-
pub fn span_fatal_with_code<S: Into<MultiSpan>>(&self, sp: S, msg: &str, code: &str) -> ! {
241+
pub fn span_fatal_with_code<S: Into<MultiSpan>>(
242+
&self,
243+
sp: S,
244+
msg: &str,
245+
code: DiagnosticId,
246+
) -> ! {
238247
panic!(self.diagnostic().span_fatal_with_code(sp, msg, code))
239248
}
240249
pub fn fatal(&self, msg: &str) -> ! {
@@ -250,7 +259,7 @@ impl Session {
250259
pub fn span_err<S: Into<MultiSpan>>(&self, sp: S, msg: &str) {
251260
self.diagnostic().span_err(sp, msg)
252261
}
253-
pub fn span_err_with_code<S: Into<MultiSpan>>(&self, sp: S, msg: &str, code: &str) {
262+
pub fn span_err_with_code<S: Into<MultiSpan>>(&self, sp: S, msg: &str, code: DiagnosticId) {
254263
self.diagnostic().span_err_with_code(sp, &msg, code)
255264
}
256265
pub fn err(&self, msg: &str) {
@@ -283,7 +292,7 @@ impl Session {
283292
pub fn span_warn<S: Into<MultiSpan>>(&self, sp: S, msg: &str) {
284293
self.diagnostic().span_warn(sp, msg)
285294
}
286-
pub fn span_warn_with_code<S: Into<MultiSpan>>(&self, sp: S, msg: &str, code: &str) {
295+
pub fn span_warn_with_code<S: Into<MultiSpan>>(&self, sp: S, msg: &str, code: DiagnosticId) {
287296
self.diagnostic().span_warn_with_code(sp, msg, code)
288297
}
289298
pub fn warn(&self, msg: &str) {

Diff for: src/librustc_borrowck/borrowck/mod.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ use std::rc::Rc;
4747
use std::hash::{Hash, Hasher};
4848
use syntax::ast;
4949
use syntax_pos::{MultiSpan, Span};
50-
use errors::DiagnosticBuilder;
50+
use errors::{DiagnosticBuilder, DiagnosticId};
5151

5252
use rustc::hir;
5353
use rustc::hir::intravisit::{self, Visitor};
@@ -256,7 +256,7 @@ impl<'b, 'tcx: 'b> BorrowckErrors for BorrowckCtxt<'b, 'tcx> {
256256
fn struct_span_err_with_code<'a, S: Into<MultiSpan>>(&'a self,
257257
sp: S,
258258
msg: &str,
259-
code: &str)
259+
code: DiagnosticId)
260260
-> DiagnosticBuilder<'a>
261261
{
262262
self.tcx.sess.struct_span_err_with_code(sp, msg, code)
@@ -755,12 +755,17 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
755755
pub fn struct_span_err_with_code<S: Into<MultiSpan>>(&self,
756756
s: S,
757757
msg: &str,
758-
code: &str)
758+
code: DiagnosticId)
759759
-> DiagnosticBuilder<'a> {
760760
self.tcx.sess.struct_span_err_with_code(s, msg, code)
761761
}
762762

763-
pub fn span_err_with_code<S: Into<MultiSpan>>(&self, s: S, msg: &str, code: &str) {
763+
pub fn span_err_with_code<S: Into<MultiSpan>>(
764+
&self,
765+
s: S,
766+
msg: &str,
767+
code: DiagnosticId,
768+
) {
764769
self.tcx.sess.span_err_with_code(s, msg, code);
765770
}
766771

Diff for: src/librustc_errors/diagnostic.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,18 @@ use snippet::Style;
2121
pub struct Diagnostic {
2222
pub level: Level,
2323
pub message: Vec<(String, Style)>,
24-
pub code: Option<String>,
24+
pub code: Option<DiagnosticId>,
2525
pub span: MultiSpan,
2626
pub children: Vec<SubDiagnostic>,
2727
pub suggestions: Vec<CodeSuggestion>,
2828
}
2929

30+
#[derive(Clone, Debug, PartialEq, Hash, RustcEncodable, RustcDecodable)]
31+
pub enum DiagnosticId {
32+
Error(String),
33+
Lint(String),
34+
}
35+
3036
/// For example a note attached to an error.
3137
#[derive(Clone, Debug, PartialEq, Hash, RustcEncodable, RustcDecodable)]
3238
pub struct SubDiagnostic {
@@ -81,7 +87,7 @@ impl Diagnostic {
8187
Diagnostic::new_with_code(level, None, message)
8288
}
8389

84-
pub fn new_with_code(level: Level, code: Option<String>, message: &str) -> Self {
90+
pub fn new_with_code(level: Level, code: Option<DiagnosticId>, message: &str) -> Self {
8591
Diagnostic {
8692
level,
8793
message: vec![(message.to_owned(), Style::NoStyle)],
@@ -267,7 +273,7 @@ impl Diagnostic {
267273
self
268274
}
269275

270-
pub fn code(&mut self, s: String) -> &mut Self {
276+
pub fn code(&mut self, s: DiagnosticId) -> &mut Self {
271277
self.code = Some(s);
272278
self
273279
}

Diff for: src/librustc_errors/diagnostic_builder.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// except according to those terms.
1010

1111
use Diagnostic;
12+
use DiagnosticId;
1213
use DiagnosticStyledString;
1314

1415
use Level;
@@ -192,7 +193,7 @@ impl<'a> DiagnosticBuilder<'a> {
192193
suggestions: Vec<String>)
193194
-> &mut Self);
194195
forward!(pub fn set_span<S: Into<MultiSpan>>(&mut self, sp: S) -> &mut Self);
195-
forward!(pub fn code(&mut self, s: String) -> &mut Self);
196+
forward!(pub fn code(&mut self, s: DiagnosticId) -> &mut Self);
196197

197198
/// Convenience function for internal use, clients should use one of the
198199
/// struct_* methods on Handler.
@@ -204,7 +205,7 @@ impl<'a> DiagnosticBuilder<'a> {
204205
/// struct_* methods on Handler.
205206
pub fn new_with_code(handler: &'a Handler,
206207
level: Level,
207-
code: Option<String>,
208+
code: Option<DiagnosticId>,
208209
message: &str)
209210
-> DiagnosticBuilder<'a> {
210211
let diagnostic = Diagnostic::new_with_code(level, code, message);

Diff for: src/librustc_errors/emitter.rs

+8-11
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use self::Destination::*;
1212

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

15-
use {Level, CodeSuggestion, DiagnosticBuilder, SubDiagnostic, CodeMapper};
15+
use {Level, CodeSuggestion, DiagnosticBuilder, SubDiagnostic, CodeMapper, DiagnosticId};
1616
use RenderSpan::*;
1717
use snippet::{Annotation, AnnotationType, Line, MultilineAnnotation, StyledString, Style};
1818
use styled_buffer::StyledBuffer;
@@ -886,7 +886,7 @@ impl EmitterWriter {
886886
fn emit_message_default(&mut self,
887887
msp: &MultiSpan,
888888
msg: &Vec<(String, Style)>,
889-
code: &Option<String>,
889+
code: &Option<DiagnosticId>,
890890
level: &Level,
891891
max_line_num_len: usize,
892892
is_secondary: bool)
@@ -905,14 +905,11 @@ impl EmitterWriter {
905905
self.msg_to_buffer(&mut buffer, msg, max_line_num_len, "note", None);
906906
} else {
907907
buffer.append(0, &level.to_string(), Style::Level(level.clone()));
908-
match code {
909-
// only render error codes, not lint codes
910-
&Some(ref code) if code.starts_with("E") && code.len() == 5 => {
911-
buffer.append(0, "[", Style::Level(level.clone()));
912-
buffer.append(0, &code, Style::Level(level.clone()));
913-
buffer.append(0, "]", Style::Level(level.clone()));
914-
}
915-
_ => {}
908+
// only render error codes, not lint codes
909+
if let Some(DiagnosticId::Error(ref code)) = *code {
910+
buffer.append(0, "[", Style::Level(level.clone()));
911+
buffer.append(0, &code, Style::Level(level.clone()));
912+
buffer.append(0, "]", Style::Level(level.clone()));
916913
}
917914
buffer.append(0, ": ", Style::HeaderMsg);
918915
for &(ref text, _) in msg.iter() {
@@ -1175,7 +1172,7 @@ impl EmitterWriter {
11751172
fn emit_messages_default(&mut self,
11761173
level: &Level,
11771174
message: &Vec<(String, Style)>,
1178-
code: &Option<String>,
1175+
code: &Option<DiagnosticId>,
11791176
span: &MultiSpan,
11801177
children: &Vec<SubDiagnostic>) {
11811178
let max_line_num = self.get_max_line_num(span, children);

Diff for: src/librustc_errors/lib.rs

+18-14
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ impl error::Error for ExplicitBug {
262262
}
263263
}
264264

265-
pub use diagnostic::{Diagnostic, SubDiagnostic, DiagnosticStyledString};
265+
pub use diagnostic::{Diagnostic, SubDiagnostic, DiagnosticStyledString, DiagnosticId};
266266
pub use diagnostic_builder::DiagnosticBuilder;
267267

268268
/// A handler deals with errors; certain errors
@@ -337,11 +337,11 @@ impl Handler {
337337
pub fn struct_span_warn_with_code<'a, S: Into<MultiSpan>>(&'a self,
338338
sp: S,
339339
msg: &str,
340-
code: &str)
340+
code: DiagnosticId)
341341
-> DiagnosticBuilder<'a> {
342342
let mut result = DiagnosticBuilder::new(self, Level::Warning, msg);
343343
result.set_span(sp);
344-
result.code(code.to_owned());
344+
result.code(code);
345345
if !self.can_emit_warnings {
346346
result.cancel();
347347
}
@@ -365,20 +365,24 @@ impl Handler {
365365
pub fn struct_span_err_with_code<'a, S: Into<MultiSpan>>(&'a self,
366366
sp: S,
367367
msg: &str,
368-
code: &str)
368+
code: DiagnosticId)
369369
-> DiagnosticBuilder<'a> {
370370
let mut result = DiagnosticBuilder::new(self, Level::Error, msg);
371371
result.set_span(sp);
372-
result.code(code.to_owned());
372+
result.code(code);
373373
result
374374
}
375375
// FIXME: This method should be removed (every error should have an associated error code).
376376
pub fn struct_err<'a>(&'a self, msg: &str) -> DiagnosticBuilder<'a> {
377377
DiagnosticBuilder::new(self, Level::Error, msg)
378378
}
379-
pub fn struct_err_with_code<'a>(&'a self, msg: &str, code: &str) -> DiagnosticBuilder<'a> {
379+
pub fn struct_err_with_code<'a>(
380+
&'a self,
381+
msg: &str,
382+
code: DiagnosticId,
383+
) -> DiagnosticBuilder<'a> {
380384
let mut result = DiagnosticBuilder::new(self, Level::Error, msg);
381-
result.code(code.to_owned());
385+
result.code(code);
382386
result
383387
}
384388
pub fn struct_span_fatal<'a, S: Into<MultiSpan>>(&'a self,
@@ -392,11 +396,11 @@ impl Handler {
392396
pub fn struct_span_fatal_with_code<'a, S: Into<MultiSpan>>(&'a self,
393397
sp: S,
394398
msg: &str,
395-
code: &str)
399+
code: DiagnosticId)
396400
-> DiagnosticBuilder<'a> {
397401
let mut result = DiagnosticBuilder::new(self, Level::Fatal, msg);
398402
result.set_span(sp);
399-
result.code(code.to_owned());
403+
result.code(code);
400404
result
401405
}
402406
pub fn struct_fatal<'a>(&'a self, msg: &str) -> DiagnosticBuilder<'a> {
@@ -420,7 +424,7 @@ impl Handler {
420424
pub fn span_fatal_with_code<S: Into<MultiSpan>>(&self,
421425
sp: S,
422426
msg: &str,
423-
code: &str)
427+
code: DiagnosticId)
424428
-> FatalError {
425429
self.emit_with_code(&sp.into(), msg, code, Fatal);
426430
FatalError
@@ -436,13 +440,13 @@ impl Handler {
436440
result.set_span(sp);
437441
result
438442
}
439-
pub fn span_err_with_code<S: Into<MultiSpan>>(&self, sp: S, msg: &str, code: &str) {
443+
pub fn span_err_with_code<S: Into<MultiSpan>>(&self, sp: S, msg: &str, code: DiagnosticId) {
440444
self.emit_with_code(&sp.into(), msg, code, Error);
441445
}
442446
pub fn span_warn<S: Into<MultiSpan>>(&self, sp: S, msg: &str) {
443447
self.emit(&sp.into(), msg, Warning);
444448
}
445-
pub fn span_warn_with_code<S: Into<MultiSpan>>(&self, sp: S, msg: &str, code: &str) {
449+
pub fn span_warn_with_code<S: Into<MultiSpan>>(&self, sp: S, msg: &str, code: DiagnosticId) {
446450
self.emit_with_code(&sp.into(), msg, code, Warning);
447451
}
448452
pub fn span_bug<S: Into<MultiSpan>>(&self, sp: S, msg: &str) -> ! {
@@ -546,11 +550,11 @@ impl Handler {
546550
self.abort_if_errors();
547551
}
548552
}
549-
pub fn emit_with_code(&self, msp: &MultiSpan, msg: &str, code: &str, lvl: Level) {
553+
pub fn emit_with_code(&self, msp: &MultiSpan, msg: &str, code: DiagnosticId, lvl: Level) {
550554
if lvl == Warning && !self.can_emit_warnings {
551555
return;
552556
}
553-
let mut db = DiagnosticBuilder::new_with_code(self, lvl, Some(code.to_owned()), msg);
557+
let mut db = DiagnosticBuilder::new_with_code(self, lvl, Some(code), msg);
554558
db.set_span(msp.clone());
555559
db.emit();
556560
if !self.continue_after_error.get() {

Diff for: src/librustc_mir/util/borrowck_errors.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// except according to those terms.
1010

1111
use rustc::ty::{self, TyCtxt};
12-
use rustc_errors::DiagnosticBuilder;
12+
use rustc_errors::{DiagnosticBuilder, DiagnosticId};
1313
use syntax_pos::{MultiSpan, Span};
1414

1515
use std::fmt;
@@ -41,7 +41,7 @@ pub trait BorrowckErrors {
4141
fn struct_span_err_with_code<'a, S: Into<MultiSpan>>(&'a self,
4242
sp: S,
4343
msg: &str,
44-
code: &str)
44+
code: DiagnosticId)
4545
-> DiagnosticBuilder<'a>;
4646

4747
fn struct_span_err<'a, S: Into<MultiSpan>>(&'a self,
@@ -445,7 +445,7 @@ impl<'b, 'gcx, 'tcx> BorrowckErrors for TyCtxt<'b, 'gcx, 'tcx> {
445445
fn struct_span_err_with_code<'a, S: Into<MultiSpan>>(&'a self,
446446
sp: S,
447447
msg: &str,
448-
code: &str)
448+
code: DiagnosticId)
449449
-> DiagnosticBuilder<'a>
450450
{
451451
self.sess.struct_span_err_with_code(sp, msg, code)

0 commit comments

Comments
 (0)