Skip to content

Commit fa35f7b

Browse files
committed
Lower at the same place as #[track_caller] is handled.
1 parent f537287 commit fa35f7b

File tree

3 files changed

+35
-30
lines changed

3 files changed

+35
-30
lines changed

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::iter;
12
use std::ops::ControlFlow;
23
use std::sync::Arc;
34

@@ -804,6 +805,37 @@ impl<'hir> LoweringContext<'_, 'hir> {
804805
}
805806
}
806807

808+
pub(super) fn forward_inline(&mut self, _span: Span, outer_hir_id: HirId, inner_hir_id: HirId) {
809+
let Some(attrs) = self.attrs.get(&outer_hir_id.local_id) else {
810+
return;
811+
};
812+
813+
let Some((inline_attr, span)) =
814+
find_attr!(*attrs, AttributeKind::Inline(attr, span) => (attr, span))
815+
else {
816+
return;
817+
};
818+
819+
let filtered_iter = attrs
820+
.iter()
821+
.cloned()
822+
.filter(|attr| !matches!(attr, hir::Attribute::Parsed(AttributeKind::Inline(_, _))));
823+
824+
let filtered_attrs = self.arena.alloc_from_iter(filtered_iter);
825+
826+
if filtered_attrs.is_empty() {
827+
self.attrs.remove(&outer_hir_id.local_id);
828+
} else {
829+
self.attrs.insert(outer_hir_id.local_id, filtered_attrs);
830+
}
831+
832+
let attr = self.arena.alloc_from_iter(iter::once(hir::Attribute::Parsed(
833+
AttributeKind::Inline(*inline_attr, *span),
834+
)));
835+
836+
self.attrs.insert(inner_hir_id.local_id, attr);
837+
}
838+
807839
/// Desugar `<expr>.await` into:
808840
/// ```ignore (pseudo-rust)
809841
/// match ::std::future::IntoFuture::into_future(<expr>) {
@@ -1167,6 +1199,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
11671199
);
11681200

11691201
this.maybe_forward_track_caller(body.span, closure_hir_id, expr.hir_id);
1202+
this.forward_inline(body.span, closure_hir_id, expr.hir_id);
11701203

11711204
(parameters, expr)
11721205
});

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1290,6 +1290,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
12901290
// FIXME(async_fn_track_caller): Can this be moved above?
12911291
let hir_id = expr.hir_id;
12921292
this.maybe_forward_track_caller(body.span, fn_id, hir_id);
1293+
this.forward_inline(body.span, fn_id, hir_id);
12931294

12941295
(parameters, expr)
12951296
})

compiler/rustc_codegen_ssa/src/codegen_attrs.rs

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@ use rustc_ast::{LitKind, MetaItem, MetaItemInner, attr};
66
use rustc_hir::attrs::{AttributeKind, InlineAttr, InstructionSetAttr, RtsanSetting, UsedBy};
77
use rustc_hir::def::DefKind;
88
use rustc_hir::def_id::{DefId, LOCAL_CRATE, LocalDefId};
9-
use rustc_hir::{
10-
self as hir, Attribute, CoroutineDesugaring, CoroutineKind, CoroutineSource, LangItem,
11-
find_attr, lang_items,
12-
};
9+
use rustc_hir::{self as hir, Attribute, LangItem, find_attr, lang_items};
1310
use rustc_middle::middle::codegen_fn_attrs::{
1411
CodegenFnAttrFlags, CodegenFnAttrs, PatchableFunctionEntry, SanitizerFnAttrs,
1512
};
@@ -389,15 +386,6 @@ fn apply_overrides(tcx: TyCtxt<'_>, did: LocalDefId, codegen_fn_attrs: &mut Code
389386
}
390387
}
391388

392-
// #129347: desugared async coutines inherit inline attrs from the async fn/closure
393-
if let Some(parent) = async_poll_inherit_inline(tcx, did.to_def_id()) {
394-
let parent_attrs = tcx.codegen_fn_attrs(parent);
395-
if matches!(parent_attrs.inline, InlineAttr::Never | InlineAttr::Hint | InlineAttr::Always)
396-
{
397-
codegen_fn_attrs.inline = parent_attrs.inline;
398-
}
399-
}
400-
401389
// When `no_builtins` is applied at the crate level, we should add the
402390
// `no-builtins` attribute to each function to ensure it takes effect in LTO.
403391
let crate_attrs = tcx.hir_attrs(rustc_hir::CRATE_HIR_ID);
@@ -659,23 +647,6 @@ fn inherited_align<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> Option<Align> {
659647
tcx.codegen_fn_attrs(tcx.trait_item_of(def_id)?).alignment
660648
}
661649

662-
// Checks if the given DefId is a desugared async coroutine that should inherit inline attrs.
663-
fn async_poll_inherit_inline(tcx: TyCtxt<'_>, def_id: DefId) -> Option<DefId> {
664-
let Some(kind) = tcx.coroutine_kind(def_id) else {
665-
return None;
666-
};
667-
668-
if matches!(
669-
kind,
670-
CoroutineKind::Desugared(CoroutineDesugaring::Async, CoroutineSource::Fn)
671-
| CoroutineKind::Desugared(CoroutineDesugaring::Async, CoroutineSource::Closure)
672-
) {
673-
Some(tcx.parent(def_id))
674-
} else {
675-
None
676-
}
677-
}
678-
679650
/// We now check the #\[rustc_autodiff\] attributes which we generated from the #[autodiff(...)]
680651
/// macros. There are two forms. The pure one without args to mark primal functions (the functions
681652
/// being differentiated). The other form is #[rustc_autodiff(Mode, ActivityList)] on top of the

0 commit comments

Comments
 (0)