rustc_attr_parsing/attributes/
confusables.rs

1use super::prelude::*;
2use crate::session_diagnostics::EmptyConfusables;
3
4#[derive(Default)]
5pub(crate) struct ConfusablesParser {
6    confusables: ThinVec<Symbol>,
7    first_span: Option<Span>,
8}
9
10impl<S: Stage> AttributeParser<S> for ConfusablesParser {
11    const ATTRIBUTES: AcceptMapping<Self, S> = &[(
12        &[sym::rustc_confusables],
13        template!(List: &[r#""name1", "name2", ..."#]),
14        |this, cx, args| {
15            let Some(list) = args.list() else {
16                cx.expected_list(cx.attr_span);
17                return;
18            };
19
20            if list.is_empty() {
21                cx.emit_err(EmptyConfusables { span: cx.attr_span });
22            }
23
24            for param in list.mixed() {
25                let span = param.span();
26
27                let Some(lit) = param.lit().and_then(|i| i.value_str()) else {
28                    cx.expected_string_literal(span, param.lit());
29                    continue;
30                };
31
32                this.confusables.push(lit);
33            }
34
35            this.first_span.get_or_insert(cx.attr_span);
36        },
37    )];
38    const ALLOWED_TARGETS: AllowedTargets =
39        AllowedTargets::AllowList(&[Allow(Target::Method(MethodKind::Inherent))]);
40
41    fn finalize(self, _cx: &FinalizeContext<'_, '_, S>) -> Option<AttributeKind> {
42        if self.confusables.is_empty() {
43            return None;
44        }
45
46        Some(AttributeKind::Confusables {
47            symbols: self.confusables,
48            first_span: self.first_span.unwrap(),
49        })
50    }
51}