rust-lang / rust-lang/rust

Confusing diagnostic when trying to define a field in a trait

Open
#161,815 3 comments 1 reaction 1 assignee View on GitHub

@raushan728 is already working on this.

Since Aug 27, 2026.

A-diagnostics A-parser T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.