You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Confusing diagnostic when trying to define a field in a trait #161815
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"
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
fnmain(){}traitTrait1{
field_name:String,//~^ ERROR expected one of `!` or `::`, found `:`}traitTrait2{pub pub_field_name:String,//~^ ERROR visibility `pub` is not followed by an item//~| ERROR non-item in item list}traitTrait3{
mut value:i32,//~^ ERROR non-item in item list}traitTrait4{staticVALUE:i32 = 5;//~^ ERROR associated `static` items are not allowed}traitTrait5{implSomeType{//~^ ERROR implementation is not supported in `trait`s or `impl`sfnmethod(&self){}}}traitTrait6{mod my_module;//~^ ERROR module is not supported in `trait`s or `impl`s}traitTrait7{constCONST_VALUE;//~^ ERROR missing type for `const` item}traitTrait8{pubtypeItem;//~^ ERROR visibility qualifiers are not permitted here}traitTrait9{self:String,//~^ ERROR expected one of `!` or `::`, found `:`}traitTrait10{// This is actually completely valid syntax in Rust!fnfoo<T>(&self);}traitTrait11{pub(self)fnprivate_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
fnmain(){}traitTrait1{
field_name:String,//~^ ERROR expected one of `!` or `::`, found `:`}traitTrait2{pub pub_field_name:String,//~^ ERROR visibility `pub` is not followed by an item//~| ERROR non-item in item list}traitTrait3{
mut value:i32,//~^ ERROR non-item in item list}traitTrait4{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 :
}
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.
Activity
raushan728 commentedon Aug 27, 2026
@rustbot claim
raushan728 commentedon Aug 27, 2026
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
Mostly is ok here are finding
needs improvement like
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.
Same as above. "non-item" is vague.
Need improvement
wischi-chr commentedon Aug 27, 2026
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.
Rollup merge of rust-lang#162103 - raushan728:issues/161815, r=davidtwco
Unrolled build for #162103 in rollup 163227