Skip to content

generic_const_exprs: Exact same code compiles as unit test but fails to compile as integration test #102683

@joonazan

Description

@joonazan

This is a minimal reproduction of an issue I encountered when I tried to use const generics in my library.

lib.rs:

#![feature(generic_const_exprs)]

pub trait NotEqual<Other> {}

impl<const X: u64, const Y: u64, T, U> NotEqual<U> for T
where
    T: IdType<Id = Id<X>>,
    U: IdType<Id = Id<Y>>,
    (): IsFalse<{ X == Y }>,
{
}

pub trait IdType {
    type Id;
}

pub struct Id<const ID: u64>;

trait IsFalse<const B: bool> {}
impl IsFalse<false> for () {}


#[cfg(test)]
mod tests {
    use super::*;
    struct A;
    impl IdType for A {
        type Id = Id<0>;
    }

    struct B;
    impl IdType for B {
        type Id = Id<1>;
    }

    fn require_not_equal<A, B>(_: A, _: B)
    where
        A: NotEqual<B>,
    {
    }

    #[test]
    fn testy() {
        require_not_equal(A, B);
    }
}

tests/same_test.rs:

use const_wtf::{Id, IdType, NotEqual};

struct A;
impl IdType for A {
    type Id = Id<0>;
}

struct B;
impl IdType for B {
    type Id = Id<1>;
}

fn require_not_equal<A, B>(_: A, _: B)
where
    A: NotEqual<B>,
{
}

#[test]
fn testy() {
    require_not_equal(A, B);
}

cargo test:

error[E0277]: the trait bound `(): const_wtf::IsFalse<_>` is not satisfied
  --> tests/same_test.rs:21:23
   |
21 |     require_not_equal(A, B);
   |     ----------------- ^ the trait `const_wtf::IsFalse<_>` is not implemented for `()`
   |     |
   |     required by a bound introduced by this call
   |
   = help: the trait `const_wtf::IsFalse<false>` is implemented for `()`
   = note: required for `A` to implement `NotEqual<_>`
note: required by a bound in `require_not_equal`
  --> tests/same_test.rs:15:8
   |
13 | fn require_not_equal<A, B>(_: A, _: B)
   |    ----------------- required by a bound in this
14 | where
15 |     A: NotEqual<B>,
   |        ^^^^^^^^^^^ required by this bound in `require_not_equal`

For more information about this error, try `rustc --explain E0277`.

rustc --version --verbose:

rustc 1.66.0-nightly (470e518c4 2022-09-27)
binary: rustc
commit-hash: 470e518c4b43265020c882bcf3c86632f5494910
commit-date: 2022-09-27
host: x86_64-unknown-linux-gnu
release: 1.66.0-nightly
LLVM version: 15.0.0

I updated to

rustc 1.66.0-nightly (01af5040f 2022-10-04)
binary: rustc
commit-hash: 01af5040fdada6ef8f1b749cda798d80a8590b2c
commit-date: 2022-10-04
host: x86_64-unknown-linux-gnu
release: 1.66.0-nightly
LLVM version: 15.0.2

and got the same output.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.F-generic_const_exprs`#![feature(generic_const_exprs)]`P-lowLow priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions