rust-lang / rust-lang/rust

Change in leaf function causes unrelated compilation error in root function

Open
#135,652 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-lifetimes A-trait-system C-bug S-has-mcve T-compiler T-types
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Description

I encountered an issue where changing the leaf function causes a compilation error in the root function, but the change doesn't affect types or auto traits of the function.
Commenting out router() makes code to compile.

Minimal Example

use axum::routing::{post, Router};

use futures_util::StreamExt;
use std::collections::HashMap;
use std::sync::Arc;

struct Transaction {
    f1: (),
    f2: (),
}

pub(crate) struct SplitAddress(());

async fn handler_inner(ctx: Context) {
    let transactions = vec![];

    // This causes the lifetime issue:
    let iter = transactions
        .iter()
        .map(|x: &Transaction| (x.f1, SplitAddress(x.f2)));

    // This works:
    // let iter: Vec<_> = transactions
    //     .iter()
    //     .map(|x: &Transaction| (x.f1, SplitAddress(x.f2)))
    //     .collect();

    ctx.0.streaming_work(iter.into_iter()).await;
}

async fn handler() -> Result<String, String> {
    let ctx = Context(Arc::new(Service()));
    handler_inner(ctx).await;
    Ok("ok".to_string())
}

// or comment out router
pub async fn router() -> Router {
    Router::new().route("/bla", post(handler))
}

#[derive(Clone)]
pub struct Context(Arc<Service>);

struct Service();

impl Service {
    pub(crate) async fn streaming_work(
        &self,
        data: impl Iterator<Item = ((), SplitAddress)>,
    ) -> HashMap<(), Vec<String>> {
        futures_util::stream::iter(data)
            .map(|_| async move { todo!() })
            .buffer_unordered(100)
            .filter_map(|x| async move { x })
            .collect()
            .await
    }
}

repo

Error Message

error: implementation of `FnOnce` is not general enough
  --> src/lib.rs:39:33
   |
39 |     Router::new().route("/bla", post(handler))
   |                                 ^^^^^^^^^^^^^ implementation of `FnOnce` is not general enough
   |
   = note: closure with signature `fn(&'0 Transaction) -> ((), SplitAddress)` must implement `FnOnce<(&'1 Transaction,)>`, for any two lifetimes `'0` and `'1`...
   = note: ...but it actually implements `FnOnce<(&Transaction,)>`

error: could not compile `rust-issue` (lib) due to 1 previous error

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 reproducing the error from the minimal example in the linked repository, focusing on src/lib.rs and the router() call at line 39. Compare the compiling and failing iterator forms, then investigate the compiler's handling of the async handler and closure lifetimes. Done means the example compiles without requiring the iterator workaround and the regression is covered by an appropriate test.

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
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.