Skip to main content

rustc_codegen_ssa/
codegen_attrs.rs

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};
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, find_attr};
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
29/// In some cases, attributes are only valid on functions, but it's the `check_attr`
30/// pass that checks that they aren't used anywhere else, rather than this module.
31/// In these cases, we bail from performing further checks that are only meaningful for
32/// functions (such as calling `fn_sig`, which ICEs if given a non-function). We also
33/// report a delayed bug, just in case `check_attr` isn't doing its job.
34fn 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
50/// Spans that are collected when processing built-in attributes,
51/// that are useful for emitting diagnostics later.
52#[derive(#[automatically_derived]
impl ::core::default::Default for InterestingAttributeDiagnosticSpans {
    #[inline]
    fn default() -> InterestingAttributeDiagnosticSpans {
        InterestingAttributeDiagnosticSpans {
            link_ordinal: ::core::default::Default::default(),
            sanitize: ::core::default::Default::default(),
            inline: ::core::default::Default::default(),
            no_mangle: ::core::default::Default::default(),
        }
    }
}Default)]
53struct InterestingAttributeDiagnosticSpans {
54    link_ordinal: Option<Span>,
55    sanitize: Option<Span>,
56    inline: Option<Span>,
57    no_mangle: Option<Span>,
58}
59
60/// Process the builtin attrs ([`hir::Attribute`]) on the item.
61/// Many of them directly translate to codegen attrs.
62fn process_builtin_attrs(
63    tcx: TyCtxt<'_>,
64    did: LocalDefId,
65    attrs: &[Attribute],
66    codegen_fn_attrs: &mut CodegenFnAttrs,
67) -> InterestingAttributeDiagnosticSpans {
68    let mut interesting_spans = InterestingAttributeDiagnosticSpans::default();
69    let rust_target_features = tcx.rust_target_features(LOCAL_CRATE);
70
71    let parsed_attrs = attrs
72        .iter()
73        .filter_map(|attr| if let hir::Attribute::Parsed(attr) = attr { Some(attr) } else { None });
74    for attr in parsed_attrs {
75        match attr {
76            AttributeKind::Cold(_) => codegen_fn_attrs.flags |= CodegenFnAttrFlags::COLD,
77            AttributeKind::ExportName { name, .. } => codegen_fn_attrs.symbol_name = Some(*name),
78            AttributeKind::Inline(inline, span) => {
79                codegen_fn_attrs.inline = *inline;
80                interesting_spans.inline = Some(*span);
81            }
82            AttributeKind::Naked(_) => codegen_fn_attrs.flags |= CodegenFnAttrFlags::NAKED,
83            AttributeKind::Align { align, .. } => codegen_fn_attrs.alignment = Some(*align),
84            AttributeKind::LinkName { name, .. } => {
85                // FIXME Remove check for foreign functions once #[link_name] on non-foreign
86                // functions is a hard error
87                if tcx.is_foreign_item(did) {
88                    codegen_fn_attrs.symbol_name = Some(*name);
89                }
90            }
91            AttributeKind::LinkOrdinal { ordinal, span } => {
92                codegen_fn_attrs.link_ordinal = Some(*ordinal);
93                interesting_spans.link_ordinal = Some(*span);
94            }
95            AttributeKind::LinkSection { name, .. } => codegen_fn_attrs.link_section = Some(*name),
96            AttributeKind::NoMangle(attr_span) => {
97                interesting_spans.no_mangle = Some(*attr_span);
98                if tcx.opt_item_name(did.to_def_id()).is_some() {
99                    codegen_fn_attrs.flags |= CodegenFnAttrFlags::NO_MANGLE;
100                } else {
101                    tcx.dcx()
102                        .span_delayed_bug(*attr_span, "no_mangle should be on a named function");
103                }
104            }
105            AttributeKind::Optimize(optimize, _) => codegen_fn_attrs.optimize = *optimize,
106            AttributeKind::TargetFeature { features, attr_span, was_forced } => {
107                let Some(sig) = tcx.hir_node_by_def_id(did).fn_sig() else {
108                    tcx.dcx().span_delayed_bug(*attr_span, "target_feature applied to non-fn");
109                    continue;
110                };
111                let safe_target_features =
112                    #[allow(non_exhaustive_omitted_patterns)] match sig.header.safety {
    hir::HeaderSafety::SafeTargetFeatures => true,
    _ => false,
}matches!(sig.header.safety, hir::HeaderSafety::SafeTargetFeatures);
113                codegen_fn_attrs.safe_target_features = safe_target_features;
114                if safe_target_features && !was_forced {
115                    if tcx.sess.target.is_like_wasm || tcx.sess.opts.actually_rustdoc {
116                        // The `#[target_feature]` attribute is allowed on
117                        // WebAssembly targets on all functions. Prior to stabilizing
118                        // the `target_feature_11` feature, `#[target_feature]` was
119                        // only permitted on unsafe functions because on most targets
120                        // execution of instructions that are not supported is
121                        // considered undefined behavior. For WebAssembly which is a
122                        // 100% safe target at execution time it's not possible to
123                        // execute undefined instructions, and even if a future
124                        // feature was added in some form for this it would be a
125                        // deterministic trap. There is no undefined behavior when
126                        // executing WebAssembly so `#[target_feature]` is allowed
127                        // on safe functions (but again, only for WebAssembly)
128                        //
129                        // Note that this is also allowed if `actually_rustdoc` so
130                        // if a target is documenting some wasm-specific code then
131                        // it's not spuriously denied.
132                        //
133                        // Now that `#[target_feature]` is permitted on safe functions,
134                        // this exception must still exist for allowing the attribute on
135                        // `main`, `start`, and other functions that are not usually
136                        // allowed.
137                    } else {
138                        check_target_feature_trait_unsafe(tcx, did, *attr_span);
139                    }
140                }
141                from_target_feature_attr(
142                    tcx,
143                    did,
144                    features,
145                    *was_forced,
146                    rust_target_features,
147                    &mut codegen_fn_attrs.target_features,
148                );
149            }
150            AttributeKind::TrackCaller(attr_span) => {
151                let is_closure = tcx.is_closure_like(did.to_def_id());
152
153                if !is_closure
154                    && let Some(fn_sig) = try_fn_sig(tcx, did, *attr_span)
155                    && fn_sig.skip_binder().abi() != ExternAbi::Rust
156                {
157                    tcx.dcx().emit_err(errors::RequiresRustAbi { span: *attr_span });
158                }
159                if is_closure
160                    && !tcx.features().closure_track_caller()
161                    && !attr_span.allows_unstable(sym::closure_track_caller)
162                {
163                    feature_err(
164                        &tcx.sess,
165                        sym::closure_track_caller,
166                        *attr_span,
167                        "`#[track_caller]` on closures is currently unstable",
168                    )
169                    .emit();
170                }
171                codegen_fn_attrs.flags |= CodegenFnAttrFlags::TRACK_CALLER
172            }
173            AttributeKind::Used { used_by, .. } => match used_by {
174                UsedBy::Compiler => codegen_fn_attrs.flags |= CodegenFnAttrFlags::USED_COMPILER,
175                UsedBy::Linker => codegen_fn_attrs.flags |= CodegenFnAttrFlags::USED_LINKER,
176                UsedBy::Default => {
177                    let used_form = if tcx.sess.target.os == Os::Illumos {
178                        // illumos' `ld` doesn't support a section header that would represent
179                        // `#[used(linker)]`, see
180                        // https://github.com/rust-lang/rust/issues/146169. For that target,
181                        // downgrade as if `#[used(compiler)]` was requested and hope for the
182                        // best.
183                        CodegenFnAttrFlags::USED_COMPILER
184                    } else {
185                        CodegenFnAttrFlags::USED_LINKER
186                    };
187                    codegen_fn_attrs.flags |= used_form;
188                }
189            },
190            AttributeKind::FfiConst(_) => codegen_fn_attrs.flags |= CodegenFnAttrFlags::FFI_CONST,
191            AttributeKind::FfiPure(_) => codegen_fn_attrs.flags |= CodegenFnAttrFlags::FFI_PURE,
192            AttributeKind::RustcStdInternalSymbol(_) => {
193                codegen_fn_attrs.flags |= CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL
194            }
195            AttributeKind::Linkage(linkage, span) => {
196                let linkage = Some(*linkage);
197
198                if tcx.is_foreign_item(did) {
199                    codegen_fn_attrs.import_linkage = linkage;
200
201                    if tcx.is_mutable_static(did.into()) {
202                        let mut diag = tcx.dcx().struct_span_err(
203                            *span,
204                            "extern mutable statics are not allowed with `#[linkage]`",
205                        );
206                        diag.note(
207                            "marking the extern static mutable would allow changing which \
208                            symbol the static references rather than make the target of the \
209                            symbol mutable",
210                        );
211                        diag.emit();
212                    }
213                } else {
214                    codegen_fn_attrs.linkage = linkage;
215                }
216            }
217            AttributeKind::Sanitize { span, .. } => {
218                interesting_spans.sanitize = Some(*span);
219            }
220            AttributeKind::RustcObjcClass { classname, .. } => {
221                codegen_fn_attrs.objc_class = Some(*classname);
222            }
223            AttributeKind::RustcObjcSelector { methname, .. } => {
224                codegen_fn_attrs.objc_selector = Some(*methname);
225            }
226            AttributeKind::EiiForeignItem => {
227                codegen_fn_attrs.flags |= CodegenFnAttrFlags::EXTERNALLY_IMPLEMENTABLE_ITEM;
228            }
229            AttributeKind::EiiImpls(impls) => {
230                for i in impls {
231                    let foreign_item = match i.resolution {
232                        EiiImplResolution::Macro(def_id) => {
233                            let Some(extern_item) = {

    #[allow(deprecated)]
    {
        {
            'done:
                {
                for i in tcx.get_all_attrs(def_id) {
                    #[allow(unused_imports)]
                    use rustc_hir::attrs::AttributeKind::*;
                    let i: &rustc_hir::Attribute = i;
                    match i {
                        rustc_hir::Attribute::Parsed(EiiDeclaration(target)) => {
                            break 'done Some(target.foreign_item);
                        }
                        rustc_hir::Attribute::Unparsed(..) =>
                            {}
                            #[deny(unreachable_patterns)]
                            _ => {}
                    }
                }
                None
            }
        }
    }
}find_attr!(tcx, def_id, EiiDeclaration(target) => target.foreign_item
234                            ) else {
235                                tcx.dcx().span_delayed_bug(
236                                    i.span,
237                                    "resolved to something that's not an EII",
238                                );
239                                continue;
240                            };
241                            extern_item
242                        }
243                        EiiImplResolution::Known(decl) => decl.foreign_item,
244                        EiiImplResolution::Error(_eg) => continue,
245                    };
246
247                    // this is to prevent a bug where a single crate defines both the default and explicit implementation
248                    // for an EII. In that case, both of them may be part of the same final object file. I'm not 100% sure
249                    // what happens, either rustc deduplicates the symbol or llvm, or it's random/order-dependent.
250                    // However, the fact that the default one of has weak linkage isn't considered and you sometimes get that
251                    // the default implementation is used while an explicit implementation is given.
252                    if
253                    // if this is a default impl
254                    i.is_default
255                        // iterate over all implementations *in the current crate*
256                        // (this is ok since we generate codegen fn attrs in the local crate)
257                        // if any of them is *not default* then don't emit the alias.
258                        && tcx.externally_implementable_items(LOCAL_CRATE).get(&foreign_item).expect("at least one").1.iter().any(|(_, imp)| !imp.is_default)
259                    {
260                        continue;
261                    }
262
263                    codegen_fn_attrs.foreign_item_symbol_aliases.push((
264                        foreign_item,
265                        if i.is_default { Linkage::LinkOnceAny } else { Linkage::External },
266                        Visibility::Default,
267                    ));
268                    codegen_fn_attrs.flags |= CodegenFnAttrFlags::EXTERNALLY_IMPLEMENTABLE_ITEM;
269                }
270            }
271            AttributeKind::ThreadLocal => {
272                codegen_fn_attrs.flags |= CodegenFnAttrFlags::THREAD_LOCAL
273            }
274            AttributeKind::InstructionSet(instruction_set) => {
275                codegen_fn_attrs.instruction_set = Some(*instruction_set)
276            }
277            AttributeKind::RustcAllocator => {
278                codegen_fn_attrs.flags |= CodegenFnAttrFlags::ALLOCATOR
279            }
280            AttributeKind::RustcDeallocator => {
281                codegen_fn_attrs.flags |= CodegenFnAttrFlags::DEALLOCATOR
282            }
283            AttributeKind::RustcReallocator => {
284                codegen_fn_attrs.flags |= CodegenFnAttrFlags::REALLOCATOR
285            }
286            AttributeKind::RustcAllocatorZeroed => {
287                codegen_fn_attrs.flags |= CodegenFnAttrFlags::ALLOCATOR_ZEROED
288            }
289            AttributeKind::RustcNounwind => {
290                codegen_fn_attrs.flags |= CodegenFnAttrFlags::NEVER_UNWIND
291            }
292            AttributeKind::RustcOffloadKernel => {
293                codegen_fn_attrs.flags |= CodegenFnAttrFlags::OFFLOAD_KERNEL
294            }
295            AttributeKind::PatchableFunctionEntry { prefix, entry } => {
296                codegen_fn_attrs.patchable_function_entry =
297                    Some(PatchableFunctionEntry::from_prefix_and_entry(*prefix, *entry));
298            }
299            _ => {}
300        }
301    }
302
303    interesting_spans
304}
305
306/// Applies overrides for codegen fn attrs. These often have a specific reason why they're necessary.
307/// Please comment why when adding a new one!
308fn apply_overrides(tcx: TyCtxt<'_>, did: LocalDefId, codegen_fn_attrs: &mut CodegenFnAttrs) {
309    // Apply the minimum function alignment here. This ensures that a function's alignment is
310    // determined by the `-C` flags of the crate it is defined in, not the `-C` flags of the crate
311    // it happens to be codegen'd (or const-eval'd) in.
312    codegen_fn_attrs.alignment =
313        Ord::max(codegen_fn_attrs.alignment, tcx.sess.opts.unstable_opts.min_function_alignment);
314
315    // Passed in sanitizer settings are always the default.
316    if !(codegen_fn_attrs.sanitizers == SanitizerFnAttrs::default()) {
    ::core::panicking::panic("assertion failed: codegen_fn_attrs.sanitizers == SanitizerFnAttrs::default()")
};assert!(codegen_fn_attrs.sanitizers == SanitizerFnAttrs::default());
317    // Replace with #[sanitize] value
318    codegen_fn_attrs.sanitizers = tcx.sanitizer_settings_for(did);
319    // On trait methods, inherit the `#[align]` of the trait's method prototype.
320    codegen_fn_attrs.alignment = Ord::max(codegen_fn_attrs.alignment, tcx.inherited_align(did));
321
322    // naked function MUST NOT be inlined! This attribute is required for the rust compiler itself,
323    // but not for the code generation backend because at that point the naked function will just be
324    // a declaration, with a definition provided in global assembly.
325    if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::NAKED) {
326        codegen_fn_attrs.inline = InlineAttr::Never;
327    }
328
329    // #73631: closures inherit `#[target_feature]` annotations
330    //
331    // If this closure is marked `#[inline(always)]`, simply skip adding `#[target_feature]`.
332    //
333    // At this point, `unsafe` has already been checked and `#[target_feature]` only affects codegen.
334    // Due to LLVM limitations, emitting both `#[inline(always)]` and `#[target_feature]` is *unsound*:
335    // the function may be inlined into a caller with fewer target features. Also see
336    // <https://github.com/rust-lang/rust/issues/116573>.
337    //
338    // Using `#[inline(always)]` implies that this closure will most likely be inlined into
339    // its parent function, which effectively inherits the features anyway. Boxing this closure
340    // would result in this closure being compiled without the inherited target features, but this
341    // is probably a poor usage of `#[inline(always)]` and easily avoided by not using the attribute.
342    if tcx.is_closure_like(did.to_def_id()) && codegen_fn_attrs.inline != InlineAttr::Always {
343        let owner_id = tcx.parent(did.to_def_id());
344        if tcx.def_kind(owner_id).has_codegen_attrs() {
345            codegen_fn_attrs
346                .target_features
347                .extend(tcx.codegen_fn_attrs(owner_id).target_features.iter().copied());
348        }
349    }
350
351    // When `no_builtins` is applied at the crate level, we should add the
352    // `no-builtins` attribute to each function to ensure it takes effect in LTO.
353    let no_builtins = {
        'done:
            {
            for i in tcx.hir_krate_attrs() {
                #[allow(unused_imports)]
                use rustc_hir::attrs::AttributeKind::*;
                let i: &rustc_hir::Attribute = i;
                match i {
                    rustc_hir::Attribute::Parsed(NoBuiltins) => {
                        break 'done Some(());
                    }
                    rustc_hir::Attribute::Unparsed(..) =>
                        {}
                        #[deny(unreachable_patterns)]
                        _ => {}
                }
            }
            None
        }
    }.is_some()find_attr!(tcx, crate, NoBuiltins);
