Skip to content

Poor error message with assigning invalid constant to enum variant #28402

@retep998

Description

@retep998
pub struct Foo(pub u32);
pub const CONSTANT: Foo = Foo(1);
#[derive(Clone)]
enum Enum {
    Variant = CONSTANT,
}
fn main() {}
<anon>:2:27: 2:33 error: constant evaluation error: unsupported constant expr [E0080]
<anon>:2 pub const CONSTANT: Foo = Foo(1);
                                   ^~~~~~
<anon>:2:27: 2:33 help: see the detailed explanation for E0080
error: aborting due to previous error

Note specifically that the error points to the constant definition and not to its use in the enum, even though it is the enum which is in the wrong. This led to a rather difficult to diagnose error when working on another project.

If the struct is changed to just pub struct Foo;, then another similarly confusing error occurs:

<anon>:2:27: 2:30 error: constant evaluation error: non-constant path in constant expr [E0080]
<anon>:2 pub const CONSTANT: Foo = Foo;
                                   ^~~
<anon>:2:27: 2:30 help: see the detailed explanation for E0080
error: aborting due to previous error

Again, the constant definition is totally fine, and it is the enum which is in the wrong.

May be related to #19265 or #28315

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lints

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions