Skip to content

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

Closed

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

  1. added
    A-diagnosticsArea: Messages for errors, warnings, and lints
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Aug 26, 2026
  2. added
    A-parserArea: The lexing & parsing of Rust source code to an AST
    on Aug 26, 2026
  3. raushan728 commented on Aug 27, 2026

    @raushan728
    Contributor

    @rustbot claim

  4. raushan728 commented on Aug 27, 2026

    @raushan728
    Contributor

    Hi! I'm new to Rust and was investigating this issue. I created a UI test to see how rustc handles invalid items inside traits. Just let me know if I missed anything or wrong

    Full code and output
    fn main() {}
    
    trait Trait1 {
        field_name: String,
        //~^ ERROR expected one of `!` or `::`, found `:`
    }
    
    trait Trait2 {
        pub pub_field_name: String,
        //~^ ERROR visibility `pub` is not followed by an item
        //~| ERROR non-item in item list
    }
    
    trait Trait3 {
        mut value: i32,
        //~^ ERROR non-item in item list
    }
    
    trait Trait4 {
        static VALUE: i32 = 5;
        //~^ ERROR associated `static` items are not allowed
    }
    
    trait Trait5 {
        impl SomeType {
        //~^ ERROR implementation is not supported in `trait`s or `impl`s
            fn method(&self) {}
        }
    }
    
    trait Trait6 {
        mod my_module;
        //~^ ERROR module is not supported in `trait`s or `impl`s
    }
    
    trait Trait7 {
        const CONST_VALUE;
        //~^ ERROR missing type for `const` item
    }
    
    trait Trait8 {
        pub type Item;
        //~^ ERROR visibility qualifiers are not permitted here
    }
    
    trait Trait9 {
        self: String,
        //~^ ERROR expected one of `!` or `::`, found `:`
    }
    
    trait Trait10 {
        // This is actually completely valid syntax in Rust!
        fn foo<T>(&self);
    }
    
    trait Trait11 {
        pub(self) fn private_foo(&self);
        //~^ ERROR visibility qualifiers are not permitted here
    }
    error: expected one of `!` or `::`, found `:`
      --> $DIR/trait-invalid-items.rs:4:15
       |
    LL | trait Trait1 {
       |              - while parsing this item list starting here
    LL |     field_name: String,
       |               ^ expected one of `!` or `::`
    LL | }
       | - the item list ends here
    
    error: visibility `pub` is not followed by an item
      --> $DIR/trait-invalid-items.rs:8:5
       |
    LL |     pub pub_field_name: String,
       |     ^^^ the visibility
       |
       = help: you likely meant to define an item, e.g., `pub fn foo() {}`
    
    error: non-item in item list
      --> $DIR/trait-invalid-items.rs:8:9
       |
    LL | trait Trait2 {
       |              - item list starts here
    LL |     pub pub_field_name: String,
       |         ^^^^^^^^^^^^^^ non-item starts here
    LL | }
       | - item list ends here
    
    error: non-item in item list
      --> $DIR/trait-invalid-items.rs:16:5
       |
    LL | trait Trait3 {
       |              - item list starts here
    LL |     mut value: i32,
       |     ^^^ non-item starts here
    LL | }
       | - item list ends here
    
    error: associated `static` items are not allowed
      --> $DIR/trait-invalid-items.rs:20:5
       |
    LL |     static VALUE: i32 = 5;
       |     ^^^^^^^^^^^^^^^^^^^^^^
    
    error: implementation is not supported in `trait`s or `impl`s
      --> $DIR/trait-invalid-items.rs:24:5
       |
    LL |     impl SomeType {
       |     ^^^^^^^^^^^^^
       |
       = help: consider moving the implementation out to a nearby module scope
    
    error: module is not supported in `trait`s or `impl`s
      --> $DIR/trait-invalid-items.rs:31:5
       |
    LL |     mod my_module;
       |     ^^^^^^^^^^^^^^
       |
       = help: consider moving the module out to a nearby module scope
    
    error: missing type for `const` item
      --> $DIR/trait-invalid-items.rs:36:22
       |
    LL |     const CONST_VALUE;
       |                      ^
       |
    help: provide a type for the item
       |
    LL |     const CONST_VALUE: <type>;
       |                      ++++++++
    
    error[E0449]: visibility qualifiers are not permitted here
      --> $DIR/trait-invalid-items.rs:41:5
       |
    LL |     pub type Item;
       |     ^^^ help: remove the qualifier
       |
       = note: trait items always share the visibility of their trait
    
    error: expected one of `!` or `::`, found `:`
      --> $DIR/trait-invalid-items.rs:47:9
       |
    LL | trait Trait9 {
       |              - while parsing this item list starting here
    LL |     self: String,
       |         ^ expected one of `!` or `::`
    LL | }
       | - the item list ends here
    
    error[E0449]: visibility qualifiers are not permitted here
      --> $DIR/trait-invalid-items.rs:56:5
       |
    LL |     pub(self) fn private_foo(&self);
       |     ^^^^^^^^^ help: remove the qualifier
       |
       = note: trait items always share the visibility of their trait
    
    error: aborting due to 11 previous errors
    
    For more information about this error, try `rustc --explain E0449`.
    

    Mostly is ok here are finding

    fn main() {}
    
    trait Trait1 {
        field_name: String,
        //~^ ERROR expected one of `!` or `::`, found `:`
    }
    
    trait Trait2 {
        pub pub_field_name: String,
        //~^ ERROR visibility `pub` is not followed by an item
        //~| ERROR non-item in item list
    }
    
    trait Trait3 {
        mut value: i32,
        //~^ ERROR non-item in item list
    }
    
    trait Trait4 {
        self: String,
        //~^ ERROR expected one of `!` or `::`, found `:`
    }
    error: expected one of `!` or `::`, found `:`
      --> $DIR/trait-invalid-items.rs:4:15
       |
    LL | trait Trait1 {
       |              - while parsing this item list starting here
    LL |     field_name: String,
       |               ^ expected one of `!` or `::`
    LL |
    LL | }
       | - the item list ends here
    
    error: visibility `pub` is not followed by an item
      --> $DIR/trait-invalid-items.rs:9:5
       |
    LL |     pub pub_field_name: String,
       |     ^^^ the visibility
       |
       = help: you likely meant to define an item, e.g., `pub fn foo() {}`
    
    error: non-item in item list
      --> $DIR/trait-invalid-items.rs:9:9
       |
    LL | trait Trait2 {
       |              - item list starts here
    LL |     pub pub_field_name: String,
       |         ^^^^^^^^^^^^^^ non-item starts here
    ...
    LL | }
       | - item list ends here
    
    error: non-item in item list
      --> $DIR/trait-invalid-items.rs:15:5
       |
    LL | trait Trait3 {
       |              - item list starts here
    LL |     mut value: i32,
       |     ^^^ non-item starts here
    LL |
    LL | }
       | - item list ends here
    
    error: expected one of `!` or `::`, found `:`
      --> $DIR/trait-invalid-items.rs:20:9
       |
    LL | trait Trait4 {
       |              - while parsing this item list starting here
    LL |     self: String,
       |         ^ expected one of `!` or `::`
    LL |
    LL | }
       | - the item list ends here
    
    error: aborting due to 5 previous errors
    

    trait Trait1 {
    field_name: String,
    //~^ ERROR expected one of ! or ::, found :
    }

    needs improvement like

    error: fields are not allowed in traits
    help: use a method instead, e.g., `fn field_name(&self) -> &String;`
    

    trait Trait2 {
    pub pub_field_name: String,
    //^ ERROR visibility pub is not followed by an item
    //
    | ERROR non-item in item list
    }

    The first error is OK but the second one "non-item in item list" is confusing. What does "non-item" mean exactly?
    It doesn't tell the user what they did wrong. They don't know what a "non-item" is or why it's wrong.

    trait Trait3 {
    mut value: i32,
    //~^ ERROR non-item in item list
    }

    Same as above. "non-item" is vague.

    trait Trait4 {
    self: String,
    //~^ ERROR expected one of ! or ::, found :
    }

    Need improvement

  5. wischi-chr commented on Aug 27, 2026

    @wischi-chr
    Author

    I guess the "non-item" error is the same situation as with the "expected one of ! or ::, found :" that the parser/lexer wasn't built to to handle those items in a trait (that actually shouldn't be there in correct Rust code) and thus can't give good diagnostics.

    I'm not sure how the Rust compiler typically handles that but I guess (if we want to fix that) it would have to parse/allow more items that actually don't belong in a trait (for example a field) to tell the developer that this is not allowed.

  6. added a commit that references this issue on Sep 23, 2026
    11682a4
  7. added a commit that references this issue on Sep 23, 2026
    8824bb3
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