Skip to content

Compiler error could suggest using an associated type for e.g. Iterator<T> #20977

Closed
@tomjakubowski

Description

@tomjakubowski
Contributor

If you accidentally write a bound like:

pub struct Foo<T, I: Iterator<T>> { /* ... */ }

The error message is:

err.rs:3:22: 3:33 error: wrong number of type arguments: expected 0, found 1
err.rs:3 pub struct Foo<T, I: Iterator<T>> { x: I }
                              ^~~~~~~~~~~

The compiler could be more helpful in these cases where a trait takes 0 arguments but has an associated type and suggest something like: Iterator has an associated type. Did you mean: I: Iterator<Item=T>?.

Activity

nikomatsakis

nikomatsakis commented on Jan 12, 2015

@nikomatsakis
Contributor

cc @Manishearth this could be a good place for a custom error attribute like the one you recently implemented

Manishearth

Manishearth commented on Jan 12, 2015

@Manishearth
Member

Ooh, this looks doable.

Unfortunately I don't see any opportunity for sharing much code here, but I can try to move some of the code out.

frewsxcv

frewsxcv commented on Sep 1, 2015

@frewsxcv
Member

Visiting for triage: nothing has changed here

playpen link

birkenfeld

birkenfeld commented on May 2, 2016

@birkenfeld
Contributor

Current situation: the original error hasn't changed, but we get a second one that complains about the missing Item associated type:

<anon>:1:25: 1:40 error: wrong number of type arguments: expected 0, found 1 [E0244]
<anon>:1 pub struct Foo { i: Box<Iterator<isize>> }
                                 ^~~~~~~~~~~~~~~
<anon>:1:25: 1:40 help: see the detailed explanation for E0244
<anon>:1:25: 1:40 error: the value of the associated type `Item` (from the trait `core::iter::Iterator`) must be specified [E0191]
<anon>:1 pub struct Foo { i: Box<Iterator<isize>> }
                                 ^~~~~~~~~~~~~~~
<anon>:1:25: 1:40 help: see the detailed explanation for E0191

... which should clear it up. (But probably not if the associated type has a default?)

added
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
on Mar 9, 2017
estebank

estebank commented on Oct 13, 2017

@estebank
Contributor

librustc_typeck/astconv.rs, methods create_substs_for_ast_path, check_type_argument_count and conv_object_ty_poly_trait_ref will need to be changed to not emit both errors, but rather condense them into one:

error[E0191]: the value of the associated type `Item` (from the trait `std::iter::Iterator`) must be specified
 --> src/main.rs:1:25
  |
1 | pub struct Foo { i: Box<Iterator<isize>> }
  |                         ^^^^^^^^^-----^ missing associated type `Item` value, found 1 type argument instead
  |                                  |
  |                                  help: did you mean to set the associated type?: `Item = isize`

error: aborting due to previous error

Care should be taken when some type arguments and associated items are expected. In those case, it's probably better to keep the current output to avoid confusion with the tradeoff of verbosity.

2 remaining items

Loading
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-associated-itemsArea: Associated items (types, constants & functions)A-diagnosticsArea: Messages for errors, warnings, and lintsA-trait-systemArea: Trait systemC-enhancementCategory: An issue proposing an enhancement or a PR with one.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.WG-diagnosticsWorking group: Diagnostics

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @steveklabnik@birkenfeld@nikomatsakis@frewsxcv@tomjakubowski

      Issue actions

        Compiler error could suggest using an associated type for e.g. Iterator<T> · Issue #20977 · rust-lang/rust