354    if no_builtins {
355        codegen_fn_attrs.flags |= CodegenFnAttrFlags::NO_BUILTINS;
356    }
357
358    // inherit track-caller properly
359    if tcx.should_inherit_track_caller(did) {
360        codegen_fn_attrs.flags |= CodegenFnAttrFlags::TRACK_CALLER;
361    }
362
363    // Foreign items by default use no mangling for their symbol name.
364    if tcx.is_foreign_item(did) {
365        codegen_fn_attrs.flags |= CodegenFnAttrFlags::FOREIGN_ITEM;
366
367        // There's a few exceptions to this rule though:
368        if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL) {
369            // * `#[rustc_std_internal_symbol]` mangles the symbol name in a special way
370            //   both for exports and imports through foreign items. This is handled further,
371            //   during symbol mangling logic.
372        } else if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::EXTERNALLY_IMPLEMENTABLE_ITEM)
373        {
374            // * externally implementable items keep their mangled symbol name.
375            //   multiple EIIs can have the same name, so not mangling them would be a bug.
376            //   Implementing an EII does the appropriate name resolution to make sure the implementations
377            //   get the same symbol name as the *mangled* foreign item they refer to so that's all good.
378        } else if codegen_fn_attrs.symbol_name.is_some() {
379            // * This can be overridden with the `#[link_name]` attribute
380        } else {
381            // NOTE: there's one more exception that we cannot apply here. On wasm,
382            // some items cannot be `no_mangle`.
383            // However, we don't have enough information here to determine that.
384            // As such, no_mangle foreign items on wasm that have the same defid as some
385            // import will *still* be mangled despite this.
386            //
387            // if none of the exceptions apply; apply no_mangle
388            codegen_fn_attrs.flags |= CodegenFnAttrFlags::NO_MANGLE;
389        }
390    }
391}
392
393fn check_result(
394    tcx: TyCtxt<'_>,
395    did: LocalDefId,
396    interesting_spans: InterestingAttributeDiagnosticSpans,
397    codegen_fn_attrs: &CodegenFnAttrs,
398) {
399    // If a function uses `#[target_feature]` it can't be inlined into general
400    // purpose functions as they wouldn't have the right target features
401    // enabled. For that reason we also forbid `#[inline(always)]` as it can't be
402    // respected.
403    //
404    // `#[rustc_force_inline]` doesn't need to be prohibited here, only
405    // `#[inline(always)]`, as forced inlining is implemented entirely within
406    // rustc (and so the MIR inliner can do any necessary checks for compatible target
407    // features).
408    //
409    // This sidesteps the LLVM blockers in enabling `target_features` +
410    // `inline(always)` to be used together (see rust-lang/rust#116573 and
411    // llvm/llvm-project#70563).
412    if !codegen_fn_attrs.target_features.is_empty()
413        && #[allow(non_exhaustive_omitted_patterns)] match codegen_fn_attrs.inline {
    InlineAttr::Always => true,
    _ => false,
}matches!(codegen_fn_attrs.inline, InlineAttr::Always)
414        && !tcx.features().target_feature_inline_always()
415        && let Some(span) = interesting_spans.inline
416    {
417        feature_err(
418            tcx.sess,
419            sym::target_feature_inline_always,
420            span,
421            "cannot use `#[inline(always)]` with `#[target_feature]`",
422        )
423        .emit();
424    }
425
426    // warn that inline has no effect when no_sanitize is present
427    if codegen_fn_attrs.sanitizers != SanitizerFnAttrs::default()
428        && codegen_fn_attrs.inline.always()
429        && let (Some(sanitize_span), Some(inline_span)) =
430            (interesting_spans.sanitize, interesting_spans.inline)
431    {
432        let hir_id = tcx.local_def_id_to_hir_id(did);
433        tcx.node_span_lint(lint::builtin::INLINE_NO_SANITIZE, hir_id, sanitize_span, |lint| {
434            lint.primary_message("non-default `sanitize` will have no effect after inlining");
435            lint.span_note(inline_span, "inlining requested here");
436        })
437    }
438
439    // warn for nonblocking async functions, blocks and closures.
440    // This doesn't behave as expected, because the executor can run blocking code without the sanitizer noticing.
441    if codegen_fn_attrs.sanitizers.rtsan_setting == RtsanSetting::Nonblocking
442        && let Some(sanitize_span) = interesting_spans.sanitize
443        // async fn
444        && (tcx.asyncness(did).is_async()
445            // async block
446            || tcx.is_coroutine(did.into())
447            // async closure
448            || (tcx.is_closure_like(did.into())
449                && tcx.hir_node_by_def_id(did).expect_closure().kind
450                    != rustc_hir::ClosureKind::Closure))
451    {
452        let hir_id = tcx.local_def_id_to_hir_id(did);
453        tcx.node_span_lint(
454            lint::builtin::RTSAN_NONBLOCKING_ASYNC,
455            hir_id,
456            sanitize_span,
457            |lint| {
458                lint.primary_message(r#"the async executor can run blocking code, without realtime sanitizer catching it"#);
459            }
460        );
461    }
462
463    // error when specifying link_name together with link_ordinal
464    if let Some(_) = codegen_fn_attrs.symbol_name
465        && let Some(_) = codegen_fn_attrs.link_ordinal
466    {
467        let msg = "cannot use `#[link_name]` with `#[link_ordinal]`";
468        if let Some(span) = interesting_spans.link_ordinal {
469            tcx.dcx().span_err(span, msg);
470        } else {
471            tcx.dcx().err(msg);
472        }
473    }
474
475    if let Some(features) = check_tied_features(
476        tcx.sess,
477        &codegen_fn_attrs
478            .target_features
479            .iter()
480            .map(|features| (features.name.as_str(), true))
481            .collect(),
482    ) {
483        let span = {

    #[allow(deprecated)]
    {
        {
            'done:
                {
                for i in tcx.get_all_attrs(did) {
                    #[allow(unused_imports)]
                    use rustc_hir::attrs::AttributeKind::*;
                    let i: &rustc_hir::Attribute = i;
                    match i {
                        rustc_hir::Attribute::Parsed(TargetFeature {
                            attr_span: span, .. }) => {
                            break 'done Some(*span);
                        }
                        rustc_hir::Attribute::Unparsed(..) =>
                            {}
                            #[deny(unreachable_patterns)]
                            _ => {}
                    }
                }
                None
            }
        }
    }
}find_attr!(tcx, did, TargetFeature{attr_span: span, ..} => *span)
484            .unwrap_or_else(|| tcx.def_span(did));
485
486        tcx.dcx()
487            .create_err(errors::TargetFeatureDisableOrEnable {
488                features,
489                span: Some(span),
490                missing_features: Some(errors::MissingFeatures),
491            })
492            .emit();
493    }
494}
495
496fn handle_lang_items(
497    tcx: TyCtxt<'_>,
498    did: LocalDefId,
499    interesting_spans: &InterestingAttributeDiagnosticSpans,
500    attrs: &[Attribute],
501    codegen_fn_attrs: &mut CodegenFnAttrs,
502) {
503    let lang_item = {
    'done:
        {
        for i in attrs {
            #[allow(unused_imports)]
            use rustc_hir::attrs::AttributeKind::*;
            let i: &rustc_hir::Attribute = i;
            match i {
                rustc_hir::Attribute::Parsed(Lang(lang, _)) => {
                    break 'done Some(lang);
                }
                rustc_hir::Attribute::Unparsed(..) =>
                    {}
                    #[deny(unreachable_patterns)]
                    _ => {}
            }
        }
        None
    }
}find_attr!(attrs, Lang(lang, _) => lang);
504
505    // Weak lang items have the same semantics as "std internal" symbols in the
506    // sense that they're preserved through all our LTO passes and only
507    // strippable by the linker.
508    //
509    // Additionally weak lang items have predetermined symbol names.
510    if let Some(lang_item) = lang_item
511        && let Some(link_name) = lang_item.link_name()
512    {
513        codegen_fn_attrs.flags |= CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL;
514        codegen_fn_attrs.symbol_name = Some(link_name);
515    }
516
517    // error when using no_mangle on a lang item item
518    if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL)
519        && codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::NO_MANGLE)
520    {
521        let mut err = tcx
522            .dcx()
523            .struct_span_err(
524                interesting_spans.no_mangle.unwrap_or_default(),
525                "`#[no_mangle]` cannot be used on internal language items",
526            )
527            .with_note("Rustc requires this item to have a specific mangled name.")
528            .with_span_label(tcx.def_span(did), "should be the internal language item");
529        if let Some(lang_item) = lang_item
530            && let Some(link_name) = lang_item.link_name()
531        {
532            err = err
533                .with_note("If you are trying to prevent mangling to ease debugging, many")
534                .with_note(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("debuggers support a command such as `rbreak {0}` to",
                link_name))
    })format!("debuggers support a command such as `rbreak {link_name}` to"))
