1use std::str::FromStr;
2
3use rustc_abi::{Align, ExternAbi};
4use rustc_ast::expand::autodiff_attrs::{AutoDiffAttrs, DiffActivity, DiffMode};
5use rustc_ast::{LitKind, MetaItem, MetaItemInner, attr};
6use rustc_hir::attrs::{
7 AttributeKind, EiiImplResolution, InlineAttr, Linkage, RtsanSetting, UsedBy,
8};
9use rustc_hir::def::DefKind;
10use rustc_hir::def_id::{DefId, LOCAL_CRATE, LocalDefId};
11use rustc_hir::{self as hir, Attribute, LangItem, find_attr, lang_items};
12use rustc_middle::middle::codegen_fn_attrs::{
13 CodegenFnAttrFlags, CodegenFnAttrs, PatchableFunctionEntry, SanitizerFnAttrs,
14};
15use rustc_middle::mir::mono::Visibility;
16use rustc_middle::query::Providers;
17use rustc_middle::span_bug;
18use rustc_middle::ty::{self as ty, TyCtxt};
19use rustc_session::lint;
20use rustc_session::parse::feature_err;
21use rustc_span::{Span, sym};
22use rustc_target::spec::Os;
23
24use crate::errors;
25use crate::target_features::{
26 check_target_feature_trait_unsafe, check_tied_features, from_target_feature_attr,
27};
28
29fn try_fn_sig<'tcx>(
35 tcx: TyCtxt<'tcx>,
36 did: LocalDefId,
37 attr_span: Span,
38) -> Option<ty::EarlyBinder<'tcx, ty::PolyFnSig<'tcx>>> {
39 use DefKind::*;
40
41 let def_kind = tcx.def_kind(did);
42 if let Fn | AssocFn | Variant | Ctor(..) = def_kind {
43 Some(tcx.fn_sig(did))
44 } else {
45 tcx.dcx().span_delayed_bug(attr_span, "this attribute can only be applied to functions");
46 None
47 }
48}
49
50fn parse_patchable_function_entry(
52 tcx: TyCtxt<'_>,
53 attr: &Attribute,
54) -> Option<PatchableFunctionEntry> {
55 attr.meta_item_list().and_then(|l| {
56 let mut prefix = None;
57 let mut entry = None;
58 for item in l {
59 let Some(meta_item) = item.meta_item() else {
60 tcx.dcx().emit_err(errors::ExpectedNameValuePair { span: item.span() });
61 continue;
62 };
63
64 let Some(name_value_lit) = meta_item.name_value_literal() else {
65 tcx.dcx().emit_err(errors::ExpectedNameValuePair { span: item.span() });
66 continue;
67 };
68
69 let attrib_to_write = match meta_item.name() {
70 Some(sym::prefix_nops) => &mut prefix,
71 Some(sym::entry_nops) => &mut entry,
72 _ => {
73 tcx.dcx().emit_err(errors::UnexpectedParameterName {
74 span: item.span(),
75 prefix_nops: sym::prefix_nops,
76 entry_nops: sym::entry_nops,
77 });
78 continue;
79 }
80 };
81
82 let rustc_ast::LitKind::Int(val, _) = name_value_lit.kind else {
83 tcx.dcx().emit_err(errors::InvalidLiteralValue { span: name_value_lit.span });
84 continue;
85 };
86
87 let Ok(val) = val.get().try_into() else {
88 tcx.dcx().emit_err(errors::OutOfRangeInteger { span: name_value_lit.span });
89 continue;
90 };
91
92 *attrib_to_write = Some(val);
93 }
94
95 if let (None, None) = (prefix, entry) {
96 tcx.dcx().span_err(attr.span(), "must specify at least one parameter");
97 }
98
99 Some(PatchableFunctionEntry::from_prefix_and_entry(prefix.unwrap_or(0), entry.unwrap_or(0)))
100 })
101}
102
103#[derive(Default)]
106struct InterestingAttributeDiagnosticSpans {
107 link_ordinal: Option<Span>,
108 sanitize: Option<Span>,
109 inline: Option<Span>,
110 no_mangle: Option<Span>,
111}
112
113fn process_builtin_attrs(
116 tcx: TyCtxt<'_>,
117 did: LocalDefId,
118 attrs: &[Attribute],
119 codegen_fn_attrs: &mut CodegenFnAttrs,
120) -> InterestingAttributeDiagnosticSpans {
121 let mut interesting_spans = InterestingAttributeDiagnosticSpans::default();
122 let rust_target_features = tcx.rust_target_features(LOCAL_CRATE);
123
124 for attr in attrs.iter() {
125 if let hir::Attribute::Parsed(p) = attr {
126 match p {
127 AttributeKind::Cold(_) => codegen_fn_attrs.flags |= CodegenFnAttrFlags::COLD,
128 AttributeKind::ExportName { name, .. } => {
129 codegen_fn_attrs.symbol_name = Some(*name)
130 }
131 AttributeKind::Inline(inline, span) => {
132 codegen_fn_attrs.inline = *inline;
133 interesting_spans.inline = Some(*span);
134 }
135 AttributeKind::Naked(_) => codegen_fn_attrs.flags |= CodegenFnAttrFlags::NAKED,
136 AttributeKind::Align { align, .. } => codegen_fn_attrs.alignment = Some(*align),
137 AttributeKind::LinkName { name, .. } => {
138 if tcx.is_foreign_item(did) {
141 codegen_fn_attrs.symbol_name = Some(*name);
142 }
143 }
144 AttributeKind::LinkOrdinal { ordinal, span } => {
145 codegen_fn_attrs.link_ordinal = Some(*ordinal);
146 interesting_spans.link_ordinal = Some(*span);
147 }
148 AttributeKind::LinkSection { name, .. } => {
149 codegen_fn_attrs.link_section = Some(*name)
150 }
151 AttributeKind::NoMangle(attr_span) => {
152 interesting_spans.no_mangle = Some(*attr_span);
153 if tcx.opt_item_name(did.to_def_id()).is_some() {
154 codegen_fn_attrs.flags |= CodegenFnAttrFlags::NO_MANGLE;
155 } else {
156 tcx.dcx().span_delayed_bug(
157 *attr_span,
158 "no_mangle should be on a named function",
159 );
160 }
161 }
162 AttributeKind::Optimize(optimize, _) => codegen_fn_attrs.optimize = *optimize,
163 AttributeKind::TargetFeature { features, attr_span, was_forced } => {
164 let Some(sig) = tcx.hir_node_by_def_id(did).fn_sig() else {
165 tcx.dcx().span_delayed_bug(*attr_span, "target_feature applied to non-fn");
166 continue;
167 };
168 let safe_target_features =
169 matches!(sig.header.safety, hir::HeaderSafety::SafeTargetFeatures);
170 codegen_fn_attrs.safe_target_features = safe_target_features;
171 if safe_target_features && !was_forced {
172 if tcx.sess.target.is_like_wasm || tcx.sess.opts.actually_rustdoc {
173 } else {
195 check_target_feature_trait_unsafe(tcx, did, *attr_span);
196 }
197 }
198 from_target_feature_attr(
199 tcx,
200 did,
201 features,
202 *was_forced,
203 rust_target_features,
204 &mut codegen_fn_attrs.target_features,
205 );
206 }
207 AttributeKind::TrackCaller(attr_span) => {
208 let is_closure = tcx.is_closure_like(did.to_def_id());
209
210 if !is_closure
211 && let Some(fn_sig) = try_fn_sig(tcx, did, *attr_span)
212 && fn_sig.skip_binder().abi() != ExternAbi::Rust
213 {
214 tcx.dcx().emit_err(errors::RequiresRustAbi { span: *attr_span });
215 }
216 if is_closure
217 && !tcx.features().closure_track_caller()
218 && !attr_span.allows_unstable(sym::closure_track_caller)
219 {
220 feature_err(
221 &tcx.sess,
222 sym::closure_track_caller,
223 *attr_span,
224 "`#[track_caller]` on closures is currently unstable",
225 )
226 .emit();
227 }
228 codegen_fn_attrs.flags |= CodegenFnAttrFlags::TRACK_CALLER
229 }
230 AttributeKind::Used { used_by, .. } => match used_by {
231 UsedBy::Compiler => codegen_fn_attrs.flags |= CodegenFnAttrFlags::USED_COMPILER,
232 UsedBy::Linker => codegen_fn_attrs.flags |= CodegenFnAttrFlags::USED_LINKER,
233 UsedBy::Default => {
234 let used_form = if tcx.sess.target.os == Os::Illumos {
235 CodegenFnAttrFlags::USED_COMPILER
241 } else {
242 CodegenFnAttrFlags::USED_LINKER
243 };
244 codegen_fn_attrs.flags |= used_form;
245 }
246 },
247 AttributeKind::FfiConst(_) => {
248 codegen_fn_attrs.flags |= CodegenFnAttrFlags::FFI_CONST
249 }
250 AttributeKind::FfiPure(_) => codegen_fn_attrs.flags |= CodegenFnAttrFlags::FFI_PURE,
251 AttributeKind::StdInternalSymbol(_) => {
252 codegen_fn_attrs.flags |= CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL
253 }
254 AttributeKind::Linkage(linkage, span) => {
255 let linkage = Some(*linkage);
256
257 if tcx.is_foreign_item(did) {
258 codegen_fn_attrs.import_linkage = linkage;
259
260 if tcx.is_mutable_static(did.into()) {
261 let mut diag = tcx.dcx().struct_span_err(
262 *span,
263 "extern mutable statics are not allowed with `#[linkage]`",
264 );
265 diag.note(
266 "marking the extern static mutable would allow changing which \
267 symbol the static references rather than make the target of the \
268 symbol mutable",
269 );
270 diag.emit();
271 }
272 } else {
273 codegen_fn_attrs.linkage = linkage;
274 }
275 }
276 AttributeKind::Sanitize { span, .. } => {
277 interesting_spans.sanitize = Some(*span);
278 }
279 AttributeKind::ObjcClass { classname, .. } => {
280 codegen_fn_attrs.objc_class = Some(*classname);
281 }
282 AttributeKind::ObjcSelector { methname, .. } => {
283 codegen_fn_attrs.objc_selector = Some(*methname);
284 }
285 AttributeKind::EiiForeignItem => {
286 codegen_fn_attrs.flags |= CodegenFnAttrFlags::EXTERNALLY_IMPLEMENTABLE_ITEM;
287 }
288 AttributeKind::EiiImpls(impls) => {
289 for i in impls {
290 let foreign_item = match i.resolution {
291 EiiImplResolution::Macro(def_id) => {
292 let Some(extern_item) = find_attr!(
293 tcx.get_all_attrs(def_id),
294 AttributeKind::EiiDeclaration(target) => target.foreign_item
295 ) else {
296 tcx.dcx().span_delayed_bug(
297 i.span,
298 "resolved to something that's not an EII",
299 );
300 continue;
301 };
302 extern_item
303 }
304 EiiImplResolution::Known(decl) => decl.foreign_item,
305 EiiImplResolution::Error(_eg) => continue,
306 };
307
308 if
314 i.is_default
316 && tcx.externally_implementable_items(LOCAL_CRATE).get(&foreign_item).expect("at least one").1.iter().any(|(_, imp)| !imp.is_default)
320 {
321 continue;
322 }
323
324 codegen_fn_attrs.foreign_item_symbol_aliases.push((
325 foreign_item,
326 if i.is_default { Linkage::LinkOnceAny } else { Linkage::External },
327 Visibility::Default,
328 ));
329 codegen_fn_attrs.flags |= CodegenFnAttrFlags::EXTERNALLY_IMPLEMENTABLE_ITEM;
330 }
331 }
332 AttributeKind::ThreadLocal => {
333 codegen_fn_attrs.flags |= CodegenFnAttrFlags::THREAD_LOCAL
334 }
335 AttributeKind::InstructionSet(instruction_set) => {
336 codegen_fn_attrs.instruction_set = Some(*instruction_set)
337 }
338 _ => {}
339 }
340 }
341
342 let Some(name) = attr.name() else {
343 continue;
344 };
345
346 match name {
347 sym::rustc_allocator => codegen_fn_attrs.flags |= CodegenFnAttrFlags::ALLOCATOR,
348 sym::rustc_nounwind => codegen_fn_attrs.flags |= CodegenFnAttrFlags::NEVER_UNWIND,
349 sym::rustc_reallocator => codegen_fn_attrs.flags |= CodegenFnAttrFlags::REALLOCATOR,
350 sym::rustc_deallocator => codegen_fn_attrs.flags |= CodegenFnAttrFlags::DEALLOCATOR,
351 sym::rustc_allocator_zeroed => {
352 codegen_fn_attrs.flags |= CodegenFnAttrFlags::ALLOCATOR_ZEROED
353 }
354 sym::patchable_function_entry => {
355 codegen_fn_attrs.patchable_function_entry =
356 parse_patchable_function_entry(tcx, attr);
357 }
358 sym::rustc_offload_kernel => {
359 codegen_fn_attrs.flags |= CodegenFnAttrFlags::OFFLOAD_KERNEL
360 }
361 _ => {}
362 }
363 }
364
365 interesting_spans
366}
367
368fn apply_overrides(tcx: TyCtxt<'_>, did: LocalDefId, codegen_fn_attrs: &mut CodegenFnAttrs) {
371 codegen_fn_attrs.alignment =
375 Ord::max(codegen_fn_attrs.alignment, tcx.sess.opts.unstable_opts.min_function_alignment);
376
377 assert!(codegen_fn_attrs.sanitizers == SanitizerFnAttrs::default());
379 codegen_fn_attrs.sanitizers = tcx.sanitizer_settings_for(did);
381 codegen_fn_attrs.alignment = Ord::max(codegen_fn_attrs.alignment, tcx.inherited_align(did));
383
384 if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::NAKED) {
388 codegen_fn_attrs.inline = InlineAttr::Never;
389 }
390
391 if tcx.is_closure_like(did.to_def_id()) && codegen_fn_attrs.inline != InlineAttr::Always {
405 let owner_id = tcx.parent(did.to_def_id());
406 if tcx.def_kind(owner_id).has_codegen_attrs() {
407 codegen_fn_attrs
408 .target_features
409 .extend(tcx.codegen_fn_attrs(owner_id).target_features.iter().copied());
410 }
411 }
412
413 let crate_attrs = tcx.hir_attrs(rustc_hir::CRATE_HIR_ID);
416 let no_builtins = attr::contains_name(crate_attrs, sym::no_builtins);
417 if no_builtins {
418 codegen_fn_attrs.flags |= CodegenFnAttrFlags::NO_BUILTINS;
419 }
420
421 if tcx.should_inherit_track_caller(did) {
423 codegen_fn_attrs.flags |= CodegenFnAttrFlags::TRACK_CALLER;
424 }
425
426 if tcx.is_foreign_item(did) {
428 codegen_fn_attrs.flags |= CodegenFnAttrFlags::FOREIGN_ITEM;
429
430 if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL) {
432 } else if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::EXTERNALLY_IMPLEMENTABLE_ITEM)
436 {
437 } else if codegen_fn_attrs.symbol_name.is_some() {
442 } else {
444 codegen_fn_attrs.flags |= CodegenFnAttrFlags::NO_MANGLE;
452 }
453 }
454}
455
456fn check_result(
457 tcx: TyCtxt<'_>,
458 did: LocalDefId,
459 interesting_spans: InterestingAttributeDiagnosticSpans,
460 codegen_fn_attrs: &CodegenFnAttrs,
461) {
462 if !codegen_fn_attrs.target_features.is_empty()
476 && matches!(codegen_fn_attrs.inline, InlineAttr::Always)
477 && !tcx.features().target_feature_inline_always()
478 && let Some(span) = interesting_spans.inline
479 {
480 feature_err(
481 tcx.sess,
482 sym::target_feature_inline_always,
483 span,
484 "cannot use `#[inline(always)]` with `#[target_feature]`",
485 )
486 .emit();
487 }
488
489 if codegen_fn_attrs.sanitizers != SanitizerFnAttrs::default()
491 && codegen_fn_attrs.inline.always()
492 && let (Some(sanitize_span), Some(inline_span)) =
493 (interesting_spans.sanitize, interesting_spans.inline)
494 {
495 let hir_id = tcx.local_def_id_to_hir_id(did);
496 tcx.node_span_lint(lint::builtin::INLINE_NO_SANITIZE, hir_id, sanitize_span, |lint| {
497 lint.primary_message("non-default `sanitize` will have no effect after inlining");
498 lint.span_note(inline_span, "inlining requested here");
499 })
500 }
501
502 if codegen_fn_attrs.sanitizers.rtsan_setting == RtsanSetting::Nonblocking
505 && let Some(sanitize_span) = interesting_spans.sanitize
506 && (tcx.asyncness(did).is_async()
508 || tcx.is_coroutine(did.into())
510 || (tcx.is_closure_like(did.into())
512 && tcx.hir_node_by_def_id(did).expect_closure().kind
513 != rustc_hir::ClosureKind::Closure))
514 {
515 let hir_id = tcx.local_def_id_to_hir_id(did);
516 tcx.node_span_lint(
517 lint::builtin::RTSAN_NONBLOCKING_ASYNC,
518 hir_id,
519 sanitize_span,
520 |lint| {
521 lint.primary_message(r#"the async executor can run blocking code, without realtime sanitizer catching it"#);
522 }
523 );
524 }
525
526 if let Some(_) = codegen_fn_attrs.symbol_name
528 && let Some(_) = codegen_fn_attrs.link_ordinal
529 {
530 let msg = "cannot use `#[link_name]` with `#[link_ordinal]`";
531 if let Some(span) = interesting_spans.link_ordinal {
532 tcx.dcx().span_err(span, msg);
533 } else {
534 tcx.dcx().err(msg);
535 }
536 }
537
538 if let Some(features) = check_tied_features(
539 tcx.sess,
540 &codegen_fn_attrs
541 .target_features
542 .iter()
543 .map(|features| (features.name.as_str(), true))
544 .collect(),
545 ) {
546 let span =
547 find_attr!(tcx.get_all_attrs(did), AttributeKind::TargetFeature{attr_span: span, ..} => *span)
548 .unwrap_or_else(|| tcx.def_span(did));
549
550 tcx.dcx()
551 .create_err(errors::TargetFeatureDisableOrEnable {
552 features,
553 span: Some(span),
554 missing_features: Some(errors::MissingFeatures),
555 })
556 .emit();
557 }
558}
559
560fn handle_lang_items(
561 tcx: TyCtxt<'_>,
562 did: LocalDefId,
563 interesting_spans: &InterestingAttributeDiagnosticSpans,
564 attrs: &[Attribute],
565 codegen_fn_attrs: &mut CodegenFnAttrs,
566) {
567 let lang_item = lang_items::extract(attrs).and_then(|(name, _)| LangItem::from_name(name));
568
569 if let Some(lang_item) = lang_item
575 && let Some(link_name) = lang_item.link_name()
576 {
577 codegen_fn_attrs.flags |= CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL;
578 codegen_fn_attrs.symbol_name = Some(link_name);
579 }
580
581 if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL)
583 && codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::NO_MANGLE)
584 {
585 let mut err = tcx
586 .dcx()
587 .struct_span_err(
588 interesting_spans.no_mangle.unwrap_or_default(),
589 "`#[no_mangle]` cannot be used on internal language items",
590 )
591 .with_note("Rustc requires this item to have a specific mangled name.")
592 .with_span_label(tcx.def_span(did), "should be the internal language item");
593 if let Some(lang_item) = lang_item
594 && let Some(link_name) = lang_item.link_name()
595 {
596 err = err
597 .with_note("If you are trying to prevent mangling to ease debugging, many")
598 .with_note(format!("debuggers support a command such as `rbreak {link_name}` to"))
599 .with_note(format!(
600 "match `.*{link_name}.*` instead of `break {link_name}` on a specific name"
601 ))
602 }
603 err.emit();
604 }
605}
606
607fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
616 if cfg!(debug_assertions) {
617 let def_kind = tcx.def_kind(did);
618 assert!(
619 def_kind.has_codegen_attrs(),
620 "unexpected `def_kind` in `codegen_fn_attrs`: {def_kind:?}",
621 );
622 }
623
624 let mut codegen_fn_attrs = CodegenFnAttrs::new();
625 let attrs = tcx.hir_attrs(tcx.local_def_id_to_hir_id(did));
626
627 let interesting_spans = process_builtin_attrs(tcx, did, attrs, &mut codegen_fn_attrs);
628 handle_lang_items(tcx, did, &interesting_spans, attrs, &mut codegen_fn_attrs);
629 apply_overrides(tcx, did, &mut codegen_fn_attrs);
630 check_result(tcx, did, interesting_spans, &codegen_fn_attrs);
631
632 codegen_fn_attrs
633}
634
635fn sanitizer_settings_for(tcx: TyCtxt<'_>, did: LocalDefId) -> SanitizerFnAttrs {
636 let mut settings = match tcx.opt_local_parent(did) {
638 Some(parent) => tcx.sanitizer_settings_for(parent),
640 None => SanitizerFnAttrs::default(),
643 };
644
645 if let Some((on_set, off_set, rtsan)) = find_attr!(tcx.get_all_attrs(did), AttributeKind::Sanitize {on_set, off_set, rtsan, ..} => (on_set, off_set, rtsan))
647 {
648 settings.disabled &= !*on_set;
651 settings.disabled |= *off_set;
654 if let Some(rtsan) = rtsan {
660 settings.rtsan_setting = *rtsan;
661 }
662 }
663 settings
664}
665
666fn should_inherit_track_caller(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
669 tcx.trait_item_of(def_id).is_some_and(|id| {
670 tcx.codegen_fn_attrs(id).flags.intersects(CodegenFnAttrFlags::TRACK_CALLER)
671 })
672}
673
674fn inherited_align<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> Option<Align> {
677 tcx.codegen_fn_attrs(tcx.trait_item_of(def_id)?).alignment
678}
679
680pub fn autodiff_attrs(tcx: TyCtxt<'_>, id: DefId) -> Option<AutoDiffAttrs> {
687 let attrs = tcx.get_attrs(id, sym::rustc_autodiff);
688
689 let attrs = attrs.filter(|attr| attr.has_name(sym::rustc_autodiff)).collect::<Vec<_>>();
690
691 let attr = match &attrs[..] {
694 [] => return None,
695 [attr] => attr,
696 _ => {
697 span_bug!(attrs[1].span(), "cg_ssa: rustc_autodiff should only exist once per source");
698 }
699 };
700
701 let list = attr.meta_item_list().unwrap_or_default();
702
703 if list.is_empty() {
705 return Some(AutoDiffAttrs::source());
706 }
707
708 let [mode, width_meta, input_activities @ .., ret_activity] = &list[..] else {
709 span_bug!(attr.span(), "rustc_autodiff attribute must contain mode, width and activities");
710 };
711 let mode = if let MetaItemInner::MetaItem(MetaItem { path: p1, .. }) = mode {
712 p1.segments.first().unwrap().ident
713 } else {
714 span_bug!(attr.span(), "rustc_autodiff attribute must contain mode");
715 };
716
717 let mode = match mode.as_str() {
719 "Forward" => DiffMode::Forward,
720 "Reverse" => DiffMode::Reverse,
721 _ => {
722 span_bug!(mode.span, "rustc_autodiff attribute contains invalid mode");
723 }
724 };
725
726 let width: u32 = match width_meta {
727 MetaItemInner::MetaItem(MetaItem { path: p1, .. }) => {
728 let w = p1.segments.first().unwrap().ident;
729 match w.as_str().parse() {
730 Ok(val) => val,
731 Err(_) => {
732 span_bug!(w.span, "rustc_autodiff width should fit u32");
733 }
734 }
735 }
736 MetaItemInner::Lit(lit) => {
737 if let LitKind::Int(val, _) = lit.kind {
738 match val.get().try_into() {
739 Ok(val) => val,
740 Err(_) => {
741 span_bug!(lit.span, "rustc_autodiff width should fit u32");
742 }
743 }
744 } else {
745 span_bug!(lit.span, "rustc_autodiff width should be an integer");
746 }
747 }
748 };
749
750 let MetaItemInner::MetaItem(MetaItem { path: p1, .. }) = ret_activity else {
752 span_bug!(attr.span(), "rustc_autodiff attribute must contain the return activity");
753 };
754 let ret_symbol = p1.segments.first().unwrap().ident;
755
756 let Ok(ret_activity) = DiffActivity::from_str(ret_symbol.as_str()) else {
758 span_bug!(ret_symbol.span, "invalid return activity");
759 };
760
761 let mut arg_activities: Vec<DiffActivity> = vec![];
763 for arg in input_activities {
764 let arg_symbol = if let MetaItemInner::MetaItem(MetaItem { path: p2, .. }) = arg {
765 match p2.segments.first() {
766 Some(x) => x.ident,
767 None => {
768 span_bug!(
769 arg.span(),
770 "rustc_autodiff attribute must contain the input activity"
771 );
772 }
773 }
774 } else {
775 span_bug!(arg.span(), "rustc_autodiff attribute must contain the input activity");
776 };
777
778 match DiffActivity::from_str(arg_symbol.as_str()) {
779 Ok(arg_activity) => arg_activities.push(arg_activity),
780 Err(_) => {
781 span_bug!(arg_symbol.span, "invalid input activity");
782 }
783 }
784 }
785
786 Some(AutoDiffAttrs { mode, width, ret_activity, input_activity: arg_activities })
787}
788
789pub(crate) fn provide(providers: &mut Providers) {
790 *providers = Providers {
791 codegen_fn_attrs,
792 should_inherit_track_caller,
793 inherited_align,
794 sanitizer_settings_for,
795 ..*providers
796 };
797}