Skip to content
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

Wrong diagnostic: Type must be annotated #[derive(PartialEq, Eq)] to be used as the type of a const parameter #80471

Open
vmarkushin opened this issue Dec 29, 2020 · 8 comments
Labels
A-const-generics Area: const generics (parameters and arguments) A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@vmarkushin
Copy link

vmarkushin commented Dec 29, 2020

I tried this code:

#![feature(const_generics)]

#[derive(PartialEq, Eq)]
enum Nat {
    Z,
    S(Box<Nat>)
}

fn foo<const N: Nat>() {}

And got this error:

error[E0741]: `Nat` must be annotated with `#[derive(PartialEq, Eq)]` to be used as the type of a const parameter
 --> src/lib.rs:9:17
  |
9 | fn foo<const N: Nat>() {}
  |                 ^^^ `Nat` doesn't derive both `PartialEq` and `Eq`

The code without the const parameter compiles, though:

#![feature(const_generics)]

#[derive(PartialEq, Eq)]
enum Nat {
    Z,
    S(Box<Nat>)
}

// fn foo<const N: Nat>() {}

Meta

rustc +nightly --version --verbose:

rustc 1.50.0-nightly (0edce6f4b 2020-12-24)
binary: rustc
commit-hash: 0edce6f4bbb4514482537f569f0b8ef48e71e0a0
commit-date: 2020-12-24
host: x86_64-apple-darwin
release: 1.50.0-nightly
@vmarkushin vmarkushin added the C-bug Category: This is a bug. label Dec 29, 2020
@jonas-schievink jonas-schievink added A-const-generics Area: const generics (parameters and arguments) A-diagnostics Area: Messages for errors, warnings, and lints F-const_generics `#![feature(const_generics)]` labels Dec 29, 2020
@SNCPlay42
Copy link
Contributor

The real problem is that Box does not #[derive(PartialEq, Eq)], but the diagnostic assumes the annotation is missing from the outermost type.

@rustbot label D-incorrect T-compiler requires-nightly

@rustbot rustbot added D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Dec 29, 2020
@vmarkushin
Copy link
Author

@SNCPlay42 but why it compiles without the const parameter? playground

@SNCPlay42
Copy link
Contributor

SNCPlay42 commented Dec 29, 2020

Box implements PartialEq and Eq, so you can derive them in structs containing a Box, but it doesn't do so by deriving them itself.

Const generic parameters require that PartialEq and Eq are derived on both the type and its fields (and its fields' fields etc.) so that how equality works for the parameter is predictable.

@vmarkushin
Copy link
Author

@SNCPlay42 interesting, that makes sense. Thank you.

@vmarkushin vmarkushin changed the title Type must be annotated #[derive(PartialEq, Eq)] to be used as the type of a const parameter Wrong diagnostic: Type must be annotated #[derive(PartialEq, Eq)] to be used as the type of a const parameter Dec 30, 2020
@lcnr
Copy link
Contributor

lcnr commented Jun 28, 2022

the error now mentions that Box is the issue, not Nat itself, marking as needs-test

#![feature(adt_const_params)]

#[derive(PartialEq, Eq)]
enum Nat {
    Z,
    S(Box<Nat>)
}

fn foo<const N: Nat>() {}

@lcnr lcnr added E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. and removed F-const_generics `#![feature(const_generics)]` labels Jun 28, 2022
@TaKO8Ki TaKO8Ki self-assigned this Jul 7, 2022
TaKO8Ki added a commit to TaKO8Ki/rust that referenced this issue Jul 7, 2022
@Mark-Simulacrum
Copy link
Member

I don't think this is fixed yet. We mention the Box, but where the Box comes from is still pretty opaque -- we should likely point at or say something about the enum variant.

Saying that Box<Nat> must be derive(PartialEq, Eq) is also pretty nonsensical, as that's not something you can do -- Box<T> could be, though.

@Mark-Simulacrum Mark-Simulacrum removed the E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. label Jul 7, 2022
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jul 7, 2022
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jul 7, 2022
bors added a commit to rust-lang-ci/rust that referenced this issue Jul 7, 2022
…askrgr

Rollup of 9 pull requests

Successful merges:

 - rust-lang#97917 (Implement ExitCodeExt for Windows)
 - rust-lang#98844 (Reword comments and rename HIR visiting methods.)
 - rust-lang#98979 (interpret: use AllocRange in UninitByteAccess)
 - rust-lang#98986 (Fix missing word in comment)
 - rust-lang#98994 (replace process exit with more detailed exit in src/bootstrap/*.rs)
 - rust-lang#98995 (Add a test for rust-lang#80471)
 - rust-lang#99002 (suggest adding a derive for #[default] applied to variants)
 - rust-lang#99004 (Add a test for rust-lang#70408)
 - rust-lang#99017 (Replace boolean argument for print_where_clause with an enum to make code more clear)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@estebank
Copy link
Contributor

estebank commented Jan 8, 2023

Current output:

error: `Nat` is forbidden as the type of a const generic parameter
 --> src/lib.rs:7:17
  |
7 | fn foo<const N: Nat>() {}
  |                 ^^^
  |
  = note: the only supported types are integers, `bool` and `char`
  = help: more complex types are supported with `#![feature(adt_const_params)]`

With adt_const_params

error[E0741]: `Box<Nat>` must be annotated with `#[derive(PartialEq, Eq)]` to be used as the type of a const parameter
 --> src/lib.rs:8:17
  |
8 | fn foo<const N: Nat>() {}
  |                 ^^^

@estebank
Copy link
Contributor

Current output with the feature enabled:

error[E0741]: `Nat` must implement `ConstParamTy` to be used as the type of a const generic parameter
 --> src/lib.rs:8:17
  |
8 | fn foo<const N: Nat>() {}
  |                 ^^^
  |
help: add `#[derive(ConstParamTy)]` to the struct
  |
3 + #[derive(ConstParamTy)]
4 | enum Nat {
  |

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-generics Area: const generics (parameters and arguments) A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

8 participants