Skip to content

Commit 2b5bc48

Browse files
committed
improve early lint to use multispan from diagnostic
1 parent 0aeab9a commit 2b5bc48

File tree

5 files changed

+36
-46
lines changed

5 files changed

+36
-46
lines changed

src/librustc/lint/context.rs

Lines changed: 15 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,11 @@ pub struct LintStore {
8383

8484
/// When you call `add_lint` on the session, you wind up storing one
8585
/// of these, which records a "potential lint" at a particular point.
86+
#[derive(PartialEq)]
8687
pub struct EarlyLint {
8788
/// what lint is this? (e.g., `dead_code`)
8889
pub id: LintId,
8990

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`)
93-
pub span: MultiSpan,
94-
9591
/// the main message
9692
pub diagnostic: Diagnostic,
9793
}
@@ -106,38 +102,22 @@ impl fmt::Debug for EarlyLint {
106102
}
107103
}
108104

109-
impl EarlyLint {
110-
pub fn new<M: EarlyLintMessage>(id: LintId, span: Span, msg: M) -> Self {
111-
let diagnostic = msg.into_diagnostic(span);
112-
EarlyLint { id: id, span: MultiSpan::from(span), diagnostic: diagnostic }
113-
}
114-
115-
pub fn with_diagnostic(id: LintId, span: Span, diagnostic: Diagnostic) -> Self {
116-
EarlyLint { id: id, span: MultiSpan::from(span), diagnostic: diagnostic }
117-
}
118-
119-
pub fn matches(&self, other: &EarlyLint) -> bool {
120-
self.id == other.id &&
121-
self.span == other.span &&
122-
self.diagnostic.message == other.diagnostic.message
123-
}
124-
}
125-
126-
pub trait EarlyLintMessage {
127-
fn into_diagnostic(self, span: Span) -> Diagnostic;
105+
pub trait IntoEarlyLint {
106+
fn into_early_lint(self, id: LintId) -> EarlyLint;
128107
}
129108

130-
impl EarlyLintMessage for String {
131-
fn into_diagnostic(self, span: Span) -> Diagnostic {
132-
let mut diagnostic = Diagnostic::new(errors::Level::Warning, &self);
109+
impl<'a> IntoEarlyLint for (Span, &'a str) {
110+
fn into_early_lint(self, id: LintId) -> EarlyLint {
111+
let (span, msg) = self;
112+
let mut diagnostic = Diagnostic::new(errors::Level::Warning, msg);
133113
diagnostic.set_span(span);
134-
diagnostic
114+
EarlyLint { id: id, diagnostic: diagnostic }
135115
}
136116
}
137117

138-
impl EarlyLintMessage for Diagnostic {
139-
fn into_diagnostic(self, _span: Span) -> Diagnostic {
140-
self
118+
impl IntoEarlyLint for Diagnostic {
119+
fn into_early_lint(self, id: LintId) -> EarlyLint {
120+
EarlyLint { id: id, diagnostic: self }
141121
}
142122
}
143123

@@ -578,8 +558,9 @@ pub trait LintContext: Sized {
578558
}
579559

580560
fn early_lint(&self, early_lint: EarlyLint) {
561+
let span = early_lint.diagnostic.span.primary_span().expect("early lint w/o primary span");
581562
let mut err = self.struct_span_lint(early_lint.id.lint,
582-
early_lint.span,
563+
span,
583564
&early_lint.diagnostic.message);
584565
err.copy_details_not_message(&early_lint.diagnostic);
585566
err.emit();
@@ -1283,7 +1264,7 @@ pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
12831264
// in the iteration code.
12841265
for (id, v) in tcx.sess.lints.borrow().iter() {
12851266
for early_lint in v {
1286-
span_bug!(early_lint.span.clone(),
1267+
span_bug!(early_lint.diagnostic.span.clone(),
12871268
"unprocessed lint {:?} at {}",
12881269
early_lint, tcx.map.node_to_string(*id));
12891270
}
@@ -1321,7 +1302,7 @@ pub fn check_ast_crate(sess: &Session, krate: &ast::Crate) {
13211302
// in the iteration code.
13221303
for (_, v) in sess.lints.borrow().iter() {
13231304
for early_lint in v {
1324-
span_bug!(early_lint.span.clone(), "unprocessed lint {:?}", early_lint);
1305+
span_bug!(early_lint.diagnostic.span.clone(), "unprocessed lint {:?}", early_lint);
13251306
}
13261307
}
13271308
}

src/librustc/lint/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ use hir;
4141

4242
pub use lint::context::{LateContext, EarlyContext, LintContext, LintStore,
4343
raw_emit_lint, check_crate, check_ast_crate, gather_attrs,
44-
raw_struct_lint, FutureIncompatibleInfo, EarlyLint, EarlyLintMessage};
44+
raw_struct_lint, FutureIncompatibleInfo, EarlyLint, IntoEarlyLint};
4545

4646
/// Specification of a single lint.
4747
#[derive(Copy, Clone, Debug)]

src/librustc/session/mod.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -258,16 +258,25 @@ impl Session {
258258
pub fn unimpl(&self, msg: &str) -> ! {
259259
self.diagnostic().unimpl(msg)
260260
}
261-
pub fn add_lint<M: lint::EarlyLintMessage>(&self,
262-
lint: &'static lint::Lint,
263-
id: ast::NodeId,
264-
sp: Span,
265-
msg: M) {
261+
pub fn add_lint(&self,
262+
lint: &'static lint::Lint,
263+
id: ast::NodeId,
264+
sp: Span,
265+
msg: String)
266+
{
267+
self.add_lint_diagnostic(lint, id, (sp, &msg[..]))
268+
}
269+
pub fn add_lint_diagnostic<M>(&self,
270+
lint: &'static lint::Lint,
271+
id: ast::NodeId,
272+
msg: M)
273+
where M: lint::IntoEarlyLint,
274+
{
266275
let lint_id = lint::LintId::of(lint);
267276
let mut lints = self.lints.borrow_mut();
268-
let early_lint = lint::EarlyLint::new(lint_id, sp, msg);
277+
let early_lint = msg.into_early_lint(lint_id);
269278
if let Some(arr) = lints.get_mut(&id) {
270-
if !arr.iter().any(|l| l.matches(&early_lint)) {
279+
if !arr.contains(&early_lint) {
271280
arr.push(early_lint);
272281
}
273282
return;

src/librustc_errors/diagnostic.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::fmt;
66
use syntax_pos::{MultiSpan, Span};
77

88
#[must_use]
9-
#[derive(Clone, Debug)]
9+
#[derive(Clone, Debug, PartialEq)]
1010
pub struct Diagnostic {
1111
pub level: Level,
1212
pub message: String,
@@ -16,7 +16,7 @@ pub struct Diagnostic {
1616
}
1717

1818
/// For example a note attached to an error.
19-
#[derive(Clone, Debug)]
19+
#[derive(Clone, Debug, PartialEq)]
2020
pub struct SubDiagnostic {
2121
pub level: Level,
2222
pub message: String,

src/librustc_errors/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ mod lock;
5757
use syntax_pos::{BytePos, Loc, FileLinesResult, FileName, MultiSpan, Span, NO_EXPANSION};
5858
use syntax_pos::MacroBacktrace;
5959

60-
#[derive(Clone, Debug)]
60+
#[derive(Clone, Debug, PartialEq)]
6161
pub enum RenderSpan {
6262
/// A FullSpan renders with both with an initial line for the
6363
/// message, prefixed by file:linenum, followed by a summary of
@@ -71,7 +71,7 @@ pub enum RenderSpan {
7171
Suggestion(CodeSuggestion),
7272
}
7373

74-
#[derive(Clone, Debug)]
74+
#[derive(Clone, Debug, PartialEq)]
7575
pub struct CodeSuggestion {
7676
pub msp: MultiSpan,
7777
pub substitutes: Vec<String>,

0 commit comments

Comments
 (0)