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

given Vec<foo:Bar> (with single ':') in a struct field, rustc provides an unintuitive affiliated types error #92685

Open
inanna-malick opened this issue Jan 9, 2022 · 2 comments
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@inanna-malick
Copy link

Given the following code: (https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=c485d59d73a93f4845ea71f4dd4a08b9

mod my_module {
    struct A;
}

// FAILS WITH UNEXPECTED ASSOCIATED TYPES ERRORS
struct MyStruct {
    // NOTE: Vec<> wrapper required to cause associated types error
    my_field: Vec<my_module:A>,
}

The current output is:

error: associated type bounds are not allowed within structs, enums, or unions
 --> src/lib.rs:8:19
  |
8 |     my_field: Vec<my_module: A>,
  |                   ^^^^^^^^^^^^

error[E0405]: cannot find trait `A` in this scope
 --> src/lib.rs:8:30
  |
6 | struct MyStruct {
  |                - help: you might be missing a type parameter: `<A>`
7 |     // NOTE: Vec<> wrapper required to cause associated types error
8 |     my_field: Vec<my_module: A>,
  |                              ^ not found in this scope

error[E0658]: associated type bounds are unstable
 --> src/lib.rs:8:19
  |
8 |     my_field: Vec<my_module: A>,
  |                   ^^^^^^^^^^^^
  |
  = note: see issue #52662 <https://github.com/rust-lang/rust/issues/52662> for more information

error[E0107]: this struct takes at least 1 generic argument but 0 generic arguments were supplied
 --> src/lib.rs:8:15
  |
8 |     my_field: Vec<my_module: A>,
  |               ^^^ expected at least 1 generic argument
  |
help: add missing generic argument
  |
8 |     my_field: Vec<T, my_module: A>,
  |                   ++

error[E0229]: associated type bindings are not allowed here
 --> src/lib.rs:8:19
  |
8 |     my_field: Vec<my_module: A>,
  |                   ^^^^^^^^^^^^ associated type not allowed here

Some errors have detailed explanations: E0107, E0229, E0405, E0658.

Ideally the output would point out the potential that the user had meant to type '::' instead of ':'.

Rationale: I hit this and I spent a few minutes trying to figure out what was going on before I realized that I had made a type and written ':' instead of '::' - it's the kind of small typo the eye easily glazes over

cc @estebank per https://twitter.com/ekuber/status/1479928446218293249, thanks so much for putting so much time into ergonomics <3

@inanna-malick inanna-malick added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jan 9, 2022
@TaKO8Ki
Copy link
Member

TaKO8Ki commented Mar 6, 2022

It seems that this problem has already been solved in nightly.

ref: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=c816dd29ce8ce457fbdaa7f0eb66d2d3

@estebank
Copy link
Contributor

estebank commented Mar 7, 2022

Looking at the output of that link, I think it'd be nice to also handle this (in the parser):

error: expected `,`, or `}`, found `:`
  --> src/lib.rs:18:24
   |
18 |     my_field: my_module:A,
   |                        ^

It would also be nice to reduce the number of errors we see for the original case to maybe one or two at most:

error[E0405]: cannot find trait `A` in this scope
  --> src/lib.rs:64:27
   |
64 |   my_field: Vec<my_module:A>,
   |                           ^ not found in this scope
   |
help: you might have meant to write a path instead of an associated type bound
   |
64 |   my_field: Vec<my_module::A>,
   |                          ~~
help: you might be missing a type parameter
   |
63 | struct MyStruct2<A> {
   |                 +++

error[E0658]: associated type bounds are unstable
  --> src/lib.rs:64:17
   |
64 |   my_field: Vec<my_module:A>,
   |                 ^^^^^^^^^^^
   |
   = note: see issue #52662 <https://github.com/rust-lang/rust/issues/52662> for more information
   = help: add `#![feature(associated_type_bounds)]` to the crate attributes to enable

error[E0107]: this struct takes at least 1 generic argument but 0 generic arguments were supplied
  --> src/lib.rs:64:13
   |
64 |   my_field: Vec<my_module:A>,
   |             ^^^ expected at least 1 generic argument
   |
help: add missing generic argument
   |
64 |   my_field: Vec<T, my_module:A>,
   |                 ++

error[E0229]: associated type bindings are not allowed here
  --> src/lib.rs:64:17
   |
64 |   my_field: Vec<my_module:A>,
   |                 ^^^^^^^^^^^ associated type not allowed here

@TaKO8Ki TaKO8Ki self-assigned this Mar 10, 2022
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Mar 11, 2022
…for-struct-field-type, r=cjgillot

Suggest using double colon when a struct field type include single colon

rust-lang#92685
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Mar 11, 2022
…for-struct-field-type, r=cjgillot

Suggest using double colon when a struct field type include single colon

rust-lang#92685
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 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