Skip to content

Commit 9c762b1

Browse files
committed
one-time diagnostics: span_suggestion, generalize methods for non-lints
304c8b1 made the Session's one-time-diagnostics set take a special-purpose `DiagnosticMessageId` enum rather than a LintID so that it could support more than just lints, but the `diag_span_note_once` and `diag_note_once` methods continued to take references to lints: for API consistency, we now make these methods take a `DiagnosticMessageId` while we add support for one-time span-suggestions.
1 parent 5f44c65 commit 9c762b1

File tree

2 files changed

+39
-14
lines changed

2 files changed

+39
-14
lines changed

src/librustc/lint/mod.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use errors::{DiagnosticBuilder, DiagnosticId};
3737
use hir::def_id::{CrateNum, LOCAL_CRATE};
3838
use hir::intravisit::{self, FnKind};
3939
use hir;
40-
use session::Session;
40+
use session::{Session, DiagnosticMessageId};
4141
#[cfg(stage0)]
4242
use std::ascii::AsciiExt;
4343
use std::hash;
@@ -426,7 +426,7 @@ pub fn struct_lint_level<'a>(sess: &'a Session,
426426
LintSource::Default => {
427427
sess.diag_note_once(
428428
&mut err,
429-
lint,
429+
DiagnosticMessageId::from(lint),
430430
&format!("#[{}({})] on by default", level.as_str(), name));
431431
}
432432
LintSource::CommandLine(lint_flag_val) => {
@@ -440,24 +440,25 @@ pub fn struct_lint_level<'a>(sess: &'a Session,
440440
if lint_flag_val.as_str() == name {
441441
sess.diag_note_once(
442442
&mut err,
443-
lint,
443+
DiagnosticMessageId::from(lint),
444444
&format!("requested on the command line with `{} {}`",
445445
flag, hyphen_case_lint_name));
446446
} else {
447447
let hyphen_case_flag_val = lint_flag_val.as_str().replace("_", "-");
448448
sess.diag_note_once(
449449
&mut err,
450-
lint,
450+
DiagnosticMessageId::from(lint),
451451
&format!("`{} {}` implied by `{} {}`",
452452
flag, hyphen_case_lint_name, flag,
453453
hyphen_case_flag_val));
454454
}
455455
}
456456
LintSource::Node(lint_attr_name, src) => {
457-
sess.diag_span_note_once(&mut err, lint, src, "lint level defined here");
457+
sess.diag_span_note_once(&mut err, DiagnosticMessageId::from(lint),
458+
src, "lint level defined here");
458459
if lint_attr_name.as_str() != name {
459460
let level_str = level.as_str();
460-
sess.diag_note_once(&mut err, lint,
461+
sess.diag_note_once(&mut err, DiagnosticMessageId::from(lint),
461462
&format!("#[{}({})] implied by #[{}({})]",
462463
level_str, name, level_str, lint_attr_name));
463464
}

src/librustc/session/mod.rs

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ pub struct PerfStats {
161161
enum DiagnosticBuilderMethod {
162162
Note,
163163
SpanNote,
164+
SpanSuggestion(String), // suggestion
164165
// add more variants as needed to support one-time diagnostics
165166
}
166167

@@ -173,6 +174,12 @@ pub enum DiagnosticMessageId {
173174
StabilityId(u32) // issue number
174175
}
175176

177+
impl From<&'static lint::Lint> for DiagnosticMessageId {
178+
fn from(lint: &'static lint::Lint) -> Self {
179+
DiagnosticMessageId::LintId(lint::LintId::of(lint))
180+
}
181+
}
182+
176183
impl Session {
177184
pub fn local_crate_disambiguator(&self) -> CrateDisambiguator {
178185
match *self.crate_disambiguator.borrow() {
@@ -358,33 +365,50 @@ impl Session {
358365
fn diag_once<'a, 'b>(&'a self,
359366
diag_builder: &'b mut DiagnosticBuilder<'a>,
360367
method: DiagnosticBuilderMethod,
361-
lint: &'static lint::Lint, message: &str, span: Option<Span>) {
368+
msg_id: DiagnosticMessageId,
369+
message: &str,
370+
span_maybe: Option<Span>) {
362371

363-
let lint_id = DiagnosticMessageId::LintId(lint::LintId::of(lint));
364-
let id_span_message = (lint_id, span, message.to_owned());
372+
let id_span_message = (msg_id, span_maybe, message.to_owned());
365373
let fresh = self.one_time_diagnostics.borrow_mut().insert(id_span_message);
366374
if fresh {
367375
match method {
368376
DiagnosticBuilderMethod::Note => {
369377
diag_builder.note(message);
370378
},
371379
DiagnosticBuilderMethod::SpanNote => {
372-
diag_builder.span_note(span.expect("span_note expects a span"), message);
380+
let span = span_maybe.expect("span_note needs a span");
381+
diag_builder.span_note(span, message);
382+
},
383+
DiagnosticBuilderMethod::SpanSuggestion(suggestion) => {
384+
let span = span_maybe.expect("span_suggestion needs a span");
385+
diag_builder.span_suggestion(span, message, suggestion);
373386
}
374387
}
375388
}
376389
}
377390

378391
pub fn diag_span_note_once<'a, 'b>(&'a self,
379392
diag_builder: &'b mut DiagnosticBuilder<'a>,
380-
lint: &'static lint::Lint, span: Span, message: &str) {
381-
self.diag_once(diag_builder, DiagnosticBuilderMethod::SpanNote, lint, message, Some(span));
393+
msg_id: DiagnosticMessageId, span: Span, message: &str) {
394+
self.diag_once(diag_builder, DiagnosticBuilderMethod::SpanNote,
395+
msg_id, message, Some(span));
382396
}
383397

384398
pub fn diag_note_once<'a, 'b>(&'a self,
385399
diag_builder: &'b mut DiagnosticBuilder<'a>,
386-
lint: &'static lint::Lint, message: &str) {
387-
self.diag_once(diag_builder, DiagnosticBuilderMethod::Note, lint, message, None);
400+
msg_id: DiagnosticMessageId, message: &str) {
401+
self.diag_once(diag_builder, DiagnosticBuilderMethod::Note, msg_id, message, None);
402+
}
403+
404+
pub fn diag_span_suggestion_once<'a, 'b>(&'a self,
405+
diag_builder: &'b mut DiagnosticBuilder<'a>,
406+
msg_id: DiagnosticMessageId,
407+
span: Span,
408+
message: &str,
409+
suggestion: String) {
410+
self.diag_once(diag_builder, DiagnosticBuilderMethod::SpanSuggestion(suggestion),
411+
msg_id, message, Some(span));
388412
}
389413

390414
pub fn codemap<'a>(&'a self) -> &'a codemap::CodeMap {

0 commit comments

Comments
 (0)