Skip to content

Commit ea1ca70

Browse files
committed
Preapare for diag structs for buffered lints
1 parent 5e43c32 commit ea1ca70

File tree

3 files changed

+89
-20
lines changed

3 files changed

+89
-20
lines changed

compiler/rustc_lint/src/context.rs

+8-13
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,
538-
span: Option<impl Into<MultiSpan>>,
539-
decorate: impl for<'a, 'b> FnOnce(&'b mut Diag<'a, ()>),
536+
span: MultiSpan,
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

+79-5
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,24 @@
33

44
use rustc_ast::util::unicode::TEXT_FLOW_CONTROL_CHARS;
55
use rustc_errors::{
6-
elided_lifetime_in_path_suggestion, pluralize, Diag, DiagMessage, LintDiagnostic,
6+
elided_lifetime_in_path_suggestion, pluralize, Diag, DiagMessage, LintDiagnostic, MultiSpan,
77
};
88
use rustc_errors::{Applicability, SuggestionStyle};
99
use rustc_middle::middle::stability;
10-
use rustc_session::lint::BuiltinLintDiag;
10+
use rustc_session::lint::{BuiltinLintDiag, Lint};
1111
use rustc_session::Session;
1212
use rustc_span::BytePos;
1313

1414
use std::fmt::Write;
1515

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

1818
mod check_cfg;
1919

2020
#[cfg(test)]
2121
mod tests;
2222

