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