Skip to content

#[inline(never)] does not work for async fn #106765

Open
@quininer

Description

@quininer

I tried this code:

#[inline(never)]
async foo() {
    // ...
}

https://rust.godbolt.org/z/97P1Goqhx

I expected to see this happen: logic code in async functions will not be inlined.

Instead, this happened: Only the function itself is not inlined, and the Future it returns is fully inlined.

I believe this is because async fn is desugared to

#[inline(never)]
fn foo() -> impl Future<Output = ()> {
    async move {
        // ...
    }
}

To get desired effect, I need to write

struct NoInline<F>(F);

impl<F: Future> Future for NoInline<F> {
    type Output = F::Output;

    #[inline(never)]
    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        // ...
    }
}

#[inline]
fn foo() -> impl Future<Output = ()> {
    async fn foo_inner() {
        // ...
    }
    NoInline(foo_inner())
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-async-awaitArea: Async & AwaitAsyncAwait-TriagedAsync-await issues that have been triaged during a working group meeting.C-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.WG-asyncWorking group: Async & await

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions