rust-lang / rust-lang/rust

Compiling fails as a sync function but succeeds in async function

Open
#119,291 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-borrow-checker A-lifetimes T-compiler T-types
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

The code can be compiled, but uncommentting the non_async_function2 and calling it in async_function will fail the compiling.

pub fn fx<'a, T: 'a, U>(_: &mut &'a mut T) -> fn(&mut &'a mut U) {
    fn f<'b, T: 'b>(_: &mut &'b mut T) {}
    f::<U>
}

struct PrintOnDrop<'a>(&'a mut String);
impl<'a> Drop for PrintOnDrop<'a> {
    fn drop(&mut self) {}
}

pub async fn async_function() {
    //pub fn non_async_function2() { 
        let mut m = "m".to_owned();
        let mut n = "n".to_owned();
        {
            let mut x1 = &mut m; // &'1 mut m
            let f = fx(&mut x1);
    
            let m1 = PrintOnDrop(x1);
            {
                let mut n1 = PrintOnDrop(&mut n);
    
                println!();
                drop(m1);
    
                f(&mut &mut n1);  // &mut &'1 mut n1
            }
        }
   // }
   // non_async_function2();
}

pub fn usage() -> Box<dyn std::future::Future<Output = ()>> {
    Box::new(async_function())
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by compiling the supplied reproducer with the non_async_function2 block uncommented, then compare it with the async_function version that succeeds. Focus on the lifetime interactions in fx, PrintOnDrop, and the call to f; done means the reported code compiles consistently in both contexts without regressing the async case.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.