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