Skip to content

Nightly (2021): trait bound error for missing use #87857

@taqtiqa-mark

Description

@taqtiqa-mark

Using this rust version:

$ cargo --version
cargo 1.56.0-nightly (cc17afbb0 2021-08-02)

Given the following code:

use lazy_static::lazy_static;
use rand::distributions::{Distribution, Uniform};
use signal_hook::consts::signal::*;
use signal_hook_tokio::Signals;
use std::io::Error;
use std::time::Duration;
use tokio::time::{sleep, Instant};

lazy_static! {
    static ref START_TIME: Instant = Instant::now();
}

async fn handle_signals(signals: Signals) {
    let mut signals = signals.fuse();
    while let Some(signal) = signals.next().await {
        match signal {
            SIGTERM | SIGINT | SIGQUIT => {
                // Lets get out of here...
                std::process::exit(1);
            }
            _ => unreachable!(),
        }
    }
}

#[tokio::main]
async fn main() -> Result<(), Error> {
    let signals = Signals::new(&[SIGHUP, SIGTERM, SIGINT, SIGQUIT])?;
    let handle = signals.handle();
    let signals_task = tokio::spawn(handle_signals(signals));

    let page = get_page(42).await;
    println!("Page #42: {:?}", page);

    // Terminate the signal stream.
    handle.close();
    signals_task.await?;
    Ok(())
}

async fn get_page(i: usize) -> Vec<usize> {
    let millis = Uniform::from(5_000..6_000).sample(&mut rand::thread_rng());
    println!(
        "[{}] # get_page({}) will complete in {} ms on {:?}",
        START_TIME.elapsed().as_millis(),
        i,
        millis,
        std::thread::current().id()
    );

    sleep(Duration::from_millis(millis)).await;
    println!(
        "[{}] # get_page({}) completed",
        START_TIME.elapsed().as_millis(),
        i
    );

    (10 * i..10 * (i + 1)).collect()
}

The current output is:

error[E0599]: the method `fuse` exists for struct `signal_hook_tokio::SignalsInfo`, but its trait bounds were not satisfied
  --> regatta/examples/01-pages-hello-ok-int-c.rs:15:31
   |
15 |     let mut signals = signals.fuse();
   |                               ^^^^ method cannot be called on `signal_hook_tokio::SignalsInfo` due to unsatisfied trait bounds
   |
  ...
   |
92 | pub struct SignalsInfo<E: Exfiltrator = SignalOnly>(OwningSignalIterator<UnixStream, E>);
   | ----------------------------------------------------------------------------------------- doesn't satisfy `signal_hook_tokio::SignalsInfo: Iterator`
   |
   = note: the following trait bounds were not satisfied:
           `signal_hook_tokio::SignalsInfo: Iterator`
           which is required by `&mut signal_hook_tokio::SignalsInfo: Iterator`

For more information about this error, try `rustc --explain E0599`.

Ideally the output should be as shown in this comment in issue #36513 (via PR #69255)and this comment (Feb 2020) in issue #40375 (which seems to have changed back to uninformative per this comment (Jun 2020))
Actually the output we should see is the same as in the comment below (a subtle variation of this code) where helpful and correct messages are produced:

   ...
   = help: items from traits can only be used if the trait is in scope
   = note: the following trait is implemented but not in scope; perhaps add a `use` for it:
           `use futures::StreamExt;`

To fix, add use futures::stream::{StreamExt};

This issue has tripped, at least two (new) users.
A question to Discord #beginners about the likely root cause of this error wasn't responded to so its a subtle issue.

Activity

added
A-diagnosticsArea: Messages for errors, warnings, and lints
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
on Aug 8, 2021
taqtiqa-mark

taqtiqa-mark commented on Aug 8, 2021

@taqtiqa-mark
Author

The expected error message in the OP is produced by this subtle change into a different case:

// add this line:
use futures::stream::{StreamExt};
use lazy_static::lazy_static;
use rand::distributions::{Distribution, Uniform};
use signal_hook::consts::signal::*;
use signal_hook_tokio::Signals;
use std::io::Error;
use std::time::Duration;
use tokio::time::{sleep, Instant};

lazy_static! {
    static ref START_TIME: Instant = Instant::now();
}

// add this module:
pub mod p2p {
    //omit this line
    //use futures::stream::{StreamExt};

    pub async fn handle_signals(signals: signal_hook_tokio::Signals) {
        let mut signals = signals.fuse();
        while let Some(signal) = signals.next().await {
            match signal {
                signal_hook::consts::SIGTERM | signal_hook::consts::SIGINT | signal_hook::consts::SIGQUIT => {
                    // Lets get out of here...
                    println!("Trapped signal to quit. Exiting");
                    std::process::exit(1);
                }
                _ => unreachable!(),
            }
        }
    }
}

#[tokio::main]
async fn main() -> Result<(), Error> {
    let signals = signal_hook_tokio::Signals::new(&[SIGHUP, SIGTERM, SIGINT, SIGQUIT])?;
    let handle = signals.handle();
    // remember the module
    let signals_task = tokio::spawn(p2p::handle_signals(signals));

    let page = get_page(42).await;
    println!("Page #42: {:?}", page);

    // Terminate the signal stream.
    handle.close();
    signals_task.await?;
    Ok(())
}

async fn get_page(i: usize) -> Vec<usize> {
    let millis = Uniform::from(5_000..6_000).sample(&mut rand::thread_rng());
    println!(
        "[{}] # get_page({}) will complete in {} ms on {:?}",
        START_TIME.elapsed().as_millis(),
        i,
        millis,
        std::thread::current().id()
    );

    sleep(Duration::from_millis(millis)).await;
    println!(
        "[{}] # get_page({}) completed",
        START_TIME.elapsed().as_millis(),
        i
    );

    (10 * i..10 * (i + 1)).collect()
}

Which produces this feedback gem:

   |19 |         let mut signals = signals.fuse();
   |                                   ^^^^ method cannot be called on `signal_hook_tokio::SignalsInfo` due to unsatisfied trait bounds   |
  ...
   |
92 | pub struct SignalsInfo<E: Exfiltrator = SignalOnly>(OwningSignalIterator<UnixStream, E>);
   | ----------------------------------------------------------------------------------------- doesn't satisfy `signal_hook_tokio::SignalsInfo: Iterator`
   |
   = note: the following trait bounds were not satisfied:
           `signal_hook_tokio::SignalsInfo: Iterator`
           which is required by `&mut signal_hook_tokio::SignalsInfo: Iterator`
   = help: items from traits can only be used if the trait is in scope
   = note: the following trait is implemented but not in scope; perhaps add a `use` for it:
           `use futures::StreamExt;`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

        Participants

        @taqtiqa-mark

        Issue actions

          Nightly (2021): trait bound error for missing use · Issue #87857 · rust-lang/rust