Skip to content

Commit b41bc78

Browse files
committed
Preapare for diag structs for buffered lints
1 parent afc694c commit b41bc78

File tree

3 files changed

+91
-19
lines changed

3 files changed

+91
-19
lines changed

compiler/rustc_lint/src/context.rs

+7-12
Original file line numberDiff line numberDiff line change
@@ -524,29 +524,24 @@ pub struct EarlyContext<'a> {
524524
pub buffered: LintBuffer,
525525
}
526526

527-
pub trait LintContext {
528-
fn sess(&self) -> &Session;
529-
527+
impl<'c> EarlyContext<'c> {
530528
/// Emit a lint at the appropriate level, with an optional associated span and an existing
531529
/// diagnostic.
532530
///
533531
/// [`lint_level`]: rustc_middle::lint::lint_level#decorate-signature
534532
#[rustc_lint_diagnostics]
535-
fn span_lint_with_diagnostics(
533+
pub fn span_lint_with_diagnostics(
536534
&self,
537535
lint: &'static Lint,
538536
span: Option<impl Into<MultiSpan>>,
539-
decorate: impl for<'a, 'b> FnOnce(&'b mut Diag<'a, ()>),
540537
diagnostic: BuiltinLintDiag,
541538
) {
542-
// We first generate a blank diagnostic.
543-
self.opt_span_lint(lint, span, diagnostics::builtin_message(&diagnostic), |db| {
544-
// Now, set up surrounding context.
545-
diagnostics::builtin(self.sess(), diagnostic, db);
546-
// Rewrap `db`, and pass control to the user.
547-
decorate(db)
548-
});
539+
diagnostics::emit_buffered_lint(self, lint, span, diagnostic)
549540
}
541+
}
542+
543+
pub trait LintContext {
544+
fn sess(&self) -> &Session;
550545

551546
// FIXME: These methods should not take an Into<MultiSpan> -- instead, callers should need to
552547
// set the span in their `decorate` function (preferably using set_span).

compiler/rustc_lint/src/context/diagnostics.rs

+82-5
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,25 @@
22
#![allow(rustc::untranslatable_diagnostic)]
33

44
use rustc_ast::util::unicode::TEXT_FLOW_CONTROL_CHARS;
5-
use rustc_errors::{add_elided_lifetime_in_path_suggestion, pluralize, Diag, DiagMessage};
5+
use rustc_errors::{
6+
add_elided_lifetime_in_path_suggestion, pluralize, Diag, DiagMessage, MultiSpan,
7+
};
68
use rustc_errors::{Applicability, SuggestionStyle};
79
use rustc_middle::middle::stability;
8-
use rustc_session::lint::BuiltinLintDiag;
10+
use rustc_session::lint::{BuiltinLintDiag, Lint};
911
use rustc_session::Session;
1012
use rustc_span::BytePos;
1113

1214
use std::fmt::Write;
1315

14-
use crate::fluent_generated as fluent;
16+
use crate::{fluent_generated as fluent, LintContext};
1517

1618
mod check_cfg;
1719

1820
#[cfg(test)]
1921
mod tests;
2022

21-
pub(super) fn builtin(sess: &Session, diagnostic: BuiltinLintDiag, diag: &mut Diag<'_, ()>) {
23+
fn buffered_decorate(sess: &Session, diagnostic: BuiltinLintDiag, diag: &mut Diag<'_, ()>) {
2224
match diagnostic {
2325
BuiltinLintDiag::UnicodeTextFlow(span, content) => {
2426
let spans: Vec<_> = content
@@ -380,7 +382,7 @@ pub(super) fn builtin(sess: &Session, diagnostic: BuiltinLintDiag, diag: &mut Di
380382
}
381383
}
382384

383-
pub(super) fn builtin_message(diagnostic: &BuiltinLintDiag) -> DiagMessage {
385+
fn buffered_message(diagnostic: &BuiltinLintDiag) -> DiagMessage {
384386
match diagnostic {
385387
BuiltinLintDiag::AbsPathWithModule(_) => fluent::lint_abs_path_with_module,
386388
BuiltinLintDiag::ProcMacroDeriveResolutionFallback { ns, ident, .. } => {
@@ -537,6 +539,81 @@ pub(super) fn builtin_message(diagnostic: &BuiltinLintDiag) -> DiagMessage {
537539
}
538540
}
539541

542+
pub(super) fn emit_buffered_lint<C: LintContext + ?Sized>(
543+
ctx: &C,
544+
lint: &'static Lint,
545+
span: Option<impl Into<MultiSpan>>,
546+
diagnostic: BuiltinLintDiag,
547+
) {
548+
match diagnostic {
549+
BuiltinLintDiag::AbsPathWithModule(_)
550+
| BuiltinLintDiag::ProcMacroDeriveResolutionFallback { .. }
551+
| BuiltinLintDiag::MacroExpandedMacroExportsAccessedByAbsolutePaths(_)
552+
| BuiltinLintDiag::ElidedLifetimesInPaths(_, _, _, _)
553+
| BuiltinLintDiag::UnknownCrateTypes(_, _, _)
554+
| BuiltinLintDiag::UnusedImports { .. }
555+
| BuiltinLintDiag::RedundantImport(_, _)
556+
| BuiltinLintDiag::DeprecatedMacro { .. }
557+
| BuiltinLintDiag::MissingAbi(_, _)
558+
| BuiltinLintDiag::UnusedDocComment(_)
559+
| BuiltinLintDiag::UnusedBuiltinAttribute { .. }
560+
| BuiltinLintDiag::PatternsInFnsWithoutBody { .. }
561+
| BuiltinLintDiag::LegacyDeriveHelpers(_)
562+
| BuiltinLintDiag::ProcMacroBackCompat(_)
563+
| BuiltinLintDiag::OrPatternsBackCompat(_, _)
564+
| BuiltinLintDiag::ReservedPrefix(_, _)
565+
| BuiltinLintDiag::TrailingMacro(_, _)
566+
| BuiltinLintDiag::BreakWithLabelAndLoop(_)
567+
| BuiltinLintDiag::UnicodeTextFlow(_, _)
568+
| BuiltinLintDiag::UnexpectedCfgName(_, _)
569+
| BuiltinLintDiag::UnexpectedCfgValue(_, _)
570+
| BuiltinLintDiag::DeprecatedWhereclauseLocation(_)
571+
| BuiltinLintDiag::SingleUseLifetime { .. }
572+
| BuiltinLintDiag::NamedArgumentUsedPositionally { .. }
573+
| BuiltinLintDiag::ByteSliceInPackedStructWithDerive { .. }
574+
| BuiltinLintDiag::UnusedExternCrate { .. }
575+
| BuiltinLintDiag::ExternCrateNotIdiomatic { .. }
576+
| BuiltinLintDiag::AmbiguousGlobImports { .. }
577+
| BuiltinLintDiag::AmbiguousGlobReexports { .. }
578+
| BuiltinLintDiag::HiddenGlobReexports { .. }
579+
| BuiltinLintDiag::UnusedQualifications { .. }
580+
| BuiltinLintDiag::AssociatedConstElidedLifetime { .. }
581+
| BuiltinLintDiag::RedundantImportVisibility { .. }
582+
| BuiltinLintDiag::InvalidCrateTypeValue
583+
| BuiltinLintDiag::MacroUseDeprecated
584+
| BuiltinLintDiag::UnusedMacroUse
585+
| BuiltinLintDiag::PrivateExternCrateReexport(_)
586+
| BuiltinLintDiag::UnusedLabel
587+
| BuiltinLintDiag::MacroIsPrivate(_)
588+
| BuiltinLintDiag::UnusedMacroDefinition(_)
589+
| BuiltinLintDiag::MacroRuleNeverUsed(_, _)
590+
| BuiltinLintDiag::UnstableFeature(_)
591+
| BuiltinLintDiag::AvoidUsingIntelSyntax
592+
| BuiltinLintDiag::AvoidUsingAttSyntax
593+
| BuiltinLintDiag::IncompleteInclude
594+
| BuiltinLintDiag::UnnameableTestItems
595+
| BuiltinLintDiag::DuplicateMacroAttribute
596+
| BuiltinLintDiag::CfgAttrNoAttributes
597+
| BuiltinLintDiag::CrateTypeInCfgAttr
598+
| BuiltinLintDiag::CrateNameInCfgAttr
599+
| BuiltinLintDiag::MissingFragmentSpecifier
600+
| BuiltinLintDiag::MetaVariableStillRepeating(_)
601+
| BuiltinLintDiag::MetaVariableWrongOperator
602+
| BuiltinLintDiag::DuplicateMatcherBinding
603+
| BuiltinLintDiag::UnknownMacroVariable(_)
604+
| BuiltinLintDiag::UnusedExternCrate2 { .. }
605+
| BuiltinLintDiag::WasmCAbi
606+
| BuiltinLintDiag::IllFormedAttributeInput { .. }
607+
| BuiltinLintDiag::InnerAttributeUnstable { .. }
608+
| BuiltinLintDiag::UnknownDiagnosticAttribute => {
609+
ctx.opt_span_lint(lint, span, buffered_message(&diagnostic), |db| {
610+
// Now, set up surrounding context.
611+
buffered_decorate(ctx.sess(), diagnostic, db);
612+
})
613+
}
614+
}
615+
}
616+
540617
/// Convert the given number into the corresponding ordinal
541618
pub(crate) fn ordinalize(v: usize) -> String {
542619
let suffix = match ((11..=13).contains(&(v % 100)), v % 10) {

compiler/rustc_lint/src/early.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
//! upon. As the ast is traversed, this keeps track of the current lint level
1515
//! for all lint attributes.
1616
17-
use crate::context::{EarlyContext, LintContext, LintStore};
17+
use crate::context::{EarlyContext, LintStore};
1818
use crate::passes::{EarlyLintPass, EarlyLintPassObject};
1919
use rustc_ast::ptr::P;
2020
use rustc_ast::visit::{self as ast_visit, walk_list, Visitor};
@@ -45,7 +45,7 @@ impl<'a, T: EarlyLintPass> EarlyContextAndPass<'a, T> {
4545
fn inlined_check_id(&mut self, id: ast::NodeId) {
4646
for early_lint in self.context.buffered.take(id) {
4747
let BufferedEarlyLint { span, node_id: _, lint_id, diagnostic } = early_lint;
48-
self.context.span_lint_with_diagnostics(lint_id.lint, Some(span), |_| {}, diagnostic);
48+
self.context.span_lint_with_diagnostics(lint_id.lint, Some(span), diagnostic);
4949
}
5050
}
5151

0 commit comments

Comments
 (0)