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

no place to add evaluatable bounds to assoc const items #104400

Closed
psionic12 opened this issue Nov 14, 2022 · 6 comments · Fixed by #113522
Closed

no place to add evaluatable bounds to assoc const items #104400

psionic12 opened this issue Nov 14, 2022 · 6 comments · Fixed by #113522
Assignees
Labels
C-bug Category: This is a bug. F-generic_const_exprs `#![feature(generic_const_exprs)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@psionic12
Copy link

psionic12 commented Nov 14, 2022

Example:

trait Foo {
    const N: usize;
    const ARRAY: [i32;Self::N];
}

The compiler will report an error:

error: unconstrained generic constant
  --> src/main.rs:38:5
   |
38 |     const ARRAY: [i32;Self::N];
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = help: try adding a `where` bound using this expression: `where [(); Self::N]:`

Honestly speaking, I don't get what the suggestion means. Is it allowed to use generic_const_exprs like this? Or the only way to do this is by writing

trait Foo<const N: usize> {
    const ARRAY: [i32;N];
}

?

@fmease
Copy link
Member

fmease commented Nov 14, 2022

Hmm, yeah. Associated constants cannot have a where clause (contrary to types and functions) and thus the suggestion is incorrect. And adding the where clause to the trait itself would lead to a cycle error.

Labeling this as a diagnostic issue for now but I think at some point we need to allow where clauses on associated constants.

@rustbot label C-bug T-compiler A-diagnostics F-generic_const_exprs

@rustbot rustbot added A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. F-generic_const_exprs `#![feature(generic_const_exprs)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 14, 2022
@psionic12
Copy link
Author

psionic12 commented Nov 14, 2022

Hi @fmease, do you suggested that the code

trait Foo {
    const N: usize;
    const ARRAY: [i32;Self::N];
}

cannot be done by now by using generic_const_exprs feature?
I don't want to use trait Foo<const N: usize> since it make the code complicated to understand (in my real case), though it works.

@fmease
Copy link
Member

fmease commented Nov 14, 2022

cannot be done by now by using generic_const_exprs feature?

I am afraid so.

@BoxyUwU BoxyUwU changed the title "unconstrained generic constant" when enabling feature #![feature(generic_const_exprs)] no place to add evaluatable { Self::ASSOC } to assoc const items Feb 6, 2023
@BoxyUwU BoxyUwU changed the title no place to add evaluatable { Self::ASSOC } to assoc const items no place to add evaluatable bounds to assoc const items Feb 6, 2023
@fmease
Copy link
Member

fmease commented Jun 19, 2023

@psionic12 In the coming days & weeks I'm going to work on implementing generic parameters & where clauses on free & associated const items.

@rustbot claim
@rustbot label -A-diagnostics

@sgodwincs
Copy link

sgodwincs commented Aug 13, 2023

Not sure if a new issue is warranted, but even with generic_const_items, this still doesn't seem to allow the following:

#![feature(generic_const_exprs)]
#![feature(generic_const_items)]

trait Foo {
    const COUNT: usize;
    const ARR: [u8; Self::COUNT]
    where
        [(); Self::COUNT]:;
}

impl<T: Foo> Foo for &T
where
    [(); T::COUNT]:,
{
    const COUNT: usize = T::COUNT;
    const ARR: [u8; Self::COUNT] = T::ARR
    where
        [(); Self::COUNT]:;
}

which gives error:

error[[E0391]](https://doc.rust-lang.org/nightly/error_codes/E0391.html): cycle detected when building an abstract representation for `<impl at src/lib.rs:11:1: 11:24>::ARR::{constant#0}`
  --> src/lib.rs:18:14
   |
18 |         [(); Self::COUNT]:;
   |              ^^^^^^^^^^^
   |
note: ...which requires building THIR for `<impl at src/lib.rs:11:1: 11:24>::ARR::{constant#0}`...
  --> src/lib.rs:18:14
   |
18 |         [(); Self::COUNT]:;
   |              ^^^^^^^^^^^
note: ...which requires type-checking `<impl at src/lib.rs:11:1: 11:24>::ARR::{constant#0}`...
  --> src/lib.rs:18:20
   |
18 |         [(); Self::COUNT]:;
   |                    ^^^^^
   = note: ...which requires evaluating trait selection obligation `&'a T: Foo`...
   = note: ...which again requires building an abstract representation for `<impl at src/lib.rs:11:1: 11:24>::ARR::{constant#0}`, completing the cycle
note: cycle used when checking that `<impl at src/lib.rs:11:1: 11:24>::ARR` is well-formed
  --> src/lib.rs:16:5
   |
16 |     const ARR: [u8; Self::COUNT] = T::ARR
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   = note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more in

See https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=ebf58d9916cfd568bb4b57eb4d107316

@fmease
Copy link
Member

fmease commented Aug 13, 2023

The issue you are describing is #107874 (except that the where-clause is on an impl not a trait but that is irrelevant).

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. F-generic_const_exprs `#![feature(generic_const_exprs)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants