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

Trait implementation changes cross-crate with const_generics and const_evaluatable_checked #82957

Open
AuroransSolis opened this issue Mar 10, 2021 · 1 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)]` requires-incomplete-features requires-nightly This issue requires a nightly compiler in some way.

Comments

@AuroransSolis
Copy link

AuroransSolis commented Mar 10, 2021

edit: updated for generic_const_exprs

Issue two of three I'm going to be filing tonight with very similar code regarding const_generics and const_evaluatable_checked.

This is the code that causes the bug:

#![feature(generic_const_exprs, array_map)]

pub struct ConstCheck<const CHECK: bool>;

pub trait True {}
impl True for ConstCheck<true> {}

pub trait OrdesDec {
    type Newlen;
    type Output;

    fn pop(self) -> (Self::Newlen, Self::Output);
}

impl<T, const N: usize> OrdesDec for [T; N]
where
    ConstCheck<{N > 1}>: True,
    [T; N - 1]: Sized,
{
    type Newlen = [T; N - 1];
    type Output = T;

    fn pop(self) -> (Self::Newlen, Self::Output) {
        let mut iter = IntoIter::new(self);
        let end = iter.next_back().unwrap();
        let new = [(); N - 1].map(move |()| iter.next().unwrap());
        (new, end)
    }
}

The issue occurs when called from another crate like so:

use ordes::OrdesDec;

fn main() {
    let foo = [0, 1, 2, 3, 4];
    let (foo, pop) = foo.pop();
}

Now you're probably thinking to yourself, "Hey Auro, isn't that exactly the same code as in #82956?" Nope, not quite. The foo assignment is just 0, not 0u8. When called in the above manner, the following error message is produced:

error[E0599]: the method `pop` exists for array `[{integer}; 5]`, but its trait bounds were not satisfied
 --> src/main.rs:5:26
  |
5 |     let (foo, pop) = foo.pop();
  |                          ^^^ method cannot be called on `[{integer}; 5]` due to unsatisfied trait bounds
  |
 ::: /media/ellen-nyan/DnData/Workspace/Repos/playground-3/blah/src/lib.rs:3:1
  |
3 | pub struct ConstCheck<const CHECK: bool>;
  | ----------------------------------------- doesn't satisfy `ConstCheck<{_: bool}>: True`
  |
  = note: the following trait bounds were not satisfied:
          `ConstCheck<{_: bool}>: True`
          which is required by `[{integer}; 5]: OrdesDec`

When called internally (like in a test module), the test runs without issue (see here for a few tests that all run perfectly fine under cargo test).

Meta

rustc --version --verbose:

rustc 1.52.0-nightly (35dbef235 2021-03-02)
binary: rustc
commit-hash: 35dbef235048f9a2939dc20effe083ca483c37ff
commit-date: 2021-03-02
host: x86_64-unknown-linux-gnu
release: 1.52.0-nightly
LLVM version: 11.0.1
@AuroransSolis AuroransSolis added the C-bug Category: This is a bug. label Mar 10, 2021
@BoxyUwU BoxyUwU added F-generic_const_exprs `#![feature(generic_const_exprs)]` A-const-generics Area: const generics (parameters and arguments) labels Sep 2, 2021
@lcnr
Copy link
Contributor

lcnr commented Jun 24, 2022

so the issue seems to be caused by the caller not having feature(generic_const_exprs) enabled.

while the error is a bit surprising, it doesn't really seem worth it to spend a lot of effort on fixing this issue.

@workingjubilee workingjubilee added requires-nightly This issue requires a nightly compiler in some way. requires-incomplete-features labels Mar 4, 2023
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)]` requires-incomplete-features requires-nightly This issue requires a nightly compiler in some way.
Projects
None yet
Development

No branches or pull requests

4 participants