Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion compiler/rustc_ast_lowering/src/expr.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use std::mem;
use std::ops::ControlFlow;
use std::sync::Arc;
use std::{iter, mem};

use rustc_ast::*;
use rustc_ast_pretty::pprust::expr_to_string;
use rustc_data_structures::stack::ensure_sufficient_stack;
use rustc_errors::msg;
use rustc_hir as hir;
use rustc_hir::attrs::AttributeKind;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::definitions::DefPathData;
use rustc_hir::{HirId, Target, find_attr};
Expand Down Expand Up @@ -829,6 +830,36 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
}
}

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, 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);

@JonathanBrouwer JonathanBrouwer Jun 21, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this code reachable with the early-return above?

View changes since the review

} else {
self.attrs.insert(outer_hir_id.local_id, filtered_attrs);

@JonathanBrouwer JonathanBrouwer Jun 21, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we overriding the attrs from outer_hir_id here?

View changes since the review

}

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 `<expr>.await` into:
/// ```ignore (pseudo-rust)
/// match ::std::future::IntoFuture::into_future(<expr>) {
Expand Down Expand Up @@ -1199,6 +1230,7 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
);

this.maybe_forward_track_caller(body.span, closure_hir_id, expr.hir_id);
this.forward_inline(body.span, closure_hir_id, expr.hir_id);

@JonathanBrouwer JonathanBrouwer Jun 21, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add some tests for this feature?

View changes since the review

@JonathanBrouwer JonathanBrouwer Jun 21, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the lang team decision:

We talked about this in the lang call today. We want attributes like this to apply to "where the code is", i.e. to the poll function in this case. The wrapper function, on the other hand, should always be inlined.

Make sure your tests cover this.
I suspect the wrapper function is already marked as inline somewhere, if it is not could you or someone else open a separate PR that does this?


(parameters, expr)
});
Expand Down
Loading