23-
pub(super) fn builtin(sess: &Session, diagnostic: BuiltinLintDiag, diag: &mut Diag<'_, ()>) {
23+
fn buffered_decorate(sess: &Session, diagnostic: BuiltinLintDiag, diag: &mut Diag<'_, ()>) {
2424
match diagnostic {
2525
BuiltinLintDiag::UnicodeTextFlow(span, content) => {
2626
let spans: Vec<_> = content
@@ -403,7 +403,7 @@ pub(super) fn builtin(sess: &Session, diagnostic: BuiltinLintDiag, diag: &mut Di
403403
}
404404
}
405405

406-
pub(super) fn builtin_message(diagnostic: &BuiltinLintDiag) -> DiagMessage {
406+
fn buffered_message(diagnostic: &BuiltinLintDiag) -> DiagMessage {
407407
match diagnostic {
408408
BuiltinLintDiag::AbsPathWithModule(_) => fluent::lint_abs_path_with_module,
409409
BuiltinLintDiag::ProcMacroDeriveResolutionFallback { ns, ident, .. } => {
@@ -561,6 +561,80 @@ pub(super) fn builtin_message(diagnostic: &BuiltinLintDiag) -> DiagMessage {
561561
}
562562
}
563563

564+
pub(super) fn emit_buffered_lint<C: LintContext + ?Sized>(
565+
ctx: &C,
566+
lint: &'static Lint,
567+
span: MultiSpan,
568+
diagnostic: BuiltinLintDiag,
569+
) {
570+
match diagnostic {
571+
BuiltinLintDiag::AbsPathWithModule(_)
572+
| BuiltinLintDiag::ProcMacroDeriveResolutionFallback { .. }
573+
| BuiltinLintDiag::MacroExpandedMacroExportsAccessedByAbsolutePaths(_)
574+
| BuiltinLintDiag::ElidedLifetimesInPaths(_, _, _, _)
575+
| BuiltinLintDiag::UnknownCrateTypes { .. }
576+
| BuiltinLintDiag::UnusedImports { .. }
577+
| BuiltinLintDiag::RedundantImport(_, _)
578+
| BuiltinLintDiag::DeprecatedMacro { .. }
579+
| BuiltinLintDiag::MissingAbi(_, _)
580+
| BuiltinLintDiag::UnusedDocComment(_)
581+
| BuiltinLintDiag::UnusedBuiltinAttribute { .. }
582+
| BuiltinLintDiag::PatternsInFnsWithoutBody { .. }
583+
| BuiltinLintDiag::LegacyDeriveHelpers(_)
584+
| BuiltinLintDiag::ProcMacroBackCompat(_)
585+
| BuiltinLintDiag::OrPatternsBackCompat(_, _)
586+
| BuiltinLintDiag::ReservedPrefix(_, _)
587+
| BuiltinLintDiag::TrailingMacro(_, _)
588+
| BuiltinLintDiag::BreakWithLabelAndLoop(_)
589+
| BuiltinLintDiag::UnicodeTextFlow(_, _)
590+
| BuiltinLintDiag::UnexpectedCfgName(_, _)
591+
| BuiltinLintDiag::UnexpectedCfgValue(_, _)
592+
| BuiltinLintDiag::DeprecatedWhereclauseLocation(_)
593+
| BuiltinLintDiag::SingleUseLifetime { .. }
594+
| BuiltinLintDiag::NamedArgumentUsedPositionally { .. }
595+
| BuiltinLintDiag::ByteSliceInPackedStructWithDerive { .. }
596+
| BuiltinLintDiag::UnusedExternCrate { .. }
597+
| BuiltinLintDiag::ExternCrateNotIdiomatic { .. }
598+
| BuiltinLintDiag::AmbiguousGlobImports { .. }
599+
| BuiltinLintDiag::AmbiguousGlobReexports { .. }
600+
| BuiltinLintDiag::HiddenGlobReexports { .. }
601+
| BuiltinLintDiag::UnusedQualifications { .. }
602+
| BuiltinLintDiag::AssociatedConstElidedLifetime { .. }
603+
| BuiltinLintDiag::RedundantImportVisibility { .. }
604+
| BuiltinLintDiag::MacroUseDeprecated
605+
| BuiltinLintDiag::UnusedMacroUse
606+
| BuiltinLintDiag::PrivateExternCrateReexport(_)
607+
| BuiltinLintDiag::UnusedLabel
608+
| BuiltinLintDiag::MacroIsPrivate(_)
609+
| BuiltinLintDiag::UnusedMacroDefinition(_)
610+
| BuiltinLintDiag::MacroRuleNeverUsed(_, _)
611+
| BuiltinLintDiag::UnstableFeature(_)
612+
| BuiltinLintDiag::AvoidUsingIntelSyntax
613+
| BuiltinLintDiag::AvoidUsingAttSyntax
614+
| BuiltinLintDiag::IncompleteInclude
615+
| BuiltinLintDiag::UnnameableTestItems
616+
| BuiltinLintDiag::DuplicateMacroAttribute
617+
| BuiltinLintDiag::CfgAttrNoAttributes
618+
| BuiltinLintDiag::CrateTypeInCfgAttr
619+
| BuiltinLintDiag::CrateNameInCfgAttr
620+
| BuiltinLintDiag::MissingFragmentSpecifier
621+
| BuiltinLintDiag::MetaVariableStillRepeating(_)
622+
| BuiltinLintDiag::MetaVariableWrongOperator
623+
| BuiltinLintDiag::DuplicateMatcherBinding
624+
| BuiltinLintDiag::UnknownMacroVariable(_)
625+
| BuiltinLintDiag::UnusedExternCrate2 { .. }
626+
| BuiltinLintDiag::WasmCAbi
627+
| BuiltinLintDiag::IllFormedAttributeInput { .. }
628+
| BuiltinLintDiag::InnerAttributeUnstable { .. }
629+
| BuiltinLintDiag::UnknownDiagnosticAttribute => {
630+
ctx.span_lint(lint, span, buffered_message(&diagnostic), |db| {
631+
// Now, set up surrounding context.
632+
buffered_decorate(ctx.sess(), diagnostic, db);
633+
})
634+
}
635+
}
636+
}
637+
564638
/// Convert the given number into the corresponding ordinal
565639
pub(crate) fn ordinalize(v: usize) -> String {
566640
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, span, diagnostic);
4949
}
5050
}
5151

0 commit comments

Comments
 (0)