535                .with_note(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("match `.*{0}.*` instead of `break {0}` on a specific name",
                link_name))
    })format!(
536                    "match `.*{link_name}.*` instead of `break {link_name}` on a specific name"
537                ))
538        }
539        err.emit();
540    }
541}
542
543/// Generate the [`CodegenFnAttrs`] for an item (identified by the [`LocalDefId`]).
544///
545/// This happens in 4 stages:
546/// - apply built-in attributes that directly translate to codegen attributes.
547/// - handle lang items. These have special codegen attrs applied to them.
548/// - apply overrides, like minimum requirements for alignment and other settings that don't rely directly the built-in attrs on the item.
549///   overrides come after applying built-in attributes since they may only apply when certain attributes were already set in the stage before.
550/// - check that the result is valid. There's various ways in which this may not be the case, such as certain combinations of attrs.
551fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
552    if truecfg!(debug_assertions) {
553        let def_kind = tcx.def_kind(did);
554        if !def_kind.has_codegen_attrs() {
    {
        ::core::panicking::panic_fmt(format_args!("unexpected `def_kind` in `codegen_fn_attrs`: {0:?}",
                def_kind));
    }
};assert!(
555            def_kind.has_codegen_attrs(),
556            "unexpected `def_kind` in `codegen_fn_attrs`: {def_kind:?}",
557        );
558    }
559
560    let mut codegen_fn_attrs = CodegenFnAttrs::new();
561    let attrs = tcx.hir_attrs(tcx.local_def_id_to_hir_id(did));
562
563    let interesting_spans = process_builtin_attrs(tcx, did, attrs, &mut codegen_fn_attrs);
564    handle_lang_items(tcx, did, &interesting_spans, attrs, &mut codegen_fn_attrs);
565    apply_overrides(tcx, did, &mut codegen_fn_attrs);
566    check_result(tcx, did, interesting_spans, &codegen_fn_attrs);
567
568    codegen_fn_attrs
569}
570
571fn sanitizer_settings_for(tcx: TyCtxt<'_>, did: LocalDefId) -> SanitizerFnAttrs {
572    // Backtrack to the crate root.
573    let mut settings = match tcx.opt_local_parent(did) {
574        // Check the parent (recursively).
575        Some(parent) => tcx.sanitizer_settings_for(parent),
576        // We reached the crate root without seeing an attribute, so
577        // there is no sanitizers to exclude.
578        None => SanitizerFnAttrs::default(),
579    };
580
581    // Check for a sanitize annotation directly on this def.
582    if let Some((on_set, off_set, rtsan)) =
583        {

    #[allow(deprecated)]
    {
        {
            'done:
                {
                for i in tcx.get_all_attrs(did) {
                    #[allow(unused_imports)]
                    use rustc_hir::attrs::AttributeKind::*;
                    let i: &rustc_hir::Attribute = i;
                    match i {
                        rustc_hir::Attribute::Parsed(Sanitize {
                            on_set, off_set, rtsan, .. }) => {
                            break 'done Some((on_set, off_set, rtsan));
                        }
                        rustc_hir::Attribute::Unparsed(..) =>
                            {}
                            #[deny(unreachable_patterns)]
                            _ => {}
                    }
                }
                None
            }
        }
    }
}find_attr!(tcx, did, Sanitize {on_set, off_set, rtsan, ..} => (on_set, off_set, rtsan))
584    {
585        // the on set is the set of sanitizers explicitly enabled.
586        // we mask those out since we want the set of disabled sanitizers here
587        settings.disabled &= !*on_set;
588        // the off set is the set of sanitizers explicitly disabled.
589        // we or those in here.
590        settings.disabled |= *off_set;
591        // the on set and off set are distjoint since there's a third option: unset.
592        // a node may not set the sanitizer setting in which case it inherits from parents.
593        // the code above in this function does this backtracking
594
595        // if rtsan was specified here override the parent
596        if let Some(rtsan) = rtsan {
597            settings.rtsan_setting = *rtsan;
598        }
599    }
600    settings
601}
602
603/// Checks if the provided DefId is a method in a trait impl for a trait which has track_caller
604/// applied to the method prototype.
605fn should_inherit_track_caller(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
606    tcx.trait_item_of(def_id).is_some_and(|id| {
607        tcx.codegen_fn_attrs(id).flags.intersects(CodegenFnAttrFlags::TRACK_CALLER)
608    })
609}
610
611/// If the provided DefId is a method in a trait impl, return the value of the `#[align]`
612/// attribute on the method prototype (if any).
613fn inherited_align<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> Option<Align> {
614    tcx.codegen_fn_attrs(tcx.trait_item_of(def_id)?).alignment
615}
616
617/// We now check the #\[rustc_autodiff\] attributes which we generated from the #[autodiff(...)]
618/// macros. There are two forms. The pure one without args to mark primal functions (the functions
619/// being differentiated). The other form is #[rustc_autodiff(Mode, ActivityList)] on top of the
620/// placeholder functions. We wrote the rustc_autodiff attributes ourself, so this should never
621/// panic, unless we introduced a bug when parsing the autodiff macro.
622//FIXME(jdonszelmann): put in the main loop. No need to have two..... :/ Let's do that when we make autodiff parsed.
623pub fn autodiff_attrs(tcx: TyCtxt<'_>, id: DefId) -> Option<AutoDiffAttrs> {
624    #[allow(deprecated)]
625    let attrs = tcx.get_attrs(id, sym::rustc_autodiff);
626
627    let attrs = attrs.filter(|attr| attr.has_name(sym::rustc_autodiff)).collect::<Vec<_>>();
628
629    // check for exactly one autodiff attribute on placeholder functions.
630    // There should only be one, since we generate a new placeholder per ad macro.
631    let attr = match &attrs[..] {
632        [] => return None,
633        [attr] => attr,
634        _ => {
635            ::rustc_middle::util::bug::span_bug_fmt(attrs[1].span(),
    format_args!("cg_ssa: rustc_autodiff should only exist once per source"));span_bug!(attrs[1].span(), "cg_ssa: rustc_autodiff should only exist once per source");
636        }
637    };
638
639    let list = attr.meta_item_list().unwrap_or_default();
640
641    // empty autodiff attribute macros (i.e. `#[autodiff]`) are used to mark source functions
642    if list.is_empty() {
643        return Some(AutoDiffAttrs::source());
644    }
645
646    let [mode, width_meta, input_activities @ .., ret_activity] = &list[..] else {
647        ::rustc_middle::util::bug::span_bug_fmt(attr.span(),
    format_args!("rustc_autodiff attribute must contain mode, width and activities"));span_bug!(attr.span(), "rustc_autodiff attribute must contain mode, width and activities");
648    };
649    let mode = if let MetaItemInner::MetaItem(MetaItem { path: p1, .. }) = mode {
650        p1.segments.first().unwrap().ident
651    } else {
652        ::rustc_middle::util::bug::span_bug_fmt(attr.span(),
    format_args!("rustc_autodiff attribute must contain mode"));span_bug!(attr.span(), "rustc_autodiff attribute must contain mode");
653    };
654
655    // parse mode
656    let mode = match mode.as_str() {
657        "Forward" => DiffMode::Forward,
658        "Reverse" => DiffMode::Reverse,
659        _ => {
660            ::rustc_middle::util::bug::span_bug_fmt(mode.span,
    format_args!("rustc_autodiff attribute contains invalid mode"));span_bug!(mode.span, "rustc_autodiff attribute contains invalid mode");
661        }
662    };
663
664    let width: u32 = match width_meta {
665        MetaItemInner::MetaItem(MetaItem { path: p1, .. }) => {
666            let w = p1.segments.first().unwrap().ident;
667            match w.as_str().parse() {
668                Ok(val) => val,
669                Err(_) => {
670                    ::rustc_middle::util::bug::span_bug_fmt(w.span,
    format_args!("rustc_autodiff width should fit u32"));span_bug!(w.span, "rustc_autodiff width should fit u32");
671                }
672            }
673        }
674        MetaItemInner::Lit(lit) => {
675            if let LitKind::Int(val, _) = lit.kind {
676                match val.get().try_into() {
677                    Ok(val) => val,
678                    Err(_) => {
679                        ::rustc_middle::util::bug::span_bug_fmt(lit.span,
    format_args!("rustc_autodiff width should fit u32"));span_bug!(lit.span, "rustc_autodiff width should fit u32");
680                    }
681                }
682            } else {
683                ::rustc_middle::util::bug::span_bug_fmt(lit.span,
    format_args!("rustc_autodiff width should be an integer"));span_bug!(lit.span, "rustc_autodiff width should be an integer");
684            }
685        }
686    };
687
688    // First read the ret symbol from the attribute
689    let MetaItemInner::MetaItem(MetaItem { path: p1, .. }) = ret_activity else {
690        ::rustc_middle::util::bug::span_bug_fmt(attr.span(),
    format_args!("rustc_autodiff attribute must contain the return activity"));span_bug!(attr.span(), "rustc_autodiff attribute must contain the return activity");
691    };
692    let ret_symbol = p1.segments.first().unwrap().ident;
693
694    // Then parse it into an actual DiffActivity
695    let Ok(ret_activity) = DiffActivity::from_str(ret_symbol.as_str()) else {
696        ::rustc_middle::util::bug::span_bug_fmt(ret_symbol.span,
    format_args!("invalid return activity"));span_bug!(ret_symbol.span, "invalid return activity");
697    };
698
699    // Now parse all the intermediate (input) activities
700    let mut arg_activities: Vec<DiffActivity> = ::alloc::vec::Vec::new()vec![];
701    for arg in input_activities {
702        let arg_symbol = if let MetaItemInner::MetaItem(MetaItem { path: p2, .. }) = arg {
703            match p2.segments.first() {
704                Some(x) => x.ident,
705                None => {
706                    ::rustc_middle::util::bug::span_bug_fmt(arg.span(),
    format_args!("rustc_autodiff attribute must contain the input activity"));span_bug!(
707                        arg.span(),
708                        "rustc_autodiff attribute must contain the input activity"
709                    );
710                }
711            }
712        } else {
713            ::rustc_middle::util::bug::span_bug_fmt(arg.span(),
    format_args!("rustc_autodiff attribute must contain the input activity"));span_bug!(arg.span(), "rustc_autodiff attribute must contain the input activity");
714        };
715
716        match DiffActivity::from_str(arg_symbol.as_str()) {
717            Ok(arg_activity) => arg_activities.push(arg_activity),
718            Err(_) => {
719                ::rustc_middle::util::bug::span_bug_fmt(arg_symbol.span,
    format_args!("invalid input activity"));span_bug!(arg_symbol.span, "invalid input activity");
720            }
721        }
722    }
723
724    Some(AutoDiffAttrs { mode, width, ret_activity, input_activity: arg_activities })
725}
726
727pub(crate) fn provide(providers: &mut Providers) {
728    *providers = Providers {
729        codegen_fn_attrs,
730        should_inherit_track_caller,
731        inherited_align,
732        sanitizer_settings_for,
733        ..*providers
734    };
735}