Skip to content

Compiler asks for already present bound #79629

Open
@newpavlov

Description

While working on RustCrypto/stream-ciphers#195. I got roughly the following code:

use generic_array::{GenericArray, ArrayLength, typenum::{Unsigned, Quot, U16}};
use core::ops::Div;

pub trait Foo: Copy + Default {
    type Size: Unsigned;

    fn load<N>(block: &GenericArray<u8, N>) -> GenericArray<Self, Quot<N, Self::Size>>
    where
        N: ArrayLength<u8> + Div<Self::Size>,
        Quot<N, Self::Size>: ArrayLength<Self>
    {
        Default::default()
    }
}

#[derive(Copy, Clone, Default)]
#[repr(transparent)]
struct Bar(u128);

impl Foo for Bar {
    type Size = U16;
}

It compiles without issues. The default method impl will not be present in the final code, I just use it to show that the bounds are correct.

But if we are to overwrite the load method in the Bar impl by simply copying the default impl:

impl Foo for Bar {
    type Size = U16;

    fn load<N>(block: &GenericArray<u8, N>) -> GenericArray<Self, Quot<N, Self::Size>>
    where
        N: ArrayLength<u8> + Div<Self::Size>,
        Quot<N, Self::Size>: ArrayLength<Self>
    {
        Default::default()
    }
}

Playground

Compiler will return the following compilation errors:

error[E0277]: cannot divide `N` by `UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B0>, B0>`
  --> src/lib.rs:23:5
   |
23 | /     fn load<N>(block: &GenericArray<u8, N>) -> GenericArray<Self, Quot<N, Self::Size>>
24 | |     where
25 | |         N: ArrayLength<u8> + Div<Self::Size>,
26 | |         Quot<N, Self::Size>: ArrayLength<Self>
   | |______________________________________________^ no implementation for `N / UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B0>, B0>`
   |
help: consider further restricting this bound
   |
25 |         N: ArrayLength<u8> + Div<Self::Size> + Div<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B0>, B0>>,
   |                                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0277]: cannot divide `N` by `UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B0>, B0>`
  --> src/lib.rs:23:5
   |
23 | /     fn load<N>(block: &GenericArray<u8, N>) -> GenericArray<Self, Quot<N, Self::Size>>
24 | |     where
25 | |         N: ArrayLength<u8> + Div<Self::Size>,
26 | |         Quot<N, Self::Size>: ArrayLength<Self>
27 | |     {
28 | |         Default::default()
29 | |     }
   | |_____^ no implementation for `N / UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B0>, B0>`
   |
help: consider further restricting this bound
   |
25 |         N: ArrayLength<u8> + Div<Self::Size> + Div<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B0>, B0>>,
   |                                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0277]: cannot divide `N` by `UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B0>, B0>`
  --> src/lib.rs:23:8
   |
23 |     fn load<N>(block: &GenericArray<u8, N>) -> GenericArray<Self, Quot<N, Self::Size>>
   |        ^^^^ no implementation for `N / UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B0>, B0>`
   |
help: consider further restricting this bound
   |
25 |         N: ArrayLength<u8> + Div<Self::Size> + Div<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B0>, B0>>,
   |                                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 3 previous errors

Replacing Div<Self::Size> with Div<U16> or even with explicit Div<Uint<..>> does not fix the error.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    A-associated-itemsArea: Associated items (types, constants & functions)A-trait-systemArea: Trait systemC-bugCategory: This is a bug.E-needs-mcveCall for participation: This issue has a repro, but needs a Minimal Complete and Verifiable ExampleT-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