about summary refs log tree commit diff
path: root/compiler/rustc_attr_parsing/src/attributes/confusables.rs
blob: afd3c012f05adecd2284cea34574400f506f341a (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
use rustc_attr_data_structures::AttributeKind;
use rustc_span::{Span, Symbol, sym};
use thin_vec::ThinVec;

use super::{AcceptMapping, AttributeParser};
use crate::context::{FinalizeContext, Stage};
use crate::session_diagnostics;

#[derive(Default)]
pub(crate) struct ConfusablesParser {
    confusables: ThinVec<Symbol>,
    first_span: Option<Span>,
}

impl<S: Stage> AttributeParser<S> for ConfusablesParser {
    const ATTRIBUTES: AcceptMapping<Self, S> = &[(&[sym::rustc_confusables], |this, cx, args| {
        let Some(list) = args.list() else {
            // FIXME(jdonszelmann): error when not a list? Bring validation code here.
            //       NOTE: currently subsequent attributes are silently ignored using
            //       tcx.get_attr().
            return;
        };

        if list.is_empty() {
            cx.emit_err(session_diagnostics::EmptyConfusables { span: cx.attr_span });
        }

        for param in list.mixed() {
            let span = param.span();

            let Some(lit) = param.lit() else {
                cx.emit_err(session_diagnostics::IncorrectMetaItem {
                    span,
                    suggestion: Some(session_diagnostics::IncorrectMetaItemSuggestion {
                        lo: span.shrink_to_lo(),
                        hi: span.shrink_to_hi(),
                    }),
                });
                continue;
            };

            this.confusables.push(lit.symbol);
        }

        this.first_span.get_or_insert(cx.attr_span);
    })];

    fn finalize(self, _cx: &FinalizeContext<'_, '_, S>) -> Option<AttributeKind> {
        if self.confusables.is_empty() {
            return None;
        }

        Some(AttributeKind::Confusables {
            symbols: self.confusables,
            first_span: self.first_span.unwrap(),
        })
    }
}