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

cannot use constants which depend on generic parameters in types #90932

Closed
JonathanWoollett-Light opened this issue Nov 15, 2021 · 3 comments
Closed
Labels
A-const-generics Area: const generics (parameters and arguments) C-bug Category: This is a bug.

Comments

@JonathanWoollett-Light
Copy link

My original stack overflow post on the issue.
Rust Playground

I was trying to write a data-structure such that it is generic over different dimensionality.

The beginning is:

/// A field of adjacent sections in a given dimensionality.
///  With 1, you have `|left|center|right|`.
///  With 2 dimensions you have:
///  ```
///    up_left│    up│up_right
///  ─────────┼──────┼──────────
///       left│center│right
///  ─────────┼──────┼──────────
///  down_left│  down│down_right
///  ```
///  Etc.
#[derive(Debug)]
struct Adjacents<T, const D: u32>([T; size(D)])

const fn size(d: u32) -> usize { 3u32.pow(d) as usize }

When trying to write implementations like:

impl<T> Adjacents<T,1u32> { /*...*/ }

I'm encountering the severe warning (in a few instances):

warning: cannot use constants which depend on generic parameters in types
  --> src/main.rs:26:9
   |
26 | impl<T> Adjacents<T,1u32> {
   |         ^^^^^^^^^^^^^^^^^
   |
   = note: `#[warn(const_evaluatable_unchecked)]` on by default
   = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
   = note: for more information, see issue #76200 <https://github.com/rust-lang/rust/issues/76200>

In my use case certain functionality can be generic across the stored data type (T) but not generic across dimensionality (D), so it seems only inevitable I must use Adjacents<T,1u32> throughout.

Given D does not depend on T, I would not expect such a warning to arise.

Meta

rustc --version --verbose:

rustc 1.58.0-nightly (82af160c2 2021-11-10)
binary: rustc
commit-hash: 82af160c2cb9c349a0373cba98d8ad7f911f0d34
commit-date: 2021-11-10
host: x86_64-unknown-linux-gnu
release: 1.58.0-nightly
LLVM version: 13.0.0
Backtrace

Since this is not technically an error I have not included a backtrace.

@JonathanWoollett-Light JonathanWoollett-Light added the C-bug Category: This is a bug. label Nov 15, 2021
@ChrisDenton ChrisDenton added the needs-triage-legacy Old issue that were never triaged. Remove this label once the issue has been sufficiently triaged. label Jul 16, 2023
@fmease fmease added A-const-generics Area: const generics (parameters and arguments) and removed needs-triage-legacy Old issue that were never triaged. Remove this label once the issue has been sufficiently triaged. labels Jan 24, 2024
@fmease
Copy link
Member

fmease commented Jan 24, 2024

Current output on stable:

error: generic parameters may not be used in const operations
 --> src/lib.rs:2:44
  |
2 | struct Adjacents<T, const D: u32>([T; size(D)]);
  |                                            ^ cannot perform const operation using `D`
  |
  = help: const parameters may only be used as standalone arguments, i.e. `D`

Current output on nightly:

error: generic parameters may not be used in const operations
 --> src/lib.rs:2:44
  |
2 | struct Adjacents<T, const D: u32>([T; size(D)]);
  |                                            ^ cannot perform const operation using `D`
  |
  = help: const parameters may only be used as standalone arguments, i.e. `D`
  = help: add `#![feature(generic_const_exprs)]` to allow generic const expressions

@fmease
Copy link
Member

fmease commented Jan 24, 2024

You need to enable the experimental feature generic_const_exprs to make this pass:

#![feature(generic_const_exprs)]

#[derive(Debug)]
struct Adjacents<T, const D: u32>([T; size(D)])
where
    [(); size(D)]:;

const fn size(d: u32) -> usize { 3u32.pow(d) as usize }

@fmease
Copy link
Member

fmease commented Jan 24, 2024

Closing in favor of the tracking issue for generic_const_exprs #76560.

@fmease fmease closed this as not planned Won't fix, can't repro, duplicate, stale Jan 24, 2024
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) C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

3 participants