1use rustc_abi::{Align, ExternAbi};
2use rustc_hir::attrs::{
3AttributeKind, EiiImplResolution, InlineAttr, Linkage, RtsanSetting, UsedBy,
4};
5use rustc_hir::def::DefKind;
6use rustc_hir::def_id::{DefId, LOCAL_CRATE, LocalDefId};
7use rustc_hir::{selfas hir, Attribute, find_attr};
8use rustc_macros::Diagnostic;
9use rustc_middle::middle::codegen_fn_attrs::{
10CodegenFnAttrFlags, CodegenFnAttrs, PatchableFunctionEntry, SanitizerFnAttrs,
11};
12use rustc_middle::mir::mono::Visibility;
13use rustc_middle::query::Providers;
14use rustc_middle::ty::{selfas ty, TyCtxt};
15use rustc_session::lint;
16use rustc_session::parse::feature_err;
17use rustc_span::{Span, sym};
18use rustc_target::spec::Os;
1920use crate::errors;
21use crate::target_features::{
22check_target_feature_trait_unsafe, check_tied_features, from_target_feature_attr,
23};
2425/// 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>>> {
35use DefKind::*;
3637let def_kind = tcx.def_kind(did);
38if let Fn | AssocFn | Variant | Ctor(..) = def_kind {
39Some(tcx.fn_sig(did))
40 } else {
41tcx.dcx().span_delayed_bug(attr_span, "this attribute can only be applied to functions");
42None43 }
44}
4546/// 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}
5556/// 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 {
64let mut interesting_spans = InterestingAttributeDiagnosticSpans::default();
65let rust_target_features = tcx.rust_target_features(LOCAL_CRATE);
6667let parsed_attrs = attrs68 .iter()
69 .filter_map(|attr| if let hir::Attribute::Parsed(attr) = attr { Some(attr) } else { None });
70for attr in parsed_attrs {
71match 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
83if 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);
94if 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 } => {
103let 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");
105continue;
106 };
107let 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;
110if safe_target_features && !was_forced {
111if 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) => {
147let is_closure = tcx.is_closure_like(did.to_def_id());
148149if !is_closure
150 && let Some(fn_sig) = try_fn_sig(tcx, did, *attr_span)
151 && fn_sig.skip_binder().abi() != ExternAbi::Rust
152 {
153 tcx.dcx().emit_err(errors::RequiresRustAbi { span: *attr_span });
154 }
155if is_closure
156 && !tcx.features().closure_track_caller()
157 && !attr_span.allows_unstable(sym::closure_track_caller)
158 {
159 feature_err(
160&tcx.sess,
161 sym::closure_track_caller,
162*attr_span,
163"`#[track_caller]` on closures is currently unstable",
164 )
165 .emit();
166 }
167 codegen_fn_attrs.flags |= CodegenFnAttrFlags::TRACK_CALLER
168 }
169 AttributeKind::Used { used_by, .. } => match used_by {
170 UsedBy::Compiler => codegen_fn_attrs.flags |= CodegenFnAttrFlags::USED_COMPILER,
171 UsedBy::Linker => codegen_fn_attrs.flags |= CodegenFnAttrFlags::USED_LINKER,
172 UsedBy::Default => {
173let used_form = if tcx.sess.target.os == Os::Illumos {
174// illumos' `ld` doesn't support a section header that would represent
175 // `#[used(linker)]`, see
176 // https://github.com/rust-lang/rust/issues/146169. For that target,
177 // downgrade as if `#[used(compiler)]` was requested and hope for the
178 // best.
179CodegenFnAttrFlags::USED_COMPILER
180 } else {
181 CodegenFnAttrFlags::USED_LINKER
182 };
183 codegen_fn_attrs.flags |= used_form;
184 }
185 },
186 AttributeKind::FfiConst(_) => codegen_fn_attrs.flags |= CodegenFnAttrFlags::FFI_CONST,
187 AttributeKind::FfiPure(_) => codegen_fn_attrs.flags |= CodegenFnAttrFlags::FFI_PURE,
188 AttributeKind::RustcStdInternalSymbol(_) => {
189 codegen_fn_attrs.flags |= CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL
190 }
191 AttributeKind::Linkage(linkage, span) => {
192let linkage = Some(*linkage);
193194if tcx.is_foreign_item(did) {
195 codegen_fn_attrs.import_linkage = linkage;
196197if tcx.is_mutable_static(did.into()) {
198let mut diag = tcx.dcx().struct_span_err(
199*span,
200"extern mutable statics are not allowed with `#[linkage]`",
201 );
202 diag.note(
203"marking the extern static mutable would allow changing which \
204 symbol the static references rather than make the target of the \
205 symbol mutable",
206 );
207 diag.emit();
208 }
209 } else {
210 codegen_fn_attrs.linkage = linkage;
211 }
212 }
213 AttributeKind::Sanitize { span, .. } => {
214 interesting_spans.sanitize = Some(*span);
215 }
216 AttributeKind::RustcObjcClass { classname, .. } => {
217 codegen_fn_attrs.objc_class = Some(*classname);
218 }
219 AttributeKind::RustcObjcSelector { methname, .. } => {
220 codegen_fn_attrs.objc_selector = Some(*methname);
221 }
222 AttributeKind::RustcEiiForeignItem => {
223 codegen_fn_attrs.flags |= CodegenFnAttrFlags::EXTERNALLY_IMPLEMENTABLE_ITEM;
224 }
225 AttributeKind::EiiImpls(impls) => {
226for i in impls {
227let foreign_item = match i.resolution {
228 EiiImplResolution::Macro(def_id) => {
229let 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
230 )else {
231 tcx.dcx().span_delayed_bug(
232 i.span,
233"resolved to something that's not an EII",
234 );
235continue;
236 };
237 extern_item
238 }
239 EiiImplResolution::Known(decl) => decl.foreign_item,
240 EiiImplResolution::Error(_eg) => continue,
241 };
242243// this is to prevent a bug where a single crate defines both the default and explicit implementation
244 // for an EII. In that case, both of them may be part of the same final object file. I'm not 100% sure
245 // what happens, either rustc deduplicates the symbol or llvm, or it's random/order-dependent.
246 // However, the fact that the default one of has weak linkage isn't considered and you sometimes get that
247 // the default implementation is used while an explicit implementation is given.
248if
249// if this is a default impl
250i.is_default
251// iterate over all implementations *in the current crate*
252 // (this is ok since we generate codegen fn attrs in the local crate)
253 // if any of them is *not default* then don't emit the alias.
254&& tcx.externally_implementable_items(LOCAL_CRATE).get(&foreign_item).expect("at least one").1.iter().any(|(_, imp)| !imp.is_default)
255 {
256continue;
257 }
258259 codegen_fn_attrs.foreign_item_symbol_aliases.push((
260 foreign_item,
261if i.is_default { Linkage::LinkOnceAny } else { Linkage::External },
262 Visibility::Default,
263 ));
264 codegen_fn_attrs.flags |= CodegenFnAttrFlags::EXTERNALLY_IMPLEMENTABLE_ITEM;
265 }
266 }
267 AttributeKind::ThreadLocal => {
268 codegen_fn_attrs.flags |= CodegenFnAttrFlags::THREAD_LOCAL
269 }
270 AttributeKind::InstructionSet(instruction_set) => {
271 codegen_fn_attrs.instruction_set = Some(*instruction_set)
272 }
273 AttributeKind::RustcAllocator => {
274 codegen_fn_attrs.flags |= CodegenFnAttrFlags::ALLOCATOR
275 }
276 AttributeKind::RustcDeallocator => {
277 codegen_fn_attrs.flags |= CodegenFnAttrFlags::DEALLOCATOR
278 }
279 AttributeKind::RustcReallocator => {
280 codegen_fn_attrs.flags |= CodegenFnAttrFlags::REALLOCATOR
281 }
282 AttributeKind::RustcAllocatorZeroed => {
283 codegen_fn_attrs.flags |= CodegenFnAttrFlags::ALLOCATOR_ZEROED
284 }
285 AttributeKind::RustcNounwind => {
286 codegen_fn_attrs.flags |= CodegenFnAttrFlags::NEVER_UNWIND
287 }
288 AttributeKind::RustcOffloadKernel => {
289 codegen_fn_attrs.flags |= CodegenFnAttrFlags::OFFLOAD_KERNEL
290 }
291 AttributeKind::PatchableFunctionEntry { prefix, entry } => {
292 codegen_fn_attrs.patchable_function_entry =
293Some(PatchableFunctionEntry::from_prefix_and_entry(*prefix, *entry));
294 }
295_ => {}
296 }
297 }
298299interesting_spans300}
301302/// Applies overrides for codegen fn attrs. These often have a specific reason why they're necessary.
303/// Please comment why when adding a new one!
304fn apply_overrides(tcx: TyCtxt<'_>, did: LocalDefId, codegen_fn_attrs: &mut CodegenFnAttrs) {
305// Apply the minimum function alignment here. This ensures that a function's alignment is
306 // determined by the `-C` flags of the crate it is defined in, not the `-C` flags of the crate
307 // it happens to be codegen'd (or const-eval'd) in.
308codegen_fn_attrs.alignment =
309 Ord::max(codegen_fn_attrs.alignment, tcx.sess.opts.unstable_opts.min_function_alignment);
310311// Passed in sanitizer settings are always the default.
312if !(codegen_fn_attrs.sanitizers == SanitizerFnAttrs::default()) {
::core::panicking::panic("assertion failed: codegen_fn_attrs.sanitizers == SanitizerFnAttrs::default()")
};assert!(codegen_fn_attrs.sanitizers == SanitizerFnAttrs::default());
313// Replace with #[sanitize] value
314codegen_fn_attrs.sanitizers = tcx.sanitizer_settings_for(did);
315// On trait methods, inherit the `#[align]` of the trait's method prototype.
316codegen_fn_attrs.alignment = Ord::max(codegen_fn_attrs.alignment, tcx.inherited_align(did));
317318// naked function MUST NOT be inlined! This attribute is required for the rust compiler itself,
319 // but not for the code generation backend because at that point the naked function will just be
320 // a declaration, with a definition provided in global assembly.
321if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::NAKED) {
322codegen_fn_attrs.inline = InlineAttr::Never;
323 }
324325// #73631: closures inherit `#[target_feature]` annotations
326 //
327 // If this closure is marked `#[inline(always)]`, simply skip adding `#[target_feature]`.
328 //
329 // At this point, `unsafe` has already been checked and `#[target_feature]` only affects codegen.
330 // Due to LLVM limitations, emitting both `#[inline(always)]` and `#[target_feature]` is *unsound*:
331 // the function may be inlined into a caller with fewer target features. Also see
332 // <https://github.com/rust-lang/rust/issues/116573>.
333 //
334 // Using `#[inline(always)]` implies that this closure will most likely be inlined into
335 // its parent function, which effectively inherits the features anyway. Boxing this closure
336 // would result in this closure being compiled without the inherited target features, but this
337 // is probably a poor usage of `#[inline(always)]` and easily avoided by not using the attribute.
338if tcx.is_closure_like(did.to_def_id()) && codegen_fn_attrs.inline != InlineAttr::Always {
339let owner_id = tcx.parent(did.to_def_id());
340if tcx.def_kind(owner_id).has_codegen_attrs() {
341codegen_fn_attrs342 .target_features
343 .extend(tcx.codegen_fn_attrs(owner_id).target_features.iter().copied());
344 }
345 }
346347// When `no_builtins` is applied at the crate level, we should add the
348 // `no-builtins` attribute to each function to ensure it takes effect in LTO.
349let 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);
350if no_builtins {
351codegen_fn_attrs.flags |= CodegenFnAttrFlags::NO_BUILTINS;
352 }
353354// inherit track-caller properly
355if tcx.should_inherit_track_caller(did) {
356codegen_fn_attrs.flags |= CodegenFnAttrFlags::TRACK_CALLER;
357 }
358359// Foreign items by default use no mangling for their symbol name.
360if tcx.is_foreign_item(did) {
361codegen_fn_attrs.flags |= CodegenFnAttrFlags::FOREIGN_ITEM;
362363// There's a few exceptions to this rule though:
364if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL) {
365// * `#[rustc_std_internal_symbol]` mangles the symbol name in a special way
366 // both for exports and imports through foreign items. This is handled further,
367 // during symbol mangling logic.
368} else if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::EXTERNALLY_IMPLEMENTABLE_ITEM)
369 {
370// * externally implementable items keep their mangled symbol name.
371 // multiple EIIs can have the same name, so not mangling them would be a bug.
372 // Implementing an EII does the appropriate name resolution to make sure the implementations
373 // get the same symbol name as the *mangled* foreign item they refer to so that's all good.
374} else if codegen_fn_attrs.symbol_name.is_some() {
375// * This can be overridden with the `#[link_name]` attribute
376} else {
377// NOTE: there's one more exception that we cannot apply here. On wasm,
378 // some items cannot be `no_mangle`.
379 // However, we don't have enough information here to determine that.
380 // As such, no_mangle foreign items on wasm that have the same defid as some
381 // import will *still* be mangled despite this.
382 //
383 // if none of the exceptions apply; apply no_mangle
384codegen_fn_attrs.flags |= CodegenFnAttrFlags::NO_MANGLE;
385 }
386 }
387}
388389#[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)]
390#[diag("non-default `sanitize` will have no effect after inlining")]
391struct SanitizeOnInline {
392#[note("inlining requested here")]
393inline_span: Span,
394}
395396#[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)]
397#[diag("the async executor can run blocking code, without realtime sanitizer catching it")]
398struct AsyncBlocking;
399400fn check_result(
401 tcx: TyCtxt<'_>,
402 did: LocalDefId,
403 interesting_spans: InterestingAttributeDiagnosticSpans,
404 codegen_fn_attrs: &CodegenFnAttrs,
405) {
406// If a function uses `#[target_feature]` it can't be inlined into general
407 // purpose functions as they wouldn't have the right target features
408 // enabled. For that reason we also forbid `#[inline(always)]` as it can't be
409 // respected.
410 //
411 // `#[rustc_force_inline]` doesn't need to be prohibited here, only
412 // `#[inline(always)]`, as forced inlining is implemented entirely within
413 // rustc (and so the MIR inliner can do any necessary checks for compatible target
414 // features).
415 //
416 // This sidesteps the LLVM blockers in enabling `target_features` +
417 // `inline(always)` to be used together (see rust-lang/rust#116573 and
418 // llvm/llvm-project#70563).
419if !codegen_fn_attrs.target_features.is_empty()
420 && #[allow(non_exhaustive_omitted_patterns)] match codegen_fn_attrs.inline {
InlineAttr::Always => true,
_ => false,
}matches!(codegen_fn_attrs.inline, InlineAttr::Always)421 && !tcx.features().target_feature_inline_always()
422 && let Some(span) = interesting_spans.inline
423 {
424feature_err(
425tcx.sess,
426 sym::target_feature_inline_always,
427span,
428"cannot use `#[inline(always)]` with `#[target_feature]`",
429 )
430 .emit();
431 }
432433// warn that inline has no effect when no_sanitize is present
434if codegen_fn_attrs.sanitizers != SanitizerFnAttrs::default()
435 && codegen_fn_attrs.inline.always()
436 && let (Some(sanitize_span), Some(inline_span)) =
437 (interesting_spans.sanitize, interesting_spans.inline)
438 {
439let hir_id = tcx.local_def_id_to_hir_id(did);
440tcx.emit_node_span_lint(
441 lint::builtin::INLINE_NO_SANITIZE,
442hir_id,
443sanitize_span,
444SanitizeOnInline { inline_span },
445 )
446 }
447448// warn for nonblocking async functions, blocks and closures.
449 // This doesn't behave as expected, because the executor can run blocking code without the sanitizer noticing.
450if codegen_fn_attrs.sanitizers.rtsan_setting == RtsanSetting::Nonblocking451 && let Some(sanitize_span) = interesting_spans.sanitize
452// async fn
453&& (tcx.asyncness(did).is_async()
454// async block
455|| tcx.is_coroutine(did.into())
456// async closure
457|| (tcx.is_closure_like(did.into())
458 && tcx.hir_node_by_def_id(did).expect_closure().kind
459 != rustc_hir::ClosureKind::Closure))
460 {
461let hir_id = tcx.local_def_id_to_hir_id(did);
462tcx.emit_node_span_lint(
463 lint::builtin::RTSAN_NONBLOCKING_ASYNC,
464hir_id,
465sanitize_span,
466AsyncBlocking,
467 );
468 }
469470// error when specifying link_name together with link_ordinal
471if let Some(_) = codegen_fn_attrs.symbol_name
472 && let Some(_) = codegen_fn_attrs.link_ordinal
473 {
474let msg = "cannot use `#[link_name]` with `#[link_ordinal]`";
475if let Some(span) = interesting_spans.link_ordinal {
476tcx.dcx().span_err(span, msg);
477 } else {
478tcx.dcx().err(msg);
479 }
480 }
481482if let Some(features) = check_tied_features(
483tcx.sess,
484&codegen_fn_attrs485 .target_features
486 .iter()
487 .map(|features| (features.name.as_str(), true))
488 .collect(),
489 ) {
490let 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)491 .unwrap_or_else(|| tcx.def_span(did));
492493tcx.dcx()
494 .create_err(errors::TargetFeatureDisableOrEnable {
495features,
496 span: Some(span),
497 missing_features: Some(errors::MissingFeatures),
498 })
499 .emit();
500 }
501}
502503fn handle_lang_items(
504 tcx: TyCtxt<'_>,
505 did: LocalDefId,
506 interesting_spans: &InterestingAttributeDiagnosticSpans,
507 attrs: &[Attribute],
508 codegen_fn_attrs: &mut CodegenFnAttrs,
509) {
510let 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);
511512// Weak lang items have the same semantics as "std internal" symbols in the
513 // sense that they're preserved through all our LTO passes and only
514 // strippable by the linker.
515 //
516 // Additionally weak lang items have predetermined symbol names.
517if let Some(lang_item) = lang_item518 && let Some(link_name) = lang_item.link_name()
519 {
520codegen_fn_attrs.flags |= CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL;
521codegen_fn_attrs.symbol_name = Some(link_name);
522 }
523524// error when using no_mangle on a lang item item
525if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL)
526 && codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::NO_MANGLE)
527 {
528let mut err = tcx529 .dcx()
530 .struct_span_err(
531interesting_spans.no_mangle.unwrap_or_default(),
532"`#[no_mangle]` cannot be used on internal language items",
533 )
534 .with_note("Rustc requires this item to have a specific mangled name.")
535 .with_span_label(tcx.def_span(did), "should be the internal language item");
536if let Some(lang_item) = lang_item537 && let Some(link_name) = lang_item.link_name()
538 {
539err = err540 .with_note("If you are trying to prevent mangling to ease debugging, many")
541 .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"))
542 .with_note(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("match `.*{0}.*` instead of `break {0}` on a specific name",
link_name))
})format!(
543"match `.*{link_name}.*` instead of `break {link_name}` on a specific name"
544))
545 }
546err.emit();
547 }
548}
549550/// Generate the [`CodegenFnAttrs`] for an item (identified by the [`LocalDefId`]).
551///
552/// This happens in 4 stages:
553/// - apply built-in attributes that directly translate to codegen attributes.
554/// - handle lang items. These have special codegen attrs applied to them.
555/// - apply overrides, like minimum requirements for alignment and other settings that don't rely directly the built-in attrs on the item.
556/// overrides come after applying built-in attributes since they may only apply when certain attributes were already set in the stage before.
557/// - check that the result is valid. There's various ways in which this may not be the case, such as certain combinations of attrs.
558fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
559if truecfg!(debug_assertions) {
560let def_kind = tcx.def_kind(did);
561if !def_kind.has_codegen_attrs() {
{
::core::panicking::panic_fmt(format_args!("unexpected `def_kind` in `codegen_fn_attrs`: {0:?}",
def_kind));
}
};assert!(
562 def_kind.has_codegen_attrs(),
563"unexpected `def_kind` in `codegen_fn_attrs`: {def_kind:?}",
564 );
565 }
566567let mut codegen_fn_attrs = CodegenFnAttrs::new();
568let attrs = tcx.hir_attrs(tcx.local_def_id_to_hir_id(did));
569570let interesting_spans = process_builtin_attrs(tcx, did, attrs, &mut codegen_fn_attrs);
571handle_lang_items(tcx, did, &interesting_spans, attrs, &mut codegen_fn_attrs);
572apply_overrides(tcx, did, &mut codegen_fn_attrs);
573check_result(tcx, did, interesting_spans, &codegen_fn_attrs);
574575codegen_fn_attrs576}
577578fn sanitizer_settings_for(tcx: TyCtxt<'_>, did: LocalDefId) -> SanitizerFnAttrs {
579// Backtrack to the crate root.
580let mut settings = match tcx.opt_local_parent(did) {
581// Check the parent (recursively).
582Some(parent) => tcx.sanitizer_settings_for(parent),
583// We reached the crate root without seeing an attribute, so
584 // there is no sanitizers to exclude.
585None => SanitizerFnAttrs::default(),
586 };
587588// Check for a sanitize annotation directly on this def.
589if let Some((on_set, off_set, rtsan)) =
590{
#[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))591 {
592// the on set is the set of sanitizers explicitly enabled.
593 // we mask those out since we want the set of disabled sanitizers here
594settings.disabled &= !*on_set;
595// the off set is the set of sanitizers explicitly disabled.
596 // we or those in here.
597settings.disabled |= *off_set;
598// the on set and off set are distjoint since there's a third option: unset.
599 // a node may not set the sanitizer setting in which case it inherits from parents.
600 // the code above in this function does this backtracking
601602 // if rtsan was specified here override the parent
603if let Some(rtsan) = rtsan {
604settings.rtsan_setting = *rtsan;
605 }
606 }
607settings608}
609610/// Checks if the provided DefId is a method in a trait impl for a trait which has track_caller
611/// applied to the method prototype.
612fn should_inherit_track_caller(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
613tcx.trait_item_of(def_id).is_some_and(|id| {
614tcx.codegen_fn_attrs(id).flags.intersects(CodegenFnAttrFlags::TRACK_CALLER)
615 })
616}
617618/// If the provided DefId is a method in a trait impl, return the value of the `#[align]`
619/// attribute on the method prototype (if any).
620fn inherited_align<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> Option<Align> {
621tcx.codegen_fn_attrs(tcx.trait_item_of(def_id)?).alignment
622}
623624pub(crate) fn provide(providers: &mut Providers) {
625*providers = Providers {
626codegen_fn_attrs,
627should_inherit_track_caller,
628inherited_align,
629sanitizer_settings_for,
630 ..*providers631 };
632}