1use std::path::PathBuf;
2
3use rustc_ast::{GenericParamKind, LitIntType, LitKind, MetaItemLit};
4use rustc_attr_ir::lang_items::LangItem;
5use rustc_attr_ir::target::AstTarget;
6use rustc_attr_ir::{
7 BorrowckGraphvizFormatKind, CguFields, CguKind, RustcAssertVarianceKind, RustcCleanAttribute,
8 RustcCleanQueries, RustcMirKind,
9};
10use rustc_data_structures::fx::FxHashMap;
11use rustc_feature::AttributeStability;
12use rustc_span::Symbol;
13
14use super::prelude::*;
15use super::util::parse_single_integer;
16use crate::diagnostics;
17use crate::diagnostics::{
18 AttributeRequiresOpt, CguFieldsMissing, RustcScalableVectorCountOutOfRange,
19 UnknownExternLangItem, UnknownLangItem,
20};
21
22pub(crate) struct RustcMainParser;
23
24impl NoArgsAttributeParser for RustcMainParser {
25 const PATH: &[Symbol] = &[sym::rustc_main];
26 const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowList(&[Allow(Target::Fn)]);
27 const STABILITY: AttributeStability = {
_ = rustc_feature::Features::rustc_attrs;
AttributeStability::Unstable {
gate_name: rustc_span::sym::rustc_attrs,
notes: &["the `rustc_main` attribute is used internally to specify test entry point function"],
}
}unstable!(
28 rustc_attrs,
29 "the `rustc_main` attribute is used internally to specify test entry point function"
30 );
31 const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcMain;
32}
33
34pub(crate) struct RustcMustImplementOneOfParser;
35
36impl SingleAttributeParser for RustcMustImplementOneOfParser {
37 const PATH: &[Symbol] = &[sym::rustc_must_implement_one_of];
38 const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowList(&[Allow(Target::Trait)]);
39 const STABILITY: AttributeStability = {
_ = rustc_feature::Features::rustc_attrs;
AttributeStability::Unstable {
gate_name: rustc_span::sym::rustc_attrs,
notes: &["the `rustc_must_implement_one_of` attribute is used to change minimal complete definition of a trait. Its syntax and semantics are highly experimental and will be subject to change before stabilization"],
}
}unstable!(
40 rustc_attrs,
41 "the `rustc_must_implement_one_of` attribute is used to change minimal complete definition of a trait. Its syntax and semantics are highly experimental and will be subject to change before stabilization"
42 );
43 const TEMPLATE: AttributeTemplate = crate::AttributeTemplate {
word: false,
list: Some(&["function1, function2, ..."]),
one_of: &[],
name_value_str: None,
docs: None,
}template!(List: &["function1, function2, ..."]);
44 fn convert(cx: &mut AcceptContext<'_, '_>, args: &ArgParser) -> Option<AttributeKind> {
45 let list = cx.expect_list(args, cx.attr_span)?;
46
47 let mut fn_names = ThinVec::new();
48
49 let inputs: Vec<_> = list.mixed().collect();
50
51 if inputs.len() < 2 {
52 cx.adcx().expected_list_with_num_args_or_more(2, list.span);
53 return None;
54 }
55
56 let mut errored = false;
57 for argument in inputs {
58 let Some(meta) = argument.meta_item_no_args() else {
59 cx.adcx().expected_identifier(argument.span());
60 return None;
61 };
62
63 let Some(ident) = meta.ident() else {
64 cx.dcx()
65 .emit_err(diagnostics::MustBeNameOfAssociatedFunction { span: meta.span() });
66 errored = true;
67 continue;
68 };
69
70 fn_names.push(ident);
71 }
72 if errored {
73 return None;
74 }
75
76 if cx.target == Target::Trait {
77 let mut seen: FxHashMap<Symbol, Span> = FxHashMap::default();
79 for ident in &fn_names {
80 if let Some(dup) = seen.insert(ident.name, ident.span) {
81 cx.emit_err(diagnostics::FunctionNamesDuplicated {
82 spans: ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[dup, ident.span]))vec![dup, ident.span],
83 });
84 }
85 }
86 }
87
88 Some(AttributeKind::RustcMustImplementOneOf { attr_span: cx.attr_span, fn_names })
89 }
90}
91
92pub(crate) struct RustcPanicsWhenZeroParser;
93
94impl NoArgsAttributeParser for RustcPanicsWhenZeroParser {
95 const PATH: &[Symbol] = &[sym::rustc_panics_when_zero];
96 const ALLOWED_TARGETS: AllowedTargets<'_> =
97 AllowedTargets::AllowList(&[Allow(Target::ConstParam)]);
98 const STABILITY: AttributeStability = {
_ = rustc_feature::Features::rustc_attrs;
AttributeStability::Unstable {
gate_name: rustc_span::sym::rustc_attrs,
notes: &[],
}
}unstable!(rustc_attrs);
99
100 const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcPanicsWhenZero;
101}
102
103pub(crate) struct RustcNoImplicitAutorefsParser;
104
105impl NoArgsAttributeParser for RustcNoImplicitAutorefsParser {
106 const PATH: &[Symbol] = &[sym::rustc_no_implicit_autorefs];
107 const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowList(&[
108 Allow(Target::Fn),
109 Allow(Target::Method(MethodKind::Inherent)),
110 Allow(Target::Method(MethodKind::Trait { body: false })),
111 Allow(Target::Method(MethodKind::Trait { body: true })),
112 Allow(Target::Method(MethodKind::TraitImpl)),
113 ]);
114 const STABILITY: AttributeStability = {
_ = rustc_feature::Features::rustc_attrs;
AttributeStability::Unstable {
gate_name: rustc_span::sym::rustc_attrs,
notes: &[],
}
}unstable!(rustc_attrs);
115
116 const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcNoImplicitAutorefs;
117}
118
119pub(crate) struct RustcLegacyConstGenericsParser;
120
121impl SingleAttributeParser for RustcLegacyConstGenericsParser {
122 const PATH: &[Symbol] = &[sym::rustc_legacy_const_generics];
123 const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowList(&[Allow(Target::Fn)]);
124 const TEMPLATE: AttributeTemplate = crate::AttributeTemplate {
word: false,
list: Some(&["N"]),
one_of: &[],
name_value_str: None,
docs: None,
}template!(List: &["N"]);
125 const STABILITY: AttributeStability = {
_ = rustc_feature::Features::rustc_attrs;
AttributeStability::Unstable {
gate_name: rustc_span::sym::rustc_attrs,
notes: &[],
}
}unstable!(rustc_attrs);
126
127 fn convert(cx: &mut AcceptContext<'_, '_>, args: &ArgParser) -> Option<AttributeKind> {
128 let meta_items = cx.expect_list(args, cx.attr_span)?;
129
130 let mut parsed_indexes = ThinVec::new();
131 let mut errored = false;
132
133 for possible_index in meta_items.mixed() {
134 if let MetaItemOrLitParser::Lit(MetaItemLit {
135 kind: LitKind::Int(index, LitIntType::Unsuffixed),
136 ..
137 }) = possible_index
138 {
139 parsed_indexes.push((index.0 as usize, possible_index.span()));
140 } else {
141 cx.adcx().expected_integer_literal(possible_index.span());
142 errored = true;
143 }
144 }
145 if errored {
146 return None;
147 } else if parsed_indexes.is_empty() {
148 cx.adcx().expected_at_least_one_argument(args.span()?);
149 return None;
150 }
151
152 Some(AttributeKind::RustcLegacyConstGenerics {
153 fn_indexes: parsed_indexes,
154 attr_span: cx.attr_span,
155 })
156 }
157
158 fn finalize_check(cx: &mut FinalizeCheckContext<'_, '_>, attr_span: Span) {
159 let AstTarget::Fn(function) = cx.ast_target else {
160 return;
161 };
162 let index_list = {
'done:
{
for i in cx.parsed_attrs {
#[allow(unused_imports)]
use ::rustc_attr_ir::AttributeKind::*;
let i: &::rustc_attr_ir::Attribute = i;
match i {
::rustc_attr_ir::Attribute::Parsed(RustcLegacyConstGenerics {
fn_indexes, .. }) => {
break 'done Some(fn_indexes);
}
::rustc_attr_ir::Attribute::Tool { .. } |
::rustc_attr_ir::Attribute::DeriveHelper(..) =>
{}
#[deny(unreachable_patterns)]
_ => {}
}
}
None
}
}rustc_attr_ir::find_attr!(cx.parsed_attrs, RustcLegacyConstGenerics { fn_indexes, .. } => fn_indexes)
163 .expect("missing parsed RustcLegacyConstGenerics attribute in finalize_check");
164 let generics = &function.generics;
165
166 for param in &generics.params {
167 if !#[allow(non_exhaustive_omitted_patterns)] match param.kind {
GenericParamKind::Const { .. } => true,
_ => false,
}matches!(param.kind, GenericParamKind::Const { .. }) {
168 cx.emit_err(diagnostics::RustcLegacyConstGenericsOnly {
169 attr_span,
170 param_span: param.span(),
171 });
172 return;
173 }
174 }
175
176 if index_list.len() != generics.params.len() {
177 cx.emit_err(diagnostics::RustcLegacyConstGenericsIndex {
178 attr_span,
179 generics_span: generics.span,
180 });
181 return;
182 }
183
184 let arg_count = function.sig.decl.inputs.len() + generics.params.len();
185 for (index, span) in index_list {
186 if *index >= arg_count {
187 cx.emit_err(diagnostics::RustcLegacyConstGenericsIndexExceed {
188 span: *span,
189 arg_count,
190 });
191 }
192 }
193 }
194}
195
196pub(crate) struct RustcInheritOverflowChecksParser;
197
198impl NoArgsAttributeParser for RustcInheritOverflowChecksParser {
199 const PATH: &[Symbol] = &[sym::rustc_inherit_overflow_checks];
200 const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowList(&[
201 Allow(Target::Fn),
202 Allow(Target::Method(MethodKind::Inherent)),
203 Allow(Target::Method(MethodKind::TraitImpl)),
204 Allow(Target::Closure),
205 ]);
206 const STABILITY: AttributeStability = {
_ = rustc_feature::Features::rustc_attrs;
AttributeStability::Unstable {
gate_name: rustc_span::sym::rustc_attrs,
notes: &[],
}
}unstable!(rustc_attrs);
207 const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcInheritOverflowChecks;
208}
209
210pub(crate) struct RustcLintOptDenyFieldAccessParser;
211
212impl SingleAttributeParser for RustcLintOptDenyFieldAccessParser {
213 const PATH: &[Symbol] = &[sym::rustc_lint_opt_deny_field_access];
214 const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowList(&[Allow(Target::Field)]);
215 const TEMPLATE: AttributeTemplate = crate::AttributeTemplate {
word: true,
list: None,
one_of: &[],
name_value_str: None,
docs: None,
}template!(Word);
216 const STABILITY: AttributeStability = {
_ = rustc_feature::Features::rustc_attrs;
AttributeStability::Unstable {
gate_name: rustc_span::sym::rustc_attrs,
notes: &[],
}
}unstable!(rustc_attrs);
217 fn convert(cx: &mut AcceptContext<'_, '_>, args: &ArgParser) -> Option<AttributeKind> {
218 let arg = cx.expect_single_element_list(args, cx.attr_span)?;
219 let lint_message = cx.expect_string_literal(arg)?;
220
221 Some(AttributeKind::RustcLintOptDenyFieldAccess { lint_message })
222 }
223}
224
225pub(crate) struct RustcLintOptTyParser;
226
227impl NoArgsAttributeParser for RustcLintOptTyParser {
228 const PATH: &[Symbol] = &[sym::rustc_lint_opt_ty];
229 const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowList(&[Allow(Target::Struct)]);
230 const STABILITY: AttributeStability = {
_ = rustc_feature::Features::rustc_attrs;
AttributeStability::Unstable {
gate_name: rustc_span::sym::rustc_attrs,
notes: &[],
}
}unstable!(rustc_attrs);
231 const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcLintOptTy;
232}
233
234fn parse_cgu_fields(
235 cx: &mut AcceptContext<'_, '_>,
236 args: &ArgParser,
237 accepts_kind: bool,
238) -> Option<(Symbol, Symbol, Option<CguKind>)> {
239 let args = cx.expect_list(args, cx.attr_span)?;
240
241 let mut cfg = None::<(Symbol, Span)>;
242 let mut module = None::<(Symbol, Span)>;
243 let mut kind = None::<(Symbol, Span)>;
244
245 for arg in args.mixed() {
246 let Some((ident, arg)) = cx.expect_name_value(arg, arg.span(), None) else {
247 continue;
248 };
249
250 let res = match ident.name {
251 sym::cfg => &mut cfg,
252 sym::module => &mut module,
253 sym::kind if accepts_kind => &mut kind,
254 _ => {
255 cx.adcx().expected_specific_argument(
256 ident.span,
257 if accepts_kind {
258 &[sym::cfg, sym::module, sym::kind]
259 } else {
260 &[sym::cfg, sym::module]
261 },
262 );
263 continue;
264 }
265 };
266
267 let str = cx.expect_string_literal(arg)?;
268
269 if res.is_some() {
270 cx.adcx().duplicate_key(ident.span.to(arg.args_span()), ident.name);
271 continue;
272 }
273
274 *res = Some((str, arg.value_span));
275 }
276
277 let Some((cfg, _)) = cfg else {
278 cx.emit_err(CguFieldsMissing { span: args.span, name: &cx.attr_path, field: sym::cfg });
279 return None;
280 };
281 let Some((module, _)) = module else {
282 cx.emit_err(CguFieldsMissing { span: args.span, name: &cx.attr_path, field: sym::module });
283 return None;
284 };
285 let kind = if let Some((kind, span)) = kind {
286 Some(match kind {
287 sym::no => CguKind::No,
288 sym::pre_dash_lto => CguKind::PreDashLto,
289 sym::post_dash_lto => CguKind::PostDashLto,
290 sym::any => CguKind::Any,
291 _ => {
292 cx.adcx().expected_specific_argument_strings(
293 span,
294 &[sym::no, sym::pre_dash_lto, sym::post_dash_lto, sym::any],
295 );
296 return None;
297 }
298 })
299 } else {
300 if accepts_kind {
302 cx.emit_err(CguFieldsMissing {
303 span: args.span,
304 name: &cx.attr_path,
305 field: sym::kind,
306 });
307 return None;
308 };
309
310 None
311 };
312
313 Some((cfg, module, kind))
314}
315
316#[derive(#[automatically_derived]impl ::core::default::Default for
RustcCguTestAttributeParser {
#[inline]
fn default() -> Self {
RustcCguTestAttributeParser {
items: ::core::default::Default::default(),
}
}
}Default)]
317pub(crate) struct RustcCguTestAttributeParser {
318 items: ThinVec<(Span, CguFields)>,
319}
320
321impl AttributeParser for RustcCguTestAttributeParser {
322 const ATTRIBUTES: AcceptMapping<Self> = &[
323 (
324 &[sym::rustc_partition_reused],
325 crate::AttributeTemplate {
word: false,
list: Some(&[r#"cfg = "...", module = "...""#]),
one_of: &[],
name_value_str: None,
docs: None,
}template!(List: &[r#"cfg = "...", module = "...""#]),
326 {
_ = rustc_feature::Features::rustc_attrs;
AttributeStability::Unstable {
gate_name: rustc_span::sym::rustc_attrs,
notes: &[],
}
}unstable!(rustc_attrs),
327 |this, cx, args| {
328 this.items.extend(parse_cgu_fields(cx, args, false).map(|(cfg, module, _)| {
329 (cx.attr_span, CguFields::PartitionReused { cfg, module })
330 }));
331 },
332 ),
333 (
334 &[sym::rustc_partition_codegened],
335 crate::AttributeTemplate {
word: false,
list: Some(&[r#"cfg = "...", module = "...""#]),
one_of: &[],
name_value_str: None,
docs: None,
}template!(List: &[r#"cfg = "...", module = "...""#]),
336 {
_ = rustc_feature::Features::rustc_attrs;
AttributeStability::Unstable {
gate_name: rustc_span::sym::rustc_attrs,
notes: &[],
}
}unstable!(rustc_attrs),
337 |this, cx, args| {
338 this.items.extend(parse_cgu_fields(cx, args, false).map(|(cfg, module, _)| {
339 (cx.attr_span, CguFields::PartitionCodegened { cfg, module })
340 }));
341 },
342 ),
343 (
344 &[sym::rustc_expected_cgu_reuse],
345 crate::AttributeTemplate {
word: false,
list: Some(&[r#"cfg = "...", module = "...", kind = "...""#]),
one_of: &[],
name_value_str: None,
docs: None,
}template!(List: &[r#"cfg = "...", module = "...", kind = "...""#]),
346 {
_ = rustc_feature::Features::rustc_attrs;
AttributeStability::Unstable {
gate_name: rustc_span::sym::rustc_attrs,
notes: &[],
}
}unstable!(rustc_attrs),
347 |this, cx, args| {
348 this.items.extend(parse_cgu_fields(cx, args, true).map(|(cfg, module, kind)| {
349 (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<'_, '_>) -> Option<AttributeKind> {
360 Some(AttributeKind::RustcCguTestAttr(self.items))
361 }
362}
363
364pub(crate) struct RustcDeprecatedSafe2024Parser;
365
366impl SingleAttributeParser for RustcDeprecatedSafe2024Parser {
367 const PATH: &[Symbol] = &[sym::rustc_deprecated_safe_2024];
368 const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowList(&[
369 Allow(Target::Fn),
370 Allow(Target::Method(MethodKind::Inherent)),
371 Allow(Target::Method(MethodKind::Trait { body: false })),
372 Allow(Target::Method(MethodKind::Trait { body: true })),
373 Allow(Target::Method(MethodKind::TraitImpl)),
374 ]);
375 const TEMPLATE: AttributeTemplate = crate::AttributeTemplate {
word: false,
list: Some(&[r#"audit_that = "...""#]),
one_of: &[],
name_value_str: None,
docs: None,
}template!(List: &[r#"audit_that = "...""#]);
376 const STABILITY: AttributeStability = {
_ = rustc_feature::Features::rustc_attrs;
AttributeStability::Unstable {
gate_name: rustc_span::sym::rustc_attrs,
notes: &[],
}
}unstable!(rustc_attrs);
377
378 fn convert(cx: &mut AcceptContext<'_, '_>, args: &ArgParser) -> Option<AttributeKind> {
379 let single = cx.expect_single_element_list(args, cx.attr_span)?;
380
381 let (path, arg) = cx.expect_name_value(single, cx.attr_span, None)?;
382
383 if path.name != sym::audit_that {
384 cx.adcx().expected_specific_argument(path.span, &[sym::audit_that]);
385 return None;
386 };
387
388 let suggestion = cx.expect_string_literal(arg)?;
389
390 Some(AttributeKind::RustcDeprecatedSafe2024 { suggestion })
391 }
392}
393
394pub(crate) struct RustcConversionSuggestionParser;
395
396impl NoArgsAttributeParser for RustcConversionSuggestionParser {
397 const PATH: &[Symbol] = &[sym::rustc_conversion_suggestion];
398 const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowList(&[
399 Allow(Target::Fn),
400 Allow(Target::Method(MethodKind::Inherent)),
401 Allow(Target::Method(MethodKind::Trait { body: false })),
402 Allow(Target::Method(MethodKind::Trait { body: true })),
403 Allow(Target::Method(MethodKind::TraitImpl)),
404 ]);
405 const STABILITY: AttributeStability = {
_ = rustc_feature::Features::rustc_attrs;
AttributeStability::Unstable {
gate_name: rustc_span::sym::rustc_attrs,
notes: &[],
}
}unstable!(rustc_attrs);
406 const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcConversionSuggestion;
407}
408
409pub(crate) struct RustcCaptureAnalysisParser;
410
411impl NoArgsAttributeParser for RustcCaptureAnalysisParser {
412 const PATH: &[Symbol] = &[sym::rustc_capture_analysis];
413 const ALLOWED_TARGETS: AllowedTargets<'_> =
414 AllowedTargets::AllowList(&[Allow(Target::Closure)]);
415 const STABILITY: AttributeStability = {
_ = rustc_feature::Features::rustc_attrs;
AttributeStability::Unstable {
gate_name: rustc_span::sym::rustc_attrs,
notes: &[],
}
}unstable!(rustc_attrs);
416 const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcCaptureAnalysis;
417}
418
419pub(crate) struct RustcTrivialFieldReadsParser;
420
421impl NoArgsAttributeParser for RustcTrivialFieldReadsParser {
422 const PATH: &[Symbol] = &[sym::rustc_trivial_field_reads];
423 const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowList(&[
424 Allow(Target::Method(MethodKind::TraitImpl)),
425 Allow(Target::Method(MethodKind::Inherent)),
426 Allow(Target::Fn),
427 ]);
428 const STABILITY: AttributeStability = {
_ = rustc_feature::Features::rustc_attrs;
AttributeStability::Unstable {
gate_name: rustc_span::sym::rustc_attrs,
notes: &[],
}
}unstable!(rustc_attrs);
429 const CREATE: fn(Span) -> AttributeKind = AttributeKind::RustcTrivialFieldReads;
430}
431
432pub(crate) struct RustcNoMirInlineParser;
433
434impl NoArgsAttributeParser for RustcNoMirInlineParser {
435 const PATH: &[Symbol] = &[sym::rustc_no_mir_inline];
436 const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowList(&[
437 Allow(Target::Fn),
438 Allow(Target::Method(MethodKind::Inherent)),
439 Allow(Target::Method(MethodKind::Trait { body: false })),
440 Allow(Target::Method(MethodKind::Trait { body: true })),
441 Allow(Target::Method(MethodKind::TraitImpl)),
442 ]);
443 const STABILITY: AttributeStability = {
_ = rustc_feature::Features::rustc_attrs;
AttributeStability::Unstable {
gate_name: rustc_span::sym::rustc_attrs,
notes: &[],
}
}unstable!(rustc_attrs);
444 const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcNoMirInline;
445}
446
447pub(crate) struct RustcNoWritableParser;
448
449impl NoArgsAttributeParser for RustcNoWritableParser {
450 const PATH: &[Symbol] = &[sym::rustc_no_writable];
451 const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowList(&[
452 Allow(Target::Fn),
453 Allow(Target::Closure),
454 Allow(Target::Method(MethodKind::Inherent)),
455 Allow(Target::Method(MethodKind::TraitImpl)),
456 Allow(Target::Method(MethodKind::Trait { body: true })),
457 ]);
458 const STABILITY: AttributeStability = {
_ = rustc_feature::Features::rustc_attrs;
AttributeStability::Unstable {
gate_name: rustc_span::sym::rustc_attrs,
notes: &[],
}
}unstable!(rustc_attrs);
459 const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcNoWritable;
460}
461
462pub(crate) struct RustcLintQueryInstabilityParser;
463
464impl NoArgsAttributeParser for RustcLintQueryInstabilityParser {
465 const PATH: &[Symbol] = &[sym::rustc_lint_query_instability];
466 const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowList(&[
467 Allow(Target::Fn),
468 Allow(Target::Method(MethodKind::Inherent)),
469 Allow(Target::Method(MethodKind::Trait { body: false })),
470 Allow(Target::Method(MethodKind::Trait { body: true })),
471 Allow(Target::Method(MethodKind::TraitImpl)),
472 ]);
473 const STABILITY: AttributeStability = {
_ = rustc_feature::Features::rustc_attrs;
AttributeStability::Unstable {
gate_name: rustc_span::sym::rustc_attrs,
notes: &[],
}
}unstable!(rustc_attrs);
474 const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcLintQueryInstability;
475}
476
477pub(crate) struct RustcRegionsParser;
478
479impl NoArgsAttributeParser for RustcRegionsParser {
480 const PATH: &[Symbol] = &[sym::rustc_regions];
481 const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowList(&[
482 Allow(Target::Fn),
483 Allow(Target::Method(MethodKind::Inherent)),
484 Allow(Target::Method(MethodKind::Trait { body: false })),
485 Allow(Target::Method(MethodKind::Trait { body: true })),
486 Allow(Target::Method(MethodKind::TraitImpl)),
487 ]);
488 const STABILITY: AttributeStability = {
_ = rustc_feature::Features::rustc_attrs;
AttributeStability::Unstable {
gate_name: rustc_span::sym::rustc_attrs,
notes: &[],
}
}unstable!(rustc_attrs);
489 const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcRegions;
490}
491
492pub(crate) struct RustcLintUntrackedQueryInformationParser;
493
494impl NoArgsAttributeParser for RustcLintUntrackedQueryInformationParser {
495 const PATH: &[Symbol] = &[sym::rustc_lint_untracked_query_information];
496 const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowList(&[
497 Allow(Target::Fn),
498 Allow(Target::Method(MethodKind::Inherent)),
499 Allow(Target::Method(MethodKind::Trait { body: false })),
500 Allow(Target::Method(MethodKind::Trait { body: true })),
501 Allow(Target::Method(MethodKind::TraitImpl)),
502 ]);
503 const STABILITY: AttributeStability = {
_ = rustc_feature::Features::rustc_attrs;
AttributeStability::Unstable {
gate_name: rustc_span::sym::rustc_attrs,
notes: &[],
}
}unstable!(rustc_attrs);
504 const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcLintUntrackedQueryInformation;
505}
506
507pub(crate) struct RustcSimdMonomorphizeLaneLimitParser;
508
509impl SingleAttributeParser for RustcSimdMonomorphizeLaneLimitParser {
510 const PATH: &[Symbol] = &[sym::rustc_simd_monomorphize_lane_limit];
511 const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowList(&[Allow(Target::Struct)]);
512 const TEMPLATE: AttributeTemplate = crate::AttributeTemplate {
word: false,
list: None,
one_of: &[],
name_value_str: Some(&["N"]),
docs: None,
}template!(NameValueStr: "N");
513 const STABILITY: AttributeStability = {
_ = rustc_feature::Features::rustc_attrs;
AttributeStability::Unstable {
gate_name: rustc_span::sym::rustc_attrs,
notes: &[],
}
}unstable!(rustc_attrs);
514
515 fn convert(cx: &mut AcceptContext<'_, '_>, args: &ArgParser) -> Option<AttributeKind> {
516 let nv = cx.expect_name_value(args, cx.attr_span, None)?;
517 Some(AttributeKind::RustcSimdMonomorphizeLaneLimit(cx.parse_limit_int(nv)?))
518 }
519}
520
521pub(crate) struct RustcScalableVectorParser;
522
523impl SingleAttributeParser for RustcScalableVectorParser {
524 const PATH: &[Symbol] = &[sym::rustc_scalable_vector];
525 const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowList(&[Allow(Target::Struct)]);
526 const TEMPLATE: AttributeTemplate = crate::AttributeTemplate {
word: true,
list: Some(&["count"]),
one_of: &[],
name_value_str: None,
docs: None,
}template!(Word, List: &["count"]);
527 const STABILITY: AttributeStability = {
_ = rustc_feature::Features::rustc_attrs;
AttributeStability::Unstable {
gate_name: rustc_span::sym::rustc_attrs,
notes: &[],
}
}unstable!(rustc_attrs);
528
529 fn convert(cx: &mut AcceptContext<'_, '_>, args: &ArgParser) -> Option<AttributeKind> {
530 if args.as_no_args().is_ok() {
531 return Some(AttributeKind::RustcScalableVector { element_count: None });
532 }
533
534 let n = parse_single_integer(cx, args)?;
535 let Ok(n) = n.try_into() else {
536 cx.emit_err(RustcScalableVectorCountOutOfRange { span: cx.attr_span, n });
537 return None;
538 };
539 Some(AttributeKind::RustcScalableVector { element_count: Some(n) })
540 }
541}
542
543pub(crate) struct LangParser;
544
545impl SingleAttributeParser for LangParser {
546 const PATH: &[Symbol] = &[sym::lang];
547 const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::ManuallyChecked;
548 const TEMPLATE: AttributeTemplate = crate::AttributeTemplate {
word: false,
list: None,
one_of: &[],
name_value_str: Some(&["name"]),
docs: None,
}template!(NameValueStr: "name");
549 const STABILITY: AttributeStability = {
_ = rustc_feature::Features::lang_items;
AttributeStability::Unstable {
gate_name: rustc_span::sym::lang_items,
notes: &[],
}
}unstable!(lang_items);
550
551 fn convert(cx: &mut AcceptContext<'_, '_>, args: &ArgParser) -> Option<AttributeKind> {
552 let nv = cx.expect_name_value(args, cx.attr_span, None)?;
553 let name = cx.expect_string_literal(nv)?;
554 let Some(lang_item) = LangItem::from_name(name) else {
555 cx.emit_err(UnknownLangItem { span: cx.attr_span, name });
556 return None;
557 };
558
559 if [Target::ForeignFn, Target::ForeignStatic, Target::ForeignMod].contains(&cx.target)
562 && !lang_item.is_weak()
563 {
564 cx.emit_err(UnknownExternLangItem { span: cx.attr_span, lang_item: lang_item.name() });
565 return None;
566 }
567
568 let allowed_targets: &[_] = &[Allow(lang_item.target())];
570 cx.check_target(&::alloc::__export::must_use({
::alloc::fmt::format(format_args!(" = \"{0}\"", name))
})format!(" = \"{name}\""), &AllowedTargets::AllowList(allowed_targets));
571
572 Some(AttributeKind::Lang(lang_item))
573 }
574}
575
576pub(crate) struct RustcHasIncoherentInherentImplsParser;
577
578impl NoArgsAttributeParser for RustcHasIncoherentInherentImplsParser {
579 const PATH: &[Symbol] = &[sym::rustc_has_incoherent_inherent_impls];
580 const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowList(&[
581 Allow(Target::Trait),
582 Allow(Target::Struct),
583 Allow(Target::Enum),
584 Allow(Target::Union),
585 Allow(Target::ForeignTy),
586 ]);
587 const STABILITY: AttributeStability = {
_ = rustc_feature::Features::rustc_attrs;
AttributeStability::Unstable {
gate_name: rustc_span::sym::rustc_attrs,
notes: &[],
}
}unstable!(rustc_attrs);
588 const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcHasIncoherentInherentImpls;
589}
590
591pub(crate) struct PanicHandlerParser;
592
593impl NoArgsAttributeParser for PanicHandlerParser {
594 const PATH: &[Symbol] = &[sym::panic_handler];
595 const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowList(&[Allow(Target::Fn)]);
596 const STABILITY: AttributeStability = AttributeStability::Stable;
597 const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::Lang(LangItem::PanicImpl);
598}
599
600pub(crate) struct RustcNounwindParser;
601
602impl NoArgsAttributeParser for RustcNounwindParser {
603 const PATH: &[Symbol] = &[sym::rustc_nounwind];
604 const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowList(&[
605 Allow(Target::Fn),
606 Allow(Target::ForeignFn),
607 Allow(Target::Method(MethodKind::Inherent)),
608 Allow(Target::Method(MethodKind::TraitImpl)),
609 Allow(Target::Method(MethodKind::Trait { body: true })),
610 ]);
611 const STABILITY: AttributeStability = {
_ = rustc_feature::Features::rustc_attrs;
AttributeStability::Unstable {
gate_name: rustc_span::sym::rustc_attrs,
notes: &[],
}
}unstable!(rustc_attrs);
612 const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcNounwind;
613}
614
615pub(crate) struct RustcOffloadKernelParser;
616
617impl NoArgsAttributeParser for RustcOffloadKernelParser {
618 const PATH: &[Symbol] = &[sym::rustc_offload_kernel];
619 const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowList(&[Allow(Target::Fn)]);
620 const STABILITY: AttributeStability = {
_ = rustc_feature::Features::rustc_attrs;
AttributeStability::Unstable {
gate_name: rustc_span::sym::rustc_attrs,
notes: &[],
}
}unstable!(rustc_attrs);
621 const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcOffloadKernel;
622}
623
624pub(crate) struct RustcMirParser;
625
626impl CombineAttributeParser for RustcMirParser {
627 const PATH: &[Symbol] = &[sym::rustc_mir];
628
629 type Item = RustcMirKind;
630
631 const CONVERT: ConvertFn<Self::Item> = |items, _| AttributeKind::RustcMir(items);
632 const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowList(&[
633 Allow(Target::Fn),
634 Allow(Target::Method(MethodKind::Inherent)),
635 Allow(Target::Method(MethodKind::TraitImpl)),
636 Allow(Target::Method(MethodKind::Trait { body: false })),
637 Allow(Target::Method(MethodKind::Trait { body: true })),
638 ]);
639 const TEMPLATE: AttributeTemplate = crate::AttributeTemplate {
word: false,
list: Some(&["arg1, arg2, ..."]),
one_of: &[],
name_value_str: None,
docs: None,
}template!(List: &["arg1, arg2, ..."]);
640 const STABILITY: AttributeStability = {
_ = rustc_feature::Features::rustc_attrs;
AttributeStability::Unstable {
gate_name: rustc_span::sym::rustc_attrs,
notes: &[],
}
}unstable!(rustc_attrs);
641
642 fn extend(
643 cx: &mut AcceptContext<'_, '_>,
644 args: &ArgParser,
645 ) -> impl IntoIterator<Item = Self::Item> {
646 let Some(list) = cx.expect_list(args, cx.attr_span) else {
647 return ThinVec::new();
648 };
649
650 list.mixed()
651 .filter_map(|arg| arg.meta_item())
652 .filter_map(|mi| {
653 if let Some(ident) = mi.ident() {
654 match ident.name {
655 sym::rustc_pretty_live_locals => {
656 cx.expect_no_args(mi.args())?;
657 Some(RustcMirKind::PrettyLiveLocals)
658 }
659 sym::rustc_pretty_precise_liveness => {
660 cx.expect_no_args(mi.args())?;
661 Some(RustcMirKind::PrettyPreciseLiveness)
662 }
663 sym::rustc_pretty_transitive_live_locals => {
664 cx.expect_no_args(mi.args())?;
665 Some(RustcMirKind::PrettyTransitiveLiveLocals)
666 }
667 sym::rustc_peek_maybe_init => Some(RustcMirKind::PeekMaybeInit),
668 sym::rustc_peek_maybe_uninit => Some(RustcMirKind::PeekMaybeUninit),
669 sym::rustc_peek_liveness => Some(RustcMirKind::PeekLiveness),
670 sym::stop_after_dataflow => Some(RustcMirKind::StopAfterDataflow),
671 sym::borrowck_graphviz_postflow => {
672 let nv = cx.expect_name_value(
673 mi.args(),
674 mi.span(),
675 Some(sym::borrowck_graphviz_postflow),
676 )?;
677 let path = cx.expect_string_literal(nv)?;
678 let path = PathBuf::from(path.to_string());
679 if path.file_name().is_some() {
680 Some(RustcMirKind::BorrowckGraphvizPostflow { path })
681 } else {
682 cx.adcx().expected_filename_literal(nv.value_span);
683 None
684 }
685 }
686 sym::borrowck_graphviz_format => {
687 let nv = cx.expect_name_value(
688 mi.args(),
689 mi.span(),
690 Some(sym::borrowck_graphviz_format),
691 )?;
692 let Some(format) = nv.value_as_ident() else {
693 cx.adcx().expected_identifier(nv.value_span);
694 return None;
695 };
696 match format.name {
697 sym::two_phase => Some(RustcMirKind::BorrowckGraphvizFormat {
698 format: BorrowckGraphvizFormatKind::TwoPhase,
699 }),
700 _ => {
701 cx.adcx()
702 .expected_specific_argument(format.span, &[sym::two_phase]);
703 None
704 }
705 }
706 }
707 _ => None,
708 }
709 } else {
710 None
711 }
712 })
713 .collect()
714 }
715}
716pub(crate) struct RustcNonConstTraitMethodParser;
717
718impl NoArgsAttributeParser for RustcNonConstTraitMethodParser {
719 const PATH: &[Symbol] = &[sym::rustc_non_const_trait_method];
720 const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowList(&[
721 Allow(Target::Method(MethodKind::Trait { body: true })),
722 Allow(Target::Method(MethodKind::Trait { body: false })),
723 ]);
724 const STABILITY: AttributeStability = {
_ = rustc_feature::Features::rustc_attrs;
AttributeStability::Unstable {
gate_name: rustc_span::sym::rustc_attrs,
notes: &["the `rustc_non_const_trait_method` attribute should only be used by the standard library to mark trait methods as non-const to allow large traits an easier transition to const"],
}
}unstable!(
725 rustc_attrs,
726 "the `rustc_non_const_trait_method` attribute should only be used by the standard library to mark trait methods as non-const to allow large traits an easier transition to const"
727 );
728 const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcNonConstTraitMethod;
729}
730
731pub(crate) struct RustcCleanParser;
732
733impl CombineAttributeParser for RustcCleanParser {
734 const PATH: &[Symbol] = &[sym::rustc_clean];
735
736 type Item = RustcCleanAttribute;
737
738 const CONVERT: ConvertFn<Self::Item> = |items, _| AttributeKind::RustcClean(items);
739 const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowList(&[
740 Allow(Target::AssocConst(AssocCtxt::Impl { of_trait: false })),
742 Allow(Target::AssocConst(AssocCtxt::Impl { of_trait: true })),
743 Allow(Target::AssocConst(AssocCtxt::Trait)),
744 Allow(Target::AssocTy(AssocCtxt::Impl { of_trait: false })),
745 Allow(Target::AssocTy(AssocCtxt::Impl { of_trait: true })),
746 Allow(Target::AssocTy(AssocCtxt::Trait)),
747 Allow(Target::Const),
748 Allow(Target::Enum),
749 Allow(Target::Expression),
750 Allow(Target::Field),
751 Allow(Target::Fn),
752 Allow(Target::ForeignMod),
753 Allow(Target::Impl { of_trait: false }),
754 Allow(Target::Impl { of_trait: true }),
755 Allow(Target::Method(MethodKind::Inherent)),
756 Allow(Target::Method(MethodKind::Trait { body: false })),
757 Allow(Target::Method(MethodKind::Trait { body: true })),
758 Allow(Target::Method(MethodKind::TraitImpl)),
759 Allow(Target::Mod),
760 Allow(Target::Static),
761 Allow(Target::Struct),
762 Allow(Target::Trait),
763 Allow(Target::TyAlias),
764 Allow(Target::Union),
765 ]);
767 const STABILITY: AttributeStability = {
_ = rustc_feature::Features::rustc_attrs;
AttributeStability::Unstable {
gate_name: rustc_span::sym::rustc_attrs,
notes: &[],
}
}unstable!(rustc_attrs);
768 const TEMPLATE: AttributeTemplate =
769 crate::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 = "...""#]);
770
771 fn extend(
772 cx: &mut AcceptContext<'_, '_>,
773 args: &ArgParser,
774 ) -> impl IntoIterator<Item = Self::Item> {
775 if !cx.cx.sess.opts.unstable_opts.query_dep_graph {
776 cx.emit_err(AttributeRequiresOpt { span: cx.attr_span, opt: "-Z query-dep-graph" });
777 }
778 let list = cx.expect_list(args, cx.attr_span)?;
779
780 let mut except = None;
781 let mut loaded_from_disk = None;
782 let mut cfg = None;
783
784 for item in list.mixed() {
785 let Some((ident, value)) = cx.expect_name_value(item, item.span(), None) else {
786 continue;
787 };
788 let value_span = value.value_span;
789 let Some(value) = cx.expect_string_literal(value) else {
790 continue;
791 };
792 match ident.name {
793 sym::cfg if cfg.is_some() => {
794 cx.adcx().duplicate_key(item.span(), sym::cfg);
795 }
796 sym::cfg => {
797 cfg = Some(value);
798 }
799 sym::except if except.is_some() => {
800 cx.adcx().duplicate_key(item.span(), sym::except);
801 }
802 sym::except => {
803 let entries =
804 value.as_str().split(',').map(|s| Symbol::intern(s.trim())).collect();
805 except = Some(RustcCleanQueries { entries, span: value_span });
806 }
807 sym::loaded_from_disk if loaded_from_disk.is_some() => {
808 cx.adcx().duplicate_key(item.span(), sym::loaded_from_disk);
809 }
810 sym::loaded_from_disk => {
811 let entries =
812 value.as_str().split(',').map(|s| Symbol::intern(s.trim())).collect();
813 loaded_from_disk = Some(RustcCleanQueries { entries, span: value_span });
814 }
815 _ => {
816 cx.adcx().expected_specific_argument(
817 ident.span,
818 &[sym::cfg, sym::except, sym::loaded_from_disk],
819 );
820 }
821 }
822 }
823 let Some(cfg) = cfg else {
824 cx.adcx().expected_specific_argument(list.span, &[sym::cfg]);
825 return None;
826 };
827
828 Some(RustcCleanAttribute { span: cx.attr_span, cfg, except, loaded_from_disk })
829 }
830}
831
832pub(crate) struct RustcIfThisChangedParser;
833
834impl SingleAttributeParser for RustcIfThisChangedParser {
835 const PATH: &[Symbol] = &[sym::rustc_if_this_changed];
836 const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowList(&[
837 Allow(Target::AssocConst(AssocCtxt::Impl { of_trait: false })),
839 Allow(Target::AssocConst(AssocCtxt::Impl { of_trait: true })),
840 Allow(Target::AssocConst(AssocCtxt::Trait)),
841 Allow(Target::AssocTy(AssocCtxt::Impl { of_trait: false })),
842 Allow(Target::AssocTy(AssocCtxt::Impl { of_trait: true })),
843 Allow(Target::AssocTy(AssocCtxt::Trait)),
844 Allow(Target::Const),
845 Allow(Target::Enum),
846 Allow(Target::Expression),
847 Allow(Target::Field),
848 Allow(Target::Fn),
849 Allow(Target::ForeignMod),
850 Allow(Target::Impl { of_trait: false }),
851 Allow(Target::Impl { of_trait: true }),
852 Allow(Target::Method(MethodKind::Inherent)),
853 Allow(Target::Method(MethodKind::Trait { body: false })),
854 Allow(Target::Method(MethodKind::Trait { body: true })),
855 Allow(Target::Method(MethodKind::TraitImpl)),
856 Allow(Target::Mod),
857 Allow(Target::Static),
858 Allow(Target::Struct),
859 Allow(Target::Trait),
860 Allow(Target::TyAlias),
861 Allow(Target::Union),
862 ]);
864 const TEMPLATE: AttributeTemplate = crate::AttributeTemplate {
word: true,
list: Some(&["DepNode"]),
one_of: &[],
name_value_str: None,
docs: None,
}template!(Word, List: &["DepNode"]);
865 const STABILITY: AttributeStability = {
_ = rustc_feature::Features::rustc_attrs;
AttributeStability::Unstable {
gate_name: rustc_span::sym::rustc_attrs,
notes: &[],
}
}unstable!(rustc_attrs);
866
867 fn convert(cx: &mut AcceptContext<'_, '_>, args: &ArgParser) -> Option<AttributeKind> {
868 if !cx.cx.sess.opts.unstable_opts.query_dep_graph {
869 cx.emit_err(AttributeRequiresOpt { span: cx.attr_span, opt: "-Z query-dep-graph" });
870 }
871 match args {
872 ArgParser::NoArgs => Some(AttributeKind::RustcIfThisChanged(cx.attr_span, None)),
873 ArgParser::List(list) => {
874 let item = cx.expect_single(list)?;
875 let Some(ident) = item.meta_item_no_args().and_then(|item| item.ident()) else {
876 cx.adcx().expected_identifier(item.span());
877 return None;
878 };
879 Some(AttributeKind::RustcIfThisChanged(cx.attr_span, Some(ident.name)))
880 }
881 ArgParser::NameValue(_) => {
882 let inner_span = cx.inner_span;
883 cx.adcx().expected_list_or_no_args(inner_span);
884 None
885 }
886 }
887 }
888}
889
890pub(crate) struct RustcThenThisWouldNeedParser;
891
892impl CombineAttributeParser for RustcThenThisWouldNeedParser {
893 const PATH: &[Symbol] = &[sym::rustc_then_this_would_need];
894 type Item = Ident;
895
896 const CONVERT: ConvertFn<Self::Item> =
897 |items, _span| AttributeKind::RustcThenThisWouldNeed(items);
898 const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowList(&[
899 Allow(Target::AssocConst(AssocCtxt::Impl { of_trait: false })),
901 Allow(Target::AssocConst(AssocCtxt::Impl { of_trait: true })),
902 Allow(Target::AssocConst(AssocCtxt::Trait)),
903 Allow(Target::AssocTy(AssocCtxt::Impl { of_trait: false })),
904 Allow(Target::AssocTy(AssocCtxt::Impl { of_trait: true })),
905 Allow(Target::AssocTy(AssocCtxt::Trait)),
906 Allow(Target::Const),
907 Allow(Target::Enum),
908 Allow(Target::Expression),
909 Allow(Target::Field),
910 Allow(Target::Fn),
911 Allow(Target::ForeignMod),
912 Allow(Target::Impl { of_trait: false }),
913 Allow(Target::Impl { of_trait: true }),
914 Allow(Target::Method(MethodKind::Inherent)),
915 Allow(Target::Method(MethodKind::Trait { body: false })),
916 Allow(Target::Method(MethodKind::Trait { body: true })),
917 Allow(Target::Method(MethodKind::TraitImpl)),
918 Allow(Target::Mod),
919 Allow(Target::Static),
920 Allow(Target::Struct),
921 Allow(Target::Trait),
922 Allow(Target::TyAlias),
923 Allow(Target::Union),
924 ]);
926 const TEMPLATE: AttributeTemplate = crate::AttributeTemplate {
word: false,
list: Some(&["DepNode"]),
one_of: &[],
name_value_str: None,
docs: None,
}template!(List: &["DepNode"]);
927 const STABILITY: AttributeStability = {
_ = rustc_feature::Features::rustc_attrs;
AttributeStability::Unstable {
gate_name: rustc_span::sym::rustc_attrs,
notes: &[],
}
}unstable!(rustc_attrs);
928
929 fn extend(
930 cx: &mut AcceptContext<'_, '_>,
931 args: &ArgParser,
932 ) -> impl IntoIterator<Item = Self::Item> {
933 if !cx.cx.sess.opts.unstable_opts.query_dep_graph {
934 cx.emit_err(AttributeRequiresOpt { span: cx.attr_span, opt: "-Z query-dep-graph" });
935 }
936 let item = cx.expect_single_element_list(args, cx.attr_span)?;
937 let Some(ident) = item.meta_item_no_args().and_then(|item| item.ident()) else {
938 cx.adcx().expected_identifier(item.span());
939 return None;
940 };
941 Some(ident)
942 }
943}
944
945pub(crate) struct RustcInsignificantDtorParser;
946
947impl NoArgsAttributeParser for RustcInsignificantDtorParser {
948 const PATH: &[Symbol] = &[sym::rustc_insignificant_dtor];
949 const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowList(&[
950 Allow(Target::Enum),
951 Allow(Target::Struct),
952 Allow(Target::ForeignTy),
953 ]);
954 const STABILITY: AttributeStability = {
_ = rustc_feature::Features::rustc_attrs;
AttributeStability::Unstable {
gate_name: rustc_span::sym::rustc_attrs,
notes: &[],
}
}unstable!(rustc_attrs);
955 const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcInsignificantDtor;
956}
957
958pub(crate) struct RustcEffectiveVisibilityParser;
959
960impl NoArgsAttributeParser for RustcEffectiveVisibilityParser {
961 const PATH: &[Symbol] = &[sym::rustc_effective_visibility];
962 const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowList(&[
963 Allow(Target::Use),
964 Allow(Target::Static),
965 Allow(Target::Const),
966 Allow(Target::Fn),
967 Allow(Target::Closure),
968 Allow(Target::Mod),
969 Allow(Target::ForeignMod),
970 Allow(Target::TyAlias),
971 Allow(Target::Enum),
972 Allow(Target::Variant),
973 Allow(Target::Struct),
974 Allow(Target::Field),
975 Allow(Target::Union),
976 Allow(Target::Trait),
977 Allow(Target::TraitAlias),
978 Allow(Target::Impl { of_trait: false }),
979 Allow(Target::Impl { of_trait: true }),
980 Allow(Target::AssocConst(AssocCtxt::Impl { of_trait: false })),
981 Allow(Target::AssocConst(AssocCtxt::Trait)),
982 Allow(Target::AssocConst(AssocCtxt::Impl { of_trait: true })),
983 Allow(Target::Method(MethodKind::Inherent)),
984 Allow(Target::Method(MethodKind::Trait { body: false })),
985 Allow(Target::Method(MethodKind::Trait { body: true })),
986 Allow(Target::Method(MethodKind::TraitImpl)),
987 Allow(Target::AssocTy(AssocCtxt::Impl { of_trait: false })),
988 Allow(Target::AssocTy(AssocCtxt::Trait)),
989 Allow(Target::AssocTy(AssocCtxt::Impl { of_trait: true })),
990 Allow(Target::ForeignFn),
991 Allow(Target::ForeignStatic),
992 Allow(Target::ForeignTy),
993 Allow(Target::MacroDef),
994 Allow(Target::PatField),
995 Allow(Target::Crate),
996 ]);
997 const STABILITY: AttributeStability = {
_ = rustc_feature::Features::rustc_attrs;
AttributeStability::Unstable {
gate_name: rustc_span::sym::rustc_attrs,
notes: &[],
}
}unstable!(rustc_attrs);
998 const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcEffectiveVisibility;
999}
1000
1001pub(crate) struct RustcDiagnosticItemParser;
1002
1003impl SingleAttributeParser for RustcDiagnosticItemParser {
1004 const PATH: &[Symbol] = &[sym::rustc_diagnostic_item];
1005 const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowList(&[
1006 Allow(Target::Trait),
1007 Allow(Target::Struct),
1008 Allow(Target::Enum),
1009 Allow(Target::MacroDef),
1010 Allow(Target::TyAlias),
1011 Allow(Target::AssocConst(AssocCtxt::Impl { of_trait: false })),
1012 Allow(Target::AssocConst(AssocCtxt::Trait)),
1013 Allow(Target::AssocConst(AssocCtxt::Impl { of_trait: true })),
1014 Allow(Target::AssocTy(AssocCtxt::Impl { of_trait: false })),
1015 Allow(Target::AssocTy(AssocCtxt::Trait)),
1016 Allow(Target::AssocTy(AssocCtxt::Impl { of_trait: true })),
1017 Allow(Target::Fn),
1018 Allow(Target::Const),
1019 Allow(Target::Mod),
1020 Allow(Target::Impl { of_trait: false }),
1021 Allow(Target::Method(MethodKind::Inherent)),
1022 Allow(Target::Method(MethodKind::Trait { body: false })),
1023 Allow(Target::Method(MethodKind::Trait { body: true })),
1024 Allow(Target::Method(MethodKind::TraitImpl)),
1025 Allow(Target::Crate),
1026 ]);
1027 const TEMPLATE: AttributeTemplate = crate::AttributeTemplate {
word: false,
list: None,
one_of: &[],
name_value_str: Some(&["name"]),
docs: None,
}template!(NameValueStr: "name");
1028 const STABILITY: AttributeStability = {
_ = rustc_feature::Features::rustc_attrs;
AttributeStability::Unstable {
gate_name: rustc_span::sym::rustc_attrs,
notes: &["the `rustc_diagnostic_item` attribute allows the compiler to reference types from the standard library for diagnostic purposes"],
}
}unstable!(
1029 rustc_attrs,
1030 "the `rustc_diagnostic_item` attribute allows the compiler to reference types from the standard library for diagnostic purposes"
1031 );
1032
1033 fn convert(cx: &mut AcceptContext<'_, '_>, args: &ArgParser) -> Option<AttributeKind> {
1034 let nv = cx.expect_name_value(args, cx.attr_span, None)?;
1035 let value = cx.expect_string_literal(nv)?;
1036 Some(AttributeKind::RustcDiagnosticItem(value))
1037 }
1038}
1039
1040pub(crate) struct RustcDoNotConstCheckParser;
1041
1042impl NoArgsAttributeParser for RustcDoNotConstCheckParser {
1043 const PATH: &[Symbol] = &[sym::rustc_do_not_const_check];
1044 const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowList(&[
1045 Allow(Target::Fn),
1046 Allow(Target::Method(MethodKind::Inherent)),
1047 Allow(Target::Method(MethodKind::TraitImpl)),
1048 Allow(Target::Method(MethodKind::Trait { body: false })),
1049 Allow(Target::Method(MethodKind::Trait { body: true })),
1050 ]);
1051 const STABILITY: AttributeStability = {
_ = rustc_feature::Features::rustc_attrs;
AttributeStability::Unstable {
gate_name: rustc_span::sym::rustc_attrs,
notes: &["the `rustc_do_not_const_check` attribute skips const-check for this function's body"],
}
}unstable!(
1052 rustc_attrs,
1053 "the `rustc_do_not_const_check` attribute skips const-check for this function's body"
1054 );
1055 const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcDoNotConstCheck;
1056}
1057
1058pub(crate) struct RustcNonnullOptimizationGuaranteedParser;
1059
1060impl NoArgsAttributeParser for RustcNonnullOptimizationGuaranteedParser {
1061 const PATH: &[Symbol] = &[sym::rustc_nonnull_optimization_guaranteed];
1062 const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowList(&[Allow(Target::Struct)]);
1063 const STABILITY: AttributeStability = {
_ = rustc_feature::Features::rustc_attrs;
AttributeStability::Unstable {
gate_name: rustc_span::sym::rustc_attrs,
notes: &["the `rustc_nonnull_optimization_guaranteed` attribute is just used to document guaranteed niche optimizations in the standard library",
"the compiler does not even check whether the type indeed is being non-null-optimized; it is your responsibility to ensure that the attribute is only used on types that are optimized"],
}
}unstable!(
1064 rustc_attrs,
1065 "the `rustc_nonnull_optimization_guaranteed` attribute is just used to document guaranteed niche optimizations in the standard library",
1066 "the compiler does not even check whether the type indeed is being non-null-optimized; it is your responsibility to ensure that the attribute is only used on types that are optimized"
1067 );
1068 const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcNonnullOptimizationGuaranteed;
1069}
1070
1071pub(crate) struct RustcStrictCoherenceParser;
1072
1073impl NoArgsAttributeParser for RustcStrictCoherenceParser {
1074 const PATH: &[Symbol] = &[sym::rustc_strict_coherence];
1075 const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowList(&[
1076 Allow(Target::Trait),
1077 Allow(Target::Struct),
1078 Allow(Target::Enum),
1079 Allow(Target::Union),
1080 Allow(Target::ForeignTy),
1081 ]);
1082 const STABILITY: AttributeStability = {
_ = rustc_feature::Features::rustc_attrs;
AttributeStability::Unstable {
gate_name: rustc_span::sym::rustc_attrs,
notes: &[],
}
}unstable!(rustc_attrs);
1083 const CREATE: fn(Span) -> AttributeKind = AttributeKind::RustcStrictCoherence;
1084}
1085
1086pub(crate) struct PreludeImportParser;
1087
1088impl NoArgsAttributeParser for PreludeImportParser {
1089 const PATH: &[Symbol] = &[sym::prelude_import];
1090 const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowList(&[Allow(Target::Use)]);
1091 const STABILITY: AttributeStability = {
_ = rustc_feature::Features::prelude_import;
AttributeStability::Unstable {
gate_name: rustc_span::sym::prelude_import,
notes: &[],
}
}unstable!(prelude_import);
1092 const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::PreludeImport;
1093}
1094
1095pub(crate) struct RustcDocPrimitiveParser;
1096
1097impl SingleAttributeParser for RustcDocPrimitiveParser {
1098 const PATH: &[Symbol] = &[sym::rustc_doc_primitive];
1099 const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowList(&[Allow(Target::Const)]);
1100 const TEMPLATE: AttributeTemplate = crate::AttributeTemplate {
word: false,
list: None,
one_of: &[],
name_value_str: Some(&["primitive name"]),
docs: None,
}template!(NameValueStr: "primitive name");
1101 const STABILITY: AttributeStability = {
_ = rustc_feature::Features::rustc_attrs;
AttributeStability::Unstable {
gate_name: rustc_span::sym::rustc_attrs,
notes: &["the `rustc_doc_primitive` attribute is used by the standard library to provide a way to generate documentation for primitive types"],
}
}unstable!(
1102 rustc_attrs,
1103 "the `rustc_doc_primitive` attribute is used by the standard library to provide a way to generate documentation for primitive types"
1104 );
1105
1106 fn convert(cx: &mut AcceptContext<'_, '_>, args: &ArgParser) -> Option<AttributeKind> {
1107 let nv = cx.expect_name_value(args, cx.attr_span, None)?;
1108 let value_str = cx.expect_string_literal(nv)?;
1109
1110 Some(AttributeKind::RustcDocPrimitive(cx.attr_span, value_str))
1111 }
1112}
1113
1114pub(crate) struct RustcIntrinsicParser;
1115
1116impl SingleAttributeParser for RustcIntrinsicParser {
1117 const PATH: &[Symbol] = &[sym::rustc_intrinsic];
1118 const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowList(&[Allow(Target::Fn)]);
1119 const TEMPLATE: AttributeTemplate = crate::AttributeTemplate {
word: true,
list: Some(&[r#"llvm = "...""#]),
one_of: &[],
name_value_str: None,
docs: None,
}template!(Word, List: &[r#"llvm = "...""#]);
1120 const STABILITY: AttributeStability = {
_ = rustc_feature::Features::intrinsics;
AttributeStability::Unstable {
gate_name: rustc_span::sym::intrinsics,
notes: &[],
}
}unstable!(intrinsics);
1121
1122 fn convert(cx: &mut AcceptContext<'_, '_>, args: &ArgParser) -> Option<AttributeKind> {
1123 match args {
1124 ArgParser::NoArgs => Some(AttributeKind::RustcIntrinsic { llvm: None }),
1125 ArgParser::List(_) => {
1126 let item = cx.expect_single_element_list(args, cx.attr_span)?;
1127 let nv = cx.expect_name_value(item, item.span(), Some(sym::llvm))?;
1128 let name = cx.expect_string_literal(nv.1)?;
1129 Some(AttributeKind::RustcIntrinsic { llvm: Some(name) })
1130 }
1131 ArgParser::NameValue(_) => {
1132 let span = cx.attr_span;
1133 cx.adcx().expected_list_or_no_args(span);
1134 None
1135 }
1136 }
1137 }
1138}
1139
1140pub(crate) struct RustcIntrinsicConstStableIndirectParser;
1141
1142impl NoArgsAttributeParser for RustcIntrinsicConstStableIndirectParser {
1143 const PATH: &'static [Symbol] = &[sym::rustc_intrinsic_const_stable_indirect];
1144 const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowList(&[Allow(Target::Fn)]);
1145 const STABILITY: AttributeStability = {
_ = rustc_feature::Features::rustc_attrs;
AttributeStability::Unstable {
gate_name: rustc_span::sym::rustc_attrs,
notes: &[],
}
}unstable!(rustc_attrs);
1146 const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcIntrinsicConstStableIndirect;
1147}
1148
1149pub(crate) struct RustcExhaustiveParser;
1150
1151impl NoArgsAttributeParser for RustcExhaustiveParser {
1152 const PATH: &'static [Symbol] = &[sym::rustc_must_match_exhaustively];
1153 const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowList(&[Allow(Target::Enum)]);
1154 const STABILITY: AttributeStability = {
_ = rustc_feature::Features::rustc_attrs;
AttributeStability::Unstable {
gate_name: rustc_span::sym::rustc_attrs,
notes: &[],
}
}unstable!(rustc_attrs);
1155 const CREATE: fn(Span) -> AttributeKind = AttributeKind::RustcMustMatchExhaustively;
1156}
1157
1158pub(crate) struct RustcCanonicalSymbolParser;
1159
1160impl NoArgsAttributeParser for RustcCanonicalSymbolParser {
1161 const PATH: &[Symbol] = &[sym::rustc_canonical_symbol];
1162 const ALLOWED_TARGETS: AllowedTargets<'_> =
1163 AllowedTargets::AllowList(&[Allow(Target::ForeignFn)]);
1164 const STABILITY: AttributeStability = {
_ = rustc_feature::Features::rustc_attrs;
AttributeStability::Unstable {
gate_name: rustc_span::sym::rustc_attrs,
notes: &["the `rustc_canonical_symbol` attribute registers a function's symbol to be linted against \
by the `invalid_runtime_symbol_definitions` and `suspicious_runtime_symbol_definitions` \
lints"],
}
}unstable!(
1165 rustc_attrs,
1166 "the `rustc_canonical_symbol` attribute registers a function's symbol to be linted against \
1167 by the `invalid_runtime_symbol_definitions` and `suspicious_runtime_symbol_definitions` \
1168 lints"
1169 );
1170 const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcCanonicalSymbol;
1171}
1172
1173pub(crate) struct RustcAssertVarianceParser;
1174
1175impl SingleAttributeParser for RustcAssertVarianceParser {
1176 const PATH: &[Symbol] = &[sym::rustc_assert_variance];
1177
1178 const STABILITY: AttributeStability = {
_ = rustc_feature::Features::rustc_attrs;
AttributeStability::Unstable {
gate_name: rustc_span::sym::rustc_attrs,
notes: &[],
}
}unstable!(rustc_attrs);
1179
1180 const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowList(&[
1181 Allow(Target::TypeParam),
1182 Allow(Target::LifetimeParam),
1183 Allow(Target::ConstParam),
1184 ]);
1185
1186 const TEMPLATE: AttributeTemplate =
1187 crate::AttributeTemplate {
word: false,
list: None,
one_of: &[sym::covariant, sym::invariant, sym::contravariant,
sym::bivariant],
name_value_str: None,
docs: None,
}template!(OneOf: &[sym::covariant, sym::invariant, sym::contravariant, sym::bivariant]);
1188
1189 fn convert(cx: &mut AcceptContext<'_, '_>, args: &ArgParser) -> Option<AttributeKind> {
1190 let arg = cx.expect_single_element_list(args, cx.attr_span)?;
1191
1192 let mut fail_arg = |span| {
1193 cx.adcx().expected_specific_argument(
1194 span,
1195 &[sym::covariant, sym::invariant, sym::contravariant, sym::bivariant],
1196 )
1197 };
1198
1199 let Some(arg) = arg.meta_item_no_args() else {
1200 fail_arg(arg.span());
1201 return None;
1202 };
1203
1204 let Some(arg) = arg.ident() else {
1205 fail_arg(arg.span());
1206 return None;
1207 };
1208
1209 let kind = match arg.name {
1210 sym::covariant => RustcAssertVarianceKind::Covariant,
1211 sym::invariant => RustcAssertVarianceKind::Invariant,
1212 sym::contravariant => RustcAssertVarianceKind::Contravariant,
1213 sym::bivariant => RustcAssertVarianceKind::Bivariant,
1214 _ => {
1215 fail_arg(arg.span);
1216 return None;
1217 }
1218 };
1219
1220 Some(AttributeKind::RustcAssertVariance { span: cx.attr_span, kind })
1221 }
1222}