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

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

Closed
bluebear94 opened this issue Feb 4, 2023 · 0 comments · Fixed by #107777
Closed

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

bluebear94 opened this issue Feb 4, 2023 · 0 comments · Fixed by #107777
Labels
C-bug Category: This is a bug.

Comments

@bluebear94
Copy link
Contributor

#[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)).

@bluebear94 bluebear94 added the C-bug Category: This is a bug. label Feb 4, 2023
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Feb 8, 2023
…lly-derive-const, r=fee1-dead

Make `derive_const` derive properly const-if-const impls

Fixes rust-lang#107774
Fixes rust-lang#107666

Also fixes rendering of const-if-const bounds in pretty printing.

r? `@oli-obk` or `@fee1-dead`
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Feb 8, 2023
…lly-derive-const, r=fee1-dead

Make `derive_const` derive properly const-if-const impls

Fixes rust-lang#107774
Fixes rust-lang#107666

Also fixes rendering of const-if-const bounds in pretty printing.

r? ``@oli-obk`` or ``@fee1-dead``
@bors bors closed this as completed in 5b8403c Feb 8, 2023
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.

1 participant