Skip to content

Associated constants equality produces errors with constants #105927

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Link1J opened this issue Dec 19, 2022 · 1 comment · Fixed by #106283
Closed

Associated constants equality produces errors with constants #105927

Link1J opened this issue Dec 19, 2022 · 1 comment · Fixed by #106283
Labels
C-bug Category: This is a bug.

Comments

@Link1J
Copy link

Link1J commented Dec 19, 2022

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
@Link1J Link1J added the C-bug Category: This is a bug. label Dec 19, 2022
@JulianKnodt
Copy link
Contributor

JulianKnodt commented Dec 22, 2022

It is expected that you need to wrap Mode::Cool with {}, as all const expressions beyond simple variable names require that. The error message could certainly be better though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants