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

generic_const_exprs: unification does not look into const items #92151

Open
lcnr opened this issue Dec 21, 2021 · 4 comments
Open

generic_const_exprs: unification does not look into const items #92151

lcnr opened this issue Dec 21, 2021 · 4 comments
Labels
A-const-generics Area: const generics (parameters and arguments) C-bug Category: This is a bug. F-generic_const_exprs `#![feature(generic_const_exprs)]`

Comments

@lcnr
Copy link
Contributor

lcnr commented Dec 21, 2021

#![feature(generic_const_exprs)]
use std::marker::PhantomData;

struct Nil;
struct Cons<N>(PhantomData<N>);

trait Len {
    const N: usize;
}

impl<N: Len> Len for Cons<N> {
    const N: usize = 1 + N::N;
}

impl Len for Nil {
    const N: usize = 0;
}

trait Piece<T> {
    type Size: Len;

    fn construct(self) -> [T; Self::Size::N];
}

impl<T: Copy + From<i32>> Piece<T> for i32 {
    type Size = Cons<Nil>;

    fn construct(self) -> [T; Self::Size::N] {
        [T::from(self)]
    }
}

impl<T: Copy, U: Piece<T>> Piece<T> for (U,) {
    type Size = U::Size;

    fn construct(self) -> [T; Self::Size::N] {
        todo!()
    }
}

this results in the following which definitely seems wrong

error: unconstrained generic constant
  --> src/lib.rs:36:31
   |
36 |     fn construct(self) -> [T; Self::Size::N] {
   |                               ^^^^^^^^^^^^^
   |
   = help: try adding a `where` bound using this expression: `where [(); Self::Size::N]:`
note: the requirement `the constant `<(U,) as Piece<T>>::construct::{constant#0}` can be evaluated` appears on the impl method `construct` but not on the corresponding trait method
  --> src/lib.rs:22:8
   |
19 | trait Piece<T> {
   |       ----- in this trait
...
22 |     fn construct(self) -> [T; Self::Size::N];
   |        ^^^^^^^^^ this trait method doesn't have the requirement `the constant `<(U,) as Piece<T>>::construct::{constant#0}` can be evaluated`

error[E0308]: method not compatible with trait
  --> src/lib.rs:36:5
   |
36 |     fn construct(self) -> [T; Self::Size::N] {
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `Self::Size::N`, found `Self::Size::N`
   |
   = note: expected type `Self::Size::N`
              found type `Self::Size::N`
@lcnr lcnr added C-bug Category: This is a bug. F-generic_const_exprs `#![feature(generic_const_exprs)]` labels Dec 21, 2021
@lcnr
Copy link
Contributor Author

lcnr commented Jan 17, 2022

minimized

#![feature(generic_const_exprs)]

trait Foo {
    const ASSOC: usize;
    fn construct(self) -> [u8; Self::ASSOC];
}

impl<const N: usize> Foo for [u8; N] {
    const ASSOC: usize = N;
    fn construct(self) -> [u8; N] {
        self
    }
}

when looking at the signature of construct, we expect Unevaluated(Self::ASSOC, [Self]) but get Param(N). As we don't look into associated consts rn this causes an error

@BoxyUwU BoxyUwU changed the title generic_const_exprs: method not compatible with trait generic_const_exprs: unification does not look into const items Jun 24, 2022
@BoxyUwU BoxyUwU added the A-const-generics Area: const generics (parameters and arguments) label Jun 24, 2022
@lcnr
Copy link
Contributor Author

lcnr commented Jun 24, 2022

tracked in rust-lang/project-const-generics#31

@BoxyUwU BoxyUwU self-assigned this Jun 28, 2022
@BoxyUwU BoxyUwU removed their assignment Jul 3, 2023
@xbjfk
Copy link

xbjfk commented Apr 1, 2024

I ran into a similar issue with this feature + adt_const_params. Playground. I'm not sure how related this is - let me know if this should be a new issue.

@BoxyUwU
Copy link
Member

BoxyUwU commented Apr 1, 2024

not related, you need to add that where clause. generic const exprs wants to avoid having generic constants that might not succesfully evaluate in the type system so we require those where clauses which look funny ✨

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. F-generic_const_exprs `#![feature(generic_const_exprs)]`
Projects
None yet
Development

No branches or pull requests

3 participants