Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
guswynn opened this issue May 20, 2021 · 2 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@guswynn
Copy link
Contributor

guswynn commented May 20, 2021

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
@guswynn guswynn added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 20, 2021
@guswynn
Copy link
Contributor Author

guswynn commented May 20, 2021

For the original error, I can't see anything special about map, buffer_unordered, or collect that would prevent me from using a specific lifetime. Both this and the Change's in the iterator stream borrow from self...

@guswynn
Copy link
Contributor Author

guswynn commented May 25, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

1 participant