Skip to content

Confusing diagnostic when trying to define a field in a trait #161815

Description

@wischi-chr

Code

trait MyTrait {
  field_name: String,
}

Current output

error: expected one of `!` or `::`, found `:`
  --> src/main.rs:25:15
   |
24 | trait MyTrait {
   |               - while parsing this item list starting here
25 |     field_name: String,
   |               ^ expected one of `!` or `::`
26 | }
   | - the item list ends here

Desired output

Maybe something like "fields not supported/allowed in traits, use getter/setter function instead"

Rust Version

rustc 1.97.0 (2d8144b78 2026-07-07)
binary: rustc
commit-hash: 2d8144b7880597b6e6d3dfd63a9a9efae3f533d3
commit-date: 2026-07-07
host: x86_64-unknown-linux-gnu
release: 1.97.0
LLVM version: 22.1.6

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-parserArea: The lexing & parsing of Rust source code to an ASTT-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