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

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

Closed
tomjakubowski opened this issue Jan 12, 2015 · 5 comments · Fixed by #55808
Closed

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

tomjakubowski opened this issue Jan 12, 2015 · 5 comments · Fixed by #55808
Labels
A-associated-items Area: Associated items (types, constants & functions) A-diagnostics Area: Messages for errors, warnings, and lints A-trait-system Area: Trait system C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. WG-diagnostics Working group: Diagnostics

Comments

@tomjakubowski
Copy link
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>?.

@sfackler sfackler added the A-diagnostics Area: Messages for errors, warnings, and lints label Jan 12, 2015
@huonw huonw added A-trait-system Area: Trait system A-associated-items Area: Associated items (types, constants & functions) labels Jan 12, 2015
@nikomatsakis
Copy link
Contributor

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

@Manishearth
Copy link
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
Copy link
Member

frewsxcv commented Sep 1, 2015

Visiting for triage: nothing has changed here

playpen link

@birkenfeld
Copy link
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?)

@steveklabnik steveklabnik added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Mar 9, 2017
@Mark-Simulacrum Mark-Simulacrum added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jul 22, 2017
@estebank estebank added E-needs-mentor WG-diagnostics Working group: Diagnostics labels Oct 13, 2017
@estebank
Copy link
Contributor

estebank commented Oct 13, 2017

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.

bors added a commit that referenced this issue Nov 23, 2018
Suggest correct syntax when writing type arg instead of assoc type

- When confusing an associated type with a type argument, suggest the appropriate syntax. Given `Iterator<isize>`, suggest `Iterator<Item = isize>`.
- When encountering multiple missing associated types, emit only one diagnostic.
- Point at associated type def span for context.
- Point at each extra type argument.

Follow up to #48288, fix #20977.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-associated-items Area: Associated items (types, constants & functions) A-diagnostics Area: Messages for errors, warnings, and lints A-trait-system Area: Trait system C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. WG-diagnostics Working group: Diagnostics
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants