|
1 | 1 | use std::str::FromStr;
|
2 | 2 |
|
3 |
| -use rustc_abi::ExternAbi; |
| 3 | +use rustc_abi::{Align, ExternAbi}; |
4 | 4 | use rustc_ast::expand::autodiff_attrs::{AutoDiffAttrs, DiffActivity, DiffMode};
|
5 | 5 | use rustc_ast::{LitKind, MetaItem, MetaItemInner, attr};
|
6 | 6 | use rustc_attr_data_structures::{
|
@@ -395,6 +395,9 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
|
395 | 395 | codegen_fn_attrs.alignment =
|
396 | 396 | Ord::max(codegen_fn_attrs.alignment, tcx.sess.opts.unstable_opts.min_function_alignment);
|
397 | 397 |
|
| 398 | + // On trait methods, inherit the `#[align]` of the trait's method prototype. |
| 399 | + codegen_fn_attrs.alignment = Ord::max(codegen_fn_attrs.alignment, tcx.inherited_align(did)); |
| 400 | + |
398 | 401 | let inline_span;
|
399 | 402 | (codegen_fn_attrs.inline, inline_span) = if let Some((inline_attr, span)) =
|
400 | 403 | find_attr!(attrs, AttributeKind::Inline(i, span) => (*i, *span))
|
@@ -549,17 +552,26 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
|
549 | 552 | codegen_fn_attrs
|
550 | 553 | }
|
551 | 554 |
|
| 555 | +/// If the provided DefId is a method in a trait impl, return the DefId of the method prototype. |
| 556 | +fn opt_trait_item(tcx: TyCtxt<'_>, def_id: DefId) -> Option<DefId> { |
| 557 | + let impl_item = tcx.opt_associated_item(def_id)?; |
| 558 | + match impl_item.container { |
| 559 | + ty::AssocItemContainer::Impl => impl_item.trait_item_def_id, |
| 560 | + _ => None, |
| 561 | + } |
| 562 | +} |
| 563 | + |
552 | 564 | /// Checks if the provided DefId is a method in a trait impl for a trait which has track_caller
|
553 | 565 | /// applied to the method prototype.
|
554 | 566 | fn should_inherit_track_caller(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
|
555 |
| - if let Some(impl_item) = tcx.opt_associated_item(def_id) |
556 |
| - && let ty::AssocItemContainer::Impl = impl_item.container |
557 |
| - && let Some(trait_item) = impl_item.trait_item_def_id |
558 |
| - { |
559 |
| - return tcx.codegen_fn_attrs(trait_item).flags.intersects(CodegenFnAttrFlags::TRACK_CALLER); |
560 |
| - } |
| 567 | + let Some(trait_item) = opt_trait_item(tcx, def_id) else { return false }; |
| 568 | + tcx.codegen_fn_attrs(trait_item).flags.intersects(CodegenFnAttrFlags::TRACK_CALLER) |
| 569 | +} |
561 | 570 |
|
562 |
| - false |
| 571 | +/// If the provided DefId is a method in a trait impl, return the value of the `#[align]` |
| 572 | +/// attribute on the method prototype (if any). |
| 573 | +fn inherited_align<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> Option<Align> { |
| 574 | + tcx.codegen_fn_attrs(opt_trait_item(tcx, def_id)?).alignment |
563 | 575 | }
|
564 | 576 |
|
565 | 577 | fn check_link_ordinal(tcx: TyCtxt<'_>, attr: &hir::Attribute) -> Option<u16> {
|
@@ -727,5 +739,6 @@ fn autodiff_attrs(tcx: TyCtxt<'_>, id: DefId) -> Option<AutoDiffAttrs> {
|
727 | 739 | }
|
728 | 740 |
|
729 | 741 | pub(crate) fn provide(providers: &mut Providers) {
|
730 |
| - *providers = Providers { codegen_fn_attrs, should_inherit_track_caller, ..*providers }; |
| 742 | + *providers = |
| 743 | + Providers { codegen_fn_attrs, should_inherit_track_caller, inherited_align, ..*providers }; |
731 | 744 | }
|
0 commit comments