Skip to content

Commit 883f5e5

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 6fa53b0 commit 883f5e5

File tree

2 files changed

+39
-14
lines changed

2 files changed

+39
-14
lines changed

src/librustc/lint/mod.rs

+7-6
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
use std::hash;
4242
use syntax::ast;
4343
use syntax::codemap::MultiSpan;
@@ -423,7 +423,7 @@ pub fn struct_lint_level<'a>(sess: &'a Session,
423423
LintSource::Default => {
424424
sess.diag_note_once(
425425
&mut err,
426-
lint,
426+
DiagnosticMessageId::from(lint),
427427
&format!("#[{}({})] on by default", level.as_str(), name));
428428
}
429429
LintSource::CommandLine(lint_flag_val) => {
@@ -437,24 +437,25 @@ pub fn struct_lint_level<'a>(sess: &'a Session,
437437
if lint_flag_val.as_str() == name {
438438
sess.diag_note_once(
439439
&mut err,
440-
lint,
440+
DiagnosticMessageId::from(lint),
441441
&format!("requested on the command line with `{} {}`",
442442
flag, hyphen_case_lint_name));
443443
} else {
444444
let hyphen_case_flag_val = lint_flag_val.as_str().replace("_", "-");
445445
sess.diag_note_once(
446446
&mut err,
447-
lint,
447+
DiagnosticMessageId::from(lint),
448448
&format!("`{} {}` implied by `{} {}`",
449449
flag, hyphen_case_lint_name, flag,
450450
hyphen_case_flag_val));
451451
}
452452
}
453453
LintSource::Node(lint_attr_name, src) => {
454-
sess.diag_span_note_once(&mut err, lint, src, "lint level defined here");
454+
sess.diag_span_note_once(&mut err, DiagnosticMessageId::from(lint),
455+
src, "lint level defined here");
455456
if lint_attr_name.as_str() != name {
456457
let level_str = level.as_str();
457-
sess.diag_note_once(&mut err, lint,
458+
sess.diag_note_once(&mut err, DiagnosticMessageId::from(lint),
458459
&format!("#[{}({})] implied by #[{}({})]",
459460
level_str, name, level_str, lint_attr_name));
460461
}

src/librustc/session/mod.rs

+32-8
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)