1
Fork 0

Filter OnceNote in diagnostic infra.

This commit is contained in:
Camille GILLOT 2022-03-20 20:02:18 +01:00
parent 056951d628
commit 0b49d05ea3
29 changed files with 183 additions and 282 deletions

View file

@ -12,7 +12,7 @@ use rustc_session::lint::{
builtin::{self, FORBIDDEN_LINT_GROUPS},
FutureIncompatibilityReason, Level, Lint, LintExpectationId, LintId,
};
use rustc_session::{DiagnosticMessageId, Session};
use rustc_session::Session;
use rustc_span::hygiene::MacroKind;
use rustc_span::source_map::{DesugaringKind, ExpnKind, MultiSpan};
use rustc_span::{symbol, Span, Symbol, DUMMY_SP};
@ -245,7 +245,6 @@ impl<'a> LintDiagnosticBuilder<'a, ErrorGuaranteed> {
}
pub fn explain_lint_level_source(
sess: &Session,
lint: &'static Lint,
level: Level,
src: LintLevelSource,
@ -254,11 +253,7 @@ pub fn explain_lint_level_source(
let name = lint.name_lower();
match src {
LintLevelSource::Default => {
sess.diag_note_once(
err,
DiagnosticMessageId::from(lint),
&format!("`#[{}({})]` on by default", level.as_str(), name),
);
err.note_once(&format!("`#[{}({})]` on by default", level.as_str(), name));
}
LintLevelSource::CommandLine(lint_flag_val, orig_level) => {
let flag = match orig_level {
@ -273,46 +268,29 @@ pub fn explain_lint_level_source(
};
let hyphen_case_lint_name = name.replace('_', "-");
if lint_flag_val.as_str() == name {
sess.diag_note_once(
err,
DiagnosticMessageId::from(lint),
&format!(
"requested on the command line with `{} {}`",
flag, hyphen_case_lint_name
),
);
err.note_once(&format!(
"requested on the command line with `{} {}`",
flag, hyphen_case_lint_name
));
} else {
let hyphen_case_flag_val = lint_flag_val.as_str().replace('_', "-");
sess.diag_note_once(
err,
DiagnosticMessageId::from(lint),
&format!(
"`{} {}` implied by `{} {}`",
flag, hyphen_case_lint_name, flag, hyphen_case_flag_val
),
);
err.note_once(&format!(
"`{} {}` implied by `{} {}`",
flag, hyphen_case_lint_name, flag, hyphen_case_flag_val
));
}
}
LintLevelSource::Node(lint_attr_name, src, reason) => {
if let Some(rationale) = reason {
err.note(rationale.as_str());
}
sess.diag_span_note_once(
err,
DiagnosticMessageId::from(lint),
src,
"the lint level is defined here",
);
err.span_note_once(src, "the lint level is defined here");
if lint_attr_name.as_str() != name {
let level_str = level.as_str();
sess.diag_note_once(
err,
DiagnosticMessageId::from(lint),
&format!(
"`#[{}({})]` implied by `#[{}({})]`",
level_str, name, level_str, lint_attr_name
),
);
err.note_once(&format!(
"`#[{}({})]` implied by `#[{}({})]`",
level_str, name, level_str, lint_attr_name
));
}
}
}
@ -412,7 +390,7 @@ pub fn struct_lint_level<'s, 'd>(
return;
}
explain_lint_level_source(sess, lint, level, src, &mut err);
explain_lint_level_source(lint, level, src, &mut err);
let name = lint.name_lower();
let is_force_warn = matches!(level, Level::ForceWarn);

View file

@ -17,9 +17,9 @@ use rustc_middle::ty::print::with_no_trimmed_paths;
use rustc_session::lint::builtin::{DEPRECATED, DEPRECATED_IN_FUTURE, SOFT_UNSTABLE};
use rustc_session::lint::{BuiltinLintDiagnostics, Level, Lint, LintBuffer};
use rustc_session::parse::feature_err_issue;
use rustc_session::{DiagnosticMessageId, Session};
use rustc_session::Session;
use rustc_span::symbol::{sym, Symbol};
use rustc_span::{MultiSpan, Span};
use rustc_span::Span;
use std::num::NonZeroU32;
#[derive(PartialEq, Clone, Copy, Debug)]
@ -94,30 +94,15 @@ pub fn report_unstable(
None => format!("use of unstable library feature '{}'", &feature),
};
let msp: MultiSpan = span.into();
let sm = &sess.parse_sess.source_map();
let span_key = msp.primary_span().and_then(|sp: Span| {
if !sp.is_dummy() {
let file = sm.lookup_char_pos(sp.lo()).file;
if file.is_imported() { None } else { Some(span) }
} else {
None
}
});
let error_id = (DiagnosticMessageId::StabilityId(issue), span_key, msg.clone());
let fresh = sess.one_time_diagnostics.borrow_mut().insert(error_id);
if fresh {
if is_soft {
soft_handler(SOFT_UNSTABLE, span, &msg)
} else {
let mut err =
feature_err_issue(&sess.parse_sess, feature, span, GateIssue::Library(issue), &msg);
if let Some((inner_types, ref msg, sugg, applicability)) = suggestion {
err.span_suggestion(inner_types, msg, sugg, applicability);
}
err.emit();
if is_soft {
soft_handler(SOFT_UNSTABLE, span, &msg)
} else {
let mut err =
feature_err_issue(&sess.parse_sess, feature, span, GateIssue::Library(issue), &msg);
if let Some((inner_types, ref msg, sugg, applicability)) = suggestion {
err.span_suggestion(inner_types, msg, sugg, applicability);
}
err.emit();
}
}