Skip to content

Failed to impl Drop for struct with const generics constrait. #87303

Closed
@TennyZhuang

Description

@TennyZhuang

Given the following code:

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=81a20c98257fda83bb4fbc6d2a35e9b7

#![feature(const_generics)]
#![feature(const_evaluatable_checked)]

struct A<const M: usize> where [(); M * 2]: {
    arr: [i32; M*2],
}

trait F {
    fn f(&mut self);
}

impl<const M: usize> F for A<M> where [(); M * 2]: {
    fn f(&mut self) {
        println!("f");
    }
}

impl<const M: usize> Drop for A<M> where [(); M * 2]: {
    fn drop(&mut self) {
        println!("drop");
    }
}

fn main() {
}

The current output is:

E0367

error[E0367]: `Drop` impl requires `[(); _]: '<empty>` but the struct it is implemented for does not
  --> src/main.rs:18:42
   |
18 | impl<const M: usize> Drop for A<M> where [(); M * 2]: {
   |                                          ^^^^^^^^^^^
   |
note: the implementor must specify the same requirement
  --> src/main.rs:4:1
   |
4  | / struct A<const M: usize> where [(); M * 2]: {
5  | |     arr: [i32; M*2],
6  | | }
   | |_^

error[E0367]: `Drop` impl requires `the constant `<A<M> as Drop>::{constant#0}` can be evaluated` but the struct it is implemented for does not
  --> src/main.rs:18:47
   |
18 | impl<const M: usize> Drop for A<M> where [(); M * 2]: {
   |                                               ^^^^^
   |
note: the implementor must specify the same requirement
  --> src/main.rs:4:1
   |
4  | / struct A<const M: usize> where [(); M * 2]: {
5  | |     arr: [i32; M*2],
6  | | }
   | |_^

For more information about this error, try `rustc --explain E0367`.
error: could not compile `playground` due to 2 previous errors; 2 warnings emitted

Ideally the output should look like:

compile pass.

where [(); M * 2]: is a necessary constait for A, this is not a subset specialization.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions