Skip to content

"not general enough" error points to the async spawn site as opposed to the offending code #85516

Open
@guswynn

Description

@guswynn

Given the following code:

Playground: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=44a059128482832f4b01a5a4897fa08d
(I'm sorry I couldn't minimize more)

use anyhow::Result;
use futures::stream::{iter, StreamExt};
use std::collections::{BTreeMap, BTreeSet};

#[derive(Debug, PartialEq, Eq)]
pub struct Change<'a> {
    key: &'a String,
}

#[derive(Default)]
pub struct Processor {
    data: BTreeMap<String, String>,
    base_data: BTreeMap<String, String>,
}

impl Processor {
    async fn something_async(&self, s: &String) {}

    async fn perform_scheduling(&mut self) {
        let paired_data: Vec<(&String, &String, Option<&String>)> = self
            .base_data
            .iter()
            .map(|(k, v)| (k, v, self.data.get(k)))
            .collect();

        let changes: Vec<Change> = paired_data
            .iter()
            .filter_map(|(k, v, d_v)| Some(vec![Change { key: k }]))
            .flatten()
            .collect();

        // do non-mutable async work before we do mutable work later
        let this: &Processor = &self;
        let changes_stream = iter(changes)
            .map(|ch| async move {
                // immutable access to data
                let v = this.data.get(ch.key);
                this.something_async(ch.key).await;
                ch.key.clone()
            })
            .buffer_unordered(10);

        // then do mutations
        let mutations: Vec<_> = changes_stream.collect().await;
        for mutation in mutations {
            self.data
                .entry(mutation)
                .or_default()
                .make_ascii_uppercase();
        }
    }

    pub async fn schedule(mut self) -> Result<()> {
        loop {
            self.perform_scheduling().await
        }
    }
}

fn main() {
    let runtime = tokio::runtime::Runtime::new().unwrap();

    runtime.spawn(Processor::default().schedule());
}

The current output is:

error: implementation of `FnOnce` is not general enough
  --> src/main.rs:63:13
   |
63 |     runtime.spawn(Processor::default().schedule());
   |             ^^^^^ implementation of `FnOnce` is not general enough
   |
   = note: closure with signature `fn(Change<'0>) -> impl futures::Future` must implement `FnOnce<(Change<'1>,)>`, for any two lifetimes `'0` and `'1`...
   = note: ...but it actually implements `FnOnce<(Change<'_>,)>`

note that Before 1.52.1 (in this case 1.50), the error message is worse:

error[E0308]: mismatched types
  --> src/main.rs:62:13
   |
62 |     runtime.spawn(Processor::default().schedule());
   |             ^^^^^ one type is more general than the other
   |
   = note: expected type `FnOnce<(Change<'_>,)>`
              found type `FnOnce<(Change<'_>,)>`

Notes:

When I first encountered this error (the 1.50 version), I didn't remember that fn-trait args are tuples, and I was convinced that I had NO tuples in my code as written, so couldn't find the error, even manually by reading the code.

In both error cases, the better 1.52 one and worse 1.50 version, I couldn't really figure out how to resolve, so I just moved the String clone (it wasn't a string at work, but thats a simple type I used as a placeholder) into the first iterator and out of the buffered stream iterator

My guess is that my fairly-spicy use of a immutable reborrow of self to resolve this lifetime issue: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=48dbe41cb31212353d874de4fb63d9e8 may have something to do with this. I believe this ownership error is because I am forced to use an async move block to move ch into the future, which also tries to move the &mut self?

My 2 questions are:

  1. can this error point to the correct site of the problem
  2. is there a way to resolve this error without moving the k clone into the first iterator

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions