Skip to main content

rustc_codegen_ssa/
codegen_attrs.rs

1use rustc_abi::{Align, ExternAbi};
2use rustc_hir::attrs::{
3    AttributeKind, EiiImplResolution, InlineAttr, Linkage, RtsanSetting, UsedBy,
4};
5use rustc_hir::def::DefKind;
6use rustc_hir::def_id::{DefId, LOCAL_CRATE, LocalDefId};
7use rustc_hir::{self as hir, Attribute, find_attr};
8use rustc_macros::Diagnostic;
9use rustc_middle::middle::codegen_fn_attrs::{
10    CodegenFnAttrFlags, CodegenFnAttrs, PatchableFunctionEntry, SanitizerFnAttrs,
11};
12use rustc_middle::mir::mono::Visibility;
13use rustc_middle::query::Providers;
14use rustc_middle::ty::{self as ty, TyCtxt};
15use rustc_session::lint;
16use rustc_session::parse::feature_err;
17use rustc_span::{Span, sym};
18use rustc_target::spec::Os;
19
20use crate::errors;
21use crate::target_features::{
22    check_target_feature_trait_unsafe, check_tied_features, from_target_feature_attr,
23};
24
25/// In some cases, attributes are only valid on functions, but it's the `check_attr`
26/// pass that checks that they aren't used anywhere else, rather than this module.
27/// In these cases, we bail from performing further checks that are only meaningful for
28/// functions (such as calling `fn_sig`, which ICEs if given a non-function). We also
29/// report a delayed bug, just in case `check_attr` isn't doing its job.
30fn try_fn_sig<'tcx>(
31    tcx: TyCtxt<'tcx>,
32    did: LocalDefId,
33    attr_span: Span,
34) -> Option<ty::EarlyBinder<'tcx, ty::PolyFnSig<'tcx>>> {
35    use DefKind::*;
36
37    let def_kind = tcx.def_kind(did);
38    if let Fn | AssocFn | Variant | Ctor(..) = def_kind {
39        Some(tcx.fn_sig(did))
40    } else {
41        tcx.dcx().span_delayed_bug(attr_span, "this attribute can only be applied to functions");
42        None
43    }
44}
45
46/// Spans that are collected when processing built-in attributes,
47/// that are useful for emitting diagnostics later.
48#[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)]
49struct InterestingAttributeDiagnosticSpans {
50    link_ordinal: Option<Span>,
51    sanitize: Option<Span>,
52    inline: Option<Span>,
53    no_mangle: Option<Span>,
54}
55
56/// Process the builtin attrs ([`hir::Attribute`]) on the item.
57/// Many of them directly translate to codegen attrs.
58fn process_builtin_attrs(
59    tcx: TyCtxt<'_>,
60    did: LocalDefId,
61    attrs: &[Attribute],
62    codegen_fn_attrs: &mut CodegenFnAttrs,
63) -> InterestingAttributeDiagnosticSpans {
64    let mut interesting_spans = InterestingAttributeDiagnosticSpans::default();
65    let rust_target_features = tcx.rust_target_features(LOCAL_CRATE);
66
67    let parsed_attrs = attrs
68        .iter()
69        .filter_map(|attr| if let hir::Attribute::Parsed(attr) = attr { Some(attr) } else { None });
70    for attr in parsed_attrs {
71        match attr {
72            AttributeKind::Cold(_) => codegen_fn_attrs.flags |= CodegenFnAttrFlags::COLD,
73            AttributeKind::ExportName { name, .. } => codegen_fn_attrs.symbol_name = Some(*name),
74            AttributeKind::Inline(inline, span) => {
75                codegen_fn_attrs.inline = *inline;
76                interesting_spans.inline = Some(*span);
77            }
78            AttributeKind::Naked(_) => codegen_fn_attrs.flags |= CodegenFnAttrFlags::NAKED,
79            AttributeKind::RustcAlign { align, .. } => codegen_fn_attrs.alignment = Some(*align),
80            AttributeKind::LinkName { name, .. } => {
81                // FIXME Remove check for foreign functions once #[link_name] on non-foreign
82                // functions is a hard error
83                if tcx.is_foreign_item(did) {
84                    codegen_fn_attrs.symbol_name = Some(*name);
85                }
86            }
87            AttributeKind::LinkOrdinal { ordinal, span } => {
88                codegen_fn_attrs.link_ordinal = Some(*ordinal);
89                interesting_spans.link_ordinal = Some(*span);
90            }
91            AttributeKind::LinkSection { name, .. } => codegen_fn_attrs.link_section = Some(*name),
92            AttributeKind::NoMangle(attr_span) => {
93                interesting_spans.no_mangle = Some(*attr_span);
94                if tcx.opt_item_name(did.to_def_id()).is_some() {
95                    codegen_fn_attrs.flags |= CodegenFnAttrFlags::NO_MANGLE;
96                } else {
97                    tcx.dcx()
98                        .span_delayed_bug(*attr_span, "no_mangle should be on a named function");
99                }
100            }
101            AttributeKind::Optimize(optimize, _) => codegen_fn_attrs.optimize = *optimize,
102            AttributeKind::TargetFeature { features, attr_span, was_forced } => {
103                let Some(sig) = tcx.hir_node_by_def_id(did).fn_sig() else {
104                    tcx.dcx().span_delayed_bug(*attr_span, "target_feature applied to non-fn");
105                    continue;
106                };
107                let safe_target_features =
108                    #[allow(non_exhaustive_omitted_patterns)] match sig.header.safety {
    hir::HeaderSafety::SafeTargetFeatures => true,
    _ => false,
}matches!(sig.header.safety, hir::HeaderSafety::SafeTargetFeatures);
109                codegen_fn_attrs.safe_target_features = safe_target_features;
110                if safe_target_features && !was_forced {
111                    if tcx.sess.target.is_like_wasm || tcx.sess.opts.actually_rustdoc {
112                        // The `#[target_feature]` attribute is allowed on
113                        // WebAssembly targets on all functions. Prior to stabilizing
114                        // the `target_feature_11` feature, `#[target_feature]` was
115                        // only permitted on unsafe functions because on most targets
116                        // execution of instructions that are not supported is
117                        // considered undefined behavior. For WebAssembly which is a
118                        // 100% safe target at execution time it's not possible to
119                        // execute undefined instructions, and even if a future
120                        // feature was added in some form for this it would be a
121                        // deterministic trap. There is no undefined behavior when
122                        // executing WebAssembly so `#[target_feature]` is allowed
123                        // on safe functions (but again, only for WebAssembly)
124                        //
125                        // Note that this is also allowed if `actually_rustdoc` so
126                        // if a target is documenting some wasm-specific code then
127                        // it's not spuriously denied.
128                        //
129                        // Now that `#[target_feature]` is permitted on safe functions,
130                        // this exception must still exist for allowing the attribute on
131                        // `main`, `start`, and other functions that are not usually
132                        // allowed.
133                    } else {
134                        check_target_feature_trait_unsafe(tcx, did, *attr_span);
135                    }
136                }
137                from_target_feature_attr(
138                    tcx,
139                    did,
140                    features,
141                    *was_forced,
142                    rust_target_features,
143                    &mut codegen_fn_attrs.target_features,
144                );
145            }
146            AttributeKind::TrackCaller(attr_span) => {
147                let is_closure = tcx.is_closure_like(did.to_def_id());
148
149                if !is_closure
150                    && let Some(fn_sig) = try_fn_sig(tcx, did, *attr_span)
151                    && fn_sig.skip_binder().abi() != ExternAbi::Rust
152                {
153                    // This error is already reported in `rustc_ast_passes/src/ast_validation.rs`.
154                    tcx.dcx().delayed_bug("`#[track_caller]` requires the Rust ABI");
155                }
156                if is_closure
157                    && !tcx.features().closure_track_caller()
158                    && !attr_span.allows_unstable(sym::closure_track_caller)
159                {
160                    feature_err(
161                        &tcx.sess,
162                        sym::closure_track_caller,
163                        *attr_span,
164                        "`#[track_caller]` on closures is currently unstable",
165                    )
166                    .emit();
167                }
168                codegen_fn_attrs.flags |= CodegenFnAttrFlags::TRACK_CALLER
169            }
170            AttributeKind::Used { used_by, .. } => match used_by {
171                UsedBy::Compiler => codegen_fn_attrs.flags |= CodegenFnAttrFlags::USED_COMPILER,
172                UsedBy::Linker => codegen_fn_attrs.flags |= CodegenFnAttrFlags::USED_LINKER,
173                UsedBy::Default => {
174                    let used_form = if tcx.sess.target.os == Os::Illumos {
175                        // illumos' `ld` doesn't support a section header that would represent
176                        // `#[used(linker)]`, see
177                        // https://github.com/rust-lang/rust/issues/146169. For that target,
178                        // downgrade as if `#[used(compiler)]` was requested and hope for the
179                        // best.
180                        CodegenFnAttrFlags::USED_COMPILER
181                    } else {
182                        CodegenFnAttrFlags::USED_LINKER
183                    };
184                    codegen_fn_attrs.flags |= used_form;
185                }
186            },
187            AttributeKind::FfiConst(_) => codegen_fn_attrs.flags |= CodegenFnAttrFlags::FFI_CONST,
188            AttributeKind::FfiPure(_) => codegen_fn_attrs.flags |= CodegenFnAttrFlags::FFI_PURE,
189            AttributeKind::RustcStdInternalSymbol(_) => {
190                codegen_fn_attrs.flags |= CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL
191            }
192            AttributeKind::Linkage(linkage, span) => {
193                let linkage = Some(*linkage);
194
195                if tcx.is_foreign_item(did) {
196                    codegen_fn_attrs.import_linkage = linkage;
197
198                    if tcx.is_mutable_static(did.into()) {
199                        let mut diag = tcx.dcx().struct_span_err(
200                            *span,
201                            "extern mutable statics are not allowed with `#[linkage]`",
202                        );
203                        diag.note(
204                            "marking the extern static mutable would allow changing which \
205                            symbol the static references rather than make the target of the \
206                            symbol mutable",
207                        );
208                        diag.emit();
209                    }
210                } else {
211                    codegen_fn_attrs.linkage = linkage;
212                }
213            }
214            AttributeKind::Sanitize { span, .. } => {
215                interesting_spans.sanitize = Some(*span);
216            }
217            AttributeKind::RustcObjcClass { classname, .. } => {
218                codegen_fn_attrs.objc_class = Some(*classname);
219            }
220            AttributeKind::RustcObjcSelector { methname, .. } => {
221                codegen_fn_attrs.objc_selector = Some(*methname);
222            }
223            AttributeKind::RustcEiiForeignItem => {
224                codegen_fn_attrs.flags |= CodegenFnAttrFlags::EXTERNALLY_IMPLEMENTABLE_ITEM;
225            }
226            AttributeKind::EiiImpls(impls) => {
227                for i in impls {
228                    let foreign_item = match i.resolution {
229                        EiiImplResolution::Macro(def_id) => {
230                            let Some(extern_item) = {
    {
        'done:
            {
            for i in ::rustc_hir::attrs::HasAttrs::get_attrs(def_id, &tcx) {
                #[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
231                            ) else {
232                                tcx.dcx().span_delayed_bug(
233                                    i.span,
234                                    "resolved to something that's not an EII",
235                                );
236                                continue;
237                            };
238                            extern_item
239                        }
240                        EiiImplResolution::Known(decl) => decl.foreign_item,
241                        EiiImplResolution::Error(_eg) => continue,
242                    };
243
244                    // this is to prevent a bug where a single crate defines both the default and explicit implementation
245                    // for an EII. In that case, both of them may be part of the same final object file. I'm not 100% sure
246                    // what happens, either rustc deduplicates the symbol or llvm, or it's random/order-dependent.
247                    // However, the fact that the default one of has weak linkage isn't considered and you sometimes get that
248                    // the default implementation is used while an explicit implementation is given.
249                    if
250                    // if this is a default impl
251                    i.is_default
252                        // iterate over all implementations *in the current crate*
253                        // (this is ok since we generate codegen fn attrs in the local crate)
254                        // if any of them is *not default* then don't emit the alias.
255                        && tcx.externally_implementable_items(LOCAL_CRATE).get(&foreign_item).expect("at least one").1.iter().any(|(_, imp)| !imp.is_default)
256                    {
257                        continue;
258                    }
259
260                    codegen_fn_attrs.foreign_item_symbol_aliases.push((
261                        foreign_item,
262                        if i.is_default { Linkage::LinkOnceAny } else { Linkage::External },
263                        Visibility::Default,
264                    ));
265                    codegen_fn_attrs.flags |= CodegenFnAttrFlags::EXTERNALLY_IMPLEMENTABLE_ITEM;
266                }
267            }
268            AttributeKind::ThreadLocal => {
269                codegen_fn_attrs.flags |= CodegenFnAttrFlags::THREAD_LOCAL
270            }
271            AttributeKind::InstructionSet(instruction_set) => {
272                codegen_fn_attrs.instruction_set = Some(*instruction_set)
273            }
274            AttributeKind::RustcAllocator => {
275                codegen_fn_attrs.flags |= CodegenFnAttrFlags::ALLOCATOR
276            }
277            AttributeKind::RustcDeallocator => {
278                codegen_fn_attrs.flags |= CodegenFnAttrFlags::DEALLOCATOR
279            }
280            AttributeKind::RustcReallocator => {
281                codegen_fn_attrs.flags |= CodegenFnAttrFlags::REALLOCATOR
282            }
283            AttributeKind::RustcAllocatorZeroed => {
284                codegen_fn_attrs.flags |= CodegenFnAttrFlags::ALLOCATOR_ZEROED
285            }
286            AttributeKind::RustcNounwind => {
287                codegen_fn_attrs.flags |= CodegenFnAttrFlags::NEVER_UNWIND
288            }
289            AttributeKind::RustcOffloadKernel => {
290                codegen_fn_attrs.flags |= CodegenFnAttrFlags::OFFLOAD_KERNEL
291            }
292            AttributeKind::PatchableFunctionEntry { prefix, entry } => {
293                codegen_fn_attrs.patchable_function_entry =
294                    Some(PatchableFunctionEntry::from_prefix_and_entry(*prefix, *entry));
295            }
296            _ => {}
297        }
298    }
299
300    interesting_spans
301}
302
303/// Applies overrides for codegen fn attrs. These often have a specific reason why they're necessary.
304/// Please comment why when adding a new one!
305fn apply_overrides(tcx: TyCtxt<'_>, did: LocalDefId, codegen_fn_attrs: &mut CodegenFnAttrs) {
306    // Apply the minimum function alignment here. This ensures that a function's alignment is
307    // determined by the `-C` flags of the crate it is defined in, not the `-C` flags of the crate
308    // it happens to be codegen'd (or const-eval'd) in.
309    codegen_fn_attrs.alignment =
310        Ord::max(codegen_fn_attrs.alignment, tcx.sess.opts.unstable_opts.min_function_alignment);
311
312    // Passed in sanitizer settings are always the default.
313    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());
314    // Replace with #[sanitize] value
315    codegen_fn_attrs.sanitizers = tcx.sanitizer_settings_for(did);
316    // On trait methods, inherit the `#[align]` of the trait's method prototype.
317    codegen_fn_attrs.alignment = Ord::max(codegen_fn_attrs.alignment, tcx.inherited_align(did));
318
319    // naked function MUST NOT be inlined! This attribute is required for the rust compiler itself,
320    // but not for the code generation backend because at that point the naked function will just be
321    // a declaration, with a definition provided in global assembly.
322    if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::NAKED) {
323        codegen_fn_attrs.inline = InlineAttr::Never;
324    }
325
326    // #73631: closures inherit `#[target_feature]` annotations
327    //
328    // If this closure is marked `#[inline(always)]`, simply skip adding `#[target_feature]`.
329    //
330    // At this point, `unsafe` has already been checked and `#[target_feature]` only affects codegen.
331    // Due to LLVM limitations, emitting both `#[inline(always)]` and `#[target_feature]` is *unsound*:
332    // the function may be inlined into a caller with fewer target features. Also see
333    // <https://github.com/rust-lang/rust/issues/116573>.
334    //
335    // Using `#[inline(always)]` implies that this closure will most likely be inlined into
336    // its parent function, which effectively inherits the features anyway. Boxing this closure
337    // would result in this closure being compiled without the inherited target features, but this
338    // is probably a poor usage of `#[inline(always)]` and easily avoided by not using the attribute.
339    if tcx.is_closure_like(did.to_def_id()) && codegen_fn_attrs.inline != InlineAttr::Always {
340        let owner_id = tcx.parent(did.to_def_id());
341        if tcx.def_kind(owner_id).has_codegen_attrs() {
342            codegen_fn_attrs
343                .target_features
344                .extend(tcx.codegen_fn_attrs(owner_id).target_features.iter().copied());
345        }
346    }
347
348    // When `no_builtins` is applied at the crate level, we should add the
349    // `no-builtins` attribute to each function to ensure it takes effect in LTO.
350    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);
351    if no_builtins {
352        codegen_fn_attrs.flags |= CodegenFnAttrFlags::NO_BUILTINS;
353    }
354
355    // inherit track-caller properly
356    if tcx.should_inherit_track_caller(did) {
357        codegen_fn_attrs.flags |= CodegenFnAttrFlags::TRACK_CALLER;
358    }
359
360    // Foreign items by default use no mangling for their symbol name.
361    if tcx.is_foreign_item(did) {
362        codegen_fn_attrs.flags |= CodegenFnAttrFlags::FOREIGN_ITEM;
363
364        // There's a few exceptions to this rule though:
365        if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL) {
366            // * `#[rustc_std_internal_symbol]` mangles the symbol name in a special way
367            //   both for exports and imports through foreign items. This is handled further,
368            //   during symbol mangling logic.
369        } else if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::EXTERNALLY_IMPLEMENTABLE_ITEM)
370        {
371            // * externally implementable items keep their mangled symbol name.
372            //   multiple EIIs can have the same name, so not mangling them would be a bug.
373            //   Implementing an EII does the appropriate name resolution to make sure the implementations
374            //   get the same symbol name as the *mangled* foreign item they refer to so that's all good.
375        } else if codegen_fn_attrs.symbol_name.is_some() {
376            // * This can be overridden with the `#[link_name]` attribute
377        } else {
378            // NOTE: there's one more exception that we cannot apply here. On wasm,
379            // some items cannot be `no_mangle`.
380            // However, we don't have enough information here to determine that.
381            // As such, no_mangle foreign items on wasm that have the same defid as some
382            // import will *still* be mangled despite this.
383            //
384            // if none of the exceptions apply; apply no_mangle
385            codegen_fn_attrs.flags |= CodegenFnAttrFlags::NO_MANGLE;
386        }
387    }
388}
389
390#[derive(const _: () =
    {
        impl<'_sess, G> rustc_errors::Diagnostic<'_sess, G> for
            SanitizeOnInline where G: rustc_errors::EmissionGuarantee {
            #[track_caller]
            fn into_diag(self, dcx: rustc_errors::DiagCtxtHandle<'_sess>,
                level: rustc_errors::Level) -> rustc_errors::Diag<'_sess, G> {
                match self {
                    SanitizeOnInline { inline_span: __binding_0 } => {
                        let mut diag =
                            rustc_errors::Diag::new(dcx, level,
                                rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("non-default `sanitize` will have no effect after inlining")));
                        ;
                        diag.span_note(__binding_0,
                            rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("inlining requested here")));
                        diag
                    }
                }
            }
        }
    };Diagnostic)]
391#[diag("non-default `sanitize` will have no effect after inlining")]
392struct SanitizeOnInline {
393    #[note("inlining requested here")]
394    inline_span: Span,
395}
396
397#[derive(const _: () =
    {
        impl<'_sess, G> rustc_errors::Diagnostic<'_sess, G> for AsyncBlocking
            where G: rustc_errors::EmissionGuarantee {
            #[track_caller]
            fn into_diag(self, dcx: rustc_errors::DiagCtxtHandle<'_sess>,
                level: rustc_errors::Level) -> rustc_errors::Diag<'_sess, G> {
                match self {
                    AsyncBlocking => {
                        let mut diag =
                            rustc_errors::Diag::new(dcx, level,
                                rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("the async executor can run blocking code, without realtime sanitizer catching it")));
                        ;
                        diag
                    }
                }
            }
        }
    };Diagnostic)]
398#[diag("the async executor can run blocking code, without realtime sanitizer catching it")]
399struct AsyncBlocking;
400
401fn check_result(
402    tcx: TyCtxt<'_>,
403    did: LocalDefId,
404    interesting_spans: InterestingAttributeDiagnosticSpans,
405    codegen_fn_attrs: &CodegenFnAttrs,
406) {
407    // If a function uses `#[target_feature]` it can't be inlined into general
408    // purpose functions as they wouldn't have the right target features
409    // enabled. For that reason we also forbid `#[inline(always)]` as it can't be
410    // respected.
411    //
412    // `#[rustc_force_inline]` doesn't need to be prohibited here, only
413    // `#[inline(always)]`, as forced inlining is implemented entirely within
414    // rustc (and so the MIR inliner can do any necessary checks for compatible target
415    // features).
416    //
417    // This sidesteps the LLVM blockers in enabling `target_features` +
418    // `inline(always)` to be used together (see rust-lang/rust#116573 and
419    // llvm/llvm-project#70563).
420    if !codegen_fn_attrs.target_features.is_empty()
421        && #[allow(non_exhaustive_omitted_patterns)] match codegen_fn_attrs.inline {
    InlineAttr::Always => true,
    _ => false,
}matches!(codegen_fn_attrs.inline, InlineAttr::Always)
422        && !tcx.features().target_feature_inline_always()
423        && let Some(span) = interesting_spans.inline
424    {
425        feature_err(
426            tcx.sess,
427            sym::target_feature_inline_always,
428            span,
429            "cannot use `#[inline(always)]` with `#[target_feature]`",
430        )
431        .emit();
432    }
433
434    // warn that inline has no effect when no_sanitize is present
435    if codegen_fn_attrs.sanitizers != SanitizerFnAttrs::default()
436        && codegen_fn_attrs.inline.always()
437        && let (Some(sanitize_span), Some(inline_span)) =
438            (interesting_spans.sanitize, interesting_spans.inline)
439    {
440        let hir_id = tcx.local_def_id_to_hir_id(did);
441        tcx.emit_node_span_lint(
442            lint::builtin::INLINE_NO_SANITIZE,
443            hir_id,
444            sanitize_span,
445            SanitizeOnInline { inline_span },
446        )
447    }
448
449    // warn for nonblocking async functions, blocks and closures.
450    // This doesn't behave as expected, because the executor can run blocking code without the sanitizer noticing.
451    if codegen_fn_attrs.sanitizers.rtsan_setting == RtsanSetting::Nonblocking
452        && let Some(sanitize_span) = interesting_spans.sanitize
453        // async fn
454        && (tcx.asyncness(did).is_async()
455            // async block
456            || tcx.is_coroutine(did.into())
457            // async closure
458            || (tcx.is_closure_like(did.into())
459                && tcx.hir_node_by_def_id(did).expect_closure().kind
460                    != rustc_hir::ClosureKind::Closure))
461    {
462        let hir_id = tcx.local_def_id_to_hir_id(did);
463        tcx.emit_node_span_lint(
464            lint::builtin::RTSAN_NONBLOCKING_ASYNC,
465            hir_id,
466            sanitize_span,
467            AsyncBlocking,
468        );
469    }
470
471    // error when specifying link_name together with link_ordinal
472    if let Some(_) = codegen_fn_attrs.symbol_name
473        && let Some(_) = codegen_fn_attrs.link_ordinal
474    {
475        let msg = "cannot use `#[link_name]` with `#[link_ordinal]`";
476        if let Some(span) = interesting_spans.link_ordinal {
477            tcx.dcx().span_err(span, msg);
478        } else {
479            tcx.dcx().err(msg);
480        }
481    }
482
483    if let Some(features) = check_tied_features(
484        tcx.sess,
485        &codegen_fn_attrs
486            .target_features
487            .iter()
488            .map(|features| (features.name.as_str(), true))
489            .collect(),
490    ) {
491        let span = {
    {
        'done:
            {
            for i in ::rustc_hir::attrs::HasAttrs::get_attrs(did, &tcx) {
                #[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)
492            .unwrap_or_else(|| tcx.def_span(did));
493
494        tcx.dcx()
495            .create_err(errors::TargetFeatureDisableOrEnable {
496                features,
497                span: Some(span),
498                missing_features: Some(errors::MissingFeatures),
499            })
500            .emit();
501    }
502}
503
504fn handle_lang_items(
505    tcx: TyCtxt<'_>,
506    did: LocalDefId,
507    interesting_spans: &InterestingAttributeDiagnosticSpans,
508    attrs: &[Attribute],
509    codegen_fn_attrs: &mut CodegenFnAttrs,
510) {
511    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);
512
513    // Weak lang items have the same semantics as "std internal" symbols in the
514    // sense that they're preserved through all our LTO passes and only
515    // strippable by the linker.
516    //
517    // Additionally weak lang items have predetermined symbol names.
518    if let Some(lang_item) = lang_item
519        && let Some(link_name) = lang_item.link_name()
520    {
521        codegen_fn_attrs.flags |= CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL;
522        codegen_fn_attrs.symbol_name = Some(link_name);
523    }
524
525    // error when using no_mangle on a lang item item
526    if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL)
527        && codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::NO_MANGLE)
528    {
529        let mut err = tcx
530            .dcx()
531            .struct_span_err(
532                interesting_spans.no_mangle.unwrap_or_default(),
533                "`#[no_mangle]` cannot be used on internal language items",
534            )
535            .with_note("Rustc requires this item to have a specific mangled name.")
536            .with_span_label(tcx.def_span(did), "should be the internal language item");
537        if let Some(lang_item) = lang_item
538            && let Some(link_name) = lang_item.link_name()
539        {
540            err = err
541                .with_note("If you are trying to prevent mangling to ease debugging, many")
542                .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"))
543                .with_note(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("match `.*{0}.*` instead of `break {0}` on a specific name",
                link_name))
    })format!(
544                    "match `.*{link_name}.*` instead of `break {link_name}` on a specific name"
545                ))
546        }
547        err.emit();
548    }
549}
550
551/// Generate the [`CodegenFnAttrs`] for an item (identified by the [`LocalDefId`]).
552///
553/// This happens in 4 stages:
554/// - apply built-in attributes that directly translate to codegen attributes.
555/// - handle lang items. These have special codegen attrs applied to them.
556/// - apply overrides, like minimum requirements for alignment and other settings that don't rely directly the built-in attrs on the item.
557///   overrides come after applying built-in attributes since they may only apply when certain attributes were already set in the stage before.
558/// - check that the result is valid. There's various ways in which this may not be the case, such as certain combinations of attrs.
559fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
560    if truecfg!(debug_assertions) {
561        let def_kind = tcx.def_kind(did);
562        if !def_kind.has_codegen_attrs() {
    {
        ::core::panicking::panic_fmt(format_args!("unexpected `def_kind` in `codegen_fn_attrs`: {0:?}",
                def_kind));
    }
};assert!(
563            def_kind.has_codegen_attrs(),
564            "unexpected `def_kind` in `codegen_fn_attrs`: {def_kind:?}",
565        );
566    }
567
568    let mut codegen_fn_attrs = CodegenFnAttrs::new();
569    let attrs = tcx.hir_attrs(tcx.local_def_id_to_hir_id(did));
570
571    let interesting_spans = process_builtin_attrs(tcx, did, attrs, &mut codegen_fn_attrs);
572    handle_lang_items(tcx, did, &interesting_spans, attrs, &mut codegen_fn_attrs);
573    apply_overrides(tcx, did, &mut codegen_fn_attrs);
574    check_result(tcx, did, interesting_spans, &codegen_fn_attrs);
575
576    codegen_fn_attrs
577}
578
579fn sanitizer_settings_for(tcx: TyCtxt<'_>, did: LocalDefId) -> SanitizerFnAttrs {
580    // Backtrack to the crate root.
581    let mut settings = match tcx.opt_local_parent(did) {
582        // Check the parent (recursively).
583        Some(parent) => tcx.sanitizer_settings_for(parent),
584        // We reached the crate root without seeing an attribute, so
585        // there is no sanitizers to exclude.
586        None => SanitizerFnAttrs::default(),
587    };
588
589    // Check for a sanitize annotation directly on this def.
590    if let Some((on_set, off_set, rtsan)) =
591        {
    {
        'done:
            {
            for i in ::rustc_hir::attrs::HasAttrs::get_attrs(did, &tcx) {
                #[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))
592    {
593        // the on set is the set of sanitizers explicitly enabled.
594        // we mask those out since we want the set of disabled sanitizers here
595        settings.disabled &= !*on_set;
596        // the off set is the set of sanitizers explicitly disabled.
597        // we or those in here.
598        settings.disabled |= *off_set;
599        // the on set and off set are distjoint since there's a third option: unset.
600        // a node may not set the sanitizer setting in which case it inherits from parents.
601        // the code above in this function does this backtracking
602
603        // if rtsan was specified here override the parent
604        if let Some(rtsan) = rtsan {
605            settings.rtsan_setting = *rtsan;
606        }
607    }
608    settings
609}
610
611/// Checks if the provided DefId is a method in a trait impl for a trait which has track_caller
612/// applied to the method prototype.
613fn should_inherit_track_caller(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
614    tcx.trait_item_of(def_id).is_some_and(|id| {
615        tcx.codegen_fn_attrs(id).flags.intersects(CodegenFnAttrFlags::TRACK_CALLER)
616    })
617}
618
619/// If the provided DefId is a method in a trait impl, return the value of the `#[align]`
620/// attribute on the method prototype (if any).
621fn inherited_align<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> Option<Align> {
622    tcx.codegen_fn_attrs(tcx.trait_item_of(def_id)?).alignment
623}
624
625pub(crate) fn provide(providers: &mut Providers) {
626    *providers = Providers {
627        codegen_fn_attrs,
628        should_inherit_track_caller,
629        inherited_align,
630        sanitizer_settings_for,
631        ..*providers
632    };
633}