Skip to content

Associated constants equality produces errors with constants #105927

Closed
@Link1J

Description

@Link1J

The following code produces errors. These errors are both nonsensical and unexpected. This happens with feature(associated_const_equality) enabled or disabled, and both on nightly and stable.

#![feature(associated_const_equality)]

pub enum Mode {
    Cool,
}

pub trait Parse {
    const MODE: Mode;
}

pub trait CoolStuff: Parse<MODE = Mode::Cool> {}

Errors:

error[E0573]: expected type, found variant `Mode::Cool`
  --> src/lib.rs:11:35
   |
11 | pub trait CoolStuff: Parse<MODE = Mode::Cool> {}
   |                                   ^^^^^^^^^^
   |                                   |
   |                                   not a type
   |                                   help: try using the variant's enum: `crate::Mode`

error: expected associated constant bound, found type
  --> src/lib.rs:11:28
   |
11 | pub trait CoolStuff: Parse<MODE = Mode::Cool> {}
   |                            ^^^^^^^^^^^^^^^^^
   |
note: associated constant defined here
  --> src/lib.rs:8:5
   |
8  |     const MODE: Mode;
   |     ^^^^^^^^^^^^^^^^

For more information about this error, try `rustc --explain E0573`.

These errors appear to happen whenever a constant value is used instead of a literal.

Another example with constant variable instead of an enum variant.

#![feature(associated_const_equality)]

const MODE_COOL: bool = true;

pub trait Parse {
    const MODE: bool;
}

pub trait CoolStuff: Parse<MODE = MODE_COOL> {}

I am not going to post the error message again, as they are basically the same.

The current workaround that I have found is to wrap the constant value in {}, with will cause the code to compile on nightly with feature(associated_const_equality) enabled, or produce the associated const equality is incomplete error message otherwise.

Meta

rustc --version --verbose:

rustc 1.68.0-nightly (0468a00ae 2022-12-17)
binary: rustc
commit-hash: 0468a00ae3fd6ef1a6a0f9eaf637d7aa9e604acc
commit-date: 2022-12-17
host: x86_64-pc-windows-msvc
release: 1.68.0-nightly
LLVM version: 15.0.6

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions