Skip to content

Unhelpful trait bound diagnostic hint for arbitrary self types #64934

Closed
@arxanas

Description

@arxanas

Playground: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=3d9fb14075d8fb9de8dea454fbb663e9

Code
#![feature(generator_trait)]

use std::ops::{Generator, GeneratorState};

struct GeneratorIterator<T> {
    generator: T,
}

impl<T: Generator<Return = ()>> Iterator for GeneratorIterator<T> {
    type Item = T::Yield;

    fn next(&mut self) -> Option<Self::Item> {
        match self.generator.resume() {
            GeneratorState::Yielded(x) => Some(x),
            GeneratorState::Complete(()) => None,
        }
    }
}

pub fn gen<Item, T: Generator<Yield = Item, Return = ()>>(
    generator: T,
) -> impl Iterator<Item = Item> {
    GeneratorIterator { generator }
}

fn main() {
    println!("Hello, world!");
}

Error message:

   Compiling playground v0.0.1 (/playground)
error[E0599]: no method named `resume` found for type `T` in the current scope
  --> src/main.rs:13:30
   |
13 |         match self.generator.resume() {
   |                              ^^^^^^ method not found in `T`
   |
   = help: items from traits can only be used if the type parameter is bounded by the trait
help: the following trait defines an item `resume`, perhaps you need to restrict type parameter `T` with it:
   |
9  | impl<T: std::ops::Generator + Generator<Return = ()>> Iterator for GeneratorIterator<T> {
   |      ^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error

For more information about this error, try `rustc --explain E0599`.
error: could not compile `playground`.

The error message hint is unhelpful because Generator is already a bound for T. Note that Generator::resume takes a Pin<&mut Self> as its self.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-bugCategory: This is a bug.D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.F-arbitrary_self_types`#![feature(arbitrary_self_types)]`T-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