diff --git a/compiler/rustc_ast_lowering/src/expr.rs b/compiler/rustc_ast_lowering/src/expr.rs index 524f8b054cb49..bd67272195b53 100644 --- a/compiler/rustc_ast_lowering/src/expr.rs +++ b/compiler/rustc_ast_lowering/src/expr.rs @@ -1,3 +1,4 @@ +use std::iter; use std::ops::ControlFlow; use std::sync::Arc; @@ -804,6 +805,37 @@ impl<'hir> LoweringContext<'_, 'hir> { } } + pub(super) fn forward_inline(&mut self, _span: Span, outer_hir_id: HirId, inner_hir_id: HirId) { + let Some(attrs) = self.attrs.get(&outer_hir_id.local_id) else { + return; + }; + + let Some((inline_attr, span)) = + find_attr!(*attrs, AttributeKind::Inline(attr, span) => (attr, span)) + else { + return; + }; + + let filtered_iter = attrs + .iter() + .cloned() + .filter(|attr| !matches!(attr, hir::Attribute::Parsed(AttributeKind::Inline(_, _)))); + + let filtered_attrs = self.arena.alloc_from_iter(filtered_iter); + + if filtered_attrs.is_empty() { + self.attrs.remove(&outer_hir_id.local_id); + } else { + self.attrs.insert(outer_hir_id.local_id, filtered_attrs); + } + + let attr = self.arena.alloc_from_iter(iter::once(hir::Attribute::Parsed( + AttributeKind::Inline(*inline_attr, *span), + ))); + + self.attrs.insert(inner_hir_id.local_id, attr); + } + /// Desugar `.await` into: /// ```ignore (pseudo-rust) /// match ::std::future::IntoFuture::into_future() { @@ -1167,6 +1199,7 @@ impl<'hir> LoweringContext<'_, 'hir> { ); this.maybe_forward_track_caller(body.span, closure_hir_id, expr.hir_id); + this.forward_inline(body.span, closure_hir_id, expr.hir_id); (parameters, expr) }); diff --git a/compiler/rustc_ast_lowering/src/item.rs b/compiler/rustc_ast_lowering/src/item.rs index f5b7065247a08..5cb5f552ac17f 100644 --- a/compiler/rustc_ast_lowering/src/item.rs +++ b/compiler/rustc_ast_lowering/src/item.rs @@ -1290,6 +1290,7 @@ impl<'hir> LoweringContext<'_, 'hir> { // FIXME(async_fn_track_caller): Can this be moved above? let hir_id = expr.hir_id; this.maybe_forward_track_caller(body.span, fn_id, hir_id); + this.forward_inline(body.span, fn_id, hir_id); (parameters, expr) })