Skip to content

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

Open
@inanna-malick

Description

@inanna-malick

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

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsT-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