Skip to main content

rustc_attr_parsing/attributes/
rustc_internal.rs

1use std::path::PathBuf;
2
3use rustc_ast::{LitIntType, LitKind, MetaItemLit};
4use rustc_hir::LangItem;
5use rustc_hir::attrs::{
6    BorrowckGraphvizFormatKind, CguFields, CguKind, DivergingBlockBehavior,
7    DivergingFallbackBehavior, RustcCleanAttribute, RustcCleanQueries, RustcLayoutType,
8    RustcMirKind,
9};
10use rustc_session::errors;
11use rustc_span::Symbol;
12
13use super::prelude::*;
14use super::util::parse_single_integer;
15use crate::session_diagnostics::{
16    AttributeRequiresOpt, CguFieldsMissing, RustcScalableVectorCountOutOfRange, UnknownLangItem,
17};
18
19pub(crate) struct RustcMainParser;
20
21impl<S: Stage> NoArgsAttributeParser<S> for RustcMainParser {
22    const PATH: &[Symbol] = &[sym::rustc_main];
23    const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
24    const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Fn)]);
25    const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcMain;
26}
27
28pub(crate) struct RustcMustImplementOneOfParser;
29
30impl<S: Stage> SingleAttributeParser<S> for RustcMustImplementOneOfParser {
31    const PATH: &[Symbol] = &[sym::rustc_must_implement_one_of];
32    const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
33    const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Trait)]);
34    const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepInnermost;
35    const TEMPLATE: AttributeTemplate = ::rustc_feature::AttributeTemplate {
    word: false,
    list: Some(&["function1, function2, ..."]),
    one_of: &[],
    name_value_str: None,
    docs: None,
}template!(List: &["function1, function2, ..."]);
36    fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
37        let Some(list) = args.list() else {
38            cx.expected_list(cx.attr_span, args);
39            return None;
40        };
41
42        let mut fn_names = ThinVec::new();
43
44        let inputs: Vec<_> = list.mixed().collect();
45
46        if inputs.len() < 2 {
47            cx.expected_list_with_num_args_or_more(2, list.span);
48            return None;
49        }
50
51        let mut errored = false;
52        for argument in inputs {
53            let Some(meta) = argument.meta_item() else {
54                cx.expected_identifier(argument.span());
55                return None;
56            };
57
58            let Some(ident) = meta.ident() else {
59                cx.dcx().emit_err(errors::MustBeNameOfAssociatedFunction { span: meta.span() });
60                errored = true;
61                continue;
62            };
63
64            fn_names.push(ident);
65        }
66        if errored {
67            return None;
68        }
69
70        Some(AttributeKind::RustcMustImplementOneOf { attr_span: cx.attr_span, fn_names })
71    }
72}
73
74pub(crate) struct RustcNeverReturnsNullPtrParser;
75
76impl<S: Stage> NoArgsAttributeParser<S> for RustcNeverReturnsNullPtrParser {
77    const PATH: &[Symbol] = &[sym::rustc_never_returns_null_ptr];
78    const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
79    const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
80        Allow(Target::Fn),
81        Allow(Target::Method(MethodKind::Inherent)),
82        Allow(Target::Method(MethodKind::Trait { body: false })),
83        Allow(Target::Method(MethodKind::Trait { body: true })),
84        Allow(Target::Method(MethodKind::TraitImpl)),
85    ]);
86    const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcNeverReturnsNullPtr;
87}
88pub(crate) struct RustcNoImplicitAutorefsParser;
89
90impl<S: Stage> NoArgsAttributeParser<S> for RustcNoImplicitAutorefsParser {
91    const PATH: &[Symbol] = &[sym::rustc_no_implicit_autorefs];
92    const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
93    const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
94        Allow(Target::Fn),
95        Allow(Target::Method(MethodKind::Inherent)),
96        Allow(Target::Method(MethodKind::Trait { body: false })),
97        Allow(Target::Method(MethodKind::Trait { body: true })),
98        Allow(Target::Method(MethodKind::TraitImpl)),
99    ]);
100
101    const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcNoImplicitAutorefs;
102}
103
104pub(crate) struct RustcLayoutScalarValidRangeStartParser;
105
106impl<S: Stage> SingleAttributeParser<S> for RustcLayoutScalarValidRangeStartParser {
107    const PATH: &[Symbol] = &[sym::rustc_layout_scalar_valid_range_start];
108    const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepInnermost;
109    const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
110    const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Struct)]);
111    const TEMPLATE: AttributeTemplate = ::rustc_feature::AttributeTemplate {
    word: false,
    list: Some(&["start"]),
    one_of: &[],
    name_value_str: None,
    docs: None,
}template!(List: &["start"]);
112
113    fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
114        parse_single_integer(cx, args)
115            .map(|n| AttributeKind::RustcLayoutScalarValidRangeStart(Box::new(n), cx.attr_span))
116    }
117}
118
119pub(crate) struct RustcLayoutScalarValidRangeEndParser;
120
121impl<S: Stage> SingleAttributeParser<S> for RustcLayoutScalarValidRangeEndParser {
122    const PATH: &[Symbol] = &[sym::rustc_layout_scalar_valid_range_end];
123    const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepInnermost;
124    const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
125    const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Struct)]);
126    const TEMPLATE: AttributeTemplate = ::rustc_feature::AttributeTemplate {
    word: false,
    list: Some(&["end"]),
    one_of: &[],
    name_value_str: None,
    docs: None,
}template!(List: &["end"]);
127
128    fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
129        parse_single_integer(cx, args)
130            .map(|n| AttributeKind::RustcLayoutScalarValidRangeEnd(Box::new(n), cx.attr_span))
131    }
132}
133
134pub(crate) struct RustcLegacyConstGenericsParser;
135
136impl<S: Stage> SingleAttributeParser<S> for RustcLegacyConstGenericsParser {
137    const PATH: &[Symbol] = &[sym::rustc_legacy_const_generics];
138    const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepInnermost;
139    const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
140    const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Fn)]);
141    const TEMPLATE: AttributeTemplate = ::rustc_feature::AttributeTemplate {
    word: false,
    list: Some(&["N"]),
    one_of: &[],
    name_value_str: None,
    docs: None,
}template!(List: &["N"]);
142
143    fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
144        let ArgParser::List(meta_items) = args else {
145            cx.expected_list(cx.attr_span, args);
146            return None;
147        };
148
149        let mut parsed_indexes = ThinVec::new();
150        let mut errored = false;
151
152        for possible_index in meta_items.mixed() {
153            if let MetaItemOrLitParser::Lit(MetaItemLit {
154                kind: LitKind::Int(index, LitIntType::Unsuffixed),
155                ..
156            }) = possible_index
157            {
158                parsed_indexes.push((index.0 as usize, possible_index.span()));
159            } else {
160                cx.expected_integer_literal(possible_index.span());
161                errored = true;
162            }
163        }
164        if errored {
165            return None;
166        } else if parsed_indexes.is_empty() {
167            cx.expected_at_least_one_argument(args.span()?);
168            return None;
169        }
170
171        Some(AttributeKind::RustcLegacyConstGenerics {
172            fn_indexes: parsed_indexes,
173            attr_span: cx.attr_span,
174        })
175    }
176}
177
178pub(crate) struct RustcInheritOverflowChecksParser;
179
180impl<S: Stage> NoArgsAttributeParser<S> for RustcInheritOverflowChecksParser {
181    const PATH: &[Symbol] = &[sym::rustc_inherit_overflow_checks];
182    const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
183    const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
184        Allow(Target::Fn),
185        Allow(Target::Method(MethodKind::Inherent)),
186        Allow(Target::Method(MethodKind::TraitImpl)),
187        Allow(Target::Closure),
188    ]);
189    const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcInheritOverflowChecks;
190}
191
192pub(crate) struct RustcLintOptDenyFieldAccessParser;
193
194impl<S: Stage> SingleAttributeParser<S> for RustcLintOptDenyFieldAccessParser {
195    const PATH: &[Symbol] = &[sym::rustc_lint_opt_deny_field_access];
196    const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepInnermost;
197    const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
198    const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Field)]);
199    const TEMPLATE: AttributeTemplate = ::rustc_feature::AttributeTemplate {
    word: true,
    list: None,
    one_of: &[],
    name_value_str: None,
    docs: None,
}template!(Word);
200    fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
201        let Some(arg) = args.list().and_then(MetaItemListParser::single) else {
202            cx.expected_single_argument(cx.attr_span);
203            return None;
204        };
205
206        let MetaItemOrLitParser::Lit(MetaItemLit { kind: LitKind::Str(lint_message, _), .. }) = arg
207        else {
208            cx.expected_string_literal(arg.span(), arg.lit());
209            return None;
210        };
211
212        Some(AttributeKind::RustcLintOptDenyFieldAccess { lint_message: *lint_message })
213    }
214}
215
216pub(crate) struct RustcLintOptTyParser;
217
218impl<S: Stage> NoArgsAttributeParser<S> for RustcLintOptTyParser {
219    const PATH: &[Symbol] = &[sym::rustc_lint_opt_ty];
220    const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
221    const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Struct)]);
222    const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcLintOptTy;
223}
224
225fn parse_cgu_fields<S: Stage>(
226    cx: &mut AcceptContext<'_, '_, S>,
227    args: &ArgParser,
228    accepts_kind: bool,
229) -> Option<(Symbol, Symbol, Option<CguKind>)> {
230    let Some(args) = args.list() else {
231        cx.expected_list(cx.attr_span, args);
232        return None;
233    };
234
235    let mut cfg = None::<(Symbol, Span)>;
236    let mut module = None::<(Symbol, Span)>;
237    let mut kind = None::<(Symbol, Span)>;
238
239    for arg in args.mixed() {
240        let Some(arg) = arg.meta_item() else {
241            cx.expected_name_value(args.span, None);
242            continue;
243        };
244
245        let res = match arg.ident().map(|i| i.name) {
246            Some(sym::cfg) => &mut cfg,
247            Some(sym::module) => &mut module,
248            Some(sym::kind) if accepts_kind => &mut kind,
249            _ => {
250                cx.expected_specific_argument(
251                    arg.path().span(),
252                    if accepts_kind {
253                        &[sym::cfg, sym::module, sym::kind]
254                    } else {
255                        &[sym::cfg, sym::module]
256                    },
257                );
258                continue;
259            }
260        };
261
262        let Some(i) = arg.args().name_value() else {
263            cx.expected_name_value(arg.span(), None);
264            continue;
265        };
266
267        let Some(str) = i.value_as_str() else {
268            cx.expected_string_literal(i.value_span, Some(i.value_as_lit()));
269            continue;
270        };
271
272        if res.is_some() {
273            cx.duplicate_key(arg.span(), arg.ident().unwrap().name);
274            continue;
275        }
276
277        *res = Some((str, i.value_span));
278    }
279
280    let Some((cfg, _)) = cfg else {
281        cx.emit_err(CguFieldsMissing { span: args.span, name: &cx.attr_path, field: sym::cfg });
282        return None;
283    };
284    let Some((module, _)) = module else {
285        cx.emit_err(CguFieldsMissing { span: args.span, name: &cx.attr_path, field: sym::module });
286        return None;
287    };
288    let kind = if let Some((kind, span)) = kind {
289        Some(match kind {
290            sym::no => CguKind::No,
291            sym::pre_dash_lto => CguKind::PreDashLto,
292            sym::post_dash_lto => CguKind::PostDashLto,
293            sym::any => CguKind::Any,
294            _ => {
295                cx.expected_specific_argument_strings(
296                    span,
297                    &[sym::no, sym::pre_dash_lto, sym::post_dash_lto, sym::any],
298                );
299                return None;
300            }
301        })
302    } else {
303        // return None so that an unwrap for the attributes that need it is ok.
304        if accepts_kind {
305            cx.emit_err(CguFieldsMissing {
306                span: args.span,
307                name: &cx.attr_path,
308                field: sym::kind,
309            });
310            return None;
311        };
312
313        None
314    };
315
316    Some((cfg, module, kind))
317}
318
319#[derive(#[automatically_derived]
impl ::core::default::Default for RustcCguTestAttributeParser {
    #[inline]
    fn default() -> RustcCguTestAttributeParser {
        RustcCguTestAttributeParser {
            items: ::core::default::Default::default(),
        }
    }
}Default)]
320pub(crate) struct RustcCguTestAttributeParser {
321    items: ThinVec<(Span, CguFields)>,
322}
323
324impl<S: Stage> AttributeParser<S> for RustcCguTestAttributeParser {
325    const ATTRIBUTES: AcceptMapping<Self, S> = &[
326        (
327            &[sym::rustc_partition_reused],
328            ::rustc_feature::AttributeTemplate {
    word: false,
    list: Some(&[r#"cfg = "...", module = "...""#]),
    one_of: &[],
    name_value_str: None,
    docs: None,
}template!(List: &[r#"cfg = "...", module = "...""#]),
329            |this, cx, args| {
330                this.items.extend(parse_cgu_fields(cx, args, false).map(|(cfg, module, _)| {
331                    (cx.attr_span, CguFields::PartitionReused { cfg, module })
332                }));
333            },
334        ),
335        (
336            &[sym::rustc_partition_codegened],
337            ::rustc_feature::AttributeTemplate {
    word: false,
    list: Some(&[r#"cfg = "...", module = "...""#]),
    one_of: &[],
    name_value_str: None,
    docs: None,
}template!(List: &[r#"cfg = "...", module = "...""#]),
338            |this, cx, args| {
339                this.items.extend(parse_cgu_fields(cx, args, false).map(|(cfg, module, _)| {
340                    (cx.attr_span, CguFields::PartitionCodegened { cfg, module })
341                }));
342            },
343        ),
344        (
345            &[sym::rustc_expected_cgu_reuse],
346            ::rustc_feature::AttributeTemplate {
    word: false,
    list: Some(&[r#"cfg = "...", module = "...", kind = "...""#]),
    one_of: &[],
    name_value_str: None,
    docs: None,
}template!(List: &[r#"cfg = "...", module = "...", kind = "...""#]),
347            |this, cx, args| {
348                this.items.extend(parse_cgu_fields(cx, args, true).map(|(cfg, module, kind)| {
349                    // unwrap ok because if not given, we return None in `parse_cgu_fields`.
350                    (cx.attr_span, CguFields::ExpectedCguReuse { cfg, module, kind: kind.unwrap() })
351                }));
352            },
353        ),
354    ];
355
356    const ALLOWED_TARGETS: AllowedTargets =
357        AllowedTargets::AllowList(&[Allow(Target::Mod), Allow(Target::Crate)]);
358
359    fn finalize(self, _cx: &FinalizeContext<'_, '_, S>) -> Option<AttributeKind> {
360        Some(AttributeKind::RustcCguTestAttr(self.items))
361    }
362}
363
364pub(crate) struct RustcDeprecatedSafe2024Parser;
365
366impl<S: Stage> SingleAttributeParser<S> for RustcDeprecatedSafe2024Parser {
367    const PATH: &[Symbol] = &[sym::rustc_deprecated_safe_2024];
368    const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
369    const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
370        Allow(Target::Fn),
371        Allow(Target::Method(MethodKind::Inherent)),
372        Allow(Target::Method(MethodKind::Trait { body: false })),
373        Allow(Target::Method(MethodKind::Trait { body: true })),
374        Allow(Target::Method(MethodKind::TraitImpl)),
375    ]);
376    const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepInnermost;
377    const TEMPLATE: AttributeTemplate = ::rustc_feature::AttributeTemplate {
    word: false,
    list: Some(&[r#"audit_that = "...""#]),
    one_of: &[],
    name_value_str: None,
    docs: None,
}template!(List: &[r#"audit_that = "...""#]);
378
379    fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
380        let Some(args) = args.list() else {
381            cx.expected_list(cx.attr_span, args);
382            return None;
383        };
384
385        let Some(single) = args.single() else {
386            cx.expected_single_argument(args.span);
387            return None;
388        };
389
390        let Some(arg) = single.meta_item() else {
391            cx.expected_name_value(args.span, None);
392            return None;
393        };
394
395        let Some(args) = arg.word_is(sym::audit_that) else {
396            cx.expected_specific_argument(arg.span(), &[sym::audit_that]);
397            return None;
398        };
399
400        let Some(nv) = args.name_value() else {
401            cx.expected_name_value(arg.span(), Some(sym::audit_that));
402            return None;
403        };
404
405        let Some(suggestion) = nv.value_as_str() else {
406            cx.expected_string_literal(nv.value_span, Some(nv.value_as_lit()));
407            return None;
408        };
409
410        Some(AttributeKind::RustcDeprecatedSafe2024 { suggestion })
411    }
412}
413
414pub(crate) struct RustcConversionSuggestionParser;
415
416impl<S: Stage> NoArgsAttributeParser<S> for RustcConversionSuggestionParser {
417    const PATH: &[Symbol] = &[sym::rustc_conversion_suggestion];
418    const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
419    const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
420        Allow(Target::Fn),
421        Allow(Target::Method(MethodKind::Inherent)),
422        Allow(Target::Method(MethodKind::Trait { body: false })),
423        Allow(Target::Method(MethodKind::Trait { body: true })),
424        Allow(Target::Method(MethodKind::TraitImpl)),
425    ]);
426    const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcConversionSuggestion;
427}
428
429pub(crate) struct RustcCaptureAnalysisParser;
430
431impl<S: Stage> NoArgsAttributeParser<S> for RustcCaptureAnalysisParser {
432    const PATH: &[Symbol] = &[sym::rustc_capture_analysis];
433    const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
434    const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Closure)]);
435    const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcCaptureAnalysis;
436}
437
438pub(crate) struct RustcNeverTypeOptionsParser;
439
440impl<S: Stage> SingleAttributeParser<S> for RustcNeverTypeOptionsParser {
441    const PATH: &[Symbol] = &[sym::rustc_never_type_options];
442    const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
443    const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Crate)]);
444    const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepInnermost;
445    const TEMPLATE: AttributeTemplate = ::rustc_feature::AttributeTemplate {
    word: false,
    list: Some(&[r#"fallback = "unit", "never", "no""#,
                    r#"diverging_block_default = "unit", "never""#]),
    one_of: &[],
    name_value_str: None,
    docs: None,
}template!(List: &[
446        r#"fallback = "unit", "never", "no""#,
447        r#"diverging_block_default = "unit", "never""#,
448    ]);
449
450    fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
451        let Some(list) = args.list() else {
452            cx.expected_list(cx.attr_span, args);
453            return None;
454        };
455
456        let mut fallback = None::<Ident>;
457        let mut diverging_block_default = None::<Ident>;
458
459        for arg in list.mixed() {
460            let Some(meta) = arg.meta_item() else {
461                cx.expected_name_value(arg.span(), None);
462                continue;
463            };
464
465            let res = match meta.ident().map(|i| i.name) {
466                Some(sym::fallback) => &mut fallback,
467                Some(sym::diverging_block_default) => &mut diverging_block_default,
468                _ => {
469                    cx.expected_specific_argument(
470                        meta.path().span(),
471                        &[sym::fallback, sym::diverging_block_default],
472                    );
473                    continue;
474                }
475            };
476
477            let Some(nv) = meta.args().name_value() else {
478                cx.expected_name_value(meta.span(), None);
479                continue;
480            };
481
482            let Some(field) = nv.value_as_str() else {
483                cx.expected_string_literal(nv.value_span, Some(nv.value_as_lit()));
484                continue;
485            };
486
487            if res.is_some() {
488                cx.duplicate_key(meta.span(), meta.ident().unwrap().name);
489                continue;
490            }
491
492            *res = Some(Ident { name: field, span: nv.value_span });
493        }
494
495        let fallback = match fallback {
496            None => None,
497            Some(Ident { name: sym::unit, .. }) => Some(DivergingFallbackBehavior::ToUnit),
498            Some(Ident { name: sym::never, .. }) => Some(DivergingFallbackBehavior::ToNever),
499            Some(Ident { name: sym::no, .. }) => Some(DivergingFallbackBehavior::NoFallback),
500            Some(Ident { span, .. }) => {
501                cx.expected_specific_argument_strings(span, &[sym::unit, sym::never, sym::no]);
502                return None;
503            }
504        };
505
506        let diverging_block_default = match diverging_block_default {
507            None => None,
508            Some(Ident { name: sym::unit, .. }) => Some(DivergingBlockBehavior::Unit),
509            Some(Ident { name: sym::never, .. }) => Some(DivergingBlockBehavior::Never),
510            Some(Ident { span, .. }) => {
511                cx.expected_specific_argument_strings(span, &[sym::unit, sym::no]);
512                return None;
513            }
514        };
515
516        Some(AttributeKind::RustcNeverTypeOptions { fallback, diverging_block_default })
517    }
518}
519
520pub(crate) struct RustcTrivialFieldReadsParser;
521
522impl<S: Stage> NoArgsAttributeParser<S> for RustcTrivialFieldReadsParser {
523    const PATH: &[Symbol] = &[sym::rustc_trivial_field_reads];
524    const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
525    const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Trait)]);
526    const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcTrivialFieldReads;
527}
528
529pub(crate) struct RustcNoMirInlineParser;
530
531impl<S: Stage> NoArgsAttributeParser<S> for RustcNoMirInlineParser {
532    const PATH: &[Symbol] = &[sym::rustc_no_mir_inline];
533    const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
534    const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
535        Allow(Target::Fn),
536        Allow(Target::Method(MethodKind::Inherent)),
537        Allow(Target::Method(MethodKind::Trait { body: false })),
538        Allow(Target::Method(MethodKind::Trait { body: true })),
539        Allow(Target::Method(MethodKind::TraitImpl)),
540    ]);
541    const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcNoMirInline;
542}
543
544pub(crate) struct RustcLintQueryInstabilityParser;
545
546impl<S: Stage> NoArgsAttributeParser<S> for RustcLintQueryInstabilityParser {
547    const PATH: &[Symbol] = &[sym::rustc_lint_query_instability];
548    const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
549    const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
550        Allow(Target::Fn),
551        Allow(Target::Method(MethodKind::Inherent)),
552        Allow(Target::Method(MethodKind::Trait { body: false })),
553        Allow(Target::Method(MethodKind::Trait { body: true })),
554        Allow(Target::Method(MethodKind::TraitImpl)),
555    ]);
556    const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcLintQueryInstability;
557}
558
559pub(crate) struct RustcRegionsParser;
560
561impl<S: Stage> NoArgsAttributeParser<S> for RustcRegionsParser {
562    const PATH: &[Symbol] = &[sym::rustc_regions];
563    const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
564    const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
565        Allow(Target::Fn),
566        Allow(Target::Method(MethodKind::Inherent)),
567        Allow(Target::Method(MethodKind::Trait { body: false })),
568        Allow(Target::Method(MethodKind::Trait { body: true })),
569        Allow(Target::Method(MethodKind::TraitImpl)),
570    ]);
571
572    const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcRegions;
573}
574
575pub(crate) struct RustcLintUntrackedQueryInformationParser;
576
577impl<S: Stage> NoArgsAttributeParser<S> for RustcLintUntrackedQueryInformationParser {
578    const PATH: &[Symbol] = &[sym::rustc_lint_untracked_query_information];
579    const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
580    const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
581        Allow(Target::Fn),
582        Allow(Target::Method(MethodKind::Inherent)),
583        Allow(Target::Method(MethodKind::Trait { body: false })),
584        Allow(Target::Method(MethodKind::Trait { body: true })),
585        Allow(Target::Method(MethodKind::TraitImpl)),
586    ]);
587
588    const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcLintUntrackedQueryInformation;
589}
590
591pub(crate) struct RustcSimdMonomorphizeLaneLimitParser;
592
593impl<S: Stage> SingleAttributeParser<S> for RustcSimdMonomorphizeLaneLimitParser {
594    const PATH: &[Symbol] = &[sym::rustc_simd_monomorphize_lane_limit];
595    const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepInnermost;
596    const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
597    const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Struct)]);
598    const TEMPLATE: AttributeTemplate = ::rustc_feature::AttributeTemplate {
    word: false,
    list: None,
    one_of: &[],
    name_value_str: Some(&["N"]),
    docs: None,
}template!(NameValueStr: "N");
599
600    fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
601        let ArgParser::NameValue(nv) = args else {
602            cx.expected_name_value(cx.attr_span, None);
603            return None;
604        };
605        Some(AttributeKind::RustcSimdMonomorphizeLaneLimit(cx.parse_limit_int(nv)?))
606    }
607}
608
609pub(crate) struct RustcScalableVectorParser;
610
611impl<S: Stage> SingleAttributeParser<S> for RustcScalableVectorParser {
612    const PATH: &[Symbol] = &[sym::rustc_scalable_vector];
613    const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepInnermost;
614    const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
615    const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Struct)]);
616    const TEMPLATE: AttributeTemplate = ::rustc_feature::AttributeTemplate {
    word: true,
    list: Some(&["count"]),
    one_of: &[],
    name_value_str: None,
    docs: None,
}template!(Word, List: &["count"]);
617
618    fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
619        if args.no_args().is_ok() {
620            return Some(AttributeKind::RustcScalableVector {
621                element_count: None,
622                span: cx.attr_span,
623            });
624        }
625
626        let n = parse_single_integer(cx, args)?;
627        let Ok(n) = n.try_into() else {
628            cx.emit_err(RustcScalableVectorCountOutOfRange { span: cx.attr_span, n });
629            return None;
630        };
631        Some(AttributeKind::RustcScalableVector { element_count: Some(n), span: cx.attr_span })
632    }
633}
634
635pub(crate) struct LangParser;
636
637impl<S: Stage> SingleAttributeParser<S> for LangParser {
638    const PATH: &[Symbol] = &[sym::lang];
639    const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepInnermost;
640    const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
641    const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS); // Targets are checked per lang item in `rustc_passes`
642    const TEMPLATE: AttributeTemplate = ::rustc_feature::AttributeTemplate {
    word: false,
    list: None,
    one_of: &[],
    name_value_str: Some(&["name"]),
    docs: None,
}template!(NameValueStr: "name");
643
644    fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
645        let Some(nv) = args.name_value() else {
646            cx.expected_name_value(cx.attr_span, None);
647            return None;
648        };
649        let Some(name) = nv.value_as_str() else {
650            cx.expected_string_literal(nv.value_span, Some(nv.value_as_lit()));
651            return None;
652        };
653        let Some(lang_item) = LangItem::from_name(name) else {
654            cx.emit_err(UnknownLangItem { span: cx.attr_span, name });
655            return None;
656        };
657        Some(AttributeKind::Lang(lang_item, cx.attr_span))
658    }
659}
660
661pub(crate) struct RustcHasIncoherentInherentImplsParser;
662
663impl<S: Stage> NoArgsAttributeParser<S> for RustcHasIncoherentInherentImplsParser {
664    const PATH: &[Symbol] = &[sym::rustc_has_incoherent_inherent_impls];
665    const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
666    const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
667        Allow(Target::Trait),
668        Allow(Target::Struct),
669        Allow(Target::Enum),
670        Allow(Target::Union),
671        Allow(Target::ForeignTy),
672    ]);
673    const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcHasIncoherentInherentImpls;
674}
675
676pub(crate) struct PanicHandlerParser;
677
678impl<S: Stage> NoArgsAttributeParser<S> for PanicHandlerParser {
679    const PATH: &[Symbol] = &[sym::panic_handler];
680    const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
681    const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS); // Targets are checked per lang item in `rustc_passes`
682    const CREATE: fn(Span) -> AttributeKind = |span| AttributeKind::Lang(LangItem::PanicImpl, span);
683}
684
685pub(crate) struct RustcHiddenTypeOfOpaquesParser;
686
687impl<S: Stage> NoArgsAttributeParser<S> for RustcHiddenTypeOfOpaquesParser {
688    const PATH: &[Symbol] = &[sym::rustc_hidden_type_of_opaques];
689    const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
690    const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Crate)]);
691    const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcHiddenTypeOfOpaques;
692}
693pub(crate) struct RustcNounwindParser;
694
695impl<S: Stage> NoArgsAttributeParser<S> for RustcNounwindParser {
696    const PATH: &[Symbol] = &[sym::rustc_nounwind];
697    const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
698    const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
699        Allow(Target::Fn),
700        Allow(Target::ForeignFn),
701        Allow(Target::Method(MethodKind::Inherent)),
702        Allow(Target::Method(MethodKind::TraitImpl)),
703        Allow(Target::Method(MethodKind::Trait { body: true })),
704    ]);
705    const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcNounwind;
706}
707
708pub(crate) struct RustcOffloadKernelParser;
709
710impl<S: Stage> NoArgsAttributeParser<S> for RustcOffloadKernelParser {
711    const PATH: &[Symbol] = &[sym::rustc_offload_kernel];
712    const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
713    const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Fn)]);
714    const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcOffloadKernel;
715}
716
717pub(crate) struct RustcLayoutParser;
718
719impl<S: Stage> CombineAttributeParser<S> for RustcLayoutParser {
720    const PATH: &[Symbol] = &[sym::rustc_layout];
721
722    type Item = RustcLayoutType;
723
724    const CONVERT: ConvertFn<Self::Item> = |items, _| AttributeKind::RustcLayout(items);
725
726    const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
727        Allow(Target::Struct),
728        Allow(Target::Enum),
729        Allow(Target::Union),
730        Allow(Target::TyAlias),
731    ]);
732
733    const TEMPLATE: AttributeTemplate =
734        ::rustc_feature::AttributeTemplate {
    word: false,
    list: Some(&["abi", "align", "size", "homogenous_aggregate", "debug"]),
    one_of: &[],
    name_value_str: None,
    docs: None,
}template!(List: &["abi", "align", "size", "homogenous_aggregate", "debug"]);
735    fn extend(
736        cx: &mut AcceptContext<'_, '_, S>,
737        args: &ArgParser,
738    ) -> impl IntoIterator<Item = Self::Item> {
739        let ArgParser::List(items) = args else {
740            cx.expected_list(cx.attr_span, args);
741            return ::alloc::vec::Vec::new()vec![];
742        };
743
744        let mut result = Vec::new();
745        for item in items.mixed() {
746            let Some(arg) = item.meta_item() else {
747                cx.unexpected_literal(item.span());
748                continue;
749            };
750            let Some(ident) = arg.ident() else {
751                cx.expected_identifier(arg.span());
752                return ::alloc::vec::Vec::new()vec![];
753            };
754            let ty = match ident.name {
755                sym::abi => RustcLayoutType::Abi,
756                sym::align => RustcLayoutType::Align,
757                sym::size => RustcLayoutType::Size,
758                sym::homogeneous_aggregate => RustcLayoutType::HomogenousAggregate,
759                sym::debug => RustcLayoutType::Debug,
760                _ => {
761                    cx.expected_specific_argument(
762                        ident.span,
763                        &[sym::abi, sym::align, sym::size, sym::homogeneous_aggregate, sym::debug],
764                    );
765                    continue;
766                }
767            };
768            result.push(ty);
769        }
770        result
771    }
772}
773
774pub(crate) struct RustcMirParser;
775
776impl<S: Stage> CombineAttributeParser<S> for RustcMirParser {
777    const PATH: &[Symbol] = &[sym::rustc_mir];
778
779    type Item = RustcMirKind;
780
781    const CONVERT: ConvertFn<Self::Item> = |items, _| AttributeKind::RustcMir(items);
782
783    const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
784        Allow(Target::Fn),
785        Allow(Target::Method(MethodKind::Inherent)),
786        Allow(Target::Method(MethodKind::TraitImpl)),
787        Allow(Target::Method(MethodKind::Trait { body: false })),
788        Allow(Target::Method(MethodKind::Trait { body: true })),
789    ]);
790
791    const TEMPLATE: AttributeTemplate = ::rustc_feature::AttributeTemplate {
    word: false,
    list: Some(&["arg1, arg2, ..."]),
    one_of: &[],
    name_value_str: None,
    docs: None,
}template!(List: &["arg1, arg2, ..."]);
792
793    fn extend(
794        cx: &mut AcceptContext<'_, '_, S>,
795        args: &ArgParser,
796    ) -> impl IntoIterator<Item = Self::Item> {
797        let Some(list) = args.list() else {
798            cx.expected_list(cx.attr_span, args);
799            return ThinVec::new();
800        };
801
802        list.mixed()
803            .filter_map(|arg| arg.meta_item())
804            .filter_map(|mi| {
805                if let Some(ident) = mi.ident() {
806                    match ident.name {
807                        sym::rustc_peek_maybe_init => Some(RustcMirKind::PeekMaybeInit),
808                        sym::rustc_peek_maybe_uninit => Some(RustcMirKind::PeekMaybeUninit),
809                        sym::rustc_peek_liveness => Some(RustcMirKind::PeekLiveness),
810                        sym::stop_after_dataflow => Some(RustcMirKind::StopAfterDataflow),
811                        sym::borrowck_graphviz_postflow => {
812                            let Some(nv) = mi.args().name_value() else {
813                                cx.expected_name_value(
814                                    mi.span(),
815                                    Some(sym::borrowck_graphviz_postflow),
816                                );
817                                return None;
818                            };
819                            let Some(path) = nv.value_as_str() else {
820                                cx.expected_string_literal(nv.value_span, None);
821                                return None;
822                            };
823                            let path = PathBuf::from(path.to_string());
824                            if path.file_name().is_some() {
825                                Some(RustcMirKind::BorrowckGraphvizPostflow { path })
826                            } else {
827                                cx.expected_filename_literal(nv.value_span);
828                                None
829                            }
830                        }
831                        sym::borrowck_graphviz_format => {
832                            let Some(nv) = mi.args().name_value() else {
833                                cx.expected_name_value(
834                                    mi.span(),
835                                    Some(sym::borrowck_graphviz_format),
836                                );
837                                return None;
838                            };
839                            let Some(format) = nv.value_as_ident() else {
840                                cx.expected_identifier(nv.value_span);
841                                return None;
842                            };
843                            match format.name {
844                                sym::two_phase => Some(RustcMirKind::BorrowckGraphvizFormat {
845                                    format: BorrowckGraphvizFormatKind::TwoPhase,
846                                }),
847                                _ => {
848                                    cx.expected_specific_argument(format.span, &[sym::two_phase]);
849                                    None
850                                }
851                            }
852                        }
853                        _ => None,
854                    }
855                } else {
856                    None
857                }
858            })
859            .collect()
860    }
861}
862pub(crate) struct RustcNonConstTraitMethodParser;
863
864impl<S: Stage> NoArgsAttributeParser<S> for RustcNonConstTraitMethodParser {
865    const PATH: &[Symbol] = &[sym::rustc_non_const_trait_method];
866    const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
867    const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
868        Allow(Target::Method(MethodKind::Trait { body: true })),
869        Allow(Target::Method(MethodKind::Trait { body: false })),
870    ]);
871    const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcNonConstTraitMethod;
872}
873
874pub(crate) struct RustcCleanParser;
875
876impl<S: Stage> CombineAttributeParser<S> for RustcCleanParser {
877    const PATH: &[Symbol] = &[sym::rustc_clean];
878
879    type Item = RustcCleanAttribute;
880
881    const CONVERT: ConvertFn<Self::Item> = |items, _| AttributeKind::RustcClean(items);
882
883    const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
884        // tidy-alphabetical-start
885        Allow(Target::AssocConst),
886        Allow(Target::AssocTy),
887        Allow(Target::Const),
888        Allow(Target::Enum),
889        Allow(Target::Expression),
890        Allow(Target::Field),
891        Allow(Target::Fn),
892        Allow(Target::ForeignMod),
893        Allow(Target::Impl { of_trait: false }),
894        Allow(Target::Impl { of_trait: true }),
895        Allow(Target::Method(MethodKind::Inherent)),
896        Allow(Target::Method(MethodKind::Trait { body: false })),
897        Allow(Target::Method(MethodKind::Trait { body: true })),
898        Allow(Target::Method(MethodKind::TraitImpl)),
899        Allow(Target::Mod),
900        Allow(Target::Static),
901        Allow(Target::Struct),
902        Allow(Target::Trait),
903        Allow(Target::TyAlias),
904        Allow(Target::Union),
905        // tidy-alphabetical-end
906    ]);
907
908    const TEMPLATE: AttributeTemplate =
909        ::rustc_feature::AttributeTemplate {
    word: false,
    list: Some(&[r#"cfg = "...", /*opt*/ label = "...", /*opt*/ except = "...""#]),
    one_of: &[],
    name_value_str: None,
    docs: None,
}template!(List: &[r#"cfg = "...", /*opt*/ label = "...", /*opt*/ except = "...""#]);
910
911    fn extend(
912        cx: &mut AcceptContext<'_, '_, S>,
913        args: &ArgParser,
914    ) -> impl IntoIterator<Item = Self::Item> {
915        if !cx.cx.sess.opts.unstable_opts.query_dep_graph {
916            cx.emit_err(AttributeRequiresOpt { span: cx.attr_span, opt: "-Z query-dep-graph" });
917        }
918        let Some(list) = args.list() else {
919            cx.expected_list(cx.attr_span, args);
920            return None;
921        };
922        let mut except = None;
923        let mut loaded_from_disk = None;
924        let mut cfg = None;
925
926        for item in list.mixed() {
927            let Some((value, name)) =
928                item.meta_item().and_then(|m| Option::zip(m.args().name_value(), m.ident()))
929            else {
930                cx.expected_name_value(item.span(), None);
931                continue;
932            };
933            let value_span = value.value_span;
934            let Some(value) = value.value_as_str() else {
935                cx.expected_string_literal(value_span, None);
936                continue;
937            };
938            match name.name {
939                sym::cfg if cfg.is_some() => {
940                    cx.duplicate_key(item.span(), sym::cfg);
941                }
942
943                sym::cfg => {
944                    cfg = Some(value);
945                }
946                sym::except if except.is_some() => {
947                    cx.duplicate_key(item.span(), sym::except);
948                }
949                sym::except => {
950                    let entries =
951                        value.as_str().split(',').map(|s| Symbol::intern(s.trim())).collect();
952                    except = Some(RustcCleanQueries { entries, span: value_span });
953                }
954                sym::loaded_from_disk if loaded_from_disk.is_some() => {
955                    cx.duplicate_key(item.span(), sym::loaded_from_disk);
956                }
957                sym::loaded_from_disk => {
958                    let entries =
959                        value.as_str().split(',').map(|s| Symbol::intern(s.trim())).collect();
960                    loaded_from_disk = Some(RustcCleanQueries { entries, span: value_span });
961                }
962                _ => {
963                    cx.expected_specific_argument(
964                        name.span,
965                        &[sym::cfg, sym::except, sym::loaded_from_disk],
966                    );
967                }
968            }
969        }
970        let Some(cfg) = cfg else {
971            cx.expected_specific_argument(list.span, &[sym::cfg]);
972            return None;
973        };
974
975        Some(RustcCleanAttribute { span: cx.attr_span, cfg, except, loaded_from_disk })
976    }
977}
978
979pub(crate) struct RustcIfThisChangedParser;
980
981impl<S: Stage> SingleAttributeParser<S> for RustcIfThisChangedParser {
982    const PATH: &[Symbol] = &[sym::rustc_if_this_changed];
983
984    const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepOutermost;
985
986    const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
987
988    const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
989        // tidy-alphabetical-start
990        Allow(Target::AssocConst),
991        Allow(Target::AssocTy),
992        Allow(Target::Const),
993        Allow(Target::Enum),
994        Allow(Target::Expression),
995        Allow(Target::Field),
996        Allow(Target::Fn),
997        Allow(Target::ForeignMod),
998        Allow(Target::Impl { of_trait: false }),
999        Allow(Target::Impl { of_trait: true }),
1000        Allow(Target::Method(MethodKind::Inherent)),
1001        Allow(Target::Method(MethodKind::Trait { body: false })),
1002        Allow(Target::Method(MethodKind::Trait { body: true })),
1003        Allow(Target::Method(MethodKind::TraitImpl)),
1004        Allow(Target::Mod),
1005        Allow(Target::Static),
1006        Allow(Target::Struct),
1007        Allow(Target::Trait),
1008        Allow(Target::TyAlias),
1009        Allow(Target::Union),
1010        // tidy-alphabetical-end
1011    ]);
1012
1013    const TEMPLATE: AttributeTemplate = ::rustc_feature::AttributeTemplate {
    word: true,
    list: Some(&["DepNode"]),
    one_of: &[],
    name_value_str: None,
    docs: None,
}template!(Word, List: &["DepNode"]);
1014
1015    fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
1016        if !cx.cx.sess.opts.unstable_opts.query_dep_graph {
1017            cx.emit_err(AttributeRequiresOpt { span: cx.attr_span, opt: "-Z query-dep-graph" });
1018        }
1019        match args {
1020            ArgParser::NoArgs => Some(AttributeKind::RustcIfThisChanged(cx.attr_span, None)),
1021            ArgParser::List(list) => {
1022                let Some(item) = list.single() else {
1023                    cx.expected_single_argument(list.span);
1024                    return None;
1025                };
1026                let Some(ident) = item.meta_item().and_then(|item| item.ident()) else {
1027                    cx.expected_identifier(item.span());
1028                    return None;
1029                };
1030                Some(AttributeKind::RustcIfThisChanged(cx.attr_span, Some(ident.name)))
1031            }
1032            ArgParser::NameValue(_) => {
1033                cx.expected_list_or_no_args(cx.inner_span);
1034                None
1035            }
1036        }
1037    }
1038}
1039
1040pub(crate) struct RustcThenThisWouldNeedParser;
1041
1042impl<S: Stage> CombineAttributeParser<S> for RustcThenThisWouldNeedParser {
1043    const PATH: &[Symbol] = &[sym::rustc_then_this_would_need];
1044    type Item = Ident;
1045
1046    const CONVERT: ConvertFn<Self::Item> =
1047        |items, span| AttributeKind::RustcThenThisWouldNeed(span, items);
1048    const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
1049        // tidy-alphabetical-start
1050        Allow(Target::AssocConst),
1051        Allow(Target::AssocTy),
1052        Allow(Target::Const),
1053        Allow(Target::Enum),
1054        Allow(Target::Expression),
1055        Allow(Target::Field),
1056        Allow(Target::Fn),
1057        Allow(Target::ForeignMod),
1058        Allow(Target::Impl { of_trait: false }),
1059        Allow(Target::Impl { of_trait: true }),
1060        Allow(Target::Method(MethodKind::Inherent)),
1061        Allow(Target::Method(MethodKind::Trait { body: false })),
1062        Allow(Target::Method(MethodKind::Trait { body: true })),
1063        Allow(Target::Method(MethodKind::TraitImpl)),
1064        Allow(Target::Mod),
1065        Allow(Target::Static),
1066        Allow(Target::Struct),
1067        Allow(Target::Trait),
1068        Allow(Target::TyAlias),
1069        Allow(Target::Union),
1070        // tidy-alphabetical-end
1071    ]);
1072
1073    const TEMPLATE: AttributeTemplate = ::rustc_feature::AttributeTemplate {
    word: false,
    list: Some(&["DepNode"]),
    one_of: &[],
    name_value_str: None,
    docs: None,
}template!(List: &["DepNode"]);
1074
1075    fn extend(
1076        cx: &mut AcceptContext<'_, '_, S>,
1077        args: &ArgParser,
1078    ) -> impl IntoIterator<Item = Self::Item> {
1079        if !cx.cx.sess.opts.unstable_opts.query_dep_graph {
1080            cx.emit_err(AttributeRequiresOpt { span: cx.attr_span, opt: "-Z query-dep-graph" });
1081        }
1082        let Some(item) = args.list().and_then(|l| l.single()) else {
1083            cx.expected_single_argument(cx.inner_span);
1084            return None;
1085        };
1086        let Some(ident) = item.meta_item().and_then(|item| item.ident()) else {
1087            cx.expected_identifier(item.span());
1088            return None;
1089        };
1090        Some(ident)
1091    }
1092}
1093
1094pub(crate) struct RustcInsignificantDtorParser;
1095
1096impl<S: Stage> NoArgsAttributeParser<S> for RustcInsignificantDtorParser {
1097    const PATH: &[Symbol] = &[sym::rustc_insignificant_dtor];
1098    const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
1099    const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
1100        Allow(Target::Enum),
1101        Allow(Target::Struct),
1102        Allow(Target::ForeignTy),
1103    ]);
1104    const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcInsignificantDtor;
1105}
1106
1107pub(crate) struct RustcEffectiveVisibilityParser;
1108
1109impl<S: Stage> NoArgsAttributeParser<S> for RustcEffectiveVisibilityParser {
1110    const PATH: &[Symbol] = &[sym::rustc_effective_visibility];
1111    const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
1112    const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
1113        Allow(Target::Use),
1114        Allow(Target::Static),
1115        Allow(Target::Const),
1116        Allow(Target::Fn),
1117        Allow(Target::Closure),
1118        Allow(Target::Mod),
1119        Allow(Target::ForeignMod),
1120        Allow(Target::TyAlias),
1121        Allow(Target::Enum),
1122        Allow(Target::Variant),
1123        Allow(Target::Struct),
1124        Allow(Target::Field),
1125        Allow(Target::Union),
1126        Allow(Target::Trait),
1127        Allow(Target::TraitAlias),
1128        Allow(Target::Impl { of_trait: false }),
1129        Allow(Target::Impl { of_trait: true }),
1130        Allow(Target::AssocConst),
1131        Allow(Target::Method(MethodKind::Inherent)),
1132        Allow(Target::Method(MethodKind::Trait { body: false })),
1133        Allow(Target::Method(MethodKind::Trait { body: true })),
1134        Allow(Target::Method(MethodKind::TraitImpl)),
1135        Allow(Target::AssocTy),
1136        Allow(Target::ForeignFn),
1137        Allow(Target::ForeignStatic),
1138        Allow(Target::ForeignTy),
1139        Allow(Target::MacroDef),
1140        Allow(Target::PatField),
1141        Allow(Target::Crate),
1142    ]);
1143    const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcEffectiveVisibility;
1144}
1145
1146pub(crate) struct RustcDiagnosticItemParser;
1147
1148impl<S: Stage> SingleAttributeParser<S> for RustcDiagnosticItemParser {
1149    const PATH: &[Symbol] = &[sym::rustc_diagnostic_item];
1150    const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepOutermost;
1151    const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
1152    const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
1153        Allow(Target::Trait),
1154        Allow(Target::Struct),
1155        Allow(Target::Enum),
1156        Allow(Target::MacroDef),
1157        Allow(Target::TyAlias),
1158        Allow(Target::AssocTy),
1159        Allow(Target::AssocConst),
1160        Allow(Target::Fn),
1161        Allow(Target::Const),
1162        Allow(Target::Mod),
1163        Allow(Target::Impl { of_trait: false }),
1164        Allow(Target::Method(MethodKind::Inherent)),
1165        Allow(Target::Method(MethodKind::Trait { body: false })),
1166        Allow(Target::Method(MethodKind::Trait { body: true })),
1167        Allow(Target::Method(MethodKind::TraitImpl)),
1168        Allow(Target::Crate),
1169    ]);
1170    const TEMPLATE: AttributeTemplate = ::rustc_feature::AttributeTemplate {
    word: false,
    list: None,
    one_of: &[],
    name_value_str: Some(&["name"]),
    docs: None,
}template!(NameValueStr: "name");
1171
1172    fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
1173        let Some(nv) = args.name_value() else {
1174            cx.expected_name_value(cx.attr_span, None);
1175            return None;
1176        };
1177        let Some(value) = nv.value_as_str() else {
1178            cx.expected_string_literal(nv.value_span, Some(nv.value_as_lit()));
1179            return None;
1180        };
1181        Some(AttributeKind::RustcDiagnosticItem(value))
1182    }
1183}
1184
1185pub(crate) struct RustcDoNotConstCheckParser;
1186
1187impl<S: Stage> NoArgsAttributeParser<S> for RustcDoNotConstCheckParser {
1188    const PATH: &[Symbol] = &[sym::rustc_do_not_const_check];
1189    const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
1190    const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
1191        Allow(Target::Fn),
1192        Allow(Target::Method(MethodKind::Inherent)),
1193        Allow(Target::Method(MethodKind::TraitImpl)),
1194        Allow(Target::Method(MethodKind::Trait { body: false })),
1195        Allow(Target::Method(MethodKind::Trait { body: true })),
1196    ]);
1197    const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcDoNotConstCheck;
1198}
1199
1200pub(crate) struct RustcNonnullOptimizationGuaranteedParser;
1201
1202impl<S: Stage> NoArgsAttributeParser<S> for RustcNonnullOptimizationGuaranteedParser {
1203    const PATH: &[Symbol] = &[sym::rustc_nonnull_optimization_guaranteed];
1204    const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
1205    const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Struct)]);
1206    const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcNonnullOptimizationGuaranteed;
1207}
1208
1209pub(crate) struct RustcSymbolNameParser;
1210
1211impl<S: Stage> SingleAttributeParser<S> for RustcSymbolNameParser {
1212    const PATH: &[Symbol] = &[sym::rustc_symbol_name];
1213    const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
1214        Allow(Target::Fn),
1215        Allow(Target::Method(MethodKind::TraitImpl)),
1216        Allow(Target::Method(MethodKind::Inherent)),
1217        Allow(Target::Method(MethodKind::Trait { body: true })),
1218        Allow(Target::ForeignFn),
1219        Allow(Target::ForeignStatic),
1220        Allow(Target::Impl { of_trait: false }),
1221    ]);
1222    const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
1223    const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepInnermost;
1224    const TEMPLATE: AttributeTemplate = ::rustc_feature::AttributeTemplate {
    word: true,
    list: None,
    one_of: &[],
    name_value_str: None,
    docs: None,
}template!(Word);
1225    fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
1226        if let Err(span) = args.no_args() {
1227            cx.expected_no_args(span);
1228            return None;
1229        }
1230        Some(AttributeKind::RustcSymbolName(cx.attr_span))
1231    }
1232}
1233
1234pub(crate) struct RustcDefPathParser;
1235
1236impl<S: Stage> SingleAttributeParser<S> for RustcDefPathParser {
1237    const PATH: &[Symbol] = &[sym::rustc_def_path];
1238    const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
1239        Allow(Target::Fn),
1240        Allow(Target::Method(MethodKind::TraitImpl)),
1241        Allow(Target::Method(MethodKind::Inherent)),
1242        Allow(Target::Method(MethodKind::Trait { body: true })),
1243        Allow(Target::ForeignFn),
1244        Allow(Target::ForeignStatic),
1245        Allow(Target::Impl { of_trait: false }),
1246    ]);
1247    const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
1248    const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepInnermost;
1249    const TEMPLATE: AttributeTemplate = ::rustc_feature::AttributeTemplate {
    word: true,
    list: None,
    one_of: &[],
    name_value_str: None,
    docs: None,
}template!(Word);
1250    fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
1251        if let Err(span) = args.no_args() {
1252            cx.expected_no_args(span);
1253            return None;
1254        }
1255        Some(AttributeKind::RustcDefPath(cx.attr_span))
1256    }
1257}
1258
1259pub(crate) struct RustcStrictCoherenceParser;
1260
1261impl<S: Stage> NoArgsAttributeParser<S> for RustcStrictCoherenceParser {
1262    const PATH: &[Symbol] = &[sym::rustc_strict_coherence];
1263    const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
1264    const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
1265        Allow(Target::Trait),
1266        Allow(Target::Struct),
1267        Allow(Target::Enum),
1268        Allow(Target::Union),
1269        Allow(Target::ForeignTy),
1270    ]);
1271    const CREATE: fn(Span) -> AttributeKind = AttributeKind::RustcStrictCoherence;
1272}
1273
1274pub(crate) struct RustcReservationImplParser;
1275
1276impl<S: Stage> SingleAttributeParser<S> for RustcReservationImplParser {
1277    const PATH: &[Symbol] = &[sym::rustc_reservation_impl];
1278    const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepOutermost;
1279    const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
1280    const ALLOWED_TARGETS: AllowedTargets =
1281        AllowedTargets::AllowList(&[Allow(Target::Impl { of_trait: true })]);
1282
1283    const TEMPLATE: AttributeTemplate = ::rustc_feature::AttributeTemplate {
    word: false,
    list: None,
    one_of: &[],
    name_value_str: Some(&["reservation message"]),
    docs: None,
}template!(NameValueStr: "reservation message");
1284
1285    fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
1286        let Some(nv) = args.name_value() else {
1287            cx.expected_name_value(args.span().unwrap_or(cx.attr_span), None);
1288            return None;
1289        };
1290
1291        let Some(value_str) = nv.value_as_str() else {
1292            cx.expected_string_literal(nv.value_span, Some(nv.value_as_lit()));
1293            return None;
1294        };
1295
1296        Some(AttributeKind::RustcReservationImpl(cx.attr_span, value_str))
1297    }
1298}
1299
1300pub(crate) struct PreludeImportParser;
1301
1302impl<S: Stage> NoArgsAttributeParser<S> for PreludeImportParser {
1303    const PATH: &[Symbol] = &[sym::prelude_import];
1304    const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
1305    const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Use)]);
1306    const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::PreludeImport;
1307}
1308
1309pub(crate) struct RustcDocPrimitiveParser;
1310
1311impl<S: Stage> SingleAttributeParser<S> for RustcDocPrimitiveParser {
1312    const PATH: &[Symbol] = &[sym::rustc_doc_primitive];
1313    const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
1314    const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepOutermost;
1315    const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Mod)]);
1316    const TEMPLATE: AttributeTemplate = ::rustc_feature::AttributeTemplate {
    word: false,
    list: None,
    one_of: &[],
    name_value_str: Some(&["primitive name"]),
    docs: None,
}template!(NameValueStr: "primitive name");
1317
1318    fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
1319        let Some(nv) = args.name_value() else {
1320            cx.expected_name_value(args.span().unwrap_or(cx.attr_span), None);
1321            return None;
1322        };
1323
1324        let Some(value_str) = nv.value_as_str() else {
1325            cx.expected_string_literal(nv.value_span, Some(nv.value_as_lit()));
1326            return None;
1327        };
1328
1329        Some(AttributeKind::RustcDocPrimitive(cx.attr_span, value_str))
1330    }
1331}
1332
1333pub(crate) struct RustcIntrinsicParser;
1334
1335impl<S: Stage> NoArgsAttributeParser<S> for RustcIntrinsicParser {
1336    const PATH: &[Symbol] = &[sym::rustc_intrinsic];
1337    const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
1338    const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Fn)]);
1339    const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcIntrinsic;
1340}
1341
1342pub(crate) struct RustcIntrinsicConstStableIndirectParser;
1343
1344impl<S: Stage> NoArgsAttributeParser<S> for RustcIntrinsicConstStableIndirectParser {
1345    const PATH: &'static [Symbol] = &[sym::rustc_intrinsic_const_stable_indirect];
1346    const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
1347    const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Fn)]);
1348    const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcIntrinsicConstStableIndirect;
1349}