Skip to content

Commit f652651

Browse files
committed
retool EarlyLint to track a Diagnostic
1 parent 888a92c commit f652651

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

src/librustc/lint/context.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ use syntax::attr;
4343
use syntax::parse::token::InternedString;
4444
use syntax::ast;
4545
use syntax_pos::Span;
46-
use errors::DiagnosticBuilder;
46+
use errors::{self, Diagnostic, DiagnosticBuilder};
4747
use hir;
4848
use hir::intravisit as hir_visit;
4949
use syntax::visit as ast_visit;
@@ -87,30 +87,36 @@ pub struct EarlyLint {
8787
/// what lint is this? (e.g., `dead_code`)
8888
pub id: LintId,
8989

90-
/// the span where the lint will be reported at
90+
/// what span was it attached to (this is used for Eq comparisons;
91+
/// it duplicates to some extent the information in
92+
/// `diagnostic.span`)
9193
pub span: Span,
9294

9395
/// the main message
94-
pub msg: String,
96+
pub diagnostic: Diagnostic,
9597
}
9698

9799
impl fmt::Debug for EarlyLint {
98100
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
99101
f.debug_struct("EarlyLint")
100102
.field("id", &self.id)
101-
.field("span", &self.span)
102-
.field("msg", &self.msg)
103+
.field("span", &self.diagnostic.span)
104+
.field("diagnostic", &self.diagnostic)
103105
.finish()
104106
}
105107
}
106108

107109
impl EarlyLint {
108110
pub fn new(id: LintId, span: Span, msg: String) -> Self {
109-
EarlyLint { id: id, span: span, msg: msg }
111+
let mut diagnostic = Diagnostic::new(errors::Level::Warning, &msg);
112+
diagnostic.set_span(span);
113+
EarlyLint { id: id, span: span, diagnostic: diagnostic }
110114
}
111115

112116
pub fn matches(&self, other: &EarlyLint) -> bool {
113-
self.id == other.id && self.span == other.span && self.msg == other.msg
117+
self.id == other.id &&
118+
self.span == other.span &&
119+
self.diagnostic.message == other.diagnostic.message
114120
}
115121
}
116122

@@ -551,7 +557,10 @@ pub trait LintContext: Sized {
551557
}
552558

553559
fn early_lint(&self, early_lint: EarlyLint) {
554-
let mut err = self.struct_span_lint(early_lint.id.lint, early_lint.span, &early_lint.msg);
560+
let mut err = self.struct_span_lint(early_lint.id.lint,
561+
early_lint.span,
562+
&early_lint.diagnostic.message);
563+
err.copy_details_not_message(&early_lint.diagnostic);
555564
err.emit();
556565
}
557566

src/librustc_errors/diagnostic.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,14 @@ impl Diagnostic {
166166
self.level
167167
}
168168

169+
/// Used by a lint. Copies over all details *but* the "main
170+
/// message".
171+
pub fn copy_details_not_message(&mut self, from: &Diagnostic) {
172+
self.span = from.span.clone();
173+
self.code = from.code.clone();
174+
self.children.extend(from.children.iter().cloned())
175+
}
176+
169177
/// Convenience function for internal use, clients should use one of the
170178
/// public methods above.
171179
fn sub(&mut self,

src/libsyntax_pos/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ pub struct Span {
6767
/// the error, and would be rendered with `^^^`.
6868
/// - they can have a *label*. In this case, the label is written next
6969
/// to the mark in the snippet when we render.
70-
#[derive(Clone)]
70+
#[derive(Clone, Debug, PartialEq, Eq)]
7171
pub struct MultiSpan {
7272
primary_spans: Vec<Span>,
7373
span_labels: Vec<(Span, String)>,

0 commit comments

Comments
 (0)