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

Unhelpful trait bound diagnostic hint for arbitrary self types #64934

Closed
arxanas opened this issue Sep 30, 2019 · 3 comments
Closed

Unhelpful trait bound diagnostic hint for arbitrary self types #64934

arxanas opened this issue Sep 30, 2019 · 3 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. F-arbitrary_self_types `#![feature(arbitrary_self_types)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@arxanas
Copy link

arxanas commented Sep 30, 2019

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.

@estebank estebank added A-diagnostics Area: Messages for errors, warnings, and lints F-arbitrary_self_types `#![feature(arbitrary_self_types)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. C-bug Category: This is a bug. labels Sep 30, 2019
@Nemo157
Copy link
Member

Nemo157 commented Oct 15, 2019

Dup of #57994

@estebank estebank added the D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. label Oct 15, 2019
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Feb 27, 2020
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Feb 28, 2020
bors added a commit that referenced this issue Feb 29, 2020
Add more context to E0599 errors

Point at the intermediary unfulfilled trait bounds.

Fix #52523, fix #61661, cc #36513, fix #68131, fix #64417, fix #61768, cc #57457, cc #9082, fix #57994, cc #64934, cc #65149.
@estebank
Copy link
Contributor

estebank commented Apr 3, 2020

Current output:

error[E0599]: no method named `resume` found for type parameter `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

CC #65149

@estebank
Copy link
Contributor

The suggestions now lead you to the correct code:

error[E0599]: no method named `resume` found for type parameter `T` in the current scope
   --> src/main.rs:13:30
    |
13  |         match self.generator.resume() {
    |                              ^^^^^^ method not found in `T`
    |
help: consider wrapping the receiver expression with the appropriate type
    |
13  |         match Pin::new(&mut self.generator).resume() {
    |               +++++++++++++               +

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 C-bug Category: This is a bug. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. F-arbitrary_self_types `#![feature(arbitrary_self_types)]` 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

3 participants