Skip to content

Change in leaf function causes unrelated compilation error in root function #135652

Open
@0xdeafbeef

Description

@0xdeafbeef

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lifetimesArea: Lifetimes / regionsA-trait-systemArea: Trait systemT-typesRelevant to the types team, which will review and decide on the PR/issue.needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions