rust-lang / rust-analyzer

A Rust compiler front-end for IDEs
https://rust-analyzer.github.io/
Apache License 2.0
14.33k stars 1.61k forks source link

Implicit drops aren't rendered in `async` functions #17514

Open kristof-mattei opened 4 months ago

kristof-mattei commented 4 months ago

rust-analyzer version: rust-analyzer version: 0.3.2011-standalone [/home/kristof/.vscode-server/extensions/rust-lang.rust-analyzer-0.3.2011-linux-x64/server/rust-analyzer]

rustc version: rustc 1.79.0 (129f3b996 2024-06-10)

editor or extension: VSCode v1.90.2 (user setup) / Rust Analyzer extension: v0.3.2011

relevant settings: "rust-analyzer.inlayHints.implicitDrops.enable": true,

repository link (if public, optional): https://github.com/kristof-mattei/async-no-implicit-drop.git

code snippet to reproduce:

#[tokio::main]
async fn main() {
    println!("Start: async_test");
    async_test().await;
    println!("End: async_test");
    println!();
    println!();
    println!("Start: non_async_test");
    non_async_test();
    println!("End: non_async_test");
}

#[allow(clippy::unused_async)]
async fn async_test() {
    {
        let drop_test = DropTest::new("async_test".into());

        drop_test.do_something();
    }
}

fn non_async_test() {
    {
        let drop_test = DropTest::new("non_async_test".into());

        drop_test.do_something();
    }
}

struct DropTest {
    from: String,
}

impl DropTest {
    fn new(from: String) -> Self {
        Self { from }
    }

    #[allow(clippy::unused_self)]
    fn do_something(&self) {
        println!("From: {}", self.from);
    }
}

impl Drop for DropTest {
    fn drop(&mut self) {
        println!("Dropped: {}", self.from);
    }
}

When enabling implicitDrops, they aren't rendered in async functions. Note that having a Drop function doesn't change the whether drop(drop_test) is rendered or not, I just put it there to prove that it was actually dropped.

Screenshot from Repo above in VSCode:

image
ShoyuVanilla commented 4 months ago

This is because Rust Analyzer isn't lowering async blocks into places when building mir https://github.com/rust-lang/rust-analyzer/blob/f2afcb874e7410121c366ae601660abe327e320b/crates/hir-ty/src/mir/lower.rs#L874