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, 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
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) = {
    'done:
        {
        for i in tcx.get_all_attrs(def_id) {
            let i: &rustc_hir::Attribute = i;
            match i {
                rustc_hir::Attribute::Parsed(AttributeKind::EiiDeclaration(target))
                    => {
                    break 'done Some(target.foreign_item);
                }
                _ => {}
            }
        }
        None
    }
}find_attr!(
234                                tcx.get_all_attrs(def_id),
235                                AttributeKind::EiiDeclaration(target) => target.foreign_item
236                            ) else {
237                                tcx.dcx().span_delayed_bug(
238                                    i.span,
239                                    "resolved to something that's not an EII",
240                                );
241                                continue;
242                            };
243                            extern_item
244                        }
245                        EiiImplResolution::Known(decl) => decl.foreign_item,
246                        EiiImplResolution::Error(_eg) => continue,
247                    };
248
249                    // this is to prevent a bug where a single crate defines both the default and explicit implementation
250                    // for an EII. In that case, both of them may be part of the same final object file. I'm not 100% sure
251                    // what happens, either rustc deduplicates the symbol or llvm, or it's random/order-dependent.
252                    // However, the fact that the default one of has weak linkage isn't considered and you sometimes get that
253                    // the default implementation is used while an explicit implementation is given.
254                    if
255                    // if this is a default impl
256                    i.is_default
257                        // iterate over all implementations *in the current crate*
258                        // (this is ok since we generate codegen fn attrs in the local crate)
259                        // if any of them is *not default* then don't emit the alias.
260                        && tcx.externally_implementable_items(LOCAL_CRATE).get(&foreign_item).expect("at least one").1.iter().any(|(_, imp)| !imp.is_default)
261                    {
262                        continue;
263                    }
264
265                    codegen_fn_attrs.foreign_item_symbol_aliases.push((
266                        foreign_item,
267                        if i.is_default { Linkage::LinkOnceAny } else { Linkage::External },
268                        Visibility::Default,
269                    ));
270                    codegen_fn_attrs.flags |= CodegenFnAttrFlags::EXTERNALLY_IMPLEMENTABLE_ITEM;
271                }
272            }
273            AttributeKind::ThreadLocal => {
274                codegen_fn_attrs.flags |= CodegenFnAttrFlags::THREAD_LOCAL
275            }
276            AttributeKind::InstructionSet(instruction_set) => {
277                codegen_fn_attrs.instruction_set = Some(*instruction_set)
278            }
279            AttributeKind::RustcAllocator => {
280                codegen_fn_attrs.flags |= CodegenFnAttrFlags::ALLOCATOR
281            }
282            AttributeKind::RustcDeallocator => {
283                codegen_fn_attrs.flags |= CodegenFnAttrFlags::DEALLOCATOR
284            }
285            AttributeKind::RustcReallocator => {
286                codegen_fn_attrs.flags |= CodegenFnAttrFlags::REALLOCATOR
287            }
288            AttributeKind::RustcAllocatorZeroed => {
289                codegen_fn_attrs.flags |= CodegenFnAttrFlags::ALLOCATOR_ZEROED
290            }
291            AttributeKind::RustcNounwind => {
292                codegen_fn_attrs.flags |= CodegenFnAttrFlags::NEVER_UNWIND
293            }
294            AttributeKind::RustcOffloadKernel => {
295                codegen_fn_attrs.flags |= CodegenFnAttrFlags::OFFLOAD_KERNEL
296            }
297            AttributeKind::PatchableFunctionEntry { prefix, entry } => {
298                codegen_fn_attrs.patchable_function_entry =
299                    Some(PatchableFunctionEntry::from_prefix_and_entry(*prefix, *entry));
300            }
301            _ => {}
302        }
303    }
304
305    interesting_spans
306}
307
308/// Applies overrides for codegen fn attrs. These often have a specific reason why they're necessary.
309/// Please comment why when adding a new one!
310fn apply_overrides(tcx: TyCtxt<'_>, did: LocalDefId, codegen_fn_attrs: &mut CodegenFnAttrs) {
311    // Apply the minimum function alignment here. This ensures that a function's alignment is
312    // determined by the `-C` flags of the crate it is defined in, not the `-C` flags of the crate
313    // it happens to be codegen'd (or const-eval'd) in.
314    codegen_fn_attrs.alignment =
315        Ord::max(codegen_fn_attrs.alignment, tcx.sess.opts.unstable_opts.min_function_alignment);
316
317    // Passed in sanitizer settings are always the default.
318    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());
319    // Replace with #[sanitize] value
320    codegen_fn_attrs.sanitizers = tcx.sanitizer_settings_for(did);
321    // On trait methods, inherit the `#[align]` of the trait's method prototype.
322    codegen_fn_attrs.alignment = Ord::max(codegen_fn_attrs.alignment, tcx.inherited_align(did));
323
324    // naked function MUST NOT be inlined! This attribute is required for the rust compiler itself,
325    // but not for the code generation backend because at that point the naked function will just be
326    // a declaration, with a definition provided in global assembly.
327    if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::NAKED) {
328        codegen_fn_attrs.inline = InlineAttr::Never;
329    }
330
331    // #73631: closures inherit `#[target_feature]` annotations
332    //
333    // If this closure is marked `#[inline(always)]`, simply skip adding `#[target_feature]`.
334    //
335    // At this point, `unsafe` has already been checked and `#[target_feature]` only affects codegen.
336    // Due to LLVM limitations, emitting both `#[inline(always)]` and `#[target_feature]` is *unsound*:
337    // the function may be inlined into a caller with fewer target features. Also see
338    // <https://github.com/rust-lang/rust/issues/116573>.
339    //
340    // Using `#[inline(always)]` implies that this closure will most likely be inlined into
341    // its parent function, which effectively inherits the features anyway. Boxing this closure
342    // would result in this closure being compiled without the inherited target features, but this
343    // is probably a poor usage of `#[inline(always)]` and easily avoided by not using the attribute.
344    if tcx.is_closure_like(did.to_def_id()) && codegen_fn_attrs.inline != InlineAttr::Always {
345        let owner_id = tcx.parent(did.to_def_id());
346        if tcx.def_kind(owner_id).has_codegen_attrs() {
347            codegen_fn_attrs
348                .target_features
349                .extend(tcx.codegen_fn_attrs(owner_id).target_features.iter().copied());
350        }
351    }
352
353    // When `no_builtins` is applied at the crate level, we should add the
354    // `no-builtins` attribute to each function to ensure it takes effect in LTO.
355    let crate_attrs = tcx.hir_attrs(rustc_hir::CRATE_HIR_ID);
356    let no_builtins = {
    {
            'done:
                {
                for i in crate_attrs {
                    let i: &rustc_hir::Attribute = i;
                    match i {
                        rustc_hir::Attribute::Parsed(AttributeKind::NoBuiltins) => {
                            break 'done Some(());
                        }
                        _ => {}
                    }
                }
                None
            }
        }.is_some()
}find_attr!(crate_attrs, AttributeKind::NoBuiltins);
357    if no_builtins {
358        codegen_fn_attrs.flags |= CodegenFnAttrFlags::NO_BUILTINS;
359    }
360
361    // inherit track-caller properly
362    if tcx.should_inherit_track_caller(did) {
363        codegen_fn_attrs.flags |= CodegenFnAttrFlags::TRACK_CALLER;
364    }
365
366    // Foreign items by default use no mangling for their symbol name.
367    if tcx.is_foreign_item(did) {
368        codegen_fn_attrs.flags |= CodegenFnAttrFlags::FOREIGN_ITEM;
369
370        // There's a few exceptions to this rule though:
371        if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL) {
372            // * `#[rustc_std_internal_symbol]` mangles the symbol name in a special way
373            //   both for exports and imports through foreign items. This is handled further,
374            //   during symbol mangling logic.
375        } else if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::EXTERNALLY_IMPLEMENTABLE_ITEM)
376        {
377            // * externally implementable items keep their mangled symbol name.
378            //   multiple EIIs can have the same name, so not mangling them would be a bug.
379            //   Implementing an EII does the appropriate name resolution to make sure the implementations
380            //   get the same symbol name as the *mangled* foreign item they refer to so that's all good.
381        } else if codegen_fn_attrs.symbol_name.is_some() {
382            // * This can be overridden with the `#[link_name]` attribute
383        } else {
384            // NOTE: there's one more exception that we cannot apply here. On wasm,
385            // some items cannot be `no_mangle`.
386            // However, we don't have enough information here to determine that.
387            // As such, no_mangle foreign items on wasm that have the same defid as some
388            // import will *still* be mangled despite this.
389            //
390            // if none of the exceptions apply; apply no_mangle
391            codegen_fn_attrs.flags |= CodegenFnAttrFlags::NO_MANGLE;
392        }
393    }
394}
395
396fn check_result(
397    tcx: TyCtxt<'_>,
398    did: LocalDefId,
399    interesting_spans: InterestingAttributeDiagnosticSpans,
400    codegen_fn_attrs: &CodegenFnAttrs,
401) {
402    // If a function uses `#[target_feature]` it can't be inlined into general
403    // purpose functions as they wouldn't have the right target features
404    // enabled. For that reason we also forbid `#[inline(always)]` as it can't be
405    // respected.
406    //
407    // `#[rustc_force_inline]` doesn't need to be prohibited here, only
408    // `#[inline(always)]`, as forced inlining is implemented entirely within
409    // rustc (and so the MIR inliner can do any necessary checks for compatible target
410    // features).
411    //
412    // This sidesteps the LLVM blockers in enabling `target_features` +
413    // `inline(always)` to be used together (see rust-lang/rust#116573 and
414    // llvm/llvm-project#70563).
415    if !codegen_fn_attrs.target_features.is_empty()
416        && #[allow(non_exhaustive_omitted_patterns)] match codegen_fn_attrs.inline {
    InlineAttr::Always => true,
    _ => false,
}matches!(codegen_fn_attrs.inline, InlineAttr::Always)
417        && !tcx.features().target_feature_inline_always()
418        && let Some(span) = interesting_spans.inline
419    {
420        feature_err(
421            tcx.sess,
422            sym::target_feature_inline_always,
423            span,
424            "cannot use `#[inline(always)]` with `#[target_feature]`",
425        )
426        .emit();
427    }
428
429    // warn that inline has no effect when no_sanitize is present
430    if codegen_fn_attrs.sanitizers != SanitizerFnAttrs::default()
431        && codegen_fn_attrs.inline.always()
432        && let (Some(sanitize_span), Some(inline_span)) =
433            (interesting_spans.sanitize, interesting_spans.inline)
434    {
435        let hir_id = tcx.local_def_id_to_hir_id(did);
436        tcx.node_span_lint(lint::builtin::INLINE_NO_SANITIZE, hir_id, sanitize_span, |lint| {
437            lint.primary_message("non-default `sanitize` will have no effect after inlining");
438            lint.span_note(inline_span, "inlining requested here");
439        })
440    }
441
442    // warn for nonblocking async functions, blocks and closures.
443    // This doesn't behave as expected, because the executor can run blocking code without the sanitizer noticing.
444    if codegen_fn_attrs.sanitizers.rtsan_setting == RtsanSetting::Nonblocking
445        && let Some(sanitize_span) = interesting_spans.sanitize
446        // async fn
447        && (tcx.asyncness(did).is_async()
448            // async block
449            || tcx.is_coroutine(did.into())
450            // async closure
451            || (tcx.is_closure_like(did.into())
452                && tcx.hir_node_by_def_id(did).expect_closure().kind
453                    != rustc_hir::ClosureKind::Closure))
454    {
455        let hir_id = tcx.local_def_id_to_hir_id(did);
456        tcx.node_span_lint(
457            lint::builtin::RTSAN_NONBLOCKING_ASYNC,
458            hir_id,
459            sanitize_span,
460            |lint| {
461                lint.primary_message(r#"the async executor can run blocking code, without realtime sanitizer catching it"#);
462            }
463        );
464    }
465
466    // error when specifying link_name together with link_ordinal
467    if let Some(_) = codegen_fn_attrs.symbol_name
468        && let Some(_) = codegen_fn_attrs.link_ordinal
469    {
470        let msg = "cannot use `#[link_name]` with `#[link_ordinal]`";
471        if let Some(span) = interesting_spans.link_ordinal {
472            tcx.dcx().span_err(span, msg);
473        } else {
474            tcx.dcx().err(msg);
475        }
476    }
477
478    if let Some(features) = check_tied_features(
479        tcx.sess,
480        &codegen_fn_attrs
481            .target_features
482            .iter()
483            .map(|features| (features.name.as_str(), true))
484            .collect(),
485    ) {
486        let span =
487            {
    'done:
        {
        for i in tcx.get_all_attrs(did) {
            let i: &rustc_hir::Attribute = i;
            match i {
                rustc_hir::Attribute::Parsed(AttributeKind::TargetFeature {
                    attr_span: span, .. }) => {
                    break 'done Some(*span);
                }
                _ => {}
            }
        }
        None
    }
}find_attr!(tcx.get_all_attrs(did), AttributeKind::TargetFeature{attr_span: span, ..} => *span)
488                .unwrap_or_else(|| tcx.def_span(did));
489
490        tcx.dcx()
491            .create_err(errors::TargetFeatureDisableOrEnable {
492                features,
493                span: Some(span),
494                missing_features: Some(errors::MissingFeatures),
495            })
496            .emit();
497    }
498}
499
500fn handle_lang_items(
501    tcx: TyCtxt<'_>,
502    did: LocalDefId,
503    interesting_spans: &InterestingAttributeDiagnosticSpans,
504    attrs: &[Attribute],
505    codegen_fn_attrs: &mut CodegenFnAttrs,
506) {
507    let lang_item = lang_items::extract(attrs).and_then(|(name, _)| LangItem::from_name(name));
508
509    // Weak lang items have the same semantics as "std internal" symbols in the
510    // sense that they're preserved through all our LTO passes and only
511    // strippable by the linker.
512    //
513    // Additionally weak lang items have predetermined symbol names.
514    if let Some(lang_item) = lang_item
515        && let Some(link_name) = lang_item.link_name()
516    {
517        codegen_fn_attrs.flags |= CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL;
518        codegen_fn_attrs.symbol_name = Some(link_name);
519    }
520
521    // error when using no_mangle on a lang item item
522    if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL)
523        && codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::NO_MANGLE)
524    {
525        let mut err = tcx
526            .dcx()
527            .struct_span_err(
528                interesting_spans.no_mangle.unwrap_or_default(),
529                "`#[no_mangle]` cannot be used on internal language items",
530            )
531            .with_note("Rustc requires this item to have a specific mangled name.")
532            .with_span_label(tcx.def_span(did), "should be the internal language item");
533        if let Some(lang_item) = lang_item
534            && let Some(link_name) = lang_item.link_name()
535        {
536            err = err
537                .with_note("If you are trying to prevent mangling to ease debugging, many")
538                .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"))
539                .with_note(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("match `.*{0}.*` instead of `break {0}` on a specific name",
                link_name))
    })format!(
540                    "match `.*{link_name}.*` instead of `break {link_name}` on a specific name"
541                ))
542        }
543        err.emit();
544    }
545}
546
547/// Generate the [`CodegenFnAttrs`] for an item (identified by the [`LocalDefId`]).
548///
549/// This happens in 4 stages:
550/// - apply built-in attributes that directly translate to codegen attributes.
551/// - handle lang items. These have special codegen attrs applied to them.
552/// - apply overrides, like minimum requirements for alignment and other settings that don't rely directly the built-in attrs on the item.
553///   overrides come after applying built-in attributes since they may only apply when certain attributes were already set in the stage before.
554/// - check that the result is valid. There's various ways in which this may not be the case, such as certain combinations of attrs.
555fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
556    if truecfg!(debug_assertions) {
557        let def_kind = tcx.def_kind(did);
558        if !def_kind.has_codegen_attrs() {
    {
        ::core::panicking::panic_fmt(format_args!("unexpected `def_kind` in `codegen_fn_attrs`: {0:?}",
                def_kind));
    }
};assert!(
559            def_kind.has_codegen_attrs(),
560            "unexpected `def_kind` in `codegen_fn_attrs`: {def_kind:?}",
561        );
562    }
563
564    let mut codegen_fn_attrs = CodegenFnAttrs::new();
565    let attrs = tcx.hir_attrs(tcx.local_def_id_to_hir_id(did));
566
567    let interesting_spans = process_builtin_attrs(tcx, did, attrs, &mut codegen_fn_attrs);
568    handle_lang_items(tcx, did, &interesting_spans, attrs, &mut codegen_fn_attrs);
569    apply_overrides(tcx, did, &mut codegen_fn_attrs);
570    check_result(tcx, did, interesting_spans, &codegen_fn_attrs);
571
572    codegen_fn_attrs
573}
574
575fn sanitizer_settings_for(tcx: TyCtxt<'_>, did: LocalDefId) -> SanitizerFnAttrs {
576    // Backtrack to the crate root.
577    let mut settings = match tcx.opt_local_parent(did) {
578        // Check the parent (recursively).
579        Some(parent) => tcx.sanitizer_settings_for(parent),
580        // We reached the crate root without seeing an attribute, so
581        // there is no sanitizers to exclude.
582        None => SanitizerFnAttrs::default(),
583    };
584
585    // Check for a sanitize annotation directly on this def.
586    if let Some((on_set, off_set, rtsan)) = {
    'done:
        {
        for i in tcx.get_all_attrs(did) {
            let i: &rustc_hir::Attribute = i;
            match i {
                rustc_hir::Attribute::Parsed(AttributeKind::Sanitize {
                    on_set, off_set, rtsan, .. }) => {
                    break 'done Some((on_set, off_set, rtsan));
                }
                _ => {}
            }
        }
        None
    }
}find_attr!(tcx.get_all_attrs(did), AttributeKind::Sanitize {on_set, off_set, rtsan, ..} => (on_set, off_set, rtsan))
587    {
588        // the on set is the set of sanitizers explicitly enabled.
589        // we mask those out since we want the set of disabled sanitizers here
590        settings.disabled &= !*on_set;
591        // the off set is the set of sanitizers explicitly disabled.
592        // we or those in here.
593        settings.disabled |= *off_set;
594        // the on set and off set are distjoint since there's a third option: unset.
595        // a node may not set the sanitizer setting in which case it inherits from parents.
596        // the code above in this function does this backtracking
597
598        // if rtsan was specified here override the parent
599        if let Some(rtsan) = rtsan {
600            settings.rtsan_setting = *rtsan;
601        }
602    }
603    settings
604}
605
606/// Checks if the provided DefId is a method in a trait impl for a trait which has track_caller
607/// applied to the method prototype.
608fn should_inherit_track_caller(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
609    tcx.trait_item_of(def_id).is_some_and(|id| {
610        tcx.codegen_fn_attrs(id).flags.intersects(CodegenFnAttrFlags::TRACK_CALLER)
611    })
612}
613
614/// If the provided DefId is a method in a trait impl, return the value of the `#[align]`
615/// attribute on the method prototype (if any).
616fn inherited_align<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> Option<Align> {
617    tcx.codegen_fn_attrs(tcx.trait_item_of(def_id)?).alignment
618}
619
620/// We now check the #\[rustc_autodiff\] attributes which we generated from the #[autodiff(...)]
621/// macros. There are two forms. The pure one without args to mark primal functions (the functions
622/// being differentiated). The other form is #[rustc_autodiff(Mode, ActivityList)] on top of the
623/// placeholder functions. We wrote the rustc_autodiff attributes ourself, so this should never
624/// panic, unless we introduced a bug when parsing the autodiff macro.
625//FIXME(jdonszelmann): put in the main loop. No need to have two..... :/ Let's do that when we make autodiff parsed.
626pub fn autodiff_attrs(tcx: TyCtxt<'_>, id: DefId) -> Option<AutoDiffAttrs> {
627    let attrs = tcx.get_attrs(id, sym::rustc_autodiff);
628
629    let attrs = attrs.filter(|attr| attr.has_name(sym::rustc_autodiff)).collect::<Vec<_>>();
630
631    // check for exactly one autodiff attribute on placeholder functions.
632    // There should only be one, since we generate a new placeholder per ad macro.
633    let attr = match &attrs[..] {
634        [] => return None,
635        [attr] => attr,
636        _ => {
637            ::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");
638        }
639    };
640
641    let list = attr.meta_item_list().unwrap_or_default();
642
643    // empty autodiff attribute macros (i.e. `#[autodiff]`) are used to mark source functions
644    if list.is_empty() {
645        return Some(AutoDiffAttrs::source());
646    }
647
648    let [mode, width_meta, input_activities @ .., ret_activity] = &list[..] else {
649        ::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");
650    };
651    let mode = if let MetaItemInner::MetaItem(MetaItem { path: p1, .. }) = mode {
652        p1.segments.first().unwrap().ident
653    } else {
654        ::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");
655    };
656
657    // parse mode
658    let mode = match mode.as_str() {
659        "Forward" => DiffMode::Forward,
660        "Reverse" => DiffMode::Reverse,
661        _ => {
662            ::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");
663        }
664    };
665
666    let width: u32 = match width_meta {
667        MetaItemInner::MetaItem(MetaItem { path: p1, .. }) => {
668            let w = p1.segments.first().unwrap().ident;
669            match w.as_str().parse() {
670                Ok(val) => val,
671                Err(_) => {
672                    ::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");
673                }
674            }
675        }
676        MetaItemInner::Lit(lit) => {
677            if let LitKind::Int(val, _) = lit.kind {
678                match val.get().try_into() {
679                    Ok(val) => val,
680                    Err(_) => {
681                        ::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");
682                    }
683                }
684            } else {
685                ::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");
686            }
687        }
688    };
689
690    // First read the ret symbol from the attribute
691    let MetaItemInner::MetaItem(MetaItem { path: p1, .. }) = ret_activity else {
692        ::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");
693    };
694    let ret_symbol = p1.segments.first().unwrap().ident;
695
696    // Then parse it into an actual DiffActivity
697    let Ok(ret_activity) = DiffActivity::from_str(ret_symbol.as_str()) else {
698        ::rustc_middle::util::bug::span_bug_fmt(ret_symbol.span,
    format_args!("invalid return activity"));span_bug!(ret_symbol.span, "invalid return activity");
699    };
700
701    // Now parse all the intermediate (input) activities
702    let mut arg_activities: Vec<DiffActivity> = ::alloc::vec::Vec::new()vec![];
703    for arg in input_activities {
704        let arg_symbol = if let MetaItemInner::MetaItem(MetaItem { path: p2, .. }) = arg {
705            match p2.segments.first() {
706                Some(x) => x.ident,
707                None => {
708                    ::rustc_middle::util::bug::span_bug_fmt(arg.span(),
    format_args!("rustc_autodiff attribute must contain the input activity"));span_bug!(
709                        arg.span(),
710                        "rustc_autodiff attribute must contain the input activity"
711                    );
712                }
713            }
714        } else {
715            ::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");
716        };
717
718        match DiffActivity::from_str(arg_symbol.as_str()) {
719            Ok(arg_activity) => arg_activities.push(arg_activity),
720            Err(_) => {
721                ::rustc_middle::util::bug::span_bug_fmt(arg_symbol.span,
    format_args!("invalid input activity"));span_bug!(arg_symbol.span, "invalid input activity");
722            }
723        }
724    }
725
726    Some(AutoDiffAttrs { mode, width, ret_activity, input_activity: arg_activities })
727}
728
729pub(crate) fn provide(providers: &mut Providers) {
730    *providers = Providers {
731        codegen_fn_attrs,
732        should_inherit_track_caller,
733        inherited_align,
734        sanitizer_settings_for,
735        ..*providers
736    };
737}