about summary refs log tree commit diff
path: root/compiler/rustc_errors/src/decorate_diag.rs
blob: 5aef26ccf973d31ee3f7d0b6204c2ee740ae995d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/// This module provides types and traits for buffering lints until later in compilation.
use rustc_ast::node_id::NodeId;
use rustc_data_structures::fx::FxIndexMap;
use rustc_error_messages::MultiSpan;
use rustc_lint_defs::{BuiltinLintDiag, Lint, LintId};

use crate::{DynSend, LintDiagnostic, LintDiagnosticBox};

/// We can't implement `LintDiagnostic` for `BuiltinLintDiag`, because decorating some of its
/// variants requires types we don't have yet. So, handle that case separately.
pub enum DecorateDiagCompat {
    Dynamic(Box<dyn for<'a> LintDiagnosticBox<'a, ()> + DynSend + 'static>),
    Builtin(BuiltinLintDiag),
}

impl std::fmt::Debug for DecorateDiagCompat {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("DecorateDiagCompat").finish()
    }
}

impl !LintDiagnostic<'_, ()> for BuiltinLintDiag {}

impl<D: for<'a> LintDiagnostic<'a, ()> + DynSend + 'static> From<D> for DecorateDiagCompat {
    #[inline]
    fn from(d: D) -> Self {
        Self::Dynamic(Box::new(d))
    }
}

impl From<BuiltinLintDiag> for DecorateDiagCompat {
    #[inline]
    fn from(b: BuiltinLintDiag) -> Self {
        Self::Builtin(b)
    }
}

/// Lints that are buffered up early on in the `Session` before the
/// `LintLevels` is calculated.
#[derive(Debug)]
pub struct BufferedEarlyLint {
    /// The span of code that we are linting on.
    pub span: Option<MultiSpan>,

    /// The `NodeId` of the AST node that generated the lint.
    pub node_id: NodeId,

    /// A lint Id that can be passed to
    /// `rustc_lint::early::EarlyContextAndPass::check_id`.
    pub lint_id: LintId,

    /// Customization of the `Diag<'_>` for the lint.
    pub diagnostic: DecorateDiagCompat,
}

#[derive(Default, Debug)]
pub struct LintBuffer {
    pub map: FxIndexMap<NodeId, Vec<BufferedEarlyLint>>,
}

impl LintBuffer {
    pub fn add_early_lint(&mut self, early_lint: BufferedEarlyLint) {
        self.map.entry(early_lint.node_id).or_default().push(early_lint);
    }

    pub fn take(&mut self, id: NodeId) -> Vec<BufferedEarlyLint> {
        // FIXME(#120456) - is `swap_remove` correct?
        self.map.swap_remove(&id).unwrap_or_default()
    }

    pub fn buffer_lint(
        &mut self,
        lint: &'static Lint,
        node_id: NodeId,
        span: impl Into<MultiSpan>,
        decorate: impl Into<DecorateDiagCompat>,
    ) {
        self.add_early_lint(BufferedEarlyLint {
            lint_id: LintId::of(lint),
            node_id,
            span: Some(span.into()),
            diagnostic: decorate.into(),
        });
    }
}