Skip to content

#[derive_const] derives should bound generic type parameters by ~const Trait #107666

Closed
@bluebear94

Description

@bluebear94

#[derive_const] derives do not introduce ~const bounds on generic parameters, preventing them from being used on most types with them.

I tried this code:

#![feature(const_trait_impl)]
#![feature(derive_const)]

#[derive_const(Clone)]
pub struct X<T>(T);

I expected to see this happen: The code should compile, with the trait implementation looking like impl<T> const Clone for X<T> where T: ~const Clone.

Instead, this happened: I get the following compilation errors:

error[[E0277]](https://doc.rust-lang.org/nightly/error-index.html#E0277): the trait bound `T: ~const Clone` is not satisfied
 --> src/lib.rs:5:17
  |
4 | #[derive_const(Clone)]
  |                ----- in this derive macro expansion
5 | pub struct X<T>(T);
  |                 ^ the trait `~const Clone` is not implemented for `T`
  |
note: the trait `Clone` is implemented for `T`, but that implementation is not `const`
 --> src/lib.rs:5:17
  |
4 | #[derive_const(Clone)]
  |                ----- in this derive macro expansion
5 | pub struct X<T>(T);
  |                 ^
  = note: this error originates in the derive macro `Clone` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider restricting type parameter `T`
  |
5 | pub struct X<T: ~const std::clone::Clone>(T);
  |               ++++++++++++++++++++++++++

error[[E0277]](https://doc.rust-lang.org/nightly/error-index.html#E0277): the trait bound `T: ~const Clone` is not satisfied
 --> src/lib.rs:5:17
  |
4 | #[derive_const(Clone)]
  |                ----- in this derive macro expansion
5 | pub struct X<T>(T);
  |                 ^ the trait `~const Clone` is not implemented for `T`
  |
note: the trait `Clone` is implemented for `T`, but that implementation is not `const`
 --> src/lib.rs:5:17
  |
4 | #[derive_const(Clone)]
  |                ----- in this derive macro expansion
5 | pub struct X<T>(T);
  |                 ^
  = note: this error originates in the derive macro `Clone` (in Nightly builds, run with -Z macro-backtrace for more info)

error[[E0015]](https://doc.rust-lang.org/nightly/error-index.html#E0015): cannot call non-const fn `<T as Clone>::clone` in constant functions
 --> src/lib.rs:5:17
  |
4 | #[derive_const(Clone)]
  |                ----- in this derive macro expansion
5 | pub struct X<T>(T);
  |                 ^
  |
  = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
  = note: this error originates in the derive macro `Clone` (in Nightly builds, run with -Z macro-backtrace for more info)

Some errors have detailed explanations: E0015, E0277.
For more information about an error, try `rustc --explain E0015`.
error: could not compile `playground` due to 3 previous errors

Using #[derive_const(PartialEq)] and #[derive_const(Default)] also causes compilation errors.

Meta

rustc --version --verbose:

rustc 1.69.0-nightly (d7948c843 2023-01-26)
binary: rustc
commit-hash: d7948c843de94245c794e8c63dd4301a78bb5ba3
commit-date: 2023-01-26
host: x86_64-unknown-linux-gnu
release: 1.69.0-nightly
LLVM version: 15.0.7

I could also reproduce this issue on the playground (1.69.0-nightly (2023-02-03 658fad6)